Using php to detect mysql replication lag

require_once("/var/www/lib/functions.php");

function check_errors($host, $user, $pass)
{
	$errors = '';
	echo "\nChecking: ". $host. " " . $user. " " . $pass."\n";
	$link = mysql_connect($host,$user,$pass);
	$res = mysql_query("SHOW SLAVE STATUS", $link);
	$row = mysql_fetch_assoc($res);

	if($row['Slave_IO_Running'] == 'No') {
		$errors .= "Slave IO not running on $host\n";
	}

	if($row['Slave_SQL_Running'] == 'No') {
		$errors .= "Slave SQL not running on $host\n";
	}

	if($row['Seconds_Behind_Master'] == 'NULL' || intval($row['Seconds_Behind_Master']) > 300 ) {
		$errors .= "Seconds FAR BEHIND, SQL not running on $host\n";
	}

	if ($errors != '' )
	{
		$errors .= "Error number: {$row['Last_SQL_Errno']}\n";
		$errors .= "Error message: {$row['Last_SQL_Error']}\n";
		$errors .= "Seconds_Behind_Master: {$row['Seconds_Behind_Master']}\n";
		$errors .= "Seconds_Behind_Master: {$row['Master_Host']}\n\n";
	}

	mysql_close($link);
	return $errors;
}

$errors = check_errors(DB_HOST_SLAVE_4, DB_USERNAME_SLAVE_4, DB_PASSWORD_SLAVE_4);
$errors .= check_errors(DB_HOST_SLAVE_3, DB_USERNAME_SLAVE_3, DB_PASSWORD_SLAVE_3); 

if ($errors)
{
}

Submit a Comment

You must be logged in to post a comment.


4 − three =