Skip to content

Instantly share code, notes, and snippets.

@daks
Forked from wrzasa/collectd-squid-plugins
Created March 6, 2017 16:59
Show Gist options
  • Save daks/f69e5917719ebf0986ac6a67dcd5160c to your computer and use it in GitHub Desktop.
Save daks/f69e5917719ebf0986ac6a67dcd5160c to your computer and use it in GitHub Desktop.
Collectd plugins to read squid statistics.
Work for me with squid 3.4 on debian jessie.
I use it with collectd exec plugin (https://collectd.org/documentation/manpages/collectd-exec.5.shtml)
to collect data from squid, send it over the network to influxdb and visualize with grafana.
Maybe someone will have some use of it. Good luck ;-)
https://gist.github.com/wrzasa/dfd7b554171159a6b2ab24b03b8e30b8/
#!/bin/sh
# https://gist.github.com/wrzasa/dfd7b554171159a6b2ab24b03b8e30b8/
# This one is adapted from: https://gist.github.com/noahcampbell/918684
# Reads squid counters.
# cpu_time and wall_time are converted to microseconds.
INTERVAL=${COLLECTD_INTERVAL:-1}
HOSTNAME=${COLLECTD_HOSTNAME:-localhost}
while sleep $INTERVAL; do
squidclient -h 127.0.0.1 cache_object://127.0.0.1/counters 2> /dev/null | awk -v interval=${INTERVAL} -v host=${HOSTNAME} -F ' = ' 'BEGIN{ data = 0 } { gsub("\\.", "_", $1); if (data == 1) { if ($1 == "cpu_time" || $1 == "wall_time") { value = sprintf("%.0f", $2*1000000) } else { value = $2 } print "PUTVAL", host"/squid/counter-"$1, "interval="interval, "N:"value } } /^sample_time/ { data = 1 }'
done
#! /bin/bash
# https://gist.github.com/wrzasa/dfd7b554171159a6b2ab24b03b8e30b8/
# This one provides data from squid info. Only selected values.
# If you need it is quite straightforward to add some values that I omitted.
# Requires additional types.db file
INTERVAL=${COLLECTD_INTERVAL:-1}
HOSTNAME=${COLLECTD_HOSTNAME:-localhost}
function print_squid_info {
squidclient -h 127.0.0.1 cache_object://127.0.0.1/info 2> /dev/null | awk -v interval=${INTERVAL} -v host=${HOSTNAME} -F ':' '
function make_label(name) {
label = name;
gsub(/^[\t]+/, "", label);
gsub(/[ \t]+/, "_", label);
gsub(/_+/, "_", label);
gsub("%", "perc", label);
gsub(/[(),-\/]/, "", label);
return label;
}
function print_item(type, name, values) {
print "PUTVAL "host"/squid-info/"type"-"make_label(name)" interval="interval" N:"values
}
function five_sixty_value(five, sixty) {gsub("%", "", five)
sixty = $4
gsub("%", "", sixty)
gsub(" 60min: ", "", sixty)
return five+0":"sixty+0
}
function used_free_value(v){
gsub("%", "", v)
gsub(/ *used */, "", v)
gsub(/ *free */, "", v)
split(v, uf, / *, */)
used = uf[1]
free = uf[2]
return used+0":"free+0
}
/^[\t ]+Number of clients accessing cache:/ { print_item("gauge", $1, $2+0) }
/^[\t ]+Hits as % of all requests:/||
/^[\t ]+Hits as % of bytes sent:/||
/^[\t ]+Memory hits as % of hit requests:/||
/^[\t ]+Disk hits as % of hit requests:/ {
print_item("percent_5m_60m", $1, five_sixty_value($3, $4))
}
/^[\t ]+Storage Swap capacity:/||
/^[\t ]+Storage Mem capacity:/ {
print_item("percent_used_free", $1, used_free_value($2))
}
/^[\t ]+Storage Swap size:/||
/^[\t ]+Mean Object Size:/||
/^[\t ]+Storage Mem size:/ {
gsub("KB", "", $2)
print_item("size_kB", $1, $2+0)
}
/^[ \t]/{
if (sect == "mst") {
gsub(/^ +/,"", $2)
gsub(/ +$/,"", $2)
gsub(/[ \t]+/,":", $2)
print_item("time_5m_60m", "MST_"$1, $2)
}
if (sect == "res") {
gsub(/%/, "", $2)
gsub("seconds", "", $2)
gsub("KB", "", $2)
print_item("gauge", $1, $2+0)
}
if (sect == "mem_usage" || sect == "mem_acct") {
gsub(/KB.*/, "", $2)
print_item("size_kB", $1, $2+0)
}
}
/^[^ \t]/ { sect = null }
/^Connection information for squid/ { sect = "conninfo" }
/^Cache information for squid:/ { sect = "cacheinfo" }
/^Median Service Times/ { sect = "mst" }
/^Resource usage for squid:/ { sect = "res" }
/^Memory usage for squid via mallinfo\(\):/ { sect = "mem_usage" }
/^Memory accounted for:/ { sect = "mem_acct" }
'
}
while sleep $INTERVAL; do
print_squid_info
done
percent_5m_60m 5min:GAUGE:0:U, 60min:GAUGE:0:U
percent_used_free used:GAUGE:0:U, free:GAUGE:0:U
size_kB value:GAUGE:0:U
time_5m_60m 5min:GAUGE:0:U, 60min:GAUGE:0:U
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment