#!/usr/bin/python2.7# -*- coding:utf-8 -*-from optparse import Optionparserimport commands,sys,jastme,refrom 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 [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 ") 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 ()
Nagios monitors MySQL read-write ratio r/w