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