dsa-check-dabackup-server: replace /org/backup.debian.org with /srv/backups/da-backup
[mirror/dsa-nagios.git] / dsa-nagios-checks / dsa-check-memory
1 #!/usr/bin/python
2
3 from optparse import OptionParser
4 import sys
5
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)")
10
11 exit_codes = {
12     'OK':       0,
13     'WARNING':  1,
14     'CRITICAL': 2,
15     'UNKNOWN':  3
16 }
17
18 (options, args) = parser.parse_args()
19
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
26 else:
27     print "What mode is %s?" % options.mode
28     sys.exit(1)
29
30 options.warn = int(options.warn)
31 options.crit = int(options.crit)
32
33 with open('/proc/meminfo', 'r') as fd:
34     data = fd.readlines()
35
36 memset = {}
37 interesting_keys = ['MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree']
38
39 for line in data:
40     temp = line.split()
41     temp[0] = temp[0][:-1]
42     if temp[0] in interesting_keys:
43         memset[temp[0]] = int(temp[1])
44
45 total_vm = memset['MemTotal'] + memset['SwapTotal']
46 avail_vm = memset['MemFree'] + memset['Buffers'] +\
47            memset['Cached']  + memset['SwapFree']
48
49 free_pct = int((avail_vm * 100)/total_vm)
50 used_mem = int(total_vm - avail_vm)
51
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'])
59     else:
60         print "OK: Free VM: %d%%" % free_pct
61         sys.exit(exit_codes['OK'])
62 else:
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'])
69     else:
70         print "OK: Free VM: %d" % avail_vm
71         sys.exit(exit_codes['OK'])