mysql 调用脚本_分享一个MySQL主从监控脚本—优化为钉钉告警

概述

因为平时我是不看邮件内容的,所以就改了一下之前的主从监控脚本告警方式为钉钉告警,仅供参考。


一、mysql主从监控脚本

其实思路很简单,查看MySQL主从状态应该都会,就是进入MySQL,然后show master status和show slave status G,然后我要做的其实就是通过脚本定时进入MySQL然后查看状态得到信息,再通过grep和awk对文本进行解析,然后关键的slave_sql_running、slave_io_running和last_error判断后,异常则发送钉钉告警。

1、配置密文

这里用mysql_config_editor的--login-path来解决明文密码的问题
mysql_config_editor命令使用:

 mysql_config_editor set --login-path=tmsdb --host=xxx --user=root --passwordbr mysql_config_editor print --all 

完成配置后,生成一个tmsdb的登录点(login-path),密钥保存在$HOME/.mylogin.cnf。以后访问数据库只要指定login-path的名称即可

 mysql --login-path=tmsdb

2、脚本内容

#!/bin/bash#######################################copyright by hwb#check MySQL_Slave Status#crontab time 00:10######################################3ip="xxxx.95"date=`date +"%Y%m%d-%H:%M:%S"`mysql_status=`mysql --login-path=tmsdb  -e "show variables like 'innodb_buffer_pool_size';"| grep -v Value |awk '{print $2/1024^2}'`num=`mysql --login-path=tmsdb   -e "show slave statusG" | grep Seconds_Behind_Master | awk '{print $2}'`array=($(mysql --login-path=tmsdb -e "show slave statusG"|egrep "Running|Seconds_Behind_Master" | awk '{print $2}'))webhook='https://oapi.dingtalk.com/robot/sexxxxxx26ac7509a5c887492a4b17d33e257ba1550d36b30'#调用函数库[ -f /etc/init.d/functions ] && source /etc/init.d/functionsexport PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binsource /etc/profile#root用户执行脚本[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1function SendMsgToDingding_all() {  curl $webhook -H 'Content-Type: application/json' -d "  {    'msgtype': 'text',    'text': {      'content': '$message '    },    'at': {      'isAtAll': true    }  }"}function SendMsgToDingding() {  curl $webhook -H 'Content-Type: application/json' -d "  {    'msgtype': 'text',    'text': {      'content': '$message '    },    'at': {      'atMobiles': [    "18825144300","18822212312"  ],  "isAtAll": false    }  }"}function mysql_slave_status(){#mysql服务状态监控if [ "${mysql_status}" > "0" ];then    action "[$date] MySQL数据库运行正常!" /bin/true    #主从复制存活状态监控message="【通知】MySQL数据库:$ip从库没有启动,请及时排查!"    #if [ "${array[0]}" == "Yes" ] && [ "${array[1]}" == "Yes" ] && [ "${array[2]} " == "0" ]; then    if [ "${array[0]}" == "Yes" ] && [ "${array[1]}" == "Yes" ]; then      action "[$date] MySQL从库运行正常!" /bin/true    else      SendMsgToDingding    fi    #主从复制延时时间监控message="【通知】MySQL数据库:$ip主从不一致,请及时排查!"    if [ $num -eq 0 ];then       action "[$date] MySQL数据库主从一致!" /bin/true    else       SendMsgToDingding    fielse    message="【通知】MySQL数据库:$ip无法访问,请及时排查!"    SendMsgToDingdingfi}mysql_slave_status
a86bd0b96550e3dcb4dd35de1811ba2a.png

二、脚本测试

1、运行正常

2d2de2a5faec090b00e2bde79357da8f.png

2、运行异常

ff24128b2674022a89d0d8768aa22360.png

三、设置定时任务

 */10 * * * *  /bin/bash /home/scripts/mysql_slave_status.sh > /dev/null 2&1

后面会分享更多devops和DBA方面内容,感兴趣的朋友可以关注下!

f5255583727128a93c3319db3f6c2875.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值