2024-02-28 09:54:57 -07:00
|
|
|
#!/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()
|
2024-02-28 10:04:13 -07:00
|
|
|
parser.add_argument('-t', '--timer', required=True, help='The name of the timer to check.')
|
2024-02-28 09:54:57 -07:00
|
|
|
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)
|