]> jfr.im git - munin-plugins.git/blob - lpstat
support for a 2nd unbound plugin
[munin-plugins.git] / lpstat
1 #!/usr/bin/perl
2 # -*- perl -*-
3
4 use strict;
5 use warnings;
6
7 =head1 NAME
8
9 lpstat - Plugin to graph the queue size for the list of printers
10 available through the command "lpstat"
11
12 =head1 CONFIGURATION
13
14 No configuration
15
16 =head1 AUTHORS
17
18 Anstat Pty Ltd
19 Nikolai Langfeldt
20
21 =head1 LICENSE
22
23 Gnu GPL
24
25 =head1 NOTES
26
27 This script was initially developed by Anstat Pty Ltd for internal use
28 and has kindly been made available to the Open Source community for
29 redistribution and further development under the terms of the
30 GNU General Public License: http://www.gnu.org/licenses/gpl.html
31
32 Readapted to munin by Nikolai Langfeldt for Oslo Airport
33
34 =head1 MAGIC MARKERS
35
36 #%# family=auto
37 #%# capabilities=autoconf
38
39 =cut
40
41
42 use Getopt::Std;
43
44 my $printer;
45 my @printers;
46 my $status;
47 my @jobs;
48 my $n_jobs;
49 my @exclude; # Should take this from environment.
50
51 # Force C output from lpstat
52 $ENV{'LC_MESSAGES'} = "C";
53
54 # This is a dumb-down. Should take hostname(s) from environment or
55 # as wildcard plugin.
56 my $host = '127.0.0.1';
57
58 my $lpstat = exists $ENV{lpstat} ? $ENV{lpstat} : '';
59
60 # If the envvar is not set, look for lpstat
61 if (!$lpstat) {
62 # Still not found? Check obvious places
63 my @dirs = split(':',$ENV{PATH});
64 push (@dirs, qw(/usr/bin /usr/sbin /usr/local/bin /usr/local/sbin) );
65
66 until ($lpstat or @dirs == 0) {
67 my $dir = shift @dirs;
68 my $path = $dir.'/lpstat';
69 $lpstat = $path if -x $path;
70 }
71 } elsif (! -x $lpstat) {
72 # If it is set, verify it
73 warn "Predefined lpstat ($lpstat) is not a executable\n";
74 undef $lpstat;
75 }
76
77 if (defined($ARGV[0]) && $ARGV[0] eq 'autoconf') {
78 if( ! -x $lpstat ) {
79 print "no (lpstat not found)\n";
80 exit 0;
81 }
82 if( ! open(LPSTAT_R, "$lpstat $host -v 2>/dev/null |") ) {
83 print "no (could not execute lpstat)\n";
84 exit 0;
85 }
86 $_ = <LPSTAT_R>;
87 if ( ! close(LPSTAT_R) ) {
88 print "no (lpstat returned non-zero)\n";
89 exit 0;
90 }
91
92 if (! m/device for /mi) {
93 print "no (no printers configured)\n";
94 exit 0;
95 }
96 print "yes\n";
97 exit 0;
98 }
99
100 ####################################################
101 # Check printers are accepting jobs
102 ####################################################
103 # Get list of printers, showing which are accepting jobs...
104 if( ! open(LPSTAT_A, "$lpstat $host -a|") ) {
105 print "graph_title Could not execute lpstat command\n";
106 exit -1;
107 }
108
109 while(<LPSTAT_A>) {
110 chomp;
111 /(\S+) (.*) since/mi ;
112 $printer = $1;
113 $status = $2;
114 if( grep /^$printer$/, @exclude ) {
115 next;
116 }
117 if( /accepting/ ) {
118 @printers = ( @printers, $printer );
119 }
120 }
121 close(LPSTAT_A);
122
123 ####################################################
124 # Check printers are enabled
125 ####################################################
126 # Get list of printers, showing which are enabled/disabled...
127 if( ! open(LPSTAT_P, "$lpstat $host -p|") ) {
128 print "graph_title Could not execute lpstat command\n";
129 exit -1;
130 }
131
132 my %jobs = ();
133
134 while(<LPSTAT_P>) {
135 if ( /^printer\s+(\S+)\s.*disabled/mi ) {
136 $printer=$1;
137 if( grep /^$printer$/, @exclude ) {
138 next;
139 }
140 }
141 }
142 close(LPSTAT_P);
143
144 # Get list of jobs for each printer...
145 foreach $printer ( @printers ) {
146 if( grep /^$printer$/, @exclude ) {
147 next;
148 }
149
150 if( ! open(LPSTAT, "$lpstat $host -o $printer|") ) {
151 print STDERR "Could not execute command: '$lpstat -o $printer' \n";
152 exit 2;
153 }
154 @jobs = ( <LPSTAT> );
155 $n_jobs = @jobs;
156 $jobs{$printer}=$n_jobs || 0;
157 }
158
159 if ( defined($ARGV[0]) && $ARGV[0] eq 'config') {
160 print "graph_title Print queues
161 graph_args --base 1000
162 graph_vlabel Queued jobs
163 graph_category printing
164 ";
165 foreach my $printer (sort(keys %jobs)) {
166 print "$printer.label $printer\n";
167 print "$printer.type GAUGE\n";
168 }
169 exit 0;
170 }
171
172 foreach my $printer (sort(keys %jobs)) {
173 print "$printer.value ",$jobs{$printer},"\n";
174 }