diff --git a/bundles/sshmon/files/check_cpu_stats b/bundles/sshmon/files/check_cpu_stats index 36e5ae3..c0985ef 100644 --- a/bundles/sshmon/files/check_cpu_stats +++ b/bundles/sshmon/files/check_cpu_stats @@ -4,27 +4,30 @@ from re import findall from subprocess import check_output from sys import exit +ITERATIONS = 10 + try: top_output = None - for line in check_output(['top', '-b', '-n1', '-d1']).decode('UTF-8').splitlines(): - if line.lower().strip().startswith('%cpu'): - top_output = line.lower().split(':', 2)[1] - break - - if not top_output: - print('%cpu not found in top output') - exit(3) + top_output = check_output(rf"top -b -n{ITERATIONS} -d1 | grep -i '^%cpu'", shell=True).decode('UTF-8') cpu_usage = {} for value, identifier in findall('([0-9\.\,]{3,5}) ([a-z]{2})', top_output): - cpu_usage[identifier] = float(value.replace(',', '.')) + if identifier not in cpu_usage: + cpu_usage[identifier] = 0.0 + cpu_usage[identifier] += float(value.replace(',', '.')) + + output = [] + for identifier, value_added in cpu_usage.items(): + value = value_added / ITERATIONS + output.append(f"{value:.2f} {identifier}") + cpu_usage[identifier] = value + + print(f"Average over {ITERATIONS} seconds: " + ", ".join(output)) warn = set() crit = set() - print(top_output) - # steal if cpu_usage['st'] > 10: crit.add('CPU steal is {}% (>10%)'.format(cpu_usage['st'])) diff --git a/bundles/sshmon/metadata.py b/bundles/sshmon/metadata.py index 8d5bb6b..2142623 100644 --- a/bundles/sshmon/metadata.py +++ b/bundles/sshmon/metadata.py @@ -19,6 +19,8 @@ defaults = { 'services': { 'CPU': { 'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_cpu_stats', + # takes samples over 10 seconds + 'vars.sshmon_timeout': 20 }, 'LOAD': { 'command_on_monitored_host': '/usr/lib/nagios/plugins/check_load -r -w 4,2,1 -c 8,4,2',