#!/usr/bin/perl
#####################################################################
#
require 5.004;
sub usage;
my $TIMEOUT = 15;
my $MYSQL = "mysql";
my %ERRORS = ('UNKNOWN' , '-1',
'OK' , '0',
'WARNING', '1',
'CRITICAL', '2');
my $host = shift || &usage(%ERRORS);
my $user = shift || &usage(%ERRORS);
my $pass = shift || "";
my $TABLE = shift || "mysql";
my $warn = shift || 20;
my $crit = shift || 30;
my $state = "OK";
my $count = 0;
my $status = "";
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
print ("ERROR: No response from MySQL server (alarm)\n");
exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
open (OUTPUT, "$MYSQL -h $host -u $user --password=\"$pass\" $TABLE -e 'show tables;' | wc -l 2>&1 |");
while (<OUTPUT>) {
if (/failed/) { $state="CRITICAL"; s/.*://; last; }
chomp;
$count = $_ - 1;
}
if ($count >= $warn) { $state = "WARNING"; }
if ($count >= $crit) { $state = "CRITICAL"; }
$status = "Tables $state - $count Tables\n";
print $status;
exit $ERRORS{$state};
sub usage {
print "Required arguments not given!\n\n";
print "MySQL table count plugin for Nagios, V1.01\n";
print "Copyright (c) 2005 Brian Zammit \n\n";
print "Usage: check_mysql.pl <host> <user> [<pass> <table> [<warn> [<crit>]]]\n\n";
print " <pass> = password to use for <user> at <host>\n";
print " <table> = table to use\n";
print " <warn> = number of threads to warn us about\n";
print " <crit> = number of threads to scream at us about\n";
exit $ERRORS{"UNKNOWN"};
}
|