来自:网络
#!/usr/bin/python2.7
# -*- coding:utf-8 -*- nagios培训
from __future__ import division
from optparse import OptionParser
import commands,sys,jastme,re
from datetime import datetime
"""
Nagios plugin to report the mysql TPS
author jastme
nagios实施
"""
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()
监控软件
try:
f=open('/tmp/Com_commit.txt')
except IOError:
f=open('/tmp/Com_commit.txt','w')
print 'wait next check,initialize the date time.txt'
finally:
f.close()
try:
f=open('/tmp/Com_rollback.txt')
except IOError:
f=open('/tmp/Com_rollback.txt','w')
print 'wait next check,initialize the date time.txt'
finally:
f.close()
nagios配置
try:
f=open('/tmp/tpstime.txt')
except IOError:
f=open('/tmp/tpstime.txt','w')
print 'wait next check,initialize the date time.txt'
finally:
f.close() 开源监控软件
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 __Com_commit(self):
now=commands.getoutput(''' mysql -u%s -p%s -h%s -P%s -e "show global status like 'Com_commit'" | grep -Po "\d+" ''' %(self.username,self.password,self.hostname,self.port))
now=re.findall(r'\d+',now)[0]
f=open('/tmp/Com_commit.txt','r')
nagios安装
before=f.readlines()
f.close()
if before == []:
ff=open('/tmp/Com_commit.txt','w')
ff.write(now)
ff.close()
else:
before=before[-1]
N = int(now)-int(before)
ff=open('/tmp/Com_commit.txt','w')
ff.write(now)
ff.close()
return N
def __Com_rollback(self):
now=commands.getoutput(''' mysql -u%s -p%s -h%s -P%s -e "show global status like 'Com_rollback'" | grep -Po "\d+" ''' %(self.username,self.password,self.hostname,self.port))
now=re.findall(r'\d+',now)[0]
f=open('/tmp/Com_rollback.txt','r')
before=f.readlines()
f.close()
if before == []:
ff=open('/tmp/Com_rollback.txt','w')
ff.write(now)
ff.close()
else:
before=before[-1]
ff=open('/tmp/Com_rollback.txt','w')
ff.write(now)
ff.close()
N = int(now)-int(before)
return N
def __Uptime(self):
time_now=datetime.now()
ff=open('/tmp/tpstime.txt','r')
time_before_str=ff.read()
ff.close()
if time_before_str=='':
ffw=open('/tmp/tpstime.txt','w')
ffw.write(str(time_now))
ffw.close()
else:
time_before=datetime.strptime(time_before_str,"%Y-%m-%d %H:%M:%S.%f")
delay=(time_now-time_before).seconds
ffw=open('/tmp/tpstime.txt','w')
ffw.write(str(time_now))
ffw.close()
return delay
def doit(self):
_Com_commit = int(self.__Com_commit())
_Com_rollback = int(self.__Com_rollback())
_Uptime = int(self.__Uptime())
R = ( _Com_rollback + _Com_commit) / _Uptime
print "TPS is %.1f | TPS=%.1f" %(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).doit()
想做Nagios, Zabbix,Cacti,iTop各种交流的,可以进入开源监控工具Nagios交流 QQ群号 :476809427
|