#!/bin/bash
mysql -e "show slave status\G" > mysql_status.txt
array=($(egrep 'Slave_IO_Running|Slave_SQL_Running|Seconds_Behind_Master' mysql_status.txt))
if [[ "${array[1]}" == 'Yes' && "${array[3]}" == 'Yes' ]]
then
        if "${array[5]}" -eq 0 ]
        then
        echo "MySQL slave running OK!"
        else
        echo "MySQL slave is behind master ${array[5]} seconds"
        fi
else
        echo "MySQL slave sync make error"
        mail -s "MySQL-Slave Error on `uname -n`" 44850823@qq.com
fi
 
 
=====================

# -*- coding: utf-8 -*
#! /usr/bin/env python
#
#sudo easy_install fabric
#sudo easy_install paramiko
#fab -f mysql.py check
#[利用Python监测MySQL主从状态](http://hypocritical.blog.51cto.com/3388028/1680778)
#[Python fabric远程自动部署简介](http://lovesoo.org/python-fabric-yuan-cheng-zi-dong-bu-shu-jian-jie.html)
#[自动化运维工具Fabric - 密码管理](http://segmentfault.com/a/1190000000497630)

from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
import string
import smtplib
import paramiko

#错误日志
#paramiko.util.log_to_file("filename.log")

#client ip
env.user='root'
env.hosts=[
'192.168.60.7',
'192.168.60.61',
]
#env.password='xx'
env.port='22'
#env.user='root'
#env.password='xx'
env.passwords={
'root@192.168.60.7:22':"password",
'root@192.168.60.61:22':"password",
}

#env.mysql_port = '3306'
@task
def 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 -ustatus -pxx -h"+slave_ip+" -e 'show slave status\G'|grep Slave_IO_Running:|awk '{print $2}'")
slave_sql = run("mysql -ustatus -pxx -h"+slave_ip+" -e 'show slave status\G'|grep Slave_SQL_Running:|awk '{print $2}'")
if slave_io.find('Yes')>=0 and slave_sql.find('Yes') >= 0:
pass
else:
HOST = "smtp.163.com"
SUBJECT = "MySQL Master-Slave Warning . "
TO = "xx@qq.com"
FROM = "xx@163.com"
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,"25")
server.starttls()
server.login("xx@163.com","password")
server.sendmail(FROM, [TO], BODY)
server.quit()

python MySQL-Slave从服务器状态检测脚本的更多相关文章

  1. 如何编写LVS对Real Server的健康状态检测脚本

    简介:Linux 虚拟服务器(Linux Virtual Server. LVS),是一个由章文松开发的自由软件.利用KVS可以实现高可用的.可伸缩缩的Web, Mail, Cache和Medial等 ...

  2. MSC服务器-主从检测脚本-check_server_state.sh

    说明: 发现keepalived会在凌晨自动进行主从切换,导致msc相关进程运行不稳定: 通过运行check_server_state.sh,及时终止/启动相关进程: 所有脚本使用supervisor ...

  3. Redis + keepalived 高可用行配置检测脚本

    Redis 在生产配置中:除redis集群.哨兵模式之外:主从模式还是比较普遍的. 配置 redis 多主从:由 keepalived 做 VIP 地址漂移.可以实现redis的高可用性. keepa ...

  4. 查看mysql主从配置的状态及修正 slave不启动问题

    1.查看master的状态 mysql> show master status;  //Position不应该为0 mysql> show processlist;  //state状态应 ...

  5. Mysql slave 状态之Seconds_Behind_Master

    在MySQL的主从环境中,我们可以通过在slave上执行show slave status来查看slave的一些状态信息,其中有一个比较重要的参数Seconds_Behind_Master.那么你是否 ...

  6. MySQL slave状态之Seconds_Behind_Master【转】

    在MySQL的主从环境中,我们可以通过在slave上执行show slave status来查看slave的一些状态信息,其中有一个比较重要的参数Seconds_Behind_Master.那么你是否 ...

  7. MySQL slave状态之Seconds_Behind_Master zz

    在MySQL的主从环境中,我们可以通过在slave上执行show slave status来查看slave的一些状态信息,其中有一个比较重要的参数Seconds_Behind_Master.那么你是否 ...

  8. MySQL执行SHOW STATUS查询服务器状态状态之Handler_read_* 详解

    在MySQL里,我们一般使用SHOW STATUS查询服务器状态,语法一般来说如下: SHOW [GLOBAL | SESSION] STATUS [LIKE ‘pattern’ | WHERE ex ...

  9. MySQL slave状态之Seconds_Behind_Master

    在MySQL的主从环境中,我们能够通过在slave上运行show slave status来查看slave的一些状态信息,当中有一个比較重要的參数Seconds_Behind_Master.那么你是否 ...

随机推荐

  1. Codeforce 9C - Hexadecimal's Numbers

    One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got ...

  2. 利用selenium模拟登录webqq

    from selenium import webdriver import selenium.webdriver.support.ui as ui import time opt = webdrive ...

  3. [LeetCode&Python] Problem 206. Reverse Linked List

    Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...

  4. seo:网站被黑的预防及处理方法

    一.网站被黑的类型有哪些 1.网站挂木马  :通过网站后台 FTP等植入恶意代码 2.网站域名被恶意泛解析 3.跳转 4.百度快照劫持:黑客劫持快照在你不工作的时候进入你的网站的,一般是凌晨1点到5. ...

  5. asm.js 和 Emscripten 入门教程

    http://www.ruanyifeng.com/blog/2017/09/asmjs_emscripten.html

  6. [小A与最大子段和][斜率优化dp+二分]

    链接:https://ac.nowcoder.com/acm/contest/545/A来源:牛客网题目描述 小A在网上看到了 "最大子段和" 问题的解法.第二天,小A向小B讲解了 ...

  7. 《DSP using MATLAB》Problem 5.7

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  8. 解决ERROR - unregister mbean error javax.management.InstanceNotFoundException: com.alibaba.druid:type=

    https://blog.csdn.net/chengsi101/article/details/72627062 https://www.cnblogs.com/gradven/p/6323195. ...

  9. python中的print函数

    python3.x中将print由一个声明转变成了一个函数. 官方说法: Converts the print statement to the print() function. print(*ob ...

  10. Java中final的用法总结

    1.         修饰基础数据成员的final 这是final的主要用途,其含义相当于C/C++的const,即该成员被修饰为常量,意味着不可修改.如java.lang.Math类中的PI和E是f ...