port dsa-check-memory to python3
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-memory
1 #!/usr/bin/python3
2
3 # Copyright 2013 Stephen Gran
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
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.
23
24 from optparse import OptionParser
25 import sys
26
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)")
31
32 exit_codes = {
33     'OK':       0,
34     'WARNING':  1,
35     'CRITICAL': 2,
36     'UNKNOWN':  3
37 }
38
39 (options, args) = parser.parse_args()
40
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
47 else:
48     print("What mode is %s?" % options.mode)
49     sys.exit(1)
50
51 options.warn = int(options.warn)
52 options.crit = int(options.crit)
53
54 with open('/proc/meminfo', 'r') as fd:
55     data = fd.readlines()
56
57 memset = {}
58 interesting_keys = ['MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree']
59
60 for line in data:
61     temp = line.split()
62     temp[0] = temp[0][:-1]
63     if temp[0] in interesting_keys:
64         memset[temp[0]] = int(temp[1])
65
66 total_vm = memset['MemTotal'] + memset['SwapTotal']
67 avail_vm = memset['MemFree'] + memset['Buffers'] +\
68            memset['Cached']  + memset['SwapFree']
69
70 free_pct = int((avail_vm * 100)/total_vm)
71 used_mem = int(total_vm - avail_vm)
72
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'])
80     else:
81         print("OK: Free VM: %d%%" % free_pct)
82         sys.exit(exit_codes['OK'])
83 else:
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'])
90     else:
91         print("OK: Free VM: %d" % avail_vm)
92         sys.exit(exit_codes['OK'])