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