summaryrefslogtreecommitdiff
path: root/nagios-check-printer-status
blob: dfb840f2fa99b9e313adb76aff86051f1c6314ba (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
#!/usr/bin/perl

# really quick and dirty

# my %ERRORS = ( OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => -1 );

use English;
use strict;

my %CODE = (
	'UNDEF'		=> -1,
	'OK'		=> 0,
	'WARNING'	=> 1,
	'CRITICAL'	=> 2,
	'UNKNOWN'	=> 3
);

sub usage($$) {
	my ($fh, $exit) = @_;
	print $fh "Usage: $PROGRAM_NAME -H <host> -p <printer>\n";
	exit $exit;
}

my $host;
my $printer;

while (@ARGV) {
	my $a = shift @ARGV;

	if ($a eq '-h') {
		usage(*STDOUT,0);
	};
	if ($a eq '-H') {
		usage(*STDERR,1) unless @ARGV;
		$host = shift;
		next;
	};
	if ($a eq '-p') {
		usage(*STDERR,1) unless @ARGV;
		$printer = shift;
		next;
	};

	usage(*STDERR,1);
}
usage(*STDERR,1) unless $host;
usage(*STDERR,1) unless $printer;
if ($host =~ /[^A-Za-z0-9-.]/) {
	die "Evil chars in hostname '$host'\n";
}
if ($printer =~ /[^A-Za-z0-9-.]/) {
	die "Evil chars in printername '$printer'\n";
}

my $exit = 'OK';
my @msg;

open(LP, "/usr/bin/lpstat -h $host -p $printer |") or die ("Cannot exec/open lpstat: $!\n");
while (<LP>) {
	chomp;
	if (/disabled/) {
		$exit = 'CRITICAL';
	}
	push @msg, $_;
};
close (LP);

print "$exit: ".(join "; ", @msg)."\n";
exit $CODE{$exit};