#! /usr/bin/perl -w 
 
use strict;

use Net::SNMP qw(snmp_dispatcher ticks_to_time);
use POSIX qw(strftime);

my $host = shift;

if (! defined $host) {
	die "Usage: Poll-HP-Supplies.pl hostname\n";
}
my ($session, $error) = Net::SNMP->session(
	-hostname    => $host,
	-community   => 'public',
	-translate   => [
		-timeticks => 0x0   # Turn off so sysUpTime is numeric
	]
);

if (!defined $session) {
	print STDERR "SNMP-Error: $error\n";
	exit 1;
}

# .1.3.6.1.2.1.43.11.1.1 is the location of
# printer supply information (See RFC 1759)

# (1.3.6.1.2.1.43.11.1.1.)
# .1.3.6.1.2.1.43.11.1.1.6.1
# 
# .4.1.x = Type of supply that is used
# .5.1.x = Supply type
# .6.1.x = Description of Supply
# .7.1.x = Unit of usage
# .8.1.x = Value if completly full
# .9.1.x = Current value 

my $MIBQuery = ".1.3.6.1.2.1.43.11.1.1.";

my $Status = 0;
my $Currenttime = strftime "%d.%m. %H:%M", localtime(time());

# Get printer model
my @HostMIB;
push @HostMIB, ".1.3.6.1.2.1.43.5.1.1.16.1";
my $result = $session->get_request(
	-varbindlist => \@HostMIB );
	
if (!defined $result) {
	die "Cannot get .1.3.6.1.2.1.43.5.1.1.16.1 from host $host, sorry\n";
} else {
	print "Printing supplies for " . $$result{'.1.3.6.1.2.1.43.5.1.1.16.1'} . "\n";
}

my $ThisValue = 1;
while ($Status == 0) { 
	my @MIB = ();
	my $Description;
	my $FullValue;
	my $CurrentValue;
	my $Align = qq| ALIGN="CENTER"|;
	foreach my $Value (qw(6 7 8 9)) { 
		my $CurMib = "$MIBQuery" . $Value .".1." . $ThisValue;
		push @MIB, $CurMib;
		if ($Value == 6) {
			$Description = $CurMib;
		} elsif ($Value == 8) {
			$FullValue = $CurMib;
		} elsif ($Value == 9) {
			$CurrentValue = $CurMib;
		}
	} 
	my $result = $session->get_request(
		-varbindlist => \@MIB );
	if (!defined $result) {
		$Status = 1;
		# End of listing 
	} else {
		my %h = %{$result};
		printf "%-30.30s %d %%\n", $h{$Description}, int (($h{$CurrentValue} / $h{$FullValue} ) * 100);
	}
	$ThisValue++;
}
exit 0;

