可用于监控mysqlmasterslave状态的python代码

代码如下:

import osimport sysimport mysqldbdef getstatus(conn): query = ” show slave status ” # print query cursor = conn.cursor() cursor.execute(query) result = cursor.fetchall() return result[0]def resolve(conn): cursor = conn.cursor() query1 = “set global sql_slave_skip_counter=1” query2 = “start slave” query3 = “show slave status” cursor.execute(query1) cursor.execute(query2) cursor.execute(query3) conn.commit()if __name__ == ‘__main__’: conn = mysqldb.connect(read_default_file=”~/.my.cnf”, db=””, port=3306, charset=”utf8″) status = getstatus(conn) print “master_log_file: %s” % status[5] print “read_master_log_pos: %s” % status[6] print “seconds_behind_master: %s” % status[-1] if status[32] is none: resolve(conn) else: print ‘resolved’

Posted in 未分类

发表评论