| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
|
| 4 |
# $Id$ |
| 5 |
|
| 6 |
## Nagios plugin to check the queue depth of a Schwartz database. |
| 7 |
|
| 8 |
use utils qw( %ERRORS $TIMEOUT ); |
| 9 |
use Getopt::Long qw( :config no_ignore_case ); |
| 10 |
use DBI; |
| 11 |
|
| 12 |
use constant QUEUE_CRITICAL => 100; |
| 13 |
use constant QUEUE_WARNING => 30; |
| 14 |
|
| 15 |
GetOptions( |
| 16 |
'h|help!' => \my ($help), |
| 17 |
'v|verbose' => \my ($verbose), |
| 18 |
'dsn=s' => \my ($dsn), |
| 19 |
'user=s' => \my ($user), |
| 20 |
'password=s' => \my ($pass), |
| 21 |
); |
| 22 |
|
| 23 |
if ($help) { |
| 24 |
print "$0 --dsn <database-dsn> --user <user> --password <password>"; |
| 25 |
exit $ERRORS{OK}; |
| 26 |
} |
| 27 |
|
| 28 |
unless ( $dsn && $user ) { |
| 29 |
print <<USAGE; |
| 30 |
You have to supply a database DSN and username. |
| 31 |
|
| 32 |
Example: |
| 33 |
|
| 34 |
$0 --dsn dbi:mysql:host=schwartz-db1;dbname=theschwartz_comet --user comet |
| 35 |
USAGE |
| 36 |
exit $ERRORS{UNKNOWN}; |
| 37 |
} |
| 38 |
|
| 39 |
sub exit_with { |
| 40 |
my ( $code, $msg ) = @_; |
| 41 |
$msg = $msg ? ' - ' . $msg : ''; |
| 42 |
print "TheSchwartz $dsn $code$msg"; |
| 43 |
exit $ERRORS{$code}; |
| 44 |
} |
| 45 |
|
| 46 |
my $dbh = DBI->connect( $dsn, $user, $pass ) |
| 47 |
or exit_with 'CRITICAL', "Can't connect to $dsn: $DBI::errstr"; |
| 48 |
|
| 49 |
my $inf = $dbh->selectrow_arrayref( <<SQL, undef, time ); |
| 50 |
SELECT COUNT(*) |
| 51 |
FROM job |
| 52 |
WHERE run_after <= ? |
| 53 |
SQL |
| 54 |
unless ( $inf && defined $inf->[0] ) { |
| 55 |
exit_with 'CRITICAL', "Failed getting job count: " . $dbh->errstr; |
| 56 |
} |
| 57 |
|
| 58 |
if ( $inf->[0] < QUEUE_WARNING ) { |
| 59 |
exit_with 'OK'; |
| 60 |
} |
| 61 |
elsif ( $inf->[0] < QUEUE_CRITICAL ) { |
| 62 |
exit_with 'WARNING', "Schwartz queue depth is $inf->[0]"; |
| 63 |
} |
| 64 |
else { |
| 65 |
exit_with 'CRITICAL', "Schwartz queue depth is $inf->[0]"; |
| 66 |
} |