importosimportsysimportMySQLdbimporttimeimportmessage_alliplist= ['10.10.7.147','10.10.7.148','10.10.7.149']
hostlist= ['EAGLE','HAWK','FRENK']
defgetStatus(conn):
query=" SHOW SLAVE STATUS "cursor=conn.cursor()
cursor.execute(query)
result=cursor.fetchall()
returnresult[0]
defresolve(conn):
cursor=conn.cursor()
query1="set global sql_slave_skip_counter=1"query2="START SLAVE"query3="SHOW SLAVE STATUS"cursor.execute(query1)
cursor.execute(query2)
cursor.execute(query3)
conn.commit()
if__name__=='__main__':
foripvarinrange(0,3):
conn=MySQLdb.connect(host=iplist[ipvar], user='cv_slave',passwd='tianhu2018yyp7',db='information_schema',unix_socket='/tmp/mysql.sock',charset='utf8')
status=getStatus(conn)
printstatusprint"Seconds_Behind_Master: %s"%status[-8]
print"Slave_IO_Running:",status[10],"Slave_SQL_Running:",status[11]
print"Master_Log_File:",status[5],"Relay_Master_Log_File:",status[9]
print"Read_Master_Log_Pos:",status[6],"Exec_Master_Log_Pos:",status[21]
try:
ifint(status[-8]) >1000:
print'Seconds_Behind_Master Lost'message_all.mailTo("ops_monitor",["xxx@163.com"],'MySQL DB 监控','MySQL Slave cv主机: %s--%s --主从同步延迟落后 %s秒(如大于1000 >请关注从库IO\Load情况) '% (hostlist[ipvar],iplist[ipvar],status[-8]))
ifstatus[10] =='No':
print'Slave_IO_Running is Not Normal'message_all.mailTo("ops_monitor",["xxx@163.com"],'MySQL DB 监控','MySQL Slave cv主机: %s--%s --致命错误 Slave_IO_Running is "No" (需立即登录从库定位,主从同步错误)'% (hostlist[ipvar],iplist[ipvar]))
ifstatus[11] =='No':
print'Slave_SQL_Running is NO'message_all.mailTo("ops_monitor",["xxx@163.com"],'MySQL DB 监控','MySQL Slave cv主机: %s--%s --致命错误 Slave_SQL_Running is "No" (需立即登录从库定位,主从同步错误)'% (hostlist[ipvar],iplist[ipvar]))
exceptException, e:
printemessage_all.mailTo("ops_monitor",["xxx@163.com"],'MySQL DB 监控','MySQL Slave cv主机: %s--%s --致命错误 %s'% (hostlist[ipvar],iplist[ipvar],e))
printtime.ctime(time.time())
print"\n"