1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
 | #!/usr/bin/python  
# -*- coding:utf-8 -*-  
from optparse import OptionParser import subprocess,sys from datetime import datetime """             nagios安装 
    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')              nagios配置         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):                      nagios实施         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__':                      nagios培训     jastme() 
 
想做Nagios, Zabbix,Cacti,iTop各种交流的,可以进入开源监控工具Nagios交流  QQ群号 :476809427  |