3 from optparse import OptionParser
6 parser = OptionParser(version='0.1')
7 parser.add_option("-w", "--warning", dest="warn", default=False, help="warning level")
8 parser.add_option("-c", "--critical", dest="crit", default=False, help="critical level")
9 parser.add_option("-m", "--mode", dest="mode", default='mb', help="Check mode (mb or pct)")
18 (options, args) = parser.parse_args()
20 if options.mode == 'mb':
21 options.warn = options.warn or 500
22 options.crit = options.crit or 100
23 elif options.mode == 'pct':
24 options.warn = options.warn or 10
25 options.crit = options.crit or 5
27 print "What mode is %s?" % options.mode
30 options.warn = int(options.warn)
31 options.crit = int(options.crit)
33 with open('/proc/meminfo', 'r') as fd:
37 interesting_keys = ['MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree']
41 temp[0] = temp[0][:-1]
42 if temp[0] in interesting_keys:
43 memset[temp[0]] = int(temp[1])
45 total_vm = memset['MemTotal'] + memset['SwapTotal']
46 avail_vm = memset['MemFree'] + memset['Buffers'] +\
47 memset['Cached'] + memset['SwapFree']
49 free_pct = int((avail_vm * 100)/total_vm)
50 used_mem = int(total_vm - avail_vm)
52 if options.mode == 'pct':
53 if free_pct < options.crit:
54 print "CRITICAL: Free VM: %d%%" % free_pct
55 sys.exit(exit_codes['CRITICAL'])
56 elif free_pct < options.warn:
57 print "WARNING: Free VM: %d%%" % free_pct
58 sys.exit(exit_codes['WARNING'])
60 print "OK: Free VM: %d%%" % free_pct
61 sys.exit(exit_codes['OK'])
63 if avail_vm < options.crit:
64 print "CRITICAL: Free VM: %d" % avail_vm
65 sys.exit(exit_codes['CRITICAL'])
66 elif avail_vm < options.warn:
67 print "WARNING: Free VM: %d" % avail_vm
68 sys.exit(exit_codes['WARNING'])
70 print "OK: Free VM: %d" % avail_vm
71 sys.exit(exit_codes['OK'])