From 404d33fc817fa43fc524f84bed5e84d092dc549f Mon Sep 17 00:00:00 2001 From: Cyberes Date: Wed, 28 Feb 2024 21:07:48 -0700 Subject: [PATCH] handle weird issue --- check_systemd_timer.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/check_systemd_timer.py b/check_systemd_timer.py index 2a602c8..149b947 100755 --- a/check_systemd_timer.py +++ b/check_systemd_timer.py @@ -16,6 +16,9 @@ import dbus def check_timer(timer_name): + if not timer_name.endswith('.timer'): + timer_name = timer_name + '.timer' + try: system_bus = dbus.SystemBus() systemd1 = system_bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1') @@ -27,10 +30,16 @@ def check_timer(timer_name): if active_state == 'active': next_elapse = timer_properties.Get('org.freedesktop.systemd1.Timer', 'NextElapseUSecRealtime') - local_timezone_offset_seconds = time.localtime().tm_gmtoff - local_timezone_offset = timedelta(seconds=local_timezone_offset_seconds) - local_datetime = datetime.utcfromtimestamp(int(next_elapse) / 1e6) + local_timezone_offset - next_elapse_str = local_datetime.strftime("%m-%d-%Y %H:%M") + try: + local_timezone_offset_seconds = time.localtime().tm_gmtoff + local_timezone_offset = timedelta(seconds=local_timezone_offset_seconds) + local_datetime = datetime.utcfromtimestamp(int(next_elapse) / 1e6) + local_timezone_offset + next_elapse_str = local_datetime.strftime("%m-%d-%Y %H:%M") + except Exception as e: + print(f'UNKNOWN - exception "{e}"') + print('next_elapse:', next_elapse) + traceback.print_exc() + sys.exit(nagios.STATE_UNKNOWN) current_time = time.time() * 1e6 # convert current time to microseconds remaining_time_sec = int((next_elapse - current_time) / 1e6) # convert remaining time to seconds @@ -52,11 +61,9 @@ def check_timer(timer_name): if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-t', '--timer', required=True, help='The name of the timer to check.') + parser.add_argument('-l', '--last-ran-delta', help='The associated service should have been triggered at least this many seconds ago.') args = parser.parse_args() - if not args.timer.endswith('.timer'): - args.timer = args.timer + '.timer' - try: check_timer(args.timer) except Exception as e: