#!/usr/bin/env python#encoding:utf-8import mysqldbimport paramiko import sys,osfrom email.mime.text import MIMETextfrom email.header import Headerimport smtplibdb1_ip= "172.17.2.51" db2_ip= "172.17.2.52" num = "mmm_info = " ios = "SqS = ' sec = ' e = ' Def db_info (IP): global ios global sqs global sec global e try: Conn = mysqldb.connect (host=ip,user= ' test ', passwd= ' 123456 ', port=7000,charset= ' UTF8 ') cur = conn.cursor () cur.execute (' Show slave status ') for n in cur.fetchall (): ios=n[10] SqS=n[11] Sec=n[32] cur.close () &nbsP; conn.close () except MySQLdb.Error,e: print "mysqldb Error ", E def send_mail (INFO): to = " [email protected] " user = "[email protected]" passwd = "xxxxxx" subject = ' Liaoning Mobile Enterprise Communications database failure warning ' mail_host = ' smtp.feinno.com ' port = ' 587 ' content=info msg = mimetext (content,_subtype= ' plain ', _ charset= ' utf-8 ') msg[' to '] = to msg[' from '] = user msg[' Subject '] = header (subject, ' utf-8 ') s = smtplib. SMTP () s.connect (Mail_host,port) s.login (user,passwd) s.sendmail (User,to, Msg.as_string ()) s.close () def mmm_status (): global num Global mmm_info host=db2_ip user= ' root ' password= ' xxxxxx ' cmd= ' mmm_control show ' s = paramiko. Sshclient () s.load_system_host_keys () s.set_missing_host_key_policy (Paramiko . Autoaddpolicy ()) s.connect (host,22,user,password,timeout=1) stdin,stdout,stderr = s.exec_command (CMD) cmd_result = stdout.read (), Stderr.read () mmm_ Info=str (List (Cmd_result) [0]) def run (IP): mmm_status () db_info (IP) if e == "": if ios != "yes" or sqs ! = "Yes": db_info= '%s replicaiton error! \nslave_io_ running:%s\nslave_sql_running:%s\nseconds_behind_master:%s\n ' \n%s ' % (ip,ios,sqs,sec,mmm_info) send_mail (db_info) else: db_info= ' %s mysql_db is not ok ,info:%s ' % (ip,e) send_mail (db_info) run (DB1_IP) run (DB2_IP)
This article is from the "Chocolee" blog, make sure to keep this source http://chocolee.blog.51cto.com/8158455/1586748
Python detects MySQL status script--reference