How to monitor MySQL replication with Nagios?

Well, I wrote that script quite a while ago, it worked for me, so here you are:

#!/usr/bin/php -q
<?php
$host = $argv[1];
$user = $argv[2];
$pass = $argv[3];
$link = mysql_connect ( $host, $user, $pass );
$result = mysql_query ( "SHOW SLAVE STATUS" );
$stat = mysql_fetch_assoc($result);
mysql_free_result ( $result );
mysql_close ( $link );
if ( $stat['Seconds_Behind_Master'] < "10" && $stat['Last_Errno'] == "0" && $stat['Slave_IO_Running'] == "Yes" && $stat['Slave_SQL_Running'] == "Yes")
{
        echo "OK Replication Host: " . $stat['Master_Host'] .  " Seconds behind master: " . $stat['Seconds_Behind_Master'] . "s\n";
        exit ( 0 ) ;
}
else if ( $stat['Seconds_Behind_Master'] > 20 && $stat['Seconds_Behind_Master'] < 100 && $stat['Last_Errno'] == "0" && $stat['Slave_IO_Running'] == "Yes" && $stat['Slave_SQL_Running'] == "Yes")
{
        echo "WARNING Replication " . $stat['Master_Host'] .  " Seconds behind master: " . $stat['Seconds_Behind_Master'] . "s\n"; 
        exit ( 1 );
}
else
{
        echo "CRITICAL Replication " . $stat['Master_Host'] .  " Seconds behind master: " . $stat['Seconds_Behind_Master'] . "s | Error: " . $stat['Last_Error'] . "\n";
        exit ( 2 );
}
?>

Just put it in a file like /usr/local/nrpe/libexec/check_mysql_replication.php and run it with the correct parameters:

/usr/local/nrpe/libexec/check_mysql_replication.php SERVERHOST USERNAME PASSWORD

Done. 

 

 

You cannot comment on this entry