Monitor MySQL master-slave status with Python

Source: Internet
Author: User

Detects MySQL master-slave status and, if never synchronized, sends an e-mail to the OPS person. Use timed tasks for second-level monitoring!

# -*- coding: utf-8 -*#! /usr/bin/env pythonfrom fabric.api import  *from fabric.colors import *from fabric.context_managers import *from  fabric.contrib.console import confirmimport stringimport smtplib#client  ipenv.port= ' 9527 ' env.user= ' dyt ' env.hosts=[' 192.168.129.138 ', ' 192.168.129.139 ']env.password= ' dyt2015 ' #env. mysql _port =  ' 3306 ' @taskdef  check ():     slave_ip  = run ("ip  Add|grep global ")     for ip in env.hosts:         if ip in slave_ip:             slave_ip = ip    slave_io  = run ("MySQL  -uroot -S /tmp/mysql_3306.sock -e  ' Show slave status\g ' |grep slave_io _running:|awk  ' {print $2} ')    slave_sql = run ("Mysql -uroot -s /tmp/mysql_3306.sock -e   ' show slave status\g ' |grep slave_sql_running:|awk  ' {print $2} ')       if slave_io ==  ' yes '  and slave_sql ==  ' yes ':         pass    else:         HOST =  "smtp.qq.com" subject =  "MYSQL MASTER-SLAVE WARNING    "to = " [email protected] "from = " [email protected] "text = "%-20s  mysql master-slave status : down " % slave_ip BODY =  String.Join (         "from: %s"  % FROM,          "to: %s"  % TO,          "subject: %s"  % subject ,         "",         text        ),  "\ r \ n") server = smtplib. SMTP () Server.connect (HOST, "Server.starttls") Server.login ("[email protected]", "password") Server.sendmail (From, [to], body) server.quit ()


This article is from the "ˉt, Stand Wentao ┃﹎" blog, please be sure to keep this source http://hypocritical.blog.51cto.com/3388028/1680778

Monitor MySQL master-slave status with Python

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.