#!/usr/bin/env python
#coding=utf8
import sys
import os
import inspect
class GetMysqlStatus():
def __init__(self):
self.val = {}
self.result = {}
def check(self):
import MySQLdb
import MySQLdb.cursors
try:
self.db = MySQLdb.connect(user="root", passwd="LVS@071103",
host="192.168.1.62", port=3306,
cursorclass=MySQLdb.cursors.DictCursor)
except Exception, e:
raise Exception, 'Cannot interface with MySQL server, %s' % e
def extract(self, key):
try:
c = self.db.cursor()
c.execute("""show global status like '%s';""" % key)
self.val = c.fetchone()
return float(self.val['Value'])
c.close()
self.db.close()
except Exception, e:
print e.message
def init(self):
for key in ('Com_insert', 'Com_delete', 'Com_select', 'Com_rollback',
'Com_update', 'Com_commit', 'Innodb_buffer_pool_pages_total',
'Innodb_buffer_pool_read_requests', 'Innodb_buffer_pool_reads',
'Innodb_buffer_pool_pages_free', 'Innodb_buffer_pool_pages_dirty',
'Key_blocks_used', 'Key_blocks_unused', 'Key_reads',
'Key_read_requests', 'Key_writes', 'Key_write_requests'):
self.result[key] = self.extract(key)
def TPS(self):
TPS = self.result['Com_commit'] + self.result['Com_rollback']
return TPS
def QPS(self):
QPS = self.result['Com_insert'] + self.result['Com_delete'] +
self.result['Com_select'] + self.result['Com_update']
return QPS
def Key_read_hit_ratio(self):
if self.result['Key_read_requests'] == 0:
Key_read_hit_ratio = 0
else:
Key_read_hit_ratio = (1 - self.result['Key_reads'] /
self.result['Key_read_requests']) * 100
return Key_read_hit_ratio
def Key_usage_ratio(self):
Key_usage_ratio = self.result['Key_blocks_used'] /
(self.result['Key_blocks_used'] + self.result['Key_blocks_unused']) * 100
return Key_usage_ratio
def Key_write_hit_ratio(self):
if self.result['Key_write_requests'] == 0:
Key_write_hit_ratio = 0
else:
Key_write_hit_ratio = (1 - self.result['Key_writes'] /
self.result['Key_write_requests']) * 100
return Key_write_hit_ratio
def Innodb_buffer_read_hit_ratio(self):
Innodb_buffer_read_hit_ratio = (1 - self.result['Innodb_buffer_pool_reads'] /
self.result['Innodb_buffer_pool_read_requests']) * 100
return Innodb_buffer_read_hit_ratio
def Innodb_buffer_usage(self):
Innodb_buffer_usage = (1 - self.result['Innodb_buffer_pool_pages_free'] /
self.result['Innodb_buffer_pool_pages_total']) * 100
return Innodb_buffer_usage
def Innodb_buffer_pool_dirty_ratio(self):
Innodb_buffer_pool_dirty_ratio = (self.result['Innodb_buffer_pool_pages_dirty'] /
self.result['Innodb_buffer_pool_pages_total']) * 100
return Innodb_buffer_pool_dirty_ratio
def MySQL_alive(self):
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
try:
sock.connect(('192.168.1.62', 3306))
#print 'MySQL is alive!'
result = 1
return result
except Exception:
#print 'MySQL 3306 not connect!'
result = 0
return result
sock.close()
class ErrorOut():
def error_print(self):
"""输出错误信息"""
print
print 'Usage: ' + sys.argv[0] + ' ' + ' MySQL_Status_Key '
print
sys.exit(1)
class Main():
def main(self):
if len(sys.argv) == 1:
error = ErrorOut()
error.error_print()
elif len(sys.argv) == 2:
method_name = sys.argv[1]
a = GetMysqlStatus()
a.check()
a.init()
if hasattr(a, method_name):
print getattr(a, method_name)()
else:
print a.extract(method_name)
if __name__ == "__main__":
run = Main()
run.main() |