#!/usr/bin/python2.7# -*- coding:utf-8 -*-from __future__ import divisionfrom optparse import optionparserimport commands,sys,jastme,refrom datetime import datetime "" " nagios plugin to report the mysql tps author jastme "" "Parser = optionparser (usage="%prog -w < warning threshold> -c <critical threshold> [ -h ]\n\nbefore use the script,please execute ' grant usage on *.* to [email Protected] ' 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") &Nbsp; 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 () 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 ') 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 () &NBSP;&NBSP;&NBsp; 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 &nbsP; 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 ()) if (_com_rollback+_com_commit) < _Uptime: _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 ()
MySQL TPS monitoring script
Nagios monitoring MySQL TPS---check_mysql_tps.py