Skip to content

Instantly share code, notes, and snippets.

@koy1619
Last active December 17, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save koy1619/5611091 to your computer and use it in GitHub Desktop.
Save koy1619/5611091 to your computer and use it in GitHub Desktop.
mysql服务&主从监控脚本 将此脚本放于mysql从库上,设置crontab -e */10 * * * * root /bin/sh /home/mysqld_monitor.sh 每十分钟跑一次,检测到mysqld服务或者是主从宕掉之后会发送邮件(只发一次)!
#!/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