check_mysql_count.pl

#!/usr/bin/perl

#####################################################################
##
## check_mysql_count.pl version 0.02
## 2008-2011 Michal Sviba
##
## 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.
##
## contact the author directly for more information at:
## michal at myserver.cz
##
######################################################################
##
## Check how many rows in tables are. you can specifi conditionals
## for WHERE causule
## This plugin requires that mysql is installed on the system.
##
## If no parameters are givin, a usage statement is output.
##
## Exit 0 on success, providing some informational output
## Exit 2 on failure.
##

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 $DB = shift || "mysql";
my $TABLE = shift || "users";
my $COND = shift || "1=1";
my $warn = shift || 20;
my $crit = shift || 30;

my $state = "OK";
my $count = 0;
my $status = "";


$SIG{'ALRM'} = sub {
    print ("ERROR: No response from MySQL server (alarm)\n");
    exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
$COND =~ s/'/\\'/g;
#$COND =~ s/"/\\"/g;
my $SQL = "SELECT COUNT(1) cnt FROM $TABLE WHERE $COND;";
#print "SQL: $SQL\n";
open (OUTPUT, "$MYSQL -h $host -u $user --password=\"$pass\" $DB -e '$SQL' 2>&1 |");

while (<OUTPUT>) {
    if (/failed/) { $state="CRITICAL"; s/.*://; last; }
    chomp;
    $count = $_;
}

if ($count >= $warn) { $state = "WARNING"; }
if ($count >= $crit) { $state = "CRITICAL"; }
if ($count =~ m/\D/) { $state = "UNKNOWN"; }

if ($ERRORS{$state} >= 0) {
    $status = "Not Ignored hosts $state - $count hosts\n";
} else {
    $status = "Not Ignored hosts $state - mysql: $count\n";
}

print $status;
exit $ERRORS{$state};

sub usage {
    print "Required arguments not given!\n\n";
    print "MySQL row count plugin for Nagios, 0.02\n";
    print "2008-2011 Michal Sviba \n\n";
    print "Usage: check_mysql_count.pl <host> <user> [<pass> <db> <table> <cond> [<warn> [<crit>]]]\n\n";
    print "       <pass> = password with SELECT privilege to use for <user> at <host>\n";
    print "       <db> = DB where table is placed\n";
    print "       <table> = table in DB\n";
    print "       <cond> = conditionals in where section\n";
    print "       <warn> = number of rows to warning state [20]\n";
    print "       <crit> = number of rows to critical state [30]\n";
    exit $ERRORS{"UNKNOWN"};
}