-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_printers.pl
More file actions
48 lines (37 loc) · 1.04 KB
/
check_printers.pl
File metadata and controls
48 lines (37 loc) · 1.04 KB
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
#!/usr/bin/perl
use strict;
use warnings;
# Initialize error variable
my $error = 0;
# Declare @output array outside the loop
my @output;
# Run lpstat -a command
my @lpstat_output = `lpstat -a`;
# Process lpstat -a output
foreach my $line (@lpstat_output) {
chomp $line;
my ($printer_name, $status) = split /\s+/, $line;
# Check if printer is not accepting jobs
if ($status ne 'accepting') {
$error++;
# Add errored line to @output array
push @output, $line;
}
# Run lpstat -p <printer_name> -l
my @lpstat_p_output = `lpstat -p $printer_name -l`;
# Check if the printer is enabled
if (!grep { /printer $printer_name is idle. enabled/ } @lpstat_p_output) {
$error++;
# Add errored line to @output array
push @output, @lpstat_p_output;
}
}
# Display results
if ($error == 0) {
print "OK - All printers are accepting jobs and enabled.\n";
exit 0;
} else {
print "WARNING - Number of errors found: $error\n";
print join("\n", @output), "\n";
exit 1;
}