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 
 | #!/usr/bin/env python  
# -*- coding:utf-8 -*-  
"""  
 
    Nagios plugin to report Memory usage by parsing /proc/meminfo             nagios安装 
    by L.S. Keijser <keijser@stone- >  
    This script takes Cached memory into consideration by adding that  
    to the total MemFree value.  
       
    modified by jastme ,can used by pnp4nagios  
 
""" 
from optparse import OptionParser import sys  
 
checkmemver = '0.1' 
# Parse commandline options: parser = OptionParser(usage="%prog -w <warning threshold> -c <critical threshold> [ -h ]",version="%prog " + checkmemver)  
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 readLines(filename):  
    f = open(filename, "r")  
    lines = f.readlines()  
    return lines  
def readMemValues():  
    global memTotal, memCached, memFree  
    for line in readLines('/proc/meminfo'):  
        if line.split()[0] == 'MemTotal:':  
            memTotal = line.split()[1]  
        if line.split()[0] == 'MemFree:':  
            memFree = line.split()[1]  
        if line.split()[0] == 'Cached:':  
            memCached = line.split()[1]  
def percMem():  
    readMemValues()  
    return (((int(memFree) + int(memCached)) * 100) / int(memTotal))              nagios配置 def realMem():  
    readMemValues()  
    return (int(memFree) + int(memCached)) / 1024 
def go():  
    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 int(options.crit_threshold) >= int(options.warn_threshold):  
        print "UNKNOWN: Critical percentage can't be equal to or bigger than warning percentage."            监控软件         sys.exit(3)  
    trueFree = percMem()  
    trueMemFree = realMem()  
    if int(trueFree) <= int(options.crit_threshold):  
        print "CRITICAL: Free memory percentage is less than or equal to %s%% %s %sMB | mem = %sMB;6546;3273;0;32732"  %(str(options.crit_threshold),str(trueFree),str(trueMemFree),str(trueMemFree))  
        sys.exit(2)  
    if int(trueFree) <= int(options.warn_threshold):  
        print "WARNING: Free memory percentage is less than or equal to %s%% %s %sMB | mem = %sMB;6546;3273;0;32732"  %(str(options.warn_threshold),str(trueFree),str(trueMemFree),str(trueMemFree))          nagios实施         sys.exit(1)  
    else:  
        print "OK: Free memory percentage is %s%% %sMB | mem = %sMB;6546;3273;0;32732"  %            (str(trueFree),str(trueMemFree),str(trueMemFree))  
        sys.exit(0)  
if __name__ == '__main__':                nagios培训     go() 
想做Nagios, Zabbix,Cacti,iTop各种交流的,可以进入开源监控工具Nagios交流  QQ群号 :476809427  |