PYTHON实现MySQL监控脚本

这个Python脚本用于获取MySQL服务器的监控信息,包括QPS、TPS、缓存命中率、慢查询等关键指标,帮助分析和优化MySQL性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#! /usr/bin/python
# -*- encoding: utf8 -*-
#from __future__ import division
import sys
import MySQLdb


# 数据库配置参数
host = '192.168.20.235'
user = 'root'
password = 'root'
db = 'orca_cmdb'


#----------------------------------------------------------------------
def getConn(host, user, passwd, db):
  try:
    conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
    return conn
  except Exception, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit(1)
    
#----------------------------------------------------------------------
def closeConn(conn):
  """关闭 mysql connection"""
  conn.close()
  
#----------------------------------------------------------------------
def getValue(conn, query):
  """ 查询相关参数 返回一个值 """
  cursor = conn.cursor()
  getNum=cursor.execute(query)
  if getNum>0:
      result = cursor.fetchone()
  else:
      result=['0']
  return int(result[1])


def getQuery(conn, query):
  """ 查询相关参数 返回多个值 """
  cursor = conn.cursor()
  cursor.execute(query)
  result = cursor.fetchall()
  return result


#执行查询的总次数
Questions = "show global status like 'Questions'"
#服务器已经运行的时间(以秒为单位)
Uptime = "show global status like 'Uptime'"


Com_commit = "show global status like 'Com_commit'"
Com_rollback = "show global status like 'Com_rollback'"
#从硬盘读取键的数据块的次数。如果Key_reads较大,则Key_buffer_size值可能太小。
#可以用Key_reads/Key_read_requests计算缓存损失率
Key_reads = &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值