check_systemd_timer: print trigger time

This commit is contained in:
Cyberes 2024-02-28 11:55:46 -07:00
parent d1bcb71eea
commit 78c809bc72
1 changed files with 10 additions and 4 deletions

View File

@ -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)