]> arthur.barton.de Git - netdata.git/blob - charts.d/squid.chart.sh
now it queries cache_object with nc, instead of wget
[netdata.git] / charts.d / squid.chart.sh
1 #!/bin/sh
2
3 squid_host="127.0.0.1"
4 squid_port="3128"
5 squid_update_every=5
6
7 squid_get_stats() {
8 nc $squid_host $squid_port <<EOF
9 GET cache_object://$squid_host:$squid_port/counters HTTP/1.0
10
11 EOF
12 }
13
14 squid_check() {
15         # check once if the url works
16         local x=`squid_get_stats | grep client_http.requests`
17         if [ ! $? -eq 0 -o -z "$x" ]
18         then
19                 echo >&2 "squid: cannot fetch the counters from $squid_host:$squid_port. Please set squid_host='host' and squid_port='port' in $confd/squid.conf"
20                 return 1
21         fi
22
23         return 0
24 }
25
26 squid_create() {
27         # create the charts
28         cat <<EOF
29 CHART squid.clients_net '' "Squid Client Bandwidth" "kilobits / $squid_update_every sec" squid '' area 20001 $squid_update_every
30 DIMENSION client_http_kbytes_in in incremental 8 $((1 * squid_update_every))
31 DIMENSION client_http_kbytes_out out incremental -8 $((1 * squid_update_every))
32 DIMENSION client_http_hit_kbytes_out hits incremental -8 $((1 * squid_update_every))
33
34 CHART squid.clients_requests '' "Squid Client Requests" "requests / $squid_update_every sec" squid '' line 20003 $squid_update_every
35 DIMENSION client_http_requests requests incremental 1 $((1 * squid_update_every))
36 DIMENSION client_http_hits hits incremental 1 $((1 * squid_update_every))
37 DIMENSION client_http_errors errors incremental -1 $((1 * squid_update_every))
38
39 CHART squid.servers_net '' "Squid Server Bandwidth" "kilobits / $squid_update_every sec" squid '' area 20002 $squid_update_every
40 DIMENSION server_all_kbytes_in in incremental 8 $((1 * squid_update_every))
41 DIMENSION server_all_kbytes_out out incremental -8 $((1 * squid_update_every))
42
43 CHART squid.servers_requests '' "Squid Server Requests" "requests / $squid_update_every sec" squid '' line 20004 $squid_update_every
44 DIMENSION server_all_requests requests incremental 1 $((1 * squid_update_every))
45 DIMENSION server_all_errors errors incremental -1 $((1 * squid_update_every))
46 EOF
47         
48         return 0
49 }
50
51
52 squid_update() {
53         # the first argument to this function is the microseconds since last update
54         # pass this parameter to the BEGIN statement (see bellow).
55
56         # do all the work to collect / calculate the values
57         # for each dimension
58         # remember: KEEP IT SIMPLE AND SHORT
59
60         # 1. wget the counters page from squid
61         # 2. sed to remove spaces; replace . with _; remove spaces around =; prepend each line with: local squid_
62         # 3. egrep lines starting with:
63         #    local squid_client_http_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
64         #    local squid_server_all_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
65         # 4. then execute this as a script with the eval
66         #
67         # be very carefull with eval:
68         # prepare the script and always grep at the end the lines that are usefull, so that
69         # even if something goes wrong, no other code can be executed
70
71         eval "`squid_get_stats |\
72                  sed -e "s/ \+/ /g" -e "s/\./_/g" -e "s/^\([a-z0-9_]\+\) *= *\([0-9]\+\)$/local squid_\1=\2/g" |\
73                 egrep "^local squid_(client_http|server_all)_[a-z0-9_]+=[0-9]+$"`"
74
75         # write the result of the work.
76         cat <<VALUESEOF
77 BEGIN squid.clients_net $1
78 SET client_http_kbytes_in = $squid_client_http_kbytes_in
79 SET client_http_kbytes_out = $squid_client_http_kbytes_out
80 SET client_http_hit_kbytes_out = $squid_client_http_hit_kbytes_out
81 END
82
83 BEGIN squid.clients_requests $1
84 SET client_http_requests = $squid_client_http_requests
85 SET client_http_hits = $squid_client_http_hits
86 SET client_http_errors = $squid_client_http_errors
87 END
88
89 BEGIN squid.servers_net $1
90 SET server_all_kbytes_in = $squid_server_all_kbytes_in
91 SET server_all_kbytes_out = $squid_server_all_kbytes_out
92 END
93
94 BEGIN squid.servers_requests $1
95 SET server_all_requests = $squid_server_all_requests
96 SET server_all_errors = $squid_server_all_errors
97 END
98 VALUESEOF
99
100         return 0
101 }
102