#!/usr/bin/perl -w # # Checks HP printers for supplies # # Copyright (c) 2006 Peter Palfrader # # # Based on snmp__supplies, a munin plugin for graphing supplies: # # Copyright (C) Rune Nordboe Skillingstad, Sveinung Marvik # Reports supplies (ie. toner level) on printers adhering to RFC1759 # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; version 2 dated June, # 1991. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. use strict; use English; use Net::SNMP; use Getopt::Long; my $VERSION = '$Revision$'; # nagios exit codes my %CODE = ( 'UNDEF' => -1, 'OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3 ); my $EXITCODE = 'UNDEF'; my $MESSAGE = {}; my $params = { 'port' => 161, 'community' => 'public', 'timeout' => 10, 'warning' => 15, 'critical' => 5, 'verbose' => 0, }; my %cache; my %supplies; my $session; sub version($$) { my ($fd, $exit) = @_; print $fd "nagios-check-printer-supplies $VERSION\n"; print $fd "Copyright (c) 2006 Peter Palfrader \n"; print $fd "Also Copyright Rune Nordboe Skillingstad, Sveinung Marvik\n"; exit 0 if $exit; }; sub help($$) { my ($exitcode, $fd) = @_; version ($fd, 0); print $fd "Usage: $PROGRAM_NAME --version\n"; print $fd "Usage: $PROGRAM_NAME [--verbose [--verbose ..]] [--community ] [--timeout ] [--port ] [--critical ] [--warning ] --host \n"; exit $exitcode }; sub record($$) { my ($newexit, $msg) = @_; die "code $newexit not defined" unless defined $CODE{$newexit}; if ($CODE{$newexit} > $CODE{$EXITCODE}) { $EXITCODE = $newexit; }; push @{$MESSAGE->{$newexit}}, $msg } sub get_multiple($$$) { my $handle = shift; my $oid = shift; my $type = shift; print STDERR "# Getting table $oid...\n" if ($params->{'verbose'} > 0); my $response = $handle->get_table($oid); if(!defined($response)) { record 'UKNOWN', "Did not get a respons when asking for $handle"; } else { for my $key (keys(%{$response})) { $supplies{keyname($key)}{$type} = $response->{$key}; print STDERR "$key -> ".$response->{$key}."\n" if ($params->{'verbose'} > 0); } } } sub keyname($) { my $key = shift; return $cache{$key} if (defined $cache{$key}); my $tkey = $key; $tkey =~ s/.*(\d+\.\d+)$/$1/; $tkey =~ s/\./_/; $cache{$key} = $tkey; return $tkey; } Getopt::Long::config('bundling'); if (!GetOptions ( 'h|help' => \$params->{'help'}, 'V|version' => \$params->{'version'}, 'v|verbose+' => \$params->{'verbose'}, 'H|host=s' => \$params->{'host'}, 'p|port=i' => \$params->{'port'}, 'C|community=s' => \$params->{'community'}, 't|timeout=i' => \$params->{'timeout'}, 'c|critical=i' => \$params->{'critical'}, 'w|warning=i' => \$params->{'warning'}, )) { die ("$PROGRAM_NAME: Usage: $PROGRAM_NAME [-fwhv]\n"); }; version(*STDOUT, 1) if $params->{'version'}; help(0, *STDOUT) if $params->{'help'}; help(1, *STDERR) unless defined $params->{'host'}; help(1, *STDERR) if scalar @ARGV > 0; my $error; ($session, $error) = Net::SNMP->session( -hostname => $params->{'host'}, -community => $params->{'community'}, -port => $params->{'port'}, -timeout => $params->{'timeout'} ); if(!defined ($session)) { die "Croaking: $error"; } get_multiple ($session, "1.3.6.1.2.1.43.11.1.1.6", "desc"); get_multiple ($session, "1.3.6.1.2.1.43.11.1.1.8", "max"); get_multiple ($session, "1.3.6.1.2.1.43.11.1.1.9", "level"); # Get rid of supply-levels reporting negative values { for my $supply (keys (%supplies)) { if ($supplies{$supply}{level} < 0) { delete $supplies{$supply}; print STDERR "# Deleting entry $supply: supply level unknown.\n" if ($params->{'verbose'} > 0); } } } # Values if (keys(%supplies) > 0) { for my $supply (keys(%supplies)) { my $level = ($supplies{$supply}{level}/$supplies{$supply}{max})*100; my $desc = $supplies{$supply}{desc}; $level = sprintf("%.2f", $level); if ($level < $params->{'critical'}) { record 'CRITICAL', "$desc is at $level"; } elsif ($level < $params->{'warning'}) { record 'WARNING', "$desc is at $level"; } else { record 'OK', "$desc is at $level"; } } } if ($EXITCODE eq 'UNDEF') { record 'UNKNOWN', "no data found" } my @msg; my $message = ''; for my $i (qw{UNKNOWN CRITICAL WARNING OK}) { push @msg, @{$MESSAGE->{$i}} if defined $MESSAGE->{$i}; }; print $EXITCODE, ': ', join('; ', @msg). "\n"; exit $CODE{$EXITCODE};