检查同步以及备份是否正常的监控脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys
import socket
import re
import socket
import commands
import json,time
import smtplib
import logging,glob
import smtplib
from email.mime.text import MIMEText

def record_log(flag,msg):
loger=logging.getLogger()
loger.setLevel(logging.DEBUG)
hf=logging.FileHandler("/home/ericzyg/moniter_replication_log")
recordfmt=logging.Formatter('%(asctime)s\t\t%(levelname)s: %(message)s')
hf.setFormatter(recordfmt)
loger.addHandler(hf)
if flag:
loger.error(msg)
else:
loger.info(msg)
def mail(title,message):
mailserver = "smtp.exmail.qq.com"
frommail = 'monitor@q-dazzle.com'
user = 'monitor@q-dazzle.com'
password = 
toall = [
"***@qq.com", #
"maintenance@q-dazzle.com",#qx
]
msg = MIMEText(message,_subtype="html",_charset="utf-8")
msg['Subject'] = title
msg['From'] = "monitor@q-dazzle.com"
for to in toall:
s = smtplib.SMTP(mailserver)
s.login(user,password)
s.sendmail(frommail,[to],msg.as_string())
s.quit()
time.sleep(30)
def checkbackup(localIP):
portlist = []
packlist = []
m = re.compile('\d{4}')
portstr = glob.glob('/usr/local/script/mysqlbackup*.sh')
for F in portstr:
if m.search(F):
portlist.append(m.search(F).group())
T=time.strftime("%Y%m%d",time.localtime())
for P in portlist:
packlist.extend(glob.glob('/data/backup/database'+P+'_'+T+'.tar.gz'))
print packlist
for CHP in portlist:
print CHP, type(CHP)
chpk_rt = 0
for PA in packlist:
if PA.find(CHP) == -1:
pass
else:
chpk_rt=1
break
if not chpk_rt:
title = "SDK 从库备份报警 [warning] ip:%s" % str(localIP)
content="mysql从库%s备份文件不存在,请检查/data/back目录!" % CHP
mail(title, content)
else:
SZ=os.stat(PA).st_size/1000/1000
if int(SZ)<100:
title = "SDK 从库备份报警 [warning] ip:%s" % str(localIP)
content="mysql从库%s备份大小不正常,请检查!" % CHP
mail(title, content)


def checkrepl(localIP):
conlist=[]
mysql_conf = glob.glob('/usr/local/script/mysqlbackup*.sh')
for FD in mysql_conf:
with open(FD, 'r') as f:
K=re.search('port=\d+', f.read())
conpt=K.group().split('=')
with open(FD, 'r') as f:
pwd=re.search('password=.*', f.read())
conpwd=pwd.group().split('=')
conlist.append((conpt[1],conpwd[1]))
for conn in conlist:
PORT,PASSWORD=conn
cmd='/usr/bin/mysql -uroot -P%s -p"%s" -h 127.0.0.1 -e "show slave status\G" | grep -E "(Slave_IO_Running|Slave_SQL_Running)"' % (PORT,PASSWORD)
status,result=commands.getstatusoutput(cmd)
if result.find('No') == -1:
pass
else:
title="主从同步告警[warning] ip:%s" % str(localIP)
content='IP: %s 从库 %s 同步已停止,请检查!' % (localIP,PORT)
mail(title,content)

 

if __name__=='__main__':
skt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
skt.connect(('8.8.8.8',80))
socketIpPort = skt.getsockname()
localIP = socketIpPort[0]
skt.close()
checkrepl(localIP)
currenttime=int(time.strftime('%H',time.localtime()))
if 9<currenttime<11:
checkbackup(localIP)

转载于:https://www.cnblogs.com/ericzh/p/7866001.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值