Check_rep.php <! --? Php <br/--> if (empty ($ _ REQUEST ["key"]) die (':) missing key '); If ($ _ REQUEST ["key"]! = 'Upeng') die (': error key '); Include ("mysql_instance.php "); Include ("check_status_api.php "); Define ("USERNAME", "USERNAME "); Define ("PASSWORD", "PASSWORD "); Define ("DEBUGMODE", false ); $ Instances = get_instances (); If ($ instances ){ Echo < <! -- Automatically refresh within 30 minutes --> 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 = "unknown exception "; Break; Case-3: $ Memo = "query failed "; Break; Case-2: $ Memo = "unable to connect to port "; Break; Case-1: $ Memo = "unknown state "; Break; Case 0: $ Memo = "OK "; Break; Case 1: $ Memo = "synchronization failed "; 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 = "synchronization not set for Database "; Break; } Echo" N "; } Else { Echo" N "; } } Echo" <Table border = ""> <Tbody> <Tr> <Td> instance </td> <Td> result </td> <Td> Slave_IO_Running </td> <Td> Slave_ SQL _Running </td> <Td> Master_Host </td> <Td> Master_Port </td> <Td> Replicate_Do_DB </td> <Td> memo </td> </Tr> <Tr> <Td> instance </td> <Td> result </td> <Td> Slave_IO_Running </td> <Td> Slave_ SQL _Running </td> <Td> Master_Host </td> <Td> Master_Port </td> <Td> Replicate_Do_DB </td> <Td> Slave_IO_State </td> <Td> Last_IO_Errno </td> <Td> Last_IO_Error </td> <Td> Last_ SQL _Errno </td> <Td> Last_ SQL _Error </td> </Tr> <Tr> <Td> {$ host} </td> <Td >{$ res ['result']} </td> <Td >{$ res ['slave _ IO_Running ']} </td> <Td >{$ res ['slave _ SQL _Running ']} </td> <Td >{$ res ['master _ host']} </td> <Td >{$ res ['master _ port']} </td> <Td >{$ res ['replicate _ Do_DB ']} </td> <Td >{$ memo} </td> </Tr> <Tr> <Td> {$ host} </td> <Td >{$ res ['result']} </td> <Td >{$ res ['slave _ IO_Running ']} </td> <Td >{$ res ['slave _ SQL _Running ']} </td> <Td >{$ res ['master _ host']} </td> <Td >{$ res ['master _ port']} </td> <Td >{$ res ['replicate _ Do_DB ']} </td> <Td >{$ res ['slave _ IO_State ']} </td> <Td >{$ res ['last _ io_errno']} </td> <Td >{$ res ['last _ IO_Error ']} </td> <Td >{$ res ['last _ SQL _errno']} </td> <Td >{$ res ['last _ SQL _Error ']} </td> </Tr> </Tbody> </Table> N "; Echo < END; } Else { Die ("no mysql instances defined ."); } Check_status_api.php <! --? Php <br/--> /* * Check the synchronization status of the mysql server. */ Function check_mysql_replication_status ($ host, $ username, $ password) { // The default status is unknown. $ R = array ( "Result" =>-1 ); Try { $ Dbh = @ mysql_connect ($ host, $ username, $ password ); If (! $ Dbh ){ // Unable to connect $ R ["result"] =-2; Return ($ r ); } $ Query = "show slave status "; $ Res = @ mysql_query ($ query, $ dbh ); $ Err = @ mysql_error (); If ($ err ){ // Unable to connect $ 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 <! --? Php // Grant replication client on *. * TO 'username '@ 'monitoring host ip' identified by 'Password'; $ mysql_instances = Array (); $ Mysql_instances [] = "remote ip: Port"; $ mysql_instances [] = "remote ip: Port "; Function get_instances () { Global $ mysql_instances; Return $ mysql_instances; } ? --> |