From e3559d2997ddb4a22a4eab7403c6fbdbef1fb709 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Thu, 10 Aug 2006 12:11:18 +0000 Subject: Add nagios-check-printer-supplies git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@172 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- nagios-check-printer-supplies | 195 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100755 nagios-check-printer-supplies diff --git a/nagios-check-printer-supplies b/nagios-check-printer-supplies new file mode 100755 index 0000000..92b84a8 --- /dev/null +++ b/nagios-check-printer-supplies @@ -0,0 +1,195 @@ +#!/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 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 help($$) { + my ($exitcode, $fd) = @_; + print $fd "Usage: $PROGRAM_NAME --version\n"; + print $fd "Usage: $PROGRAM_NAME [--verbose [--verbose ..]] [--community ] [--timeout ] [--port ] [--critical ] [--warning ] --host \n"; + exit $exitcode +}; + +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"); +}; + +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}; -- cgit v1.2.3