码迷,mamicode.com
首页 > 数据库 > 详细

checkmysql.py

时间:2017-09-30 21:06:34      阅读:298      评论:0      收藏:0      [点我收藏+]

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

#!/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   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!