shell脚本使用钉钉来进行mysql主从监控报警
相信很多朋友都有过mysql主从不同步的报警方案,比如短信,邮件,微信等等,今天我给大家一个用钉钉机器人来进行报警的方案.怎么申请钉钉机器人,我这里就不介绍了,大家自己网上搜下吧.先说下我的环境,因为我的mysql主从是在阿里云vpc内网里,没有公网ip,所以ssh远程连接到另外一台机子,用另外一台机子来发送报警信息.
系统:centos 7(64位)
1.监控脚本:
cat /root/soft_shell/check_slave.sh
#!/bin/bash mysql_binfile=/usr/local/mysql/bin/mysql mysql_user=root mysql_pass=password mysql_sockfile=/data/mysql/mysql.sock slave_ip="192.168.1.23x" status=$($mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "show slave status\G" | grep -i "running") Slave_IO_Running=`echo $status | grep Slave_IO_Running | awk ' {print $2}'` Slave_SQL_Running=`echo $status | grep Slave_SQL_Running | awk '{print $2}'` REMARKFILE='/tmp/monitor_load.remark' DELMARKFILE='/tmp/monitor_load.delmark' ISSEND=0 ASSEND=0 EXPIRE=300 ENDTIME=3600 NOW=$(date +%s) if [ -f "$REMARKFILE" ] && [ -s "$REMARKFILE" ]; then REMARK=$(cat $REMARKFILE) # 删除过期的发送时间记录文件 if [ $(( $NOW - $REMARK )) -gt "$EXPIRE" ]; then rm -f ${REMARKFILE} REMARK="" fi else REMARK="" fi if [ -f "$DELMARKFILE" ] && [ -s "$DELMARKFILE" ]; then DELMARK=$(cat $DELMARKFILE) # 删除过期的发送时间记录文件 if [ $(( $NOW - $DELMARK )) -gt "$ENDTIME" ]; then rm -f ${DELMARKFILE} DELMARK="" fi else DELMARK="" fi if [ "$REMARK" = "" ] ; then if [ "$Slave_IO_Running" != "Yes" -a "$Slave_SQL_Running" != "Yes" ]; then ssh -p 18330 root@192.168.1.24x "/root/soft_shell/send_message.sh" ISSEND=1 fi fi if [ "$DELMARK" = "" ] ; then if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]; then ssh -p 18330 root@192.168.1.24x "/root/soft_shell/restore_message.sh" ASSEND=1 fi fi if [ "$ISSEND" = "1" ]; then echo "$(date +%s)" > $REMARKFILE fi if [ "$ASSEND" = "1" ]; then echo "$(date +%s)" > $DELMARKFILE fi exit 0
2.报警脚本
cat /root/soft_shell/send_message.sh
#!/bin/bash ipadd="192.168.1.2xx" datetime=`date +"%Y-%m-%d/%H:%M:%S"` curl 'https://oapi.dingtalk.com/robot/send?access_token=这里写你自己的key' -H 'Content-Type: application/json' -d ' {"msgtype": "text", "text": {"content": "'${datetime},${ipadd}_mysql从库已不同步,请尽快处理.'"}}'
cat /root/soft_shell/restore_message.sh
#!/bin/bash ipadd="192.168.1.2xx" datetime=`date +"%Y-%m-%d/%H:%M:%S"` curl 'https://oapi.dingtalk.com/robot/send?access_token=这里写你自己的key' -H 'Content-Type: application/json' -d ' {"msgtype": "text", "text": {"content": "'${datetime},${ipadd}_mysql主从同步正常.'"}}'
好了,最后去钉钉里查看是否有收到报警短信.
评论: