当前位置:Gxlcms > 数据库问题 > checkmysql.py

checkmysql.py

时间:2021-07-01 10:21:17 帮助过:21人阅读

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

import mysql.connector
import psutil
    
"""
SHOW SLAVE STATUS命令输出内容
"""
class GetMysqlResult:
       
    def __init__(self,**getdbconfig):       
        self.getdbconfig = getdbconfig
         
    """
    获取 SHOW SLAVE STATUS 输出结果
    """
    def get_slave_status(self):
        show_slave_status = "SHOW SLAVE STATUS;"        
        cnn = mysql.connector.connect(**self.getdbconfig[‘getdbconfig‘])
        cursor = cnn.cursor(dictionary=True)
        cursor.execute(show_slave_status)
        result_fet = cursor.fetchall()
        result = result_fet[0]
        return result

    """
    检查mysqld是否存在
    """
    def check_mysqld(self):
        check_ture_false_mysqld = True
        pids = psutil.pids()    
        pid_mysqld = ‘mysqld‘
        for pid in pids:
            is_pid_mysqld = psutil.Process(pid).name()                         
            if is_pid_mysqld == ‘mysqld‘:
                pass            
            else:
                check_ture_false_mysqld = False
                                                     
    """
    检查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 "check_ture_false_slave_status",check_ture_false_slave_status
           
    """
    获取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  "Last_SQL_Error",result.get(‘Last_SQL_Error‘)
    
checkdbconfig_13_106={
‘host‘:‘xxxxxx‘,
‘user‘:‘xxxxxx‘,
‘password‘:‘xxxxxx‘,
‘port‘:‘xxxxxx‘
}

print_result = GetMysqlResult(getdbconfig=checkdbconfig_13_106)
print_result.check_slave_status()
print_result.get_last_sql_error()

checkmysql.py

标签:else   存在   imp   fetch   ret   内容   use   __init__   conf   

人气教程排行