From 9cf6a45412bd4533e395ef6d3fc7e50926d1044f Mon Sep 17 00:00:00 2001 From: Cyberes Date: Wed, 28 Feb 2024 09:54:57 -0700 Subject: [PATCH] add check_systemd_timer --- Other/auto-acknowledge-apt.sh | 2 +- check_systemd_timer.py | 56 +++++++++++++++++++++++++++++++++++ requirements-everybody.txt | 1 + 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 check_systemd_timer.py diff --git a/Other/auto-acknowledge-apt.sh b/Other/auto-acknowledge-apt.sh index e6b2cd5..5b6b3be 100755 --- a/Other/auto-acknowledge-apt.sh +++ b/Other/auto-acknowledge-apt.sh @@ -1,6 +1,5 @@ #!/bin/bash -# Check OPNsense network traffic for a host. usage() { echo "Usage: $0 --api --user --password [--fail]" exit 1 @@ -30,6 +29,7 @@ done if [ -z "$api" ] || [ -z "$user" ] || [ -z "$password" ]; then echo "Missing required details." usage + exit 1 fi # Acknowledge all services that meet this filter. diff --git a/check_systemd_timer.py b/check_systemd_timer.py new file mode 100644 index 0000000..3937e3b --- /dev/null +++ b/check_systemd_timer.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +import argparse +import datetime +import sys +import time +import traceback + +import humanize + +from checker import nagios +from checker.result import quit_check + +sys.path.insert(0, "/usr/lib/python3/dist-packages") +import dbus + + +def check_timer(timer_name): + try: + system_bus = dbus.SystemBus() + systemd1 = system_bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1') + manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager') + timer_unit_path = manager.GetUnit(timer_name) + timer_unit = system_bus.get_object('org.freedesktop.systemd1', timer_unit_path) + timer_properties = dbus.Interface(timer_unit, 'org.freedesktop.DBus.Properties') + active_state = timer_properties.Get('org.freedesktop.systemd1.Unit', 'ActiveState') + if active_state == 'active': + next_elapse = timer_properties.Get('org.freedesktop.systemd1.Timer', 'NextElapseUSecRealtime') + current_time = time.time() * 1e6 # convert current time to microseconds + remaining_time_sec = int((next_elapse - current_time) / 1e6) # convert remaining time to seconds + remaining_time_human = str(humanize.naturaltime(datetime.datetime.now() + datetime.timedelta(seconds=remaining_time_sec))).strip(' from now') + perfdata_dict = { + 'remaining_time': { + 'value': remaining_time_sec, + 'unit': 's', + 'min': 0 + } + } + quit_check(f'{timer_name} is active. Remaining time: {remaining_time_human}.', nagios.STATE_OK, perfdata_dict) + else: + quit_check(f'{timer_name} is not active.', nagios.STATE_CRIT) + except dbus.exceptions.DBusException as e: + quit_check(f'{timer_name} could not be found.', nagios.STATE_UNKNOWN) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('-t', '--timer', required=True, help='The name of the timer to check') + args = parser.parse_args() + + try: + check_timer(args.timer) + except Exception as e: + print(f'UNKNOWN - exception "{e}"') + traceback.print_exc() + sys.exit(nagios.STATE_UNKNOWN) diff --git a/requirements-everybody.txt b/requirements-everybody.txt index 8d3a043..edb84ef 100644 --- a/requirements-everybody.txt +++ b/requirements-everybody.txt @@ -10,3 +10,4 @@ cf_speedtest==0.1.7 zfslib==0.11.0 hurry.filesize==0.9 dateparser==1.2.0 +humanize==4.9.0 \ No newline at end of file