check_memory - sync with upstream

Major changes :
- update config-file path
- update output style
- works with no-SWAP
This commit is contained in:
Alban Vidal 2021-02-20 09:58:58 +01:00
parent cb2f612def
commit bbc38d2c42
No known key found for this signature in database
GPG Key ID: 96BD4FF64E709FDE

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Copyright © 2019 Aurélien Grimal # Copyright © 2019-2021 Aurélien Grimal
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -16,19 +16,17 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
##### #####
# It is assumed that the /proc/meminfo file use kB as unit. If not, this program will not work. # It is assumed that the /proc/meminfo file use kB as unit. If not, this program will not work.
# Usage examples : # Usage examples :
# 1) check_linux_mem.py # 1) check_memory
# 2) check_linux_mem.py --mem-warn 0.55 --mem-crit 0.8 --swap-warn 0.5 --swap-crit 0.75 # 2) check_memory --mem-warn 55 --mem-crit 80 --swap-warn 50 --swap-crit 75
# 3) check_memory --config-file=/etc/zorval/conf-check_memory
##### #####
import sys import sys
result = {'rc': 0, 'text': [], 'perfdata': [], 'params': {}} result = {'rc': 0, 'text': [], 'perfdata': [], 'params': {}}
default_config_file = '/etc/env_check_nrpe' default_config_file = '/etc/zorval/conf-check_memory'
try: try:
@ -38,9 +36,9 @@ try:
parser.add_argument( parser.add_argument(
"--mem-warn", "--mem-warn",
help = "Threshold percent for memory warning (default 70)", help = "Threshold percent for memory warning (default 80)",
type = int, type = int,
default = 70 default = 80
) )
parser.add_argument( parser.add_argument(
"--mem-crit", "--mem-crit",
@ -142,46 +140,46 @@ try:
raise ValueError('What is this unit ?', res) raise ValueError('What is this unit ?', res)
except IOError: except IOError:
if args.config_file is not None: if args.config_file is not None:
print("ERROR: the file '" + config_file + "' does not exist !") print("[CRITICAL] The file '" + config_file + "' does not exist !")
sys.exit(2) sys.exit(2)
except ValueError as e: except ValueError as e:
print("ERROR: reading the file '" + config_file + "',", e) print("[CRITICAL] Reading the file '" + config_file + "',", e)
sys.exit(2) sys.exit(2)
error = False error = False
# Test mem_warn and mem_crit values # Test mem_warn and mem_crit values
if args.mem_warn < 0: if args.mem_warn < 0:
print("ERROR: --mem-warn can't be negative") print("[CRITICAL] --mem-warn can't be negative")
error = True error = True
elif args.mem_warn > 100: elif args.mem_warn > 100:
print("ERROR: --mem-warn value exceeds 100") print("[CRITICAL] --mem-warn value exceeds 100")
error = True error = True
if args.mem_crit < 0: if args.mem_crit < 0:
print("ERROR: --mem-crit can't be negative") print("[CRITICAL] --mem-crit can't be negative")
error = True error = True
elif args.mem_crit > 100: elif args.mem_crit > 100:
print("ERROR: --mem-crit value exceeds 100") print("[CRITICAL] --mem-crit value exceeds 100")
error = True error = True
if args.mem_crit < args.mem_warn: if args.mem_crit < args.mem_warn:
print("ERROR: --mem-crit value is less than --mem-warn value") print("[CRITICAL] --mem-crit value is less than --mem-warn value")
error = True error = True
# Test swap_warn and swap_crit values # Test swap_warn and swap_crit values
if args.swap_warn < 0: if args.swap_warn < 0:
print("ERROR: --swap-warn can't be negative") print("[CRITICAL] --swap-warn can't be negative")
error = True error = True
elif args.swap_warn > 100: elif args.swap_warn > 100:
print("ERROR: --swap-warn value exceeds 100") print("[CRITICAL] --swap-warn value exceeds 100")
error = True error = True
if args.swap_crit < 0: if args.swap_crit < 0:
print("ERROR: --swap-crit can't be negative") print("[CRITICAL] --swap-crit can't be negative")
error = True error = True
elif args.swap_crit > 100: elif args.swap_crit > 100:
print("ERROR: --swap-crit value exceeds 100") print("[CRITICAL] --swap-crit value exceeds 100")
error = True error = True
if args.swap_crit < args.swap_warn: if args.swap_crit < args.swap_warn:
print("ERROR: --swap-crit value is less than --swap-warn value") print("[CRITICAL] --swap-crit value is less than --swap-warn value")
error = True error = True
if error: if error:
@ -223,7 +221,7 @@ try:
# Detect if param is missing from file # Detect if param is missing from file
for param, param_dict in result['params'].items(): for param, param_dict in result['params'].items():
if param_dict['value'] == -1: if param_dict['value'] == -1:
print('ERROR: Missing parameter "' + param + '" in file /proc/meminfo') print('[CRITICAL] Missing parameter "' + param + '" in file /proc/meminfo')
error = True error = True
# Exit if missing param # Exit if missing param
@ -252,7 +250,11 @@ try:
# Analyze swap # Analyze swap
if not args.without_swap: if not args.without_swap:
if result['params']['SwapTotal']['value'] != 0: if result['params']['SwapTotal']['value'] == 0:
result['params']['SwapUsed'] = { 'value': 0 }
swap_used_percent = 'N/A'
result['text'].append("NO SWAP")
else:
result['params']['SwapUsed'] = { result['params']['SwapUsed'] = {
'value': result['params']['SwapTotal']['value'] - result['params']['SwapFree']['value'] 'value': result['params']['SwapTotal']['value'] - result['params']['SwapFree']['value']
} }
@ -284,8 +286,8 @@ try:
total_value = result['params']['MemTotal']['value'] total_value = result['params']['MemTotal']['value']
string = 'mem_used=' + str(value) + args.unit + ';' string = 'mem_used=' + str(value) + args.unit + ';'
if args.dont_use_available: if args.dont_use_available:
string += str(int(total_value * args.mem_warn)) + ';' + \ string += str(int(total_value * args.mem_warn / 100)) + ';' + \
str(int(total_value * args.mem_crit)) + ';' str(int(total_value * args.mem_crit / 100)) + ';'
else: else:
string += ';;' string += ';;'
string += '0;' + str(total_value) string += '0;' + str(total_value)
@ -295,8 +297,8 @@ try:
value = result['params']['MemNotAvailable']['value'] value = result['params']['MemNotAvailable']['value']
string = 'mem_not_avail=' + str(value) + args.unit + ';' string = 'mem_not_avail=' + str(value) + args.unit + ';'
if not args.dont_use_available: if not args.dont_use_available:
string += str(int(total_value * args.mem_warn)) + ';' + \ string += str(int(total_value * args.mem_warn / 100)) + ';' + \
str(int(total_value * args.mem_crit)) + ';' str(int(total_value * args.mem_crit / 100)) + ';'
else: else:
string += ';;' string += ';;'
string += '0;' + str(total_value) string += '0;' + str(total_value)
@ -328,16 +330,16 @@ try:
# #
if result['rc'] == 0: if result['rc'] == 0:
print("OK -", " ".join(result['text']), end='') print("[OK]", " ".join(result['text']), end='')
elif result['rc'] == 1: elif result['rc'] == 1:
print("WARNING:", " - ".join(result['text']), end='') print("[WARNING]", " - ".join(result['text']), end='')
else: else:
print("CRITICAL:", " - ".join(result['text']), end='') print("[CRITICAL]", " - ".join(result['text']), end='')
print(" |", " ".join(result['perfdata'])) print(" |", " ".join(result['perfdata']))
sys.exit(result['rc']) sys.exit(result['rc'])
except Exception: except Exception:
print("CRITICAL:", traceback.format_exc()) print("[CRITICAL]", traceback.format_exc())
print("\n".join(result['text'])) print("\n".join(result['text']))
sys.exit(2) sys.exit(2)