#!/bin/bash
#
# Copyright (C) 2006 Benjamin Schweizer. All rights reserved.
#
#
# Abstract
# ~~~~~~~~
# munin plugin that logs the cache efficiency
# 
# Config
# ~~~~~~
# by default plugin will try to reach localhost:3128
# you can change that by editing munin-node config file, adding the following code (example for 192.168.1.1:8080)
# (of course remove has signs)
#
# [squid_eff]
# env.squidhost 192.168.1.1
# env.squidport 8080
#
# Authors
# ~~~~~~~
# Benjamin Schweizer <code at benjamin-schweizer dot de>
# _KaszpiR_ < kaszpir at gmail dot com >
#
# Changes
# ~~~~~~~
# 2008-11-25, kaszpir: addded cvars to enable setting up custom host and port
# 2006-11-16, schweizer: removed 5 minutes stats, fixed 5% bug
# 2006-10-26, schweizer: excluded negative values from result
# 2006-10-11, schweizer: initial release.
#
# Todo
# ~~~~
# - parsing host and port from plugin filename for multi squid config, for now you would have to make few symlinks and name the differently
#   aswell as setting env vars in munin-node config file (havent tried that nwo, really, just thinking)
#
#%# family=auto
#%# capabilities=autoconf

squid_host=$squidhost || "localhost";
squid_port=$squidport || 3128;
#echo host: $squid_host
#echo port: $squid_port


if [ "$1" = "autoconf" ]; then
	SQUID_STATS=`printf "GET cache_object://$squid_host:$squid_port/info HTTP/1.0\n\n" | netcat $squid_host $squid_port`
	if [ -n "${SQUID_STATS}" ]; then
		echo yes 
		exit 0
	else
		echo "no (HTTP GET failed)"
		exit 1
	fi
fi

if [ "$1" = "config" ]; then
	echo "graph_title Squid Efficiency: $squid_host:$squid_port"
	echo "graph_info This graph shows the Squid proxy efficiency that runs at $squid_host:$squid_port."
	echo 'graph_category squid'
	echo "graph_args --lower-limit 0 --upper-limit 100"
	echo 'graph_vlabel %'

	echo 'request.label request hit'

	echo 'byte.label byte hit'

	exit 0
fi

SQUID_STATS=`printf "GET cache_object://$squid_host:$squid_port/info HTTP/1.0\n\n" | netcat $squid_host $squid_port`
#        Request Hit Ratios:     5min: 0.0%, 60min: 17.4%
#        Byte Hit Ratios:        5min: 75.0%, 60min: 12.0%

SQUID_REQUEST_HIT=`echo "$SQUID_STATS" | grep "Request Hit Ratios" | cut -d ":" -f4 | cut -d "." -f1 | xargs echo`
if [ $SQUID_REQUEST_HIT -lt 1 ]; then SQUID_REQUEST_HIT=0; fi
SQUID_BYTE_HIT=`echo "$SQUID_STATS" | grep "Byte Hit Ratios" | cut -d ":" -f4 | cut -d "." -f1 | xargs echo`
if [ $SQUID_BYTE_HIT -lt 1 ]; then SQUID_BYTE_HIT=0; fi

echo "request.value ${SQUID_REQUEST_HIT}"
echo "byte.value ${SQUID_BYTE_HIT}"

# eof.

