This file is indexed.

/usr/lib/nagios/plugins/check_printer is in nagios-plugins-contrib 21.20170222.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/perl
# nagios: -epn
#
# Copyright (c) 2007,2011 Eric F Crist
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or 
# without modification, are permitted provided that the following 
# conditions are met:
#
# Redistributions of source code must retain the above copyright 
# notice, this list of conditions and the following disclaimer.
# 
# Redistributions in binary form must reproduce the above copyright 
# notice, this list of conditions and the following disclaimer in 
# the documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id: check_printer 363 2014-07-23 14:37:59Z ecrist $
# $HeadURL: trunk/nagios/check_printer $

use strict;
use warnings;

sub load_module {
    my @names = @_;
    my $module;
    for my $name (@names) {
        my $file = $name;
        # requires need either a bare word or a file name
        $file =~ s{::}{/}gsxm;
        $file .= '.pm';
        eval {
            require $file;
            $name->import(qw(%ERRORS));
            $module = $name;
		};
		last if $module;
    }
    return $module;
}

my $plugin_module;
BEGIN {
	$plugin_module = load_module( 'Monitoring::Plugin', 'Nagios::Plugin' );
}

use Getopt::Long;
use Pod::Usage;

'$Revision: 363 $' =~ m/Revision: (\d+)/;
my $revision = $1;
'$HeadURL: trunk/nagios/check_printer $' =~ m/HeadURL: ([\w\:\/\-\.\_]+) /;
my $src_url = $1;
my $debug = 0;
my ($host, $warning, $critical, @ignore);
my $community = "public";

our %getopts;
Getopt::Long::Configure("bundling");
GetOptions(
	"h|host=s"	=>	\$host,
	"C|community=s"	=>	\$community,
	"w|warn=i"	=>	\$warning,
	"c|crit=i"	=>	\$critical,
	"i=i"		=>	\@ignore,
	'V|version'	=>	sub { print '$Id: check_printer 363 2014-07-23 14:37:59Z ecrist $' ."\n"; exit 0 },
	"d|debug"	=>	\$debug,
	"man"		=>	sub { pod2usage(-exitstatus => 0, -verbose => 2); exit 0 }
) or pod2usage(2);


pod2usage(2)  unless (defined($host));

my $base_oid = ".1.3.6.1.2.1.43.11.1.1";
my $type_oid = "3.1";
my $name_oid = "6.1";
my $uom_oid = "7.1";
my $max_oid  = "8.1";
my $curr_oid = "9.1";
my $loop;



my $comm = $ARGV[1];

my @vars = ("snmpwalk -On -v 1 -c $community $host $base_oid.$name_oid",
	   "snmpwalk -On -v 1 -c $community $host $base_oid.$uom_oid",
	   "snmpwalk -On -v 1 -c $community $host $base_oid.$max_oid",
	   "snmpwalk -On -v 1 -c $community $host $base_oid.$curr_oid");

my(@values, @names, @max, @min);

our %uom = (     "4" => "µm",
		"7" => "impressions",
		"8" => "pages",
		"11" => "hrs",
		"12" => "oz/1000",
		"13" => "gr/10",
		"14" => "gr/100",
		"15" => "mL",
		"16" => "feet",
		"17" => "meters",
		"18" => "items",
		"19" => "%");

foreach(@vars){
	@values = (@values, split /\n/, `$_`);
	if ($? != 0){
		print "Printer Supplies UNKNOWN";
		exit 3;
	}
}

if ($debug){
        use Data::Dumper;
	print Dumper(\@values);
}
my %finvalues;

my $ignore = join('|', @ignore);

foreach(@values){
	# matching the following line
	#            $1                    $2           $3 
	# .1.3.6.1.2.1.43.11.1.1.6.1.1 = STRING: "Black Cartridge"
	$_ =~ m/^([\.\d]+) = ([\w-]+):[\s\"]+([\(\)\-\d\w\s,\/:]+)\"*$/ or next;
	my $loid = $1;
	my $ltype = $2;
	my $string = $3;
	if ($loid =~ m/\.(${ignore})$/){
		next;
	}
	if ($ltype =~ m/HEX/i){
		# hex string, convert to ascii
		$string = join "", map {chr hex $_ } split m/ /, $string;
	}
	$finvalues{"$loid"} = $string;
}

my %status;
# get OID field numbers for matching later
while (my ($key, $value) = each(%finvalues)){
	## sort this array into an associative in the following format:
	#    $status['name']   : Supply Name
	#    $status['curr']   : Supply Status
	#    $status['max']    : Supply Level When New
	my $search = quotemeta("$base_oid.$name_oid");
	if ($key =~ /^$search\.([\d\.]+)$/){
		## we'll assume a name
		$status{$value}{"sub"} = $1;
	} else {
	}
}
# get maximum values,  matching on OID field number
while (my ($key, $value) = each(%finvalues)){
	my $search = quotemeta("$base_oid.$max_oid");
	if ($key =~ /^$search\.([\d\.]+)$/){
		## assign it to the other variable
		## search the existing keys, find the one we want
		foreach my $subvalue (values %status){
			if ($subvalue->{"sub"} eq $1){
				$subvalue->{"max"} = $value;
			}
		}
	}
}

# get current value, matching on OID field number
while (my ($key, $value) = each(%finvalues)){
	my $search = quotemeta("$base_oid.$curr_oid");
	if ($key =~ /^$search\.([\d\.]+)$/){
		## assign it to the other variable
		## search the existing keys, find the one we want
		foreach my $subvalue (values %status){
			if ($subvalue->{"sub"} eq $1){
				$subvalue->{"curr"} = $value;
			}
		}
	}
}

# get uom, add it to data
while (my ($key, $value) = each(%finvalues)){
	my $search = quotemeta("$base_oid.$uom_oid");
	if ($key =~ /^$search\.([\d\.]+)$/){
		## assign it to the other variable
		## search the existing keys, find the one we want
		foreach my $subvalue (values %status){
			if ($subvalue->{"sub"} eq $1){
				$subvalue->{"uom"} = $uom{$value};
			}
		}
	}
}

## Assemble performance data and error string.
my $perf_str = "";
my $is_warn = 0;
my $is_crit = 0;
my $err_str = "";
while (my($key, $value) = each(%status)){
	my ($maximum, $current);
	if (!exists($value->{"curr"}) || $value->{"curr"} == -3){
		## we can process as yes/no
		$current = 100;
		## Override the critical/warning, since they're moot
		$critical = 0;
		$warning = 0;
	} elsif (($value->{"max"} == -2) and ($value->{"uom"} ne '%')){
		## we don't know (-2 == unknown) the max and can't compute
		next;
	} else {
		$maximum = $value->{"max"};
	}
	
	if (!exists($value->{"curr"}) || $value->{"curr"} < 0){
		## weird value, set to 0 for math reasons
		$current = 0;
	} else {
		$current = round(($value->{"curr"} / $maximum) * 100);
	}

	my $lcurrent;
	if (($key =~ m/waste/i) or ($key =~ m/Toneruppsamlare/i) or ($key =~ m/Restonner/i)){
		# invert the $current as waste is calculated in reverse
		if ($value->{"curr"} == -3){
			$lcurrent = 0;
		} else {
			$lcurrent = $maximum - $current;
		}
	} else {
		$lcurrent = $current;
	}
	if ($lcurrent < $critical){
		$is_crit = $is_crit + 1;
		if ($err_str eq ""){
			$err_str = "$key";
		} else {
			$err_str = "$err_str, $key";
		}
	} elsif ($lcurrent < $warning){
		$is_warn = $is_warn + 1;
		if ($err_str eq ""){
			$err_str = "$key";
		} else {
			$err_str = "$err_str, $key";
		}
	}
	if ($debug){
		print "Key: $key\nCur: $current\nWarn: $warning\nCrit: $critical\n";
	}
	$perf_str = "$perf_str '$key'=$current%;$warning;$critical;0;100 ";
}

if ($debug){
	print Dumper(\%status);
	print "\n\n############ ATTENTION ############\n";
        print "You have debug enabled.  If asked to enable debug by the Debian Maintainer,\n";
        print "please send all of the output to the Debian Bug Tracking System, either by \n";
        print "replying to an existing bug or by opening a new bug.\n";

	print '$Id: check_printer 363 2014-07-23 14:37:59Z ecrist $'."\n\n";
}
if ($is_crit){
	print "$err_str CRITICAL.  See http://$ARGV[0] | $perf_str\n";
	exit 2;
} elsif ($is_warn){
	print "$err_str WARNING. See http://$ARGV[0] | $perf_str\n";
	exit 1;
} else {
	print "Printer Supplies OK | $perf_str\n";
	exit 0;
}


###
### subroutines here
###

sub round {
	my($number) = shift;
	return int($number + .5 * ($number <=> 0));
}

__END__

=head1 check_printer

check_printer - Gather supply metrics from a printer via SNMP query

=head1 SYNOPSIS

check_printer [options]

Options:
	
	-h, --host		Printer hostname or IP address.
	-C, --community		SNMP community string, defaults to "public"
	-w, --warn		Warning threshold
	-c, --crit		Critical threshold
	-i, --ignore		Supply ignore.  See man page for more information (--man)
	-V, --version		Prints version string and exits
	-d, --debug		Prints debug output, useful when reporting a problem to the developer, or getting index numbers for --ignore
	--man			Prints man page

=head1 DESCRIPTION

B<check_printer> is a Perl script that retrieves supply metrics from printers using the snmpwalk command.

=head1 OPTIONS

=over

=item -h, --host

Defines the host or IP address passed to snmpwalk.

=item -C, --community

Identifies the SNMP community string.  Requires read access only.

=item -w, --warn

Value at which a warning is generated.  For example, if -w 10 is set, when 
supply levels fall to 10% or below, a warning will be sent as part of the 
performance data.

=item -c, --crit

Value at which a critical alert is generated.  This number should be lower
than --warn is set, but this is not enforced.

=item -i, --ignore

Supply index that should be ignore from the output.  Multiple values are 
accepted, separately:

=over

check_printer -i 1 -i 2

=back

Will exclude supply indexes 1 and 2.  To get the supply index list, run the 
check_printer utility with --debug defined and the top of the output will
list available supply metrics as follows:

=over

$VAR1 = [
    '.1.3.6.1.2.1.43.11.1.1.6.1.1 = STRING: "Black Toner"',
    '.1.3.6.1.2.1.43.11.1.1.6.1.2 = STRING: "Drum Unit"',
    '.1.3.6.1.2.1.43.11.1.1.7.1.1 = INTEGER: 13',
    '.1.3.6.1.2.1.43.11.1.1.7.1.2 = INTEGER: 7',
    '.1.3.6.1.2.1.43.11.1.1.8.1.1 = INTEGER: -2',
    '.1.3.6.1.2.1.43.11.1.1.8.1.2 = INTEGER: -2',
    '.1.3.6.1.2.1.43.11.1.1.9.1.1 = INTEGER: -3',
    '.1.3.6.1.2.1.43.11.1.1.9.1.2 = INTEGER: 0'
];

=back

In the above example, we are looking for the OID string starting with 
B<.1.3.6.1.2.1.43.11.1.1.6.1> and the index is the last number. To exclude the
Drum Unit, pass I<-i 2> to check_printer

=item -V, --version

Print the check_printer version string and exits.

=item -d, --debug

Outputs debug information.  If you are having problems with a printer or device,
send the debug output to B<ecrist@secure-computing.net> and identify your printer
model and manufacturer.

=item --man

Prints this man page.  :)