MySQL master-slave status monitoring for Python combat series (09)

Source: Internet
Author: User

1. Requirements Description

Because MySQL uses master-slave AB replication in the bottom of OpenStack, in order to monitor the master-slave state information of the database, the master-slave state of MySQL needs to be monitored to ensure that the database is operating at the bottom level and provides better functionality for OpenStack. This article carries on the monitoring to the database, the concrete content reference below.

2. Program Contents

#!/usr/bin/env python#_*_ coding:utf8 _*_#author:happyliu# is used to monitor MySQL master-slave replication status Import osimport  sysimport os.pathimport urllibimport urllib2import mysqldbimport loggingauth_ credentials = {' host ': ' localhost ',  ' port ':3306,  ' user ': ' Root ', ' passwd ': ' Password '} ' logging ' "Logging.basicconfig (        filename =  '/var/log/openstack _mysql_replication_monitor.log ',        filemode =  ' a ',         format =  '% (asctime) s % (filename) s[line:% ( Lineno) d] % (funcName) s % (levelname) s % (message) s ',         datefmt= '%a, %d %b %y %h:%m:%s ',         Level = logging.info) def monitor_mysql_replication ():          '          for monitoring MySQL master-slave replication status, exception alarm          '          status = true        try :                 conn =  mysqldb.connect (**auth_credentials)                  cursor = conn.cursor (cursorclass=mysqldb.cursors.dictcursor)                  cursor.execute (' SHOW  SLAVE STATUS; ')                 result =  cursor.fetchone ()                  if result[' slave_io_running '] ==  "Yes"  and result[' Slave_SQL_Running '] ==  "Yes":                         logging.info (' Mysql master/slave replication  status is successfully ')                  else:                         logging.error (' MySQL Master/Slave  replication fail,please check it ')                          status =  false         except exception as e:                 logging.error (e)                  status = false         return statusdef send_warnings (Receiver,content,title):          "         send RTX alerts to business leaders           '         rtx_url =   "Http://www.example.com:11770/sendRtxByPost"         data =  {                 "AppId"          :6,                  "AppKey"         : ' Password ',                 ' UserName "      :receiver,                  "title"          :title,                  "Content"         :content        }         postdata = urllib.urlencode (data)         req =  urllib2. Request (Rtx_url,postdata)         return urllib2.urlopen (req) def  get_hostname ():         "          get the hostname of the Linux system          '          return os.environ[' HOSTNAME '] def clean_log ():          "         clean up the log file, and when the file is larger than 100M, clean the file and prevent the log from taking up too much space           '         log_file =  '/var/log/ Openstack_mysql_replication_monitor.log '          size =  os.path.getsize (log_file)  /  (1024 * 1024)          if size >= 100:                 os.remove (log_file)                  logging.info ('%s have been remove '  %  (log_file)) if __name__ ==  "__main__":         clean_log ()          warn_receiver =  "Happy;"         if monitor_mysQl_replication ():                 send_warnings (receiver=warn_receiver,content= ' cloud platform MySQL master-slave replication state failed, please check the database status ', Title=get_hostname ())


This article from "Happy Laboratory" blog, declined reprint!

MySQL master-slave status monitoring for Python combat series (09)

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.