关闭

批量检测Mysql复制状态的shell 脚本

标签: mysqlshell脚本
125人阅读 评论(0) 收藏 举报
分类:

批量检查Mysql 复制状态脚本,发现复制异常的Mysql数据库通过企业邮箱进行报警。

##############check replication result###########################


#! /bin/bash

time=`date +%Y%m%d-%H:%M:%S`


if [ -f /root/script/checkresult.txt ];then
                cat /dev/null > /root/script/checkresult.txt
        else
                touch /root/script/checkresult.txt
        fi


for checklist in `cat /root/script/check_replication_list.txt`;do
        #echo ${checklist}
        cn=0
        ip=`echo ${checklist} | awk -F ":" '{print $1}'`
        port=`echo ${checklist} | awk -F ":" '{print $2}'`
        output=`/opt/mysql3306/bin/mysql -urepcheck -prepcheck -h${ip} -P${port} -e "show slave status\G;" | grep -P 'Slave_(IO|SQL)_Running:'`
        while read line;do
                #echo $line
                if [ `echo $line | awk -F ": " '{print $2}'`=='YES' ];then
                        cn=`expr ${cn} + 1`
                fi
        done<<EOF
${output}
EOF
        if [ ${cn} -eq 2 ]; then
                echo ${time} 'ip address:'${ip}' port:'${port}' REPLICATION IS OK!' >> /root/script/checkresult.txt
        else
                echo ${time} 'ip address:'${ip}' port:'${port}' REPLICATION IS ERROR!' >> /root/script/checkresult.txt
        fi

done


########################在通过python检测结果发现异常通过邮箱发送邮件############################

[root@mysqltest script]# cat alert.py 
from sendmail import sendmail




with open('/root/script/checkresult.txt', 'r') as f:
        for line in f.readlines():
                flag=line.find('OK')
                if flag == -1:
                        sendmail(line)


################sendmail()#####################

[root@mysqltest script]# cat sendmail.py
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib


def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr(( \
        Header(name, 'utf-8').encode(), \
        addr.encode('utf-8') if isinstance(addr, unicode) else addr))


def sendmail(s):


        from_addr = 'huym@corp.21cn.com'
        password = '287473617@qq.com'
        to_addr = 'huym@corp.21cn.com'
        smtp_server = 'smtp.corp.21cn.com'  ##公司smtp服务器


        msg = MIMEText(s, 'plain', 'utf-8')
        msg['From'] = _format_addr(u'Python alert <%s>' % from_addr)
        msg['To'] = _format_addr(u'Administrator <%s>' % to_addr)
        msg['Subject'] = Header(u'replication error', 'utf-8').encode()


        server = smtplib.SMTP(smtp_server, 25)
        server.set_debuglevel(1)
        server.login(from_addr, password)
        server.sendmail(from_addr, [to_addr], msg.as_string())
        server.quit()

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1818次
    • 积分:49
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    文章存档
    最新评论