#! /usr/bin/perl
# anders@aftenposten.no, 2007-06-06
# Show the rate of requests (per second) for Squid

#use Sys::Hostname;
#use Time::HiRes qw( time );
#use IO::Socket;
use Net::SNMP;

# ----- config -----
$oid=".1.3.6.1.4.1.3495.1.3.2.1.2";
$host="localhost";
$community="foobar";
$port=3401;
# ----- config -----

sub snmpgetvalue {
	my $response = $session->get_request($oid);
	if (!defined($response->{$oid})) {
		return undef;
	} else {
		return $response->{$oid};
	}
}

sub printvalues {
	my $label = shift;

	my ($session, $error) = Net::SNMP->session(
		-hostname => $host,
		-community => $community,
		-port => $port
	);
	if (defined ($session)) {
		my $response = $session->get_request($oid);

		if (!defined($response->{$oid})) {
			print "$label.value 0\n";
		} else {
			my $value=$response->{$oid};
			print "$label.value $value\n";
		}
	} else {
		die("Can not connect to SNMP agent on $host:$port");
	}
}

if ($ARGV[0] && $ARGV[0] eq "autoconf") {
	print "yes\n";
} elsif ($ARGV[0] && $ARGV[0] eq "config") {
	print "graph_title Cache hit/miss ratio\n";

        print "graph_title Hitrate\n";
        print "graph_vlabel hits per second\n";
        print "graph_category squid\n";
        print "graph_info This graph shows the rate of requests, hits per second\n";

	print "requests.label requests\n";
       print "requests.type COUNTER\n";
#	print "requests.type DERIVE\n";
#	print "requests.min 0\n";
	print "requests.graph yes\n";
} else {
	printvalues("requests");
}
