]> jfr.im git - munin-plugins.git/blame - lpstat
support for a 2nd unbound plugin
[munin-plugins.git] / lpstat
CommitLineData
3decc5d1
JR
1#!/usr/bin/perl
2# -*- perl -*-
3
4use strict;
5use warnings;
6
7=head1 NAME
8
9lpstat - Plugin to graph the queue size for the list of printers
10available through the command "lpstat"
11
12=head1 CONFIGURATION
13
14No configuration
15
16=head1 AUTHORS
17
18Anstat Pty Ltd
19Nikolai Langfeldt
20
21=head1 LICENSE
22
23Gnu GPL
24
25=head1 NOTES
26
27This script was initially developed by Anstat Pty Ltd for internal use
28and has kindly been made available to the Open Source community for
29redistribution and further development under the terms of the
30GNU General Public License: http://www.gnu.org/licenses/gpl.html
31
32Readapted 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
42use Getopt::Std;
43
44my $printer;
45my @printers;
46my $status;
47my @jobs;
48my $n_jobs;
49my @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.
56my $host = '127.0.0.1';
57
58my $lpstat = exists $ENV{lpstat} ? $ENV{lpstat} : '';
59
60# If the envvar is not set, look for lpstat
61if (!$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
77if (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...
104if( ! open(LPSTAT_A, "$lpstat $host -a|") ) {
105 print "graph_title Could not execute lpstat command\n";
106 exit -1;
107}
108
109while(<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}
121close(LPSTAT_A);
122
123####################################################
124# Check printers are enabled
125####################################################
126# Get list of printers, showing which are enabled/disabled...
127if( ! open(LPSTAT_P, "$lpstat $host -p|") ) {
128 print "graph_title Could not execute lpstat command\n";
129 exit -1;
130}
131
132my %jobs = ();
133
134while(<LPSTAT_P>) {
135 if ( /^printer\s+(\S+)\s.*disabled/mi ) {
136 $printer=$1;
137 if( grep /^$printer$/, @exclude ) {
138 next;
139 }
140 }
141}
142close(LPSTAT_P);
143
144# Get list of jobs for each printer...
145foreach $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
159if ( defined($ARGV[0]) && $ARGV[0] eq 'config') {
160 print "graph_title Print queues
161graph_args --base 1000
162graph_vlabel Queued jobs
163graph_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
172foreach my $printer (sort(keys %jobs)) {
173 print "$printer.value ",$jobs{$printer},"\n";
174}