Author: skate
Time: 2013/05/13
MySQL replication Environment check script
This script is deployed on the slave side. You can monitor the mysqld health, replication Environment Health, and replication latency of slave by text message or email.
Script: mysql_rep_moni.sh
#! /Bin/bash
### Author: skate
### Time: 2013-05-14
### Function: This script detects the mysqld health, replicaton health, replication latency
# VARIABLES
Pwd = pwd
Dir =/MySQL/script/
Ipaddr = 1.1.1.1
### Determine whether there is connection
Mysql_status = 'netstat-nl | awk' Nr> 2 {if ($4 ~ /. *: 3306/) {print "yes"; exit 0 }}''
If ["$ mysql_status" = "yes"]; then
Slave_status = 'mysql-uroot-p $ PWD-e "show slave status \ G" | grep "running" | awk '{if ($2! = "Yes") {print "no"; Exit 1 }}''
Slave_delay = 'mysql-uroot-p $ PWD-e "show slave status \ G" | grep "seconds_behind_master" | awk '{if ($2> = 0) {print $2; exit
0 }}''
Echo "delay" $ slave_delay
Echo "status" $ slave_status
### Replication is not working
If ["$ slave_status" = "no"]; then
Echo "slave is not working! "
Sh "$ dir" Y. Sh "phone" "$ ipaddr" "_ warn_zxg_rep" "$ ipaddr" "_ mysql_replication_is_not_working" "dba"
Sh "$ dir" Notify. Sh "email" "$ ipaddr" "_ error_zxg_rep" "mysql_replication_is_not_working" "dba"
### Replication is working, but replication have delay
Elif ["$ slave_status "! = "No"] & [$ slave_delay-ge 10]; then
Echo "slave is working, but replication have delay! "
Sh "$ dir" Y. Sh "phone" "$ ipaddr" "_ warn_zxg_rep" "$ ipaddr" "_ mysql_replication_is_delay: $ slave_delay" "dba"
Sh "$ dir" Notify. Sh "email" "$ ipaddr" "_ warn_zxg_rep" "mysql_replication_is_delay: $ slave_delay" "dba"
Else
Echo "slave is working ."
Fi
Else
Echo "mysqld is down! "
Sh "$ dir" Notify. Sh "email" "$ ipaddr" "_ error_zxg_mysqld" "$ ipaddr" "_ mysqld_is_down" "dba"
Sh "$ dir" Notify. Sh "phone" "$ ipaddr" "_ error_zxg_mysqld" "mysqld_is_down" "dba"
Fi
Create a scheduled crontab
*/5 * su-mysql-c "sh/MySQL/script/mysql_rep_moni.sh>/dev/null 2> & 1"
Note:
The preceding alarm API (notify. Sh) can be modified based on your usage.
------ End ------