get_slave_status.py

#!/usr/bin/env python
#-*- encoding: utf8 -*-

import mysql.connector
import get_mysql_conn_info
    
"""
SHOW SLAVE STATUS命令输出内容
"""
class GetSlaveResult:
    """
    getdbconfig 参数是一个列表,第一列是IP,第二列是账号,第三列是密码,第四列是端口号。
    """   
    def __init__(self,getdbconfig):       
        self.getdbconfig = getdbconfig
         
    """
    获取 SHOW SLAVE STATUS 输出结果
    """
    def get_slave_status(self):
        dbconfig = self.getdbconfig
        for i in range(int(len(dbconfig))):                           
            show_slave_status = "SHOW slave STATUS;"        
            cnn = mysql.connector.connect(host=dbconfig[i][0] ,user=dbconfig[i][1] ,password=dbconfig[i][2] ,port=dbconfig[i][3])
            cursor = cnn.cursor(dictionary=True)
            cursor.execute(show_slave_status)
            result_fet = cursor.fetchall()
            result = result_fet[0]
            result["checkmysql_host"] = dbconfig[i][0]
            result["checkmysql_user"] = dbconfig[i][1]
            result["checkmysql_port"] = int(dbconfig[i][3])
            return result        
                                                     
    """
    检查SHOW SLAVE STATUS状态
    """
    def check_slave_status(self):
        check_ture_false_slave_status = True
        result = self.get_slave_status()
        if result.get('Slave_IO_Running') == 'Yes' and result.get('Slave_SQL_Running') == 'Yes':
            pass
        else:
            check_ture_false_slave_status = False
        print  "主机名: %s , 端口号: %s , Slave_IO_Running 状态 = %s , Slave_SQL_Running 状态 = %s " %(str(result.get('checkmysql_host')),str(result.get('checkmysql_port')),str(result.get('Slave_IO_Running')),str(result.get('Slave_SQL_Running')))
 
    """
    获取Last_SQL_Error内容
    """
    def get_last_sql_error(self):
        check_ture_false_last_sql_error = True
        result = self.get_slave_status()     
        if result.get('Last_SQL_Error')== '':
            pass
        else:
            check_ture_false_last_sql_error = False
        print  "主机名: %s , 端口号: %s , Last_SQL_Error 内容 = %s " %(str(result.get('checkmysql_host')),str(result.get('checkmysql_port')),str(result.get('Last_SQL_Error')))
                
checkdbconfig = get_mysql_conn_info.GetConn('/data/lgj/dblist.xlsx').get_csv()

print_result = GetSlaveResult(checkdbconfig)
print_result.check_slave_status()
print_result.get_last_sql_error()
 


原文地址:https://www.cnblogs.com/liang545621/p/7616032.html