nagios mysql 插件_nagios插件check_mysql_qps

#!/usr/bin/python

# -*- coding:utf-8 -*-

from optparse import OptionParser

import subprocess,sys

from datetime import datetime

"""

Nagios plugin to report the mysql QPS

by jastme

"""

try:

f=open('/usr/local/nagios/etc/qps.txt')

except IOError:

f=open('/usr/local/nagios/etc/qps.txt','w')

print 'wait next check,initialize the date'

finally:

f.close()

try:

f1=open('/usr/local/nagios/etc/time.txt')

except IOError:

f1=open('/usr/local/nagios/etc/time.txt','w')

print 'wait next check,initialize the date'

finally:

f1.close()

parser = OptionParser(usage="%prog -w  -c  [ -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()

def QPS():

'''mysql qps'''

d=time_delay()

fff=open('/usr/local/nagios/etc/qps.txt','r')

qpsbefore=fff.read()

fff.close()

now=subprocess.Popen('''mysql -umonitor -pmonitor -h 127.0.0.1 -e "show global status like 'Questions'" | awk 'NR==2{print $2}' ''',shell=True,stdout=subprocess.PIPE)

now.wait()

qpsnow=now.communicate()[0][:-1]

if qpsbefore=='':

fffw=open('/usr/local/nagios/etc/qps.txt','w')

fffw.write(qpsnow)

fffw.close()

else:

mysqlqps=(int(qpsnow)-int(qpsbefore))/int(d)

fffw=open('/usr/local/nagios/etc/qps.txt','w')

fffw.write(qpsnow)

fffw.close()

return mysqlqps

def time_delay():

'''Compute the time difference'''

time_now=datetime.now()

ff=open('/usr/local/nagios/etc/time.txt','r')

time_before_str=ff.read()

ff.close()

if time_before_str=='':

ffw=open('/usr/local/nagios/etc/time.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('/usr/local/nagios/etc/time.txt','w')

ffw.write(str(time_now))

ffw.close()

return delay

def jastme():

q=QPS()

if not options.crit_threshold:

print "UNKNOWN: Missing critical threshold value."

sys.exit(3)

if not options.warn_threshold:

print "UNKNOWN: Missing warning threshold value."

sys.exit(3)

if q==None:

print 'wait next check,initialize the date'

sys.exit(3)

elif int(q) >= int(options.crit_threshold):

print 'Criticl,The QPS is %s | QPS=%stimes;%s;%s;0' %(q,q,options.warn_threshold,options.crit_threshold)

sys.exit(2)

elif int(options.crit_threshold) > int(q) >= int(options.warn_threshold):

print 'Warning,The QPS is %s | QPS=%stimes;%s;%s;0' %(q,q,options.warn_threshold,options.crit_threshold)

sys.exit(1)

else:

print 'OK,The QPS is %s | QPS=%stimes;%s;%s;0' %(q,q,options.warn_threshold,options.crit_threshold)

sys.exit(0)

if __name__ == '__main__':

jastme()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值