MySQL主从同步shell脚本检测并发邮件报警或自动修复

cat mon_slave_io_sql.sh


#!/bin/bash
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
export PATH
mailto="rscpass@163.com"
log_file="/server/shell_scripts/check_mysql_slave_and_mail/log.log"
port=3306
io_stat=`mysql -e 'show slave status\G' |grep -E "Slave_IO_Running:"| awk '{print $2}'`
sql_stat=`mysql -e 'show slave status\G' |grep -E "Slave_SQL_Running:"| awk '{print $2}'`
host_ip=`ifconfig  eth0 | grep "inet addr:"| awk -F ":" ' {print $2}'| awk -F " " '{print $1}'`
function log(){
datetime=$(date +%Y-%m-%d_%T)
[ ! -f $log_file ] &&  touch $log_file 
echo $datetime $1 >>$log_file
if [ `du -s ${log_file}| awk '{print $1}'` -gt 1000  ] ;then
mv ${log_file}  ${log_file}_$(date +%Y-%m-%d)
fi

}


function is_run(){
[ `lsof -i:$port | wc -l` -lt 2 ]&&{
echo "MySQL Server is stopped" 
log "MySQL Server is stopped"
}
}


function sendmail(){

/usr/bin/python  ./sendmail.py      $1   $2   $3
}


function check(){
if [ "$io_stat" !=  "Yes" ];then
sendmail   $mailto   "ERROR!!!${host_ip}_Slave_IO_Running:$io_stat"    "ERROR!!!_Slave_IO_Running:${io_stat}ERROR!!!" 
log "$mailto   ERROR!!!_Slave_IO_Running:$io_stat    ERROR!!!_Slave_IO_Running:${io_stat}ERROR!!!"
else
log "${host_ip}_Slave_IO_Running:Yes"
fi


if [ "$sql_stat" !=  "Yes" ];then
sendmail   $mailto   "ERROR!!!${host_ip}_Slave_SQL_Running:${sql_stat}"    "ERROR!!!_Slave_SQL_Running:${sql_stat}ERROR!!!_slave_will_restart" 
        mysql -e 'stop slave;'
sleep 3 
mysql -e 'start slave;'
log   "$mailto   ERROR!!!_Slave_SQL_Running:${sql_stat}    ERROR!!!_Slave_SQL_Running:${sql_stat}ERROR!!!" 
else
log "${host_ip}_Slave_SQL_Running:Yes"
fi
}


function main(){
while true
do
is_run
check
sleep 60
done
}


main





发送邮件Python脚本:

cat sendmail.py

#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
import sys
from email.mime.text import MIMEText
import time
#reload(sys)
#sys.setdefaultencoding('utf8')
current_time=time.strftime('%Y-%m-%d%H:%M',time.localtime(time.time()))
mail_host='mail.163.cn'
mail_user='rsc@163.com'
mail_pwd='123'
def send_email(mailto,get_sub,content):
    #msg = MIMEText( content.encode('utf8'),_subtype = 'html', _charset = 'utf8')
    msg = MIMEText(content,_subtype='plain',_charset='gb2312')
    msg['From']=mail_user
    msg['Subject']=get_sub
    #msg['To']=",".join(mailto)
    msg['To']=mailto
    try:
s=smtplib.SMTP_SSL(mail_host,465)
  s.login(mail_user,mail_pwd)
s.sendmail(mail_user,mailto,msg.as_string())
s.close()
    except Exception as e:
        print 'Exception: ', e
    title=sys.argv[2]
    cont="""
    ---------------------------------
    摘要:  %s
    ---------------------------------
    时间:  %s
    ---------------------------------
    """%(sys.argv[3],current_time)
    to_list = ['%s' %(sys.argv[1]),]
    with open('/tmp/sendmail_qs.log','ab') as f:
      f.write('%s  Send to  address:  %s Title: %s Substring: %s \n'%(current_time,sys.argv[1],title,sys.argv[3]))
if __name__ == "__main__":
    send_email(sys.argv[1], sys.argv[2], sys.argv[3])

分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS