Stellarcore.net

Stellarcore.net

I used this script to monitor the status of the mysql daemon. First it checks for the running daemon via ps and if that succeeds then it tries to issue a mysqladmin ping to verify that server is responding. Faliure of either will issue a reboot. The script logs to syslog so as a cron job and generally redirect output to /dev/null

#!/usr/local/bin/perl

#Simple Mysql Daemon Watcher....
#Written by Mike Tremaine mgtstellarcore.net
#02/14/2003

#Globals
my $mysqladmin = "/usr/local/bin/mysqladmin";
my $mysql_user = "-u nobody";
my $mysql_password = "";
my $mysql_server_start = "/etc/init.d/mysqld start";
my $mysql_server_stop = "/etc/init.d/mysqld stop";

#Stage 1
my $call = "ps -ef | grep 'mysqld' | grep -v 'tail -f' | grep -v grep";
my @daemons = grep { index( $_, 'grep' ) == -1 } `$call`;

if( (scalar @daemons) ) {
    #Mysql is running lets do the ping
    print "Daemon running onto ping test\n";
    } else {
    print "daemon not running restarting\n";
    `logger -p daemon.notice -t mysql_pinger 'Mysql deamon is not running..restarting'`;
    `$mysql_server_start`;
    exit;
    };

#Stage 2
if (-e "$mysqladmin") {

    my $status = `$mysqladmin $mysql_user ping 2>&1`;
    chomp $status;

    if ($status == "mysqld is alive") {
        #Everything is fine just exit
        exit;
        } elsif ( $status =~ /access denied/im) {
        print "Password Failed $status please check the script\n";
        `logger -p daemon.notice -t mysql_pinger 'Password Failed please check the script'`;
        exit;
        } else {
        #Time to reboot
        print "Mysql database is not alive..restarting\n";
        `logger -p daemon.notice -t mysql_pinger 'Mysql database is not alive..restarting'`;
        `$mysql_server_stop`;
        sleep 5;
        `$mysql_server_start`;
    }
} else {

print "Unable to find mysqladin\n";

}

exit;