add check_systemd_timer

This commit is contained in:
Cyberes 2024-02-28 09:54:57 -07:00
parent f277b2de1e
commit 9cf6a45412
3 changed files with 58 additions and 1 deletions

View File

@ -1,6 +1,5 @@
#!/bin/bash
# Check OPNsense network traffic for a host.
usage() {
echo "Usage: $0 --api <api_url> --user <username> --password <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.

56
check_systemd_timer.py Normal file
View File

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

View File

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