Linux OP Koriyuki Shell programming
One, monitoring MySQL error code and master-slave replication synchronization exception
Title: Monitor MySQL master-slave synchronization is abnormal, if the exception, send SMS or mail to the administrator. Note: If there is no master-slave synchronization environment, you can use the following text in the file to read to simulate:
Phase 1: Develop a daemon script to detect every 30 seconds.
Phase 2: If the following error number (1158,1159,1008,1007,1062) is present in the synchronization, the error is skipped.
Phase 3: Use array technology to implement the above script (get master-slave judgment and error number section)
Phase 2:
#!/bin/bash
#
contact= ' [email protected] '
err_log= '/var/log/mariadb/mariadb.log '
Email_to_admin () {
Mailsubject= "MySQL Error."
mailbody= "' Date ' +%f%h:%m:%s ': MySQL error,please Check and repair."
echo $mailbody | Mail-s "$mailsubject" $CONTACT
}
While True;do
For Error_code in ' grep ' error_code: [[:d igit:]]\+ ' $ERR _log | Grep-o ' [[:d igit:]]\+ ';d o
Case $ERROR _code in
1007)
Continue
;;
1008)
Continue
;;
1158)
Continue
;;
1159)
Continue
;;
1162)
Continue
;;
*) ;;
Esac
Email_to_admin
Done
Sleep 30
Done
Phase 3:
#!/bin/bash
#
Db_user= ' Root '
Db_password= ' Jingpei '
Db_port= ' 3306 '
Master= ' 192.168.0.76 '
Slave= ' 192.168.0.77 '
Cmd_conn_master= ' Mysql-u$db_user-p$db_password-p$db_port-h$master '
Cmd_conn_slave= ' Mysql-u$db_user-p$db_password '-p$db_port-h$slave '
contact= ' [email protected] '
Declare-a Master_status
master_status[0]= "Cmd_conn_master-e ' show MASTER status\g ' | Egrep File | Cut-d:-f2 "
master_status[1]= "Cmd_conn_master-e ' show MASTER status\g ' | Egrep Position | Cut-d:-f2 "
Declare-a Slave_status
slave_status[0]= "Cmd_conn_slave-e ' show slave status\g ' | Egrep Master_log_file | Cut-d:-f2 "
slave_status[1]= "Cmd_conn_slave-e ' show slave status\g ' | Egrep Read_master_log_pos | Cut-d:-f2 "
slave_status[2]= "Cmd_conn_slave-e ' show slave status\g ' | Egrep Last_errno | Cut-d:-f2 "
Email_to_admin () {
Mailsubject= "Syncing failed."
mailbody= "' Date ' +%f%h:%m:%s ': Syncing of Master-slave is failed,please check and repair."
echo $mailbody | Mail-s "$mailsubject" $CONTACT
}
While true; Do
If [${master_status[0]} = = ${slave_status[0]}-a ${master_status[1]} = = ${slave_status[1]}-a ${slave_status[2]} = = 0]; Then
echo "Syncing of Master-slave is OK."
Else
echo "Syncing of Master-slave is failed."
Email_to_admin
Fi
Sleep 30
Done
Linux Koriyuki Shell programming------(a) Monitoring MySQL error code and master-slave replication synchronization exception