1. [root@VM-17-13-centos server]# cat /server/scripts/Master-salve-monitoring.sh
    2. #!/bin/bash
    3. set +o posix
    4. IPaddr=`ifconfig |grep 10.200.23.13|awk -F " " '{print $2}'`
    5. #IPaddr=`ifconfig |grep 192.168.100.201|awk -F " " '{print $2}'`
    6. Hostname=`hostname`
    7. User="root"
    8. Password="zk123\afc"
    9. Host="127.0.0.1"
    10. Time=$(date "+%Y-%m-%d %H:%M:%S")
    11. Logfile="/server/scripts/Mestre_escravo_status.logs"
    12. #webhook="https://oapi.dingtalk.com/robot/send?access_token=92b229bb93205a22a9ce4bbc97f16c966332279f9271759f413f93c9f540fcfe"
    13. webhook="https://oapi.dingtalk.com/robot/send?access_token=7263909af3197516f50feee7502cf77aea8ccfca1d0859877f9c8121a4d2d530"
    14. mysql -u$User -p$Password -e "show slave status\G" -h${Host}|grep -i "running:" >$Logfile
    15. Slave_IO_Running=`grep Slave_IO_Running $Logfile| awk ' {print $2}'`
    16. Slave_SQL_Running=`grep Slave_SQL_Running $Logfile | awk '{print $2}'`
    17. Check_mysql_status() {
    18. if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]
    19. then
    20. echo "slave is Running!" >$Logfile
    21. # SendMsgToDingding
    22. else
    23. echo "Slave is not running! " >$Logfile
    24. SendMsgToDingding
    25. fi
    26. }
    27. function SendMsgToDingding() {
    28. curl $webhook -H 'Content-Type: application/json' -d "
    29. {
    30. 'msgtype': 'text',
    31. 'text': {
    32. 'content': '告警主题:中免环境MySQL主从状态监控 \n主机地址: $IPaddr\n主机名称: $Hostname \n当前时间: $Time \n当前状态: `cat /server/scripts/Mestre_escravo_status.logs` \n '
    33. },
    34. 'at': {
    35. 'isAtAll': false
    36. }
    37. }"
    38. }
    39. Check_mysql_status
    40. 您在 /var/spool/mail/root 中有新邮件