#! /usr/bin/perl
# anders@aftenposten.no, 2007-05-02
# Nagios plugin to monitor cache hit ratio
# Requires monitoring of munin plugin varnish_cachehitratio to be running

$dfile = "/var/tmp/varnish_cachehitratio.dat";
# File must be updated at the minimum every n seconds:
$updatemininmum = 900;

use Getopt::Std;

if (! -r $dfile) {
	print "Can not read data file with cache hit ratio.\n";
	exit(3);
}
$mytime = int(time);
$dfiletime = (stat($dfile))[9];
$dfileage = $mytime-$dfiletime;
if ($dfileage > $updatemininmum) {
	print "Data file with cache hit ratio has not been updated for $dfileage seconds. Can not use this information.\n";
	exit(3);
}

getopt('c:w:');
if ((!$opt_c) && (!$opt_w)) {
	print "Must use -w <warning%> or -c <critical%>.\n";
	exit(3);
}

open(DAT, $dfile);
$line = <DAT>;
#print $line;
#$chr = split(" ", $line);
$chr = (split(" ", $line))[3];
close(DAT);

#print "Cache hit ratio er: $chr\n";
if ($opt_c && $chr < $opt_c) {
	print "Cache hit ratio is at $chr, lower than critical limit $opt_c.\n";	exit(2);
} elsif ($opt_w && $chr < $opt_w) {
	print "Cache hit ratio is at $chr, lower than warning limit $opt_w.\n";
	exit(1);
} else {
	print "Cache hit ratio is at $chr, OK.\n";
	exit(0);
}
