Python detects MySQL status script--reference

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.