监控mysql数据库主从状态

python版本2.6.6


#!/usr/bin/python

import MySQLdb

def check_mysql():
    status = True
    try:
        conn=MySQLdb.connect(host='172.7.10.8',user='summer',passwd='summer',db='cms')
        cur=conn.cursor()
        cur.execute("show slave status;")
        result = cur.fetchall()
        io_thread=result[0][10]
        sql_thread=result[0][11]
        #print io_thread,sql_thread
        cur.close()
        conn.close()
        status = True
        if io_thread == "Yes" and sql_thread == "Yes":
            print '2'
            status = True
        else:
            print '0'
            status = False
    except Exception,e:
        print Exception,":",e

go = check_mysql()


听大神的修改后代码:





#!/usr/bin/python

import MySQLdb

def check_mysql():
    try:
         conn=MySQLdb.connect(host='172.7.10.8',user='summer',passwd='summer',

        db='cms')

        cur=conn.cursor(MySQLdb.cursors.DictCursor)
        cur.execute('show slave status')
        result=cur.fetchall()
        if result is None:
            raise EnvironmentError('result is None')
        slave_status=result[0]
        io_thread=slave_status.get('Slave_IO_Running')
        sql_thread=slave_status.get('Slave_SQL_Running')
        if io_thread == "Yes" and sql_thread == "Yes":
            print 'it is status ok'
        else:
            print 'io_status is %s \nsql_status is %s' %(io_thread,sql_thread)

    finally:
        cur.close()
        conn.close()

if __name__ == '__main__':
    check_mysql()
~










本文转自 妙曼  51CTO博客,原文链接:http://blog.51cto.com/yanruohan/1942758,如需转载请自行联系原作者
上一篇:zabbix监控外网地址-使用zabbix icmp-ping监控网络状态


下一篇:一场不期而遇的美好 当无线遇上大数据