File: lpd.monitor

package info (click to toggle)
mon 1.2.0-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,860 kB
  • ctags: 538
  • sloc: perl: 15,847; ansic: 774; sh: 330; makefile: 46
file content (378 lines) | stat: -rwxr-xr-x 7,405 bytes parent folder | download | duplicates (9)
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
#!/usr/bin/perl
#
# Try to connect to an lpd server and get status of print queues.
# For use with "mon".
#
# lpd.monitor [-l] [-d] [-s secs] [-p port] [-t secs] -h host queue [queue...]
#
#    -l            interpret queue output as lprng (error, status, etc.)
#    -d            do not show detail
#    -e            report queues with "error" jobs
#    -s secs       report jobs stalled longer than "secs" as an error
#    -h host       host running lpd
#    -p port       TCP port to connect to (defaults to 515)
#    -t secs       timeout, defaults to 30
#
# "get" routine based on other monitors written by Jon Meek
#
# $Id: lpd.monitor,v 1.1.1.1 2004/06/09 05:18:05 trockij Exp $
#
#    Copyright (C) 2001, 2002, Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use Getopt::Std;
use English;

sub lpdGET;
sub check_lprng;
sub OpenSocket;

getopts ("deh:s:lp:t:P:");
$PORT = $opt_p || 515;
$TIMEOUT = $opt_t || 30;
$STALLED = $opt_s || 60; # stalled 1 mins
$HOST = $opt_h || die "no host supplied with -h\n";

my %good;
my %bad;
my %details;

exit 0 if (!@ARGV);

foreach my $queue (@ARGV)
{
    my $result = lpdGET ($HOST, $PORT, $queue);

    if (!$result->{"ok"})
    {
    	$bad{$queue} = $result;
    }

    #
    # look in lprng output for bad things
    #
    elsif ($opt_l)
    {
	my $err = check_lprng ($result->{"header"});

	$details{$queue} = $err->{"fields"};

	if (!$err->{"ok"})
	{
	    $bad{$queue} = $result;
	    $bad{$queue}->{"error"} = $err->{"error"};
	}

	else
	{
	    $good{$queue} = $result;
	}
    }

    else
    {
    	$good{$queue} = $result;
    }
}

my $ret;

if (keys %bad)
{
    $ret = 1;
    print join (" ", sort keys %bad), "\n";
}

else
{
    $ret = 0;
    print "\n";
}

#
# show detail
#
if (!$opt_d)
{
    #
    # failure detail
    #
    foreach my $q (keys %bad)
    {
	print "------------------------------------------------------------------------------\n";
	print "HOST $HOST QUEUE $q: $bad{$q}->{error}\n";
	print $details{$q}->{"Printer"}, "\n";
	print "------------------------------------------------------------------------------\n";

	if ($opt_l)
	{
	    # this will probably never be true
	    if ($details{$q}->{"queuelist"} eq "")
	    {
	    	print "queue empty\n";
	    }

	    else
	    {
		print $details{$q}->{"queuelist"};
	    }
	}

	elsif ($bad{$q}->{"header"} ne "")
	{
	    print $bad{$q}->{"header"}, "\n";
	}

	print "\n";
    }

    if (keys %good)
    {
    	print <<'EOF';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%                                                                %%%%%%%
%%%%%%% the following are queues which have no problems at this moment %%%%%%%
%%%%%%%                                                                %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
EOF
    }

    #
    # non-failure detail
    #
    foreach my $q (keys %good)
    {
	print "------------------------------------------------------------------------------\n";
	print "HOST $HOST QUEUE $q: ok\n";
	print $details{$q}->{"Printer"}, "\n";
	print "------------------------------------------------------------------------------\n";

	if ($opt_l)
	{
	    if ($details{$q}->{"queuelist"} eq "")
	    {
	    	print "queue empty\n";
	    }

	    else
	    {
		print "$details{$q}->{queuelist}\n";
	    }
	}

	else
	{
	    print $good{$q}->{"header"}, "\n";
	}

	print "\n";
    }
}

exit $ret;


sub lpdGET
{
    use Socket;
    use Sys::Hostname;

    my($Server, $Port, $Queue) = @_;
    my($ServerOK, $TheContent);

    $TheContent = '';

    my $result;

    eval
    {
	local $SIG{ALRM} = sub { die "Timeout Alarm" };
	alarm $TIMEOUT;

	my $err = &OpenSocket($Server, $Port); # Open a connection to the server

	if ($err ne "") { # Failure to open the socket
	    $result = {
	    	"ok" => 0,
		"error" => $err,
		"header" => undef,
	    };

	    return undef;
	}

	#
	# lpd queue list
	# "short" listing command is 0x03
	# "long" listing command is 0x04
	#
	print S "\x04$Queue\x0a";

	$/ = "\x0a";

	while (defined ($in = <S>)) {
	    $TheContent .= $in;  # Store data for later processing
	}

	close(S);
	alarm 0; # Cancel the alarm

	$ServerOK = 1;
    };

    if ($EVAL_ERROR and ($EVAL_ERROR =~ /^Timeout Alarm/)) {
	return {
	    "ok" => 0,
	    "error" => "timeout after $TIMEOUT seconds",
	    "header" => $TheContent,
	};
    }

    if ($result->{"error"} ne "")
    {
    	return $result;
    }



    return {
    	"ok" => $ServerOK,
	"header" => $TheContent,
	"error" => undef,
    };
}


#
# look for badness in lprng output
#       error
#       stalled > $opt_n secs
#
sub check_lprng
{
    my ($buff) = @_;

    my $in_rank = 0;
    my $fail = 0;

    my $status = {
    	"ok" => 1,
	"fields" => {},
	"error" => "",
    };

    foreach my $l (split (/\x0d?\x0a/sm, $buff))
    {
	#
	# sort data
	#
	if ($l =~ /^\s+([^:]+):\s+(.*)$/)
	{
	    $status->{"fields"}->{$1} .= $2;
	}

	elsif ($l =~ /^Printer:\s+(.*)/)
	{
	    $status->{"fields"}->{"Printer"} = $1;
	}

	if ($l =~ /^\s+Rank.*Owner/)
	{
	    $in_rank++;
	    $status->{"fields"}->{"queuelist"} = "";
	}

	if ($in_rank)
	{
	    $status->{"fields"}->{"queuelist"} .= "$l\n";
	}

	#
	# check for errors
	#
	if ($in_rank && $opt_e && $l =~ /^error/ && !$fail)
	{
	    $status->{"ok"} = 0;
	    $status->{"error"} = "job error";

	    $fail = 1;
	}

	elsif ($in_rank && $l =~ /^stalled\((\d+)sec/ && $1 > $STALLED && !$fail)
	{
	    $status->{"ok"} = 0;
	    $status->{"error"} = "job stalled $1 seconds";

	    $fail = 1;
	}

	elsif ($in_rank && $l =~ /^active\(attempt-(\d+)/ && !$fail)
	{
	    $status->{"ok"} = 0;
	    $status->{"error"} = "multiple attempts, currently $1";

	    $fail = 1;
	}

    }

    return $status;
}


#
# Make a Berkeley socket connection between this program and a TCP port
# on another (or this) host. Port can be a number or a named service
#
# returns "" on success, or an error string on failure
#
sub OpenSocket {
    my ($host, $port) = @_;

    my $proto = (getprotobyname('tcp'))[2];

    return ("could not get protocol") if (!defined $proto);

    my $conn_port;

    if ($port =~ /^\d+$/) {
    	$conn_port = $port;

    } else {
	$conn_port = (getservbyname($port, 'tcp'))[2];
	return ("could not getservbyname for $port")
		if (!defined $conn_port);
    }

    my $host_addr = (gethostbyname($host))[4];

    return ("gethostbyname failure")
    		if (!defined $host_addr);

    my $that = sockaddr_in ($conn_port, $host_addr);

    if (!socket (S, &PF_INET, &SOCK_STREAM, $proto)) {
    	return ("socket: $!");
    }

    if (!connect (S, $that)) {
    	return ("connect: $!");
    }

    select(S); $| = 1; select(STDOUT);

    "";
}