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