From 78c809bc721b335864d07e0b3edad16ae2f66fa6 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Wed, 28 Feb 2024 11:55:46 -0700 Subject: [PATCH] check_systemd_timer: print trigger time --- check_systemd_timer.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/check_systemd_timer.py b/check_systemd_timer.py index 5992ee8..ca1efd5 100755 --- a/check_systemd_timer.py +++ b/check_systemd_timer.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 import argparse -import datetime import sys import time import traceback +from datetime import datetime, timedelta import humanize @@ -26,9 +26,15 @@ def check_timer(timer_name): active_state = timer_properties.Get('org.freedesktop.systemd1.Unit', 'ActiveState') 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") + 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') + remaining_time_human = str(humanize.naturaltime(datetime.now() + timedelta(seconds=remaining_time_sec))).strip(' from now') perfdata_dict = { 'remaining_time': { 'value': remaining_time_sec, @@ -36,10 +42,10 @@ def check_timer(timer_name): 'min': 0 } } - quit_check(f'{timer_name} is active. Remaining time: {remaining_time_human}.', nagios.STATE_OK, perfdata_dict) + quit_check(f'{timer_name} is active. Trigger time: {next_elapse_str}. 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: + except dbus.exceptions.DBusException: quit_check(f'{timer_name} could not be found.', nagios.STATE_UNKNOWN)