php脚本监控mysql数据库主从同步

<?php

error_reporting(0);

require(‘/home/kerwin/PHPMailer_v5.1/class.phpmailer.php’);

$host = `ifconfig eth0|grep “inet addr”|awk -F: ‘{print $2}’|sed ‘s/ .*//g’`;

//require(‘/home/kerwin/PHPMailer_v5.1/class.phpmailer.php’);

$con1 = mysql_connect(“localhost”,”root”,”123141256″);

if(!$con1)
{
die(‘connect mysql fail:’.mysql_error());
}

mysql_select_db(“raidcall”,$con1);

$result1 = mysql_query(“show slave status;”,$con1);

$all1 = mysql_fetch_assoc($result1);

 

$con2 = mysql_connect(“192.168.3.142″,”root”,”12345126″);

if(!$con2)
{
die(‘connect mysql fail:’.mysql_error());
}

mysql_select_db(“raidcall”,$con2);

$result2 = mysql_query(“show master status”,$con2);

$all2 = mysql_fetch_assoc($result2);

$position_differ = $all2[Position] – $all1[Exec_Master_Log_Pos];

//echo $position_differ;

if($all1[Slave_IO_Running] == “Yes” and $all1[Slave_SQL_Running] == “Yes” and $all1[Seconds_Behind_Master] < 60 and $all1[Relay_Master_Log_File] == $all2[File] and $position_differ < 1)

echo trim($host).”: MYSQL主从库步n”;

else

{

echo trim($host).”: MYSQL主从库不同步,将会发信息通知负责人处理”.”n”;

while(1)

{

mysql_close($con);

$mail = new PHPMailer();

$mail->IsSMTP(); // set mailer to use SMTP

$mail->Host = “smtp.126.com”; // specify main and backup server

$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->Username = “XXX”; // SMTP username 邮箱用户名

$mail->Password = “XXXXXX”; // SMTP password 邮箱密码

$mail->From = “XXXXXX”;

$mail->FromName = “kerwin”;

$mail->AddAddress(“XXXXXXXXX”);

$mail->Subject = “$host Database is not rsync”;

$mail->Body = “$Slave_IO,$Slave_SQL,$Seconds”;

if(!$mail->Send())
{
echo “发送邮件失败。”;
echo “失败原因: ” . $mail->ErrorInfo;
continue;
}

else

exit;

}

}

?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>