This file is indexed.

/usr/lib/nagios/plugins/check_cups is in nagios-plugins-contrib 9.20140106.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/usr/bin/perl
# nagios: -epn

=head1 NAME

check_cups - monitor CUPS queues

=head1 AUTHOR

Steve Huff <shuff@hmdc.harvard.edu>

=head1 SYNOPSIS

Polls a CUPS server to discover queues, then reports on queue status.

=head1 REQUIRES

Perl5.004, strict, warnings, Data::Dumper, Nagios::Plugin, Net::CUPS, Date::Manip, POSIX

=head1 EXPORTS

Nothing

=head1 DESCRIPTION

C<check_cups> polls the specified CUPS server and discovers print queues.  It tests against a queue length threshold and an age of the oldest job in the queue threshold.

Performance data (queue length and maximum queue age) is provided.

Future development plans include the option to specify the number of expected print queues.

=cut

# these need to be outside the BEGIN
use strict;
use warnings;

# see this page for an explanation regarding the levels of warnings:
# http://search.cpan.org/~rgarcia/perl-5.6.2/pod/perllexwarn.pod
no warnings qw( redefine prototype );

BEGIN {

    # use Opsview libs
    use lib '/usr/local/nagios/perl/lib';
    use lib '/usr/local/nagios/lib';

    use Nagios::Plugin;

    use Data::Dumper;

    use Date::Manip;
    use POSIX qw( strftime );

    use Net::CUPS;

    # reregister interrupt
    $SIG{INT}      = \&DoInterrupt;

} ## end BEGIN

########################################################################
# IMPORTANT VARIABLES
#
# default values
my( $timeout, $verbose );
my $WARNINGJOBS     = 6;
my $CRITICALJOBS    = 10;
my $WARNINGAGE      = '20 minutes';
my $CRITICALAGE     = '1 hour';

#
########################################################################

#
# MAIN
#

# instantiate the Nagios::Plugin object
my( $usagemsg ) = <<USAGE;
Usage: %s -H <hostname> [-w <number of queued jobs>] [-c <number of queued jobs>] [-W <age of oldest job in queue>] [-C <age of oldest job in queue>] [-t timeout] [-v|d] [-h]
USAGE
my( $blurb ) = <<BLURB;
check_cups polls the specified CUPS server and discovers print queues.  It tests against a queue length threshold and an age of the oldest job in the queue threshold.

Performance data (queue length and maximum queue age) is provided.

Future development plans include the option to specify the number of expected print queues.
BLURB
my( $license ) = <<LICENSE;
This Nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. 
It may be used, redistributed and/or modified under the terms of the GNU 
General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).

This plugin was written at The Harvard-MIT Data Center
(http://www.hmdc.harvard.edu) by Steve Huff (<shuff\@hmdc.harvard.edu>).
LICENSE
my( $plugin ) = Nagios::Plugin->new( 
                                    shortname   => 'check_cups',
                                    usage       => $usagemsg,
                                    version     => '0.2',
                                    blurb       => $blurb,
                                    license     => $license,
                                );

=head2 Plugin Metadata

Metadata is documented in L<Nagios::Plugin> and L<Nagios::Plugin::Getopt>.

Other available options of interest include C<url>, C<extra>, and C<timeout>.

=cut

# add the additional options
$plugin->add_arg( 
                 spec       => 'hostname|H=s', 
                 help       => [ 
                                "Hostname to query",
                                "IP address to query",
                               ],
                 label      => [ 'HOSTNAME', 'IP' ],
                 required   => 1,
             ); 
            
# queued jobs
$plugin->add_arg( 
                 spec       => 'warning|w=i', 
                 help       => "Warning threshold in number of queued jobs (default $WARNINGJOBS)",
                 default    => $WARNINGJOBS,
                 label      => [ 'QUEUED JOBS' ],
                 required   => 0,
             ); 
            
$plugin->add_arg( 
                 spec       => 'critical|c=i', 
                 help       => "Critical threshold in number of queued jobs (default $CRITICALJOBS)",
                 default    => $CRITICALJOBS,
                 label      => [ 'QUEUED JOBS' ],
                 required   => 0,
             ); 
            
# job age
$plugin->add_arg( 
                 spec       => 'warningage|W=s', 
                 help       => "Warning threshold of job age (default '$WARNINGAGE')",
                 default    => $WARNINGAGE,
                 label      => [ 'TIME SPECIFICATION' ],
                 required   => 0,
             ); 
            
$plugin->add_arg( 
                 spec       => 'criticalage|C=s', 
                 help       => "Critical threshold of job age (default '$CRITICALAGE')",
                 default    => $CRITICALAGE,
                 label      => [ 'TIME SPECIFICATION' ],
                 required   => 0,
             ); 
            
=head2 Plugin Options

Refer to L<Nagios::Plugin::Getopt> for option specifications - they differ slightly from pure L<Getopt::Long>-style option specifications.

A number of standard options (C<--help>, C<--version>, C<--usage>, C<--timeout>, and C<--verbose>) are implemented by default, thanks to the arguments passed to C<Nagios::Plugin->new()>, and do not need to be specified.  Moreover, if you attempt to override them, you will simply create two such options, which produces confusing C<--help> output and unexpected behavior.
            
=cut

# parse options
$plugin->getopts;

my( $opts ) = $plugin->opts;

# declare variables
my( $server, $queues, $warningjobs, $criticaljobs, $warningage, $criticalage );

# every variable must have a value
$server = $opts->get( 'hostname' );
$timeout = $opts->get( 'timeout' );  # See above: option provided by default
$verbose = $opts->get( 'verbose' );  # See above: option provided by default
$warningjobs = $opts->get( 'warning' );
$criticaljobs = $opts->get( 'critical' );
$warningage = $opts->get( 'warningage' );
$criticalage = $opts->get( 'criticalage' );

# sanity check
defined( $server )
    or $plugin->nagios_die( "No value provided for --hostname!" );

# parse time differentials
my( $nowdate ) = ParseDate( 'now' );

my( $warningdelta ) = ParseDateDelta( $warningage );
unless( defined( $warningdelta ) ) {

    $plugin->nagios_die( "'$warningage' is not a valid time specification!" );
}

my( $criticaldelta ) = ParseDateDelta( $criticalage );
unless( defined( $criticaldelta ) ) {

    $plugin->nagios_die( "'$criticalage' is not a valid time specification!" );
}

my( $warningminutes ) = sprintf( '%d', Delta_Format( $warningdelta, 'approx', 0, '%mt' ) );
my( $criticalminutes ) = sprintf( '%d', Delta_Format( $criticaldelta, 'approx', 0, '%mt' ) );

# critical thresholds must be greater than warning thresholds
if ( $warningjobs >= $criticaljobs ) {

    $plugin->nagios_die( "Job number warning threshold ($warningjobs) must be less than critical threshold ($criticaljobs)." );
}

if ( $warningminutes >= $criticalminutes ) {

    $plugin->nagios_die( "Job age warning threshold ($warningage) must be less than critical threshold ($criticalage)." );
}

=head2 Testing and Exit Status

The basic methodology of a Nagios plugin is as follows:

=over

=item 1.

Run some sort of test.

=item 2.

Validate the result against provided (or default) thresholds.

=item 3.

Exit with the appropriate error code, optionally outputting text.

=back

Running the test is up to you.  

The function to use for validating the result is C<Nagios::Plugin::check_threshold()> (see L<Nagios::Plugin::Threshold> and L<Nagios::Plugin::Range> for details and specifications), which returns a value appropriate for use as an exit code.

The functions to use for exiting are C<Nagios::Plugin::nagios_exit()> and C<Nagios::Plugin::nagios_die()>.  C<nagios_exit()> is the general-purpose exit function, while C<nagios_die()> should be used for any unexpected exits and indicates that something went wrong with the plugin's operation.

=head3 Return String

The plugin return string performs two functions:

=over

=item 1.

It communicates in human-readable format more details about what exactly has gone wrong (e.g. "Disk usage on '/var' is at 92% (threshold 90%)").

=item 2.

It (optionally) communicates in machine-readable format performance data which can be aggregated by tools such as Nagiosgraph (e.g. '| time=0.042745s;5.000000;10.000000;0.000000 size=2162B;;;0').  See L<Nagios::Plugin::Performance> for details and specifications; the function to use is C<Nagios::plugin::add_perfdata()>.

=back

The plugin string will automatically be populated with the plugin name and the exit status (e.g. "check_snmp_disk OK - "); everything after the "- " is what you provide with the message arguments to C<nagios_exit()> or C<nagios_die()>.  C<add_perfdata()> takes care of appending the "|" and properly formatting performance data.

=cut

# begin tracking status and message
my( $status, $message ) = ( OK, '' );

# do some test, with a timeout
alarm $timeout; # the heavy lifting is done in Nagios::Plugin::Getopt
my( $result ) = doTest( $server );
alarm 0;

# validate the result, queue by queue
my( %ok, %warning, %critical, %unknown );

debug( "Analyzing results..." );
foreach my $queuename ( keys( %{$result} ) ) {

    # FIXME - allow per-queue overrides

    my( $queue ) = $result->{$queuename};

    my( $age, $jobs ) = ( $queue->{delta}, $queue->{numjobs} );
    # sanitize values
    defined( $age ) or $age = 0;
    defined( $jobs ) or $jobs = 0;

    my( $agestatus ) = $plugin->check_threshold(
                                                check       => $age,
                                                warning     => $warningminutes,
                                                critical    => $criticalminutes,
                                               );

    my( $jobsstatus ) = $plugin->check_threshold(
                                                 check      => $jobs,
                                                 warning    => $warningjobs,
                                                 critical   => $criticaljobs,
                                                );

    # sort the queue appropriately
    if ( $agestatus == CRITICAL or $jobsstatus == CRITICAL ) {

        $critical{$queuename} = { age => $age, jobs => $jobs };
    }
    elsif ( $agestatus == WARNING or $jobsstatus == WARNING ) {

        $warning{$queuename} = { age => $age, jobs => $jobs };
    }
    elsif ( $agestatus == UNKNOWN or $jobsstatus == UNKNOWN ) {

        $unknown{$queuename} = { age => $age, jobs => $jobs };
    }
    else {

        $ok{$queuename} = { age => $age, jobs => $jobs };
    }
    
    # add performance data
    $plugin->add_perfdata(
                          label     => $queuename . "_jobs",
                          value     => $jobs,
                          uom       => undef,
                          warning   => $warningjobs,
                          critical  => $criticaljobs,
                         );

    # add performance data
    $plugin->add_perfdata(
                          label     => $queuename . "_age",
                          value     => $age,
                          uom       => undef,
                          warning   => $warningminutes,
                          critical  => $criticalminutes,
                         );
}

# figure out our status
if ( scalar( keys( %critical ) ) ) {

    debug( Data::Dumper->Dump( [\%critical], [qw(*critical)] ), 3 );

    $status = CRITICAL;

    foreach my $queue ( sort( keys( %critical ) ) ) {

        $message .= "$queue ( ";

        my( $age, $jobs ) = ( $critical{$queue}->{age}, $critical{$queue}->{jobs} );

        my( $prettyage ) = prettyDelta( ParseDateDelta( "$age minutes" ) );

        my( @messages );

        if ( $age > $criticalminutes ) {

            push( @messages, "job $prettyage old" );
        }
        if ( $jobs > $criticaljobs ) {

            push( @messages, "$jobs queued jobs" );
        }

        $message .= join( ',', @messages );

        $message .= ' ) ';
    }
}
elsif ( scalar( keys( %warning ) ) ) {

    debug( Data::Dumper->Dump( [\%warning], [qw(*warning)] ), 3 );

    $status = WARNING;

    foreach my $queue ( sort( keys( %warning ) ) ) {

        $message .= "$queue ( ";

        my( $age, $jobs ) = ( $warning{$queue}->{age}, $warning{$queue}->{jobs} );

        my( $prettyage ) = prettyDelta( parseDateDelta( "$age minutes" ) );

        my( @messages );

        if ( $age > $warningminutes ) {

            push( @messages, "job $prettyage old" );
        }
        if ( $jobs > $warningjobs ) {

            push( @messages, "$jobs queued jobs" );
        }

        $message .= join( ',', @messages );

        $message .= ' ) ';
    }
}
elsif ( scalar( keys( %unknown ) ) ) {

    debug( Data::Dumper->Dump( [\%unknown], [qw(*unknown)] ), 3 );

    $status = UNKNOWN;

    $message .= 'Unable to determine status: ';

    $message .= join( ',', sort( keys( %unknown ) ) );
}
else {

    debug( Data::Dumper->Dump( [\%ok], [qw(*ok)] ), 3 );

    $message .= 'All queues within parameters.';
}

# exit with appropriate return values
$plugin->nagios_exit( $status, $message );

################################################################################
#                                                                              #
# doTest - poll server for printers
#                                                                              #
################################################################################
sub doTest( $ ) {

    my( $host ) = @_;

    my( %queues );

    debug( "Creating Net::CUPS object...", 2 );
    my( $cups ) = Net::CUPS->new();

    unless ( defined( $cups ) ) {

        debug( "Unable to create Net::CUPS object: $!" );
        return( undef );
    }

    debug( "Setting server to '$host'...", 2 );
    $cups->setServer( $host );

    debug( "Polling for queues...", 1 );
    foreach my $queue ( $cups->getDestinations() ) {

        my( $name ) = $queue->getName();

        if ( defined( $name ) ) {

            $queues{$name} = { destination => $queue };
            debug( "Found queue '$name'.", 3 );
        }
        else {

            debug( "Unable to determine queue name!" );
        }
    }

    debug( "Parsing queues...", 1 );
    my( $now ) = strftime( '%s', localtime() );

    foreach my $queue ( keys( %queues ) ) {

        my( $destination ) = $queues{$queue}->{destination};

        my( @jobs ) = $destination->getJobs( 0, 0 );
        ( @jobs ) or ( @jobs ) = ();

        foreach my $jobid ( @jobs ) {

            my( $job ) = \%{$destination->getJob( $jobid )};
            $queues{$queue}->{jobs}->{$jobid} = $job;

            my( $delta ) = deltaMinutes( $job->{creation_time}, $now );

            # track the largest delta in the queue
            if ( ( ! exists( $queues{$queue}->{delta} ) ) || 
                 ( $delta > $queues{$queue}->{delta} ) ) {

                $queues{$queue}->{delta} = $delta;
            }
        }
        $queues{$queue}->{numjobs} = keys( %{$queues{$queue}->{jobs}} );
    }
    debug( Data::Dumper->Dump( [\%queues], [qw(*queues)] ), 3 );

    return( \%queues );
}

################################################################################
#                                                                              #
# deltaMinutes - determine the delta in days between two datestamps
#                                                                              #
################################################################################
sub deltaMinutes( $$ ) {

    my( $lesser, $greater ) = sort( @_ );
    debug( "Received '$lesser', '$greater'.", 3 );

    my( $lesserdate ) = ParseDateString( "epoch $lesser" );
    my( $greaterdate ) = ParseDateString( "epoch $greater" );

    my( $delta ) = DateCalc( $lesserdate, $greaterdate );

    my( $deltaminutes ) = sprintf( '%d', Delta_Format( $delta, 'approx', 0, '%mt' ) );
    defined( $deltaminutes ) or $deltaminutes = 0;

    debug( "Delta: $deltaminutes minutes.", 3 );
    return( $deltaminutes );
}

################################################################################
#                                                                              #
# prettyDelta - display time delta in sensible format
#                                                                              #
################################################################################
sub prettyDelta( $ ) {

    my( $delta ) = @_;

    debug( "Delta: '$delta'", 3 );

    # parse the delta
    my( $days, $hours, $minutes ) = Delta_Format( 
                                        $delta, 'approx', 0, ( qw(
                                                                  %dh
                                                                  %hv
                                                                  %mv
                                                                 ) )
                                                );

    debug( "\$days: $days\n\$hours: $hours\n\$minutes: $minutes", 3 );

    # assemble the string
    my( $pretty );

    # a day or more?
    if ( $days ) {

        $pretty .= "$days day";

        if ( $days > 1 ) { 

            $pretty .= "s";
        }
    }

    # hours?
    if ( $hours ) {

        if ( $days ) {

            if ( $minutes ) {

                $pretty .= ", ";
            }
            else {

                $pretty .= " and ";
            }
        }

        $pretty .= "$hours hour";

        if ( $hours > 1 ) {

            $pretty .= "s";
        }
    }

    # minutes?
    if ( $minutes ) {

        if ( $days && $hours ) {

            $pretty .= ",";
        }

        if ( $days || $hours ) {

            $pretty .= " and ";
        }

        $pretty .= "$minutes minute";

        if ( $minutes > 1 ) {

            $pretty .= "s";
        }
    }

    debug( "\$pretty: '$pretty'", 3 );
    return( $pretty );
}

################################################################################
#                                                                              #
# debug - print debug info
#                                                                              #
################################################################################
sub debug( $;$ ) {

    my( $message, $level ) = @_;

    defined( $level ) or $level = 1;

    if ( $verbose >= $level ) { 
        
        chomp $message;
        print "$message\n";
    }
}

################################################################################
#                                                                              #
# DoInterrupt - handle SIGINT and clean up
#                                                                              #
################################################################################
sub DoInterrupt {

    $plugin->nagios_die( "Interrupt received" );
}