Forked from koy1619/mysql_master_slave_monitor.sh
Last active
August 29, 2015 14:28
-
-
Save yashodhank/bc3dd7b35be8ede274c3 to your computer and use it in GitHub Desktop.
mysql服务&主从监控脚本
将此脚本放于mysql从库上,设置crontab -e
*/10 * * * * root /bin/sh /home/mysqld_monitor.sh
每十分钟跑一次,检测到mysqld服务或者是主从宕掉之后会发送邮件(只发一次)!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
mysql_status=`netstat -nl | awk 'NR>2{if ($4 ~ /.*:3306/) {print "Yes";exit 0}}'` | |
PortNum=`netstat -lnt|grep 3306|wc -l` | |
if [ "$mysql_status" == "Yes" ];then | |
slave_status=`mysql -uroot -p123456 -e"show slave status\G" | grep "Running" | awk '{if ($2 != "Yes") {print "No";exit 1}}'` | |
if [ "$slave_status" == "No" ];then | |
echo "slave is not working!" | |
[ ! -f "/tmp/slave" ] && echo "Slave is not working!" | mail -s "Warn!MySQL Slave is not working" monitor@test.me | |
touch /tmp/slave | |
else | |
echo "slave is working." | |
[ -f "/tmp/slave" ] && rm -f /tmp/slave | |
fi | |
fi | |
if [ $PortNum -eq 1 ];then | |
echo "mysqld is running." | |
else | |
echo "Mysql Server is down!" | mail -s "Warn!MySQL server is down!" monitor@test.me | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment