-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_mem
80 lines (68 loc) · 2.24 KB
/
check_mem
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
75
76
77
78
79
80
#!/usr/bin/python
#### Author : Taha Ali
#### Created: 09/01/2015
#### Contact: [email protected]
###
##
# Summary: this is script or program calculates how much total memory is available.
# it also takes in account for buffers and Caches. Defaults are set at 85% for warning and 90% for critical
##
###
import sys
import optparse
__author__ = 'bindas'
# nagios exit status
OK = 0
WARN = 1
CRIT = 2
UNKNOWN = 3
# Parameters optioning
parser = optparse.OptionParser(
usage="%prog -c <critical_value> -w <warning_value> \nAuthor:\t\tTaha Ali \nContact:\[email protected]",
prog='check_mem.py',
version='1.0',
)
parser.add_option('-c', '--critical',
dest="critical",
default="90",
type="float",
help="critical value in percentage, Default set at 90%",
)
parser.add_option('-w', '--warning',
dest="warning",
default="85",
type="float",
help="warning value in percentage, Default set at 85%",
)
(options, args) = parser.parse_args()
# get data from parameters optioning and feed it into warning and critical variables
critical = options.critical
warning = options.warning
with open('/proc/meminfo', 'r') as memfile:
entry = {}
for line in memfile:
line = line.strip()
line = line.replace('kB', '')
if not line: break
name, value = map(str.strip, line.split(':'))
entry[name] = value
# get and calculate free memory value
free = int(entry['MemFree']) + int(entry['Buffers']) + int(entry['Cached'])
# get total memory value
total = int(entry['MemTotal'])
def mem_load():
# change to percentage
use_percent = 100 - ((free * 100) / total)
if use_percent >= critical:
print "CRITICAL: memory (ram) over", use_percent, '%', "used"
sys.exit(CRIT)
elif warning <= use_percent < critical:
print "WARNING: memory (ram) over", use_percent, '%', "used"
sys.exit(WARN)
elif use_percent < warning:
print "OK: memory is only", use_percent, '%', "used"
sys.exit(OK)
else:
print ("UNKNOWN: unable to process free memory\n", use_percent)
sys.exit(UNKNOWN)
mem_load()