python脚本-mysql监控指标

python写的监控mysql各项指标的脚本:

#!/usr/bin/env python
#! coding:utf-8

import pymysql
import socket

myname = socket.getfqdn(socket.gethostname())

class DBMonitor():
    def __init__(self, db_host, db_user, db_passwd, db_port=3306):
        self.db_host = db_host
        self.db_port = db_port
        self.db_user = db_user
        self.db_passwd = db_passwd

    def connect(self):
        try:
            conn = pymysql.connect(host=self.db_host, port=self.db_port, user=self.db_user, passwd=self.db_passwd, charset='utf8')
            curr = conn.cursor(cursor=pymysql.cursors.DictCursor)
            return curr
        except Exception as e:
            print e

    def query(self, sql):
        cur = self.connect()
        res = cur.execute(sql)
        return cur.fetchall()


if __name__ == "__main__":
    db = DBMonitor(db_host=socket.gethostbyname(myname), db_user='zabbix', db_passwd='zabbix@123456')
    # 已执行语句(由客户端发出)计数
    Questions = db.query('SHOW GLOBAL STATUS LIKE

你可能感兴趣的:(运维常用)