forked from taha-bindas/openstack_nagios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_memory
More file actions
executable file
·131 lines (110 loc) · 4.42 KB
/
check_memory
File metadata and controls
executable file
·131 lines (110 loc) · 4.42 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
#### Original Author : Taha Ali,
#### Modified by: Dan Barker
#### Created: 09/01/2015
#### Updated: 03/05/2024 (Python 3)
#### Contact: tahazohair@gmail.com, github.com/danbarker
###
##
# Summary: This script calculates how much total and swap memory is available.
# It also takes into account buffers and caches. Defaults are set at 85% for warning and 90% for critical
# for both RAM and swap memory.
##
###
import sys
import argparse
__author__ = 'bindas'
# Nagios exit status
OK = 0
WARN = 1
CRIT = 2
UNKNOWN = 3
parser = argparse.ArgumentParser(
usage="%(prog)s -c <critical_pc> -w <warning_pc> -sc <swap_critical_pc> -sw <swap_warning_pc>",
prog='check_memory',
description='This script calculates available system and swap memory accounting for buffers and caches.'
)
parser.add_argument('-c', '--critical',
dest="critical_pc",
default=90.0,
type=float,
help="Critical value in percentage for RAM usage, default set at 90")
parser.add_argument('-w', '--warning',
dest="warning_pc",
default=85.0,
type=float,
help="Warning value in percentage for RAM usage, default set at 85")
parser.add_argument('-sc', '--swap_critical',
dest="swap_critical_pc",
default=90.0,
type=float,
help="Critical value in percentage for swap usage, default set at 90")
parser.add_argument('-sw', '--swap_warning',
dest="swap_warning_pc",
default=85.0,
type=float,
help="Warning value in percentage for swap usage, default set at 85")
options = parser.parse_args()
critical = options.critical_pc
warning = options.warning_pc
swap_critical = options.swap_critical_pc
swap_warning = options.swap_warning_pc
# Read memory info
entry = {}
with open('/proc/meminfo', 'r') as memfile:
for line in memfile:
line = line.strip().replace('kB', '').split(':')
if line[0]:
entry[line[0].strip()] = int(line[1].strip())
# Calculate free memory
free = entry['MemFree'] + entry.get('Buffers', 0) + entry.get('Cached', 0)
# Total memory
total = entry['MemTotal']
# Calculate swap usage
swap_free = entry['SwapFree']
swap_total = entry['SwapTotal']
def mem_load():
# Memory usage calculation
mem_used = total - free
swap_used = swap_total - swap_free
# Convert from kB to GB for easier interpretation
mem_used_gb = mem_used / 1024 / 1024
total_gb = total / 1024 / 1024
swap_used_gb = swap_used / 1024 / 1024
swap_total_gb = swap_total / 1024 / 1024
# Percentage calculation
mem_use_percent = 100 - ((free * 100) / total)
# Swap percentage calculation. If swap_total is 0, set swap_use_percent to 0
if swap_total == 0:
swap_use_percent = 0
else:
swap_use_percent = 100 - ((swap_free * 100) / swap_total)
# Performance data with more detail
mem_perf_data = f"mem_used_pc={mem_use_percent}%;{warning};{critical};0;100 " \
f"mem_used_gb={mem_used_gb:.2f}GB; mem_total_gb={total_gb:.2f}GB"
swap_perf_data = f"swap_used_pc={swap_use_percent}%;{swap_warning};{swap_critical};0;100 " \
f"swap_used_gb={swap_used_gb:.2f}GB; swap_total_gb={swap_total_gb:.2f}GB"
# Determine the highest severity status
max_status = OK
messages = []
if mem_use_percent >= critical:
messages.append(f"CRITICAL: RAM over {mem_use_percent:.2f}% used")
max_status = CRIT
elif warning <= mem_use_percent < critical:
messages.append(f"WARNING: RAM over {mem_use_percent:.2f}% used")
if max_status < WARN:
max_status = WARN
if swap_use_percent >= swap_critical:
messages.append(f"CRITICAL: Swap over {swap_use_percent:.2f}% used")
max_status = CRIT
elif swap_warning <= swap_use_percent < swap_critical:
messages.append(f"WARNING: Swap over {swap_use_percent:.2f}% used")
if max_status < WARN:
max_status = WARN
if not messages: # All is OK
messages.append(f"OK: RAM is {mem_use_percent:.2f}%, Swap is {swap_use_percent:.2f}%")
# Combine all messages and exit with the highest severity found
final_message = ' | '.join(messages)
print(f"{final_message} | {mem_perf_data} {swap_perf_data}")
sys.exit(max_status)
mem_load()