check_rep.php
if(empty($_REQUEST["key"])) die(':) missing key');
if($_REQUEST["key"] != 'xupeng') die(':) error key');
include("mysql_instance.php");
include("check_status_api.php");
define("USERNAME", "用户名");
define("PASSWORD", "密码");
define("DEBUGMODE", false);
$instances = get_instances();
if($instances){
echo <<
END;
echo "
n";
if(!DEBUGMODE){
echo "
n";
}else{
echo "
n";
}
foreach($instances as $host){
$res = check_mysql_replication_status($host, USERNAME, PASSWORD);
if(!DEBUGMODE){
switch($res["result"]){
case -4:
$memo = "未知异常";
break;
case -3:
$memo = "查询失败";
break;
case -2:
$memo = "无法连接端口";
break;
case -1:
$memo = "状态未知";
break;
case 0:
$memo = "OK";
break;
case 1:
$memo = "同步失败";
if($res["Slave_IO_Running"] <> "Yes"){
$memo .= $res["Last_IO_Error"] . "(" . $res
["Last_IO_Errno"] . ")";
}
if($res["Slave_SQL_Running"] <> "Yes"){
$memo .= $res["Last_SQL_Error"] . "(" . $res
["Last_SQL_Errno"] . ")";
}
break;
case 2:
$memo = "数据库未设置同步";
break;
}
echo "
n";
}else{
echo "
n";
}
}
echo "
instance |
result |
Slave_IO_Running |
Slave_SQL_Running |
Master_Host |
Master_Port |
Replicate_Do_DB |
memo |
instance |
result |
Slave_IO_Running |
Slave_SQL_Running |
Master_Host |
Master_Port |
Replicate_Do_DB |
Slave_IO_State |
Last_IO_Errno |
Last_IO_Error |
Last_SQL_Errno |
Last_SQL_Error |
{$host} |
{$res['result']} |
{$res['Slave_IO_Running']} |
{$res['Slave_SQL_Running']} |
{$res['Master_Host']} |
{$res['Master_Port']} |
{$res['Replicate_Do_DB']} |
{$memo} |
{$host} |
{$res['result']} |
{$res['Slave_IO_Running']} |
{$res['Slave_SQL_Running']} |
{$res['Master_Host']} |
{$res['Master_Port']} |
{$res['Replicate_Do_DB']} |
{$res['Slave_IO_State']} |
{$res['Last_IO_Errno']} |
{$res['Last_IO_Error']} |
{$res['Last_SQL_Errno']} |
{$res['Last_SQL_Error']} |
n";
echo <<
END;
}else{
die("no mysql instances defined.");
}
check_status_api.php
/*
* 检查mysql服务器的同步状态
*/
function check_mysql_replication_status($host, $username, $password)
{
//默认状态未知
$r = array(
"result" => -1
);
try{
$dbh = @mysql_connect($host, $username, $password);
if(!$dbh){
//无法连接
$r["result"] = -2;
return($r);
}
$query = "SHOW SLAVE STATUS";
$res = @mysql_query($query, $dbh);
$err = @mysql_error();
if($err){
//无法连接
$r["result"] = -3;
return($r);
}
$row = mysql_fetch_array($res);
$r = $row;
if(($r["Slave_IO_Running"] == "Yes") && ($r["Slave_SQL_Running"] == "Yes"))
{
$r["result"] = 0;
}else{
if(!empty($row)){
$r["result"] = 1;
}else{
$r["result"] = 2;
}
}
}catch(Exception $e){
$r["result"] = -4;
}
return($r);
}
mysql_instance.php
|