标签:
#!/usr/bin/python2.7
# -*- coding:utf-8 -*-
from optparse import OptionParser
import commands,sys,jastme,re
from datetime import datetime
"""
Nagios plugin to report the mysql R/W Ratio
author jastme
"""
parser = OptionParser(usage="%prog -w <warning threshold> -c <critical threshold> [ -h ]\n\nBefore use the script,please execute ‘grant usage on *.* to monitor@‘127.0.0.1‘ identified by ‘monitor‘;\nflush privileges",version="%prog ")
parser.add_option("-w", "--warning",action="store", type="string", dest="warn_threshold", help="Warning threshold in percentage")
parser.add_option("-c", "--critical",action="store", type="string", dest="crit_threshold", help="Critical threshold in percentage")
(options, args) = parser.parse_args()
class Monitor:
def __init__(self,username,password,hostname,port):
""" you can call sam var here """
self.username = username
self.password = password
self.port = port
self.hostname = hostname
def __Qcache_hits(self,):
R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Qcache_hits‘" | grep -Po "\d+" ‘‘‘ %(self.username,self.password,self.hostname,self.port))
return R
def __Com_select(self):
R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_select‘" | grep -Po "\d+" ‘‘‘ %(self.username,self.password,self.hostname,self.port))
return R
def __Com_insert(self):
R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_insert‘" | grep -Po "\d+" ‘‘‘ %(self.username,self.password,self.hostname,self.port))
return R
def __Com_update(self):
R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_update‘" | grep -Po "\d+" ‘‘‘ %(self.username,self.password,self.hostname,self.port))
return R
def __Com_delete(self):
R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_delete‘" | grep -Po "\d+" ‘‘‘ %(self.username,self.password,self.hostname,self.port))
return R
def __Com_replace(self):
R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_replace‘" | grep -Po "\d+" ‘‘‘ %(self.username,self.password,self.hostname,self.port))
return R
def __doit(self):
_Qcache_hits = int(re.findall(r"\d+",self.__Qcache_hits())[0])
_Com_select = int(re.findall(r"\d+",self.__Com_select())[0])
_Com_insert = int(re.findall(r"\d+",self.__Com_insert())[0])
_Com_update = int(re.findall(r"\d+",self.__Com_update())[0])
_Com_delete = int(re.findall(r"\d+",self.__Com_delete())[0])
_Com_replace = int(re.findall(r"\d+",self.__Com_replace())[0])
R = ( _Com_select + _Qcache_hits) / ( _Com_insert + _Com_update + _Com_delete + _Com_replace ) * 100
return R
def Result(self):
R = self.__doit()
print "R/W Ratio is %d | R/W Ratio=%d" %(R,R)
if __name__ == "__main__":
username = "monitor"
password = str(jastme.decrypt(119,u‘NALBOBJBCBHAGBOA‘))
hostname = "127.0.0.1"
port = "3369"
Monitor(username,password,hostname,port).Result()
标签:
原文地址:http://my.oschina.net/jastme/blog/496892