summaryrefslogtreecommitdiff
path: root/nagios-check-printer-supplies
blob: 0d1abd17625240407c90f770ad0e36e2844d7dbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/perl -w
#
# Checks HP printers for supplies
#
# Copyright (c) 2006 Peter Palfrader <peter@palfrader.org>
#
#
# 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 <peter\@palfrader.org>\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 <community>] [--timeout <timeout>] [--port <port>] [--critical <critical>] [--warning <warning>] --host <hostname>\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};