X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=charts.d%2Fsquid.chart.sh;h=2c19c35d59068f11728cddc6433a786a5bed24fc;hb=f589fe54cfd38c1543ad722b7efee40600a47305;hp=71f8bd0425b2aa7b94f06cf87e119654dcceae29;hpb=90617f2f1764ea72eb85b2f942847bab05558019;p=netdata.git diff --git a/charts.d/squid.chart.sh b/charts.d/squid.chart.sh old mode 100755 new mode 100644 index 71f8bd04..2c19c35d --- a/charts.d/squid.chart.sh +++ b/charts.d/squid.chart.sh @@ -1,13 +1,65 @@ -#!/bin/sh +# no need for shebang - this file is loaded from charts.d.plugin + +# netdata +# real-time performance and health monitoring, done right! +# (C) 2016 Costa Tsaousis +# GPL v3+ +# + +squid_host= +squid_port= +squid_url= +squid_timeout=2 +squid_update_every=2 +squid_priority=60000 + +squid_get_stats_internal() { + local host="$1" port="$2" url="$3" + run squidclient -h $host -p $port $url +} + +squid_get_stats() { + squid_get_stats_internal "$squid_host" "$squid_port" "$squid_url" +} -squid_url="http://127.0.0.1:8080/squid-internal-mgr/counters" +squid_autodetect() { + local host="127.0.0.1" port url x + + for port in 3128 8080 + do + for url in "cache_object://$host:$port/counters" "/squid-internal-mgr/counters" + do + x=$(squid_get_stats_internal "$host" "$port" "$url" | grep client_http.requests) + if [ ! -z "$x" ] + then + squid_host="$host" + squid_port="$port" + squid_url="$url" + debug "found squid at '$host:$port' with url '$url'" + return 0 + fi + done + done + + error "cannot find squid running in localhost. Please set squid_url='url' and squid_host='IP' and squid_port='PORT' in $confd/squid.conf" + return 1 +} squid_check() { + require_cmd squidclient || return 1 + require_cmd sed || return 1 + require_cmd egrep || return 1 + + if [ -z "$squid_host" -o -z "$squid_port" -o -z "$squid_url" ] + then + squid_autodetect || return 1 + fi + # check once if the url works - wget 2>/dev/null -O /dev/null "$squid_url" - if [ ! $? -eq 0 ] + local x="$(squid_get_stats | grep client_http.requests)" + if [ ! $? -eq 0 -o -z "$x" ] then - echo >&2 "squid: cannot fetch the url: $squid_url. Please set squid_url='url' in $confd/squid.conf" + error "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" return 1 fi @@ -17,61 +69,76 @@ squid_check() { squid_create() { # create the charts cat </dev/null -O - "$squid_url" | sed -e "s/\./_/g" -e "s/ = /=/g" | egrep "(^client_http_|^server_all_)"` + # remember: KEEP IT SIMPLE AND SHORT + + # 1. get the counters page from squid + # 2. sed to remove spaces; replace . with _; remove spaces around =; prepend each line with: local squid_ + # 3. egrep lines starting with: + # local squid_client_http_ then one or more of these a-z 0-9 _ then = and one of more of 0-9 + # local squid_server_all_ then one or more of these a-z 0-9 _ then = and one of more of 0-9 + # 4. then execute this as a script with the eval + # + # be very carefull with eval: + # prepare the script and always grep at the end the lines that are usefull, so that + # even if something goes wrong, no other code can be executed + + eval "$(squid_get_stats |\ + sed -e "s/ \+/ /g" -e "s/\./_/g" -e "s/^\([a-z0-9_]\+\) *= *\([0-9]\+\)$/local squid_\1=\2/g" |\ + egrep "^local squid_(client_http|server_all)_[a-z0-9_]+=[0-9]+$")" # write the result of the work. cat <