#!/usr/bin/perl 
#
# copied from mysql_check.pl and modified by Sheeri Kritzer
#
# This plugin requires that mysqladmin(1) is installed on the system.
# Since it is part of the MySQL distribution, that should be a problem.
#
# If no parameters are giving, a usage statement is output.
#
# Exit 0 on success, providing some informational output
# Exit 2 on failure, provide what we can...
#

require 5.004;

sub usage;

my $TIMEOUT = 15;
my $MYSQLADMIN = "/usr/local/mysql/bin/mysqladmin";
if (!-x $MYSQLADMIN){
print "$MYSQLADMIN does not exist or is not executable.\n";
exit $ERRORS{"UNKNOWN"};
}

my %ERRORS = (
              'OK' , '0',
              'WARNING', '1',
              'CRITICAL', '2',
              'UNKNOWN' , '3');

my $host = shift || &usage(%ERRORS);
my $user = shift || &usage(%ERRORS);
my $pass = shift || "";

my $state = "OK";

# 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);

# get info
open (OUTPUT,
      "$MYSQLADMIN -h $host -u $user --password=\"$pass\" extended-status 2>&1
      |");

while (<OUTPUT>) {
  if (/failed/) { $state="CRITICAL"; s/.*://; $status=$_; last; }
  next if /^\s*$/;
  if (/\| Slave_running\s+\|\s+(\S*)\s+\|/) { $running = $1; next; }
}

if ($running eq "ON") { $running="Replication working."; $state = "OK"; }
elsif ($running eq "OFF") { $running="Replication broken, see http://tinyurl.com/6hm4t"; $state = "CRITICAL"; }
else {$state = "UNKNOWN";}

print $running. "|\n" . $state;
exit $ERRORS{$state};

sub usage {
   print "Required arguments not given!\n\n";
   print "MySQL status checker plugin for Nagios, V1.01\n";
   print "Copyright (c) 1999-2000 Mitch Wright \n\n";
   print "Usage: $0 <host> <user> [<pass>]\n\n";
   print "       <pass> = password to use for <user>";
    exit $ERRORS{"UNKNOWN"};
}
