]> arthur.barton.de Git - netdata.git/blob - charts.d/squid.chart.sh
Merge pull request #687 from ktsaou/master
[netdata.git] / charts.d / squid.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 squid_host=
4 squid_port=
5 squid_url=
6 squid_timeout=2
7 squid_update_every=2
8 squid_priority=60000
9
10 squid_get_stats_internal() {
11         local host="$1" port="$2" url="$3"
12         squidclient -h $host -p $port $url
13 }
14
15 squid_get_stats() {
16         squid_get_stats_internal "$squid_host" "$squid_port" "$squid_url"
17 }
18
19 squid_autodetect() {
20         local host="127.0.0.1" port url x
21
22         for port in 3128 8080
23         do
24                 for url in "cache_object://$host:$port/counters" "/squid-internal-mgr/counters"
25                 do
26                         x=$(squid_get_stats_internal "$host" "$port" "$url" | grep client_http.requests)
27                         if [ ! -z "$x" ]
28                                 then
29                                 squid_host="$host"
30                                 squid_port="$port"
31                                 squid_url="$url"
32                                 echo >&2 "squid: found squid at '$host:$port' with url '$url'"
33                                 return 0
34                         fi
35                 done
36         done
37
38         echo >&2 "squid: cannot find squid running in localhost. Please set squid_url='url' and squid_host='IP' and squid_port='PORT' in $confd/squid.conf"
39         return 1
40 }
41
42 squid_check() {
43         require_cmd squidclient || return 1
44         require_cmd sed || return 1
45         require_cmd egrep || return 1
46
47         if [ -z "$squid_host" -o -z "$squid_port" -o -z "$squid_url" ]
48                 then
49                 squid_autodetect || return 1
50         fi
51
52         # check once if the url works
53         local x="$(squid_get_stats | grep client_http.requests)"
54         if [ ! $? -eq 0 -o -z "$x" ]
55         then
56                 echo >&2 "squid: cannot fetch URL '$squid_url' by connecting to $squid_host:$squid_port. Please set squid_url='url' and squid_host='host' and squid_port='port' in $confd/squid.conf"
57                 return 1
58         fi
59
60         return 0
61 }
62
63 squid_create() {
64         # create the charts
65         cat <<EOF
66 CHART squid_local.clients_net '' "Squid Client Bandwidth" "kilobits / sec" clients squid.clients.net area $((squid_priority + 1)) $squid_update_every
67 DIMENSION client_http_kbytes_in in incremental 8 1
68 DIMENSION client_http_kbytes_out out incremental -8 1
69 DIMENSION client_http_hit_kbytes_out hits incremental -8 1
70
71 CHART squid_local.clients_requests '' "Squid Client Requests" "requests / sec" clients squid.clients.requests line $((squid_priority + 3)) $squid_update_every
72 DIMENSION client_http_requests requests incremental 1 1
73 DIMENSION client_http_hits hits incremental 1 1
74 DIMENSION client_http_errors errors incremental -1 1
75
76 CHART squid_local.servers_net '' "Squid Server Bandwidth" "kilobits / sec" servers squid.servers.net area $((squid_priority + 2)) $squid_update_every
77 DIMENSION server_all_kbytes_in in incremental 8 1
78 DIMENSION server_all_kbytes_out out incremental -8 1
79
80 CHART squid_local.servers_requests '' "Squid Server Requests" "requests / sec" servers squid.servers.requests line $((squid_priority + 4)) $squid_update_every
81 DIMENSION server_all_requests requests incremental 1 1
82 DIMENSION server_all_errors errors incremental -1 1
83 EOF
84
85         return 0
86 }
87
88
89 squid_update() {
90         # the first argument to this function is the microseconds since last update
91         # pass this parameter to the BEGIN statement (see bellow).
92
93         # do all the work to collect / calculate the values
94         # for each dimension
95         # remember: KEEP IT SIMPLE AND SHORT
96
97         # 1. get the counters page from squid
98         # 2. sed to remove spaces; replace . with _; remove spaces around =; prepend each line with: local squid_
99         # 3. egrep lines starting with:
100         #    local squid_client_http_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
101         #    local squid_server_all_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
102         # 4. then execute this as a script with the eval
103         #
104         # be very carefull with eval:
105         # prepare the script and always grep at the end the lines that are usefull, so that
106         # even if something goes wrong, no other code can be executed
107
108         eval "$(squid_get_stats |\
109                  sed -e "s/ \+/ /g" -e "s/\./_/g" -e "s/^\([a-z0-9_]\+\) *= *\([0-9]\+\)$/local squid_\1=\2/g" |\
110                 egrep "^local squid_(client_http|server_all)_[a-z0-9_]+=[0-9]+$")"
111
112         # write the result of the work.
113         cat <<VALUESEOF
114 BEGIN squid_local.clients_net $1
115 SET client_http_kbytes_in = $squid_client_http_kbytes_in
116 SET client_http_kbytes_out = $squid_client_http_kbytes_out
117 SET client_http_hit_kbytes_out = $squid_client_http_hit_kbytes_out
118 END
119
120 BEGIN squid_local.clients_requests $1
121 SET client_http_requests = $squid_client_http_requests
122 SET client_http_hits = $squid_client_http_hits
123 SET client_http_errors = $squid_client_http_errors
124 END
125
126 BEGIN squid_local.servers_net $1
127 SET server_all_kbytes_in = $squid_server_all_kbytes_in
128 SET server_all_kbytes_out = $squid_server_all_kbytes_out
129 END
130
131 BEGIN squid_local.servers_requests $1
132 SET server_all_requests = $squid_server_all_requests
133 SET server_all_errors = $squid_server_all_errors
134 END
135 VALUESEOF
136
137         return 0
138 }