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