nagios插件check_mysql_qps
博客专区 > jastme 的博客 > 博客详情
nagios插件check_mysql_qps
jastme 发表于4年前
nagios插件check_mysql_qps
  • 发表于 4年前
  • 阅读 462
  • 收藏 2
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

摘要: 检查mysql的每秒请求数,无需安装其他模块。支持pnp4nagios
#!/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 <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()

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()


共有 人打赏支持
粉丝 35
博文 122
码字总数 59358
×
jastme
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: