3 # Copyright 2013 Stephen Gran
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 from optparse import OptionParser
27 parser = OptionParser(version='0.1')
28 parser.add_option("-w", "--warning", dest="warn", default=False, help="warning level")
29 parser.add_option("-c", "--critical", dest="crit", default=False, help="critical level")
30 parser.add_option("-m", "--mode", dest="mode", default='mb', help="Check mode (mb or pct)")
39 (options, args) = parser.parse_args()
41 if options.mode == 'mb':
42 options.warn = options.warn or 500
43 options.crit = options.crit or 100
44 elif options.mode == 'pct':
45 options.warn = options.warn or 10
46 options.crit = options.crit or 5
48 print "What mode is %s?" % options.mode
51 options.warn = int(options.warn)
52 options.crit = int(options.crit)
54 with open('/proc/meminfo', 'r') as fd:
58 interesting_keys = ['MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree']
62 temp[0] = temp[0][:-1]
63 if temp[0] in interesting_keys:
64 memset[temp[0]] = int(temp[1])
66 total_vm = memset['MemTotal'] + memset['SwapTotal']
67 avail_vm = memset['MemFree'] + memset['Buffers'] +\
68 memset['Cached'] + memset['SwapFree']
70 free_pct = int((avail_vm * 100)/total_vm)
71 used_mem = int(total_vm - avail_vm)
73 if options.mode == 'pct':
74 if free_pct < options.crit:
75 print "CRITICAL: Free VM: %d%%" % free_pct
76 sys.exit(exit_codes['CRITICAL'])
77 elif free_pct < options.warn:
78 print "WARNING: Free VM: %d%%" % free_pct
79 sys.exit(exit_codes['WARNING'])
81 print "OK: Free VM: %d%%" % free_pct
82 sys.exit(exit_codes['OK'])
84 if avail_vm < options.crit:
85 print "CRITICAL: Free VM: %d" % avail_vm
86 sys.exit(exit_codes['CRITICAL'])
87 elif avail_vm < options.warn:
88 print "WARNING: Free VM: %d" % avail_vm
89 sys.exit(exit_codes['WARNING'])
91 print "OK: Free VM: %d" % avail_vm
92 sys.exit(exit_codes['OK'])