[backcolor=rgb(51, 51, 51) !important]#!/usr/bin/python2.7 nagios安装
[backcolor=rgb(51, 51, 51) !important]# -*- coding:utf-8 -*-
[backcolor=rgb(51, 51, 51) !important]from __future__ import division
[backcolor=rgb(51, 51, 51) !important]from optparse import OptionParser
[backcolor=rgb(51, 51, 51) !important]import commands,sys,jastme,re
[backcolor=rgb(51, 51, 51) !important]from datetime import datetime
[backcolor=rgb(51, 51, 51) !important]"""
[backcolor=rgb(51, 51, 51) !important] Nagios plugin to report the mysql TPS 开源监控软件
[backcolor=rgb(51, 51, 51) !important] author jastme
[backcolor=rgb(51, 51, 51) !important]"""
[backcolor=rgb(51, 51, 51) !important]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 ")
[backcolor=rgb(51, 51, 51) !important]
[backcolor=rgb(51, 51, 51) !important]parser.add_option("-w", "--warning",action="store", type="string", dest="warn_threshold", help="Warning threshold in percentage")
[backcolor=rgb(51, 51, 51) !important]
[backcolor=rgb(51, 51, 51) !important]parser.add_option("-c", "--critical",action="store", type="string", dest="crit_threshold", help="Critical threshold in percentage")
[backcolor=rgb(51, 51, 51) !important] nagios配置
[backcolor=rgb(51, 51, 51) !important](options, args) = parser.parse_args()
[backcolor=rgb(51, 51, 51) !important]try:
[backcolor=rgb(51, 51, 51) !important] f=open('/tmp/Com_commit.txt')
[backcolor=rgb(51, 51, 51) !important]except IOError:
[backcolor=rgb(51, 51, 51) !important] f=open('/tmp/Com_commit.txt','w')
[backcolor=rgb(51, 51, 51) !important] print 'wait next check,initialize the date time.txt'
[backcolor=rgb(51, 51, 51) !important]finally:
[backcolor=rgb(51, 51, 51) !important] f.close()
[backcolor=rgb(51, 51, 51) !important]try:
[backcolor=rgb(51, 51, 51) !important] f=open('/tmp/Com_rollback.txt')
[backcolor=rgb(51, 51, 51) !important]except IOError:
[backcolor=rgb(51, 51, 51) !important] f=open('/tmp/Com_rollback.txt','w')
[backcolor=rgb(51, 51, 51) !important] print 'wait next check,initialize the date time.txt' 监控软件
[backcolor=rgb(51, 51, 51) !important]finally:
[backcolor=rgb(51, 51, 51) !important] f.close()
[backcolor=rgb(51, 51, 51) !important]try:
[backcolor=rgb(51, 51, 51) !important] f=open('/tmp/tpstime.txt')
[backcolor=rgb(51, 51, 51) !important]except IOError:
[backcolor=rgb(51, 51, 51) !important] f=open('/tmp/tpstime.txt','w')
[backcolor=rgb(51, 51, 51) !important] print 'wait next check,initialize the date time.txt'
[backcolor=rgb(51, 51, 51) !important]finally:
[backcolor=rgb(51, 51, 51) !important] f.close()
[backcolor=rgb(51, 51, 51) !important]class Monitor:
[backcolor=rgb(51, 51, 51) !important] def __init__(self,username,password,hostname,port):
[backcolor=rgb(51, 51, 51) !important] """ you can call sam var here """
[backcolor=rgb(51, 51, 51) !important] self.username = username
[backcolor=rgb(51, 51, 51) !important] self.password = password nagios实施
[backcolor=rgb(51, 51, 51) !important] self.port = port
[backcolor=rgb(51, 51, 51) !important] self.hostname = hostname
[backcolor=rgb(51, 51, 51) !important] def __Com_commit(self):
[backcolor=rgb(51, 51, 51) !important] 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))
[backcolor=rgb(51, 51, 51) !important] now=re.findall(r'\d+',now)[0]
[backcolor=rgb(51, 51, 51) !important] f=open('/tmp/Com_commit.txt','r')
[backcolor=rgb(51, 51, 51) !important] before=f.readlines()
[backcolor=rgb(51, 51, 51) !important] f.close()
[backcolor=rgb(51, 51, 51) !important] if before == []:
[backcolor=rgb(51, 51, 51) !important] ff=open('/tmp/Com_commit.txt','w')
[backcolor=rgb(51, 51, 51) !important] ff.write(now)
[backcolor=rgb(51, 51, 51) !important] ff.close()
[backcolor=rgb(51, 51, 51) !important] else:
[backcolor=rgb(51, 51, 51) !important] before=before[-1]
[backcolor=rgb(51, 51, 51) !important] N = int(now)-int(before)
[backcolor=rgb(51, 51, 51) !important] ff=open('/tmp/Com_commit.txt','w')
[backcolor=rgb(51, 51, 51) !important] ff.write(now)
[backcolor=rgb(51, 51, 51) !important] ff.close()
[backcolor=rgb(51, 51, 51) !important] return N
[backcolor=rgb(51, 51, 51) !important] def __Com_rollback(self):
[backcolor=rgb(51, 51, 51) !important] 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))
[backcolor=rgb(51, 51, 51) !important] now=re.findall(r'\d+',now)[0] nagios培训
[backcolor=rgb(51, 51, 51) !important] f=open('/tmp/Com_rollback.txt','r')
[backcolor=rgb(51, 51, 51) !important] before=f.readlines()
[backcolor=rgb(51, 51, 51) !important] f.close()
[backcolor=rgb(51, 51, 51) !important] if before == []:
[backcolor=rgb(51, 51, 51) !important] ff=open('/tmp/Com_rollback.txt','w')
[backcolor=rgb(51, 51, 51) !important] ff.write(now)
[backcolor=rgb(51, 51, 51) !important] ff.close()
[backcolor=rgb(51, 51, 51) !important] else:
[backcolor=rgb(51, 51, 51) !important] before=before[-1]
[backcolor=rgb(51, 51, 51) !important] ff=open('/tmp/Com_rollback.txt','w')
[backcolor=rgb(51, 51, 51) !important] ff.write(now)
[backcolor=rgb(51, 51, 51) !important] ff.close()
[backcolor=rgb(51, 51, 51) !important] N = int(now)-int(before)
[backcolor=rgb(51, 51, 51) !important] return N
[backcolor=rgb(51, 51, 51) !important] def __Uptime(self):
[backcolor=rgb(51, 51, 51) !important] time_now=datetime.now()
[backcolor=rgb(51, 51, 51) !important] ff=open('/tmp/tpstime.txt','r')
[backcolor=rgb(51, 51, 51) !important] time_before_str=ff.read()
[backcolor=rgb(51, 51, 51) !important] ff.close()
[backcolor=rgb(51, 51, 51) !important] if time_before_str=='':
[backcolor=rgb(51, 51, 51) !important] ffw=open('/tmp/tpstime.txt','w')
[backcolor=rgb(51, 51, 51) !important] ffw.write(str(time_now))
[backcolor=rgb(51, 51, 51) !important] ffw.close()
[backcolor=rgb(51, 51, 51) !important] else:
[backcolor=rgb(51, 51, 51) !important] time_before=datetime.strptime(time_before_str,"%Y-%m-%d %H:%M:%S.%f")
[backcolor=rgb(51, 51, 51) !important] delay=(time_now-time_before).seconds
[backcolor=rgb(51, 51, 51) !important] ffw=open('/tmp/tpstime.txt','w')
[backcolor=rgb(51, 51, 51) !important] ffw.write(str(time_now))
[backcolor=rgb(51, 51, 51) !important] ffw.close()
[backcolor=rgb(51, 51, 51) !important] return delay
[backcolor=rgb(51, 51, 51) !important] def doit(self):
[backcolor=rgb(51, 51, 51) !important] _Com_commit = int(self.__Com_commit())
[backcolor=rgb(51, 51, 51) !important] _Com_rollback = int(self.__Com_rollback())
[backcolor=rgb(51, 51, 51) !important] _Uptime = int(self.__Uptime())
[backcolor=rgb(51, 51, 51) !important] R = ( _Com_rollback + _Com_commit) / _Uptime
[backcolor=rgb(51, 51, 51) !important] print "TPS is %.1f | TPS=%.1f" %(R,R)
[backcolor=rgb(51, 51, 51) !important]if __name__ == "__main__":
[backcolor=rgb(51, 51, 51) !important] username = "monitor"
[backcolor=rgb(51, 51, 51) !important] password = str(jastme.decrypt(119,u'NALBOBJBCBHAGBOA'))
[backcolor=rgb(51, 51, 51) !important] hostname = "127.0.0.1"
[backcolor=rgb(51, 51, 51) !important] port = "3369"
[backcolor=rgb(51, 51, 51) !important] Monitor(username,password,hostname,port).doit()