diff --git a/bundles/systemd/files/check_timesyncd_sync b/bundles/systemd/files/check_timesyncd_sync new file mode 100644 index 0000000..bde95a2 --- /dev/null +++ b/bundles/systemd/files/check_timesyncd_sync @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +from subprocess import check_output +from sys import exit + +CONVERSIONS = [ + ('ns', 1/1000000), + ('us', 1/1000), + ('ms', 1), + ('s', 1000), +] + +try: + in_sync = check_output( + "timedatectl status | grep -iF 'system clock synchronized' | cut -d: -f2", + shell=True, + ).strip().decode() + + if in_sync.lower() != 'yes': + print(f'systemd-timesyncd reports sync status: {in_sync}') + exit(2) + + out = check_output( + "timedatectl timesync-status | grep -iF offset | cut -d: -f2", + shell=True, + ).strip().decode() + + if not out: + print('NTP service is active, but could not get offset. This is probably fine.') + exit(0) + + if out.startswith('+'): + out = out[1:] + + offset = None + for unit, factor in CONVERSIONS: + if out.endswith(unit): + offset = float(out[:(len(unit)*-1)]) + offset = offset * factor + break + else: + raise ValueError(out) + + print(f'Sync offset is {round(offset, 2)} ms') + + if -50 < offset < 50: + exit(0) + elif -100 < offset < 100: + exit(1) + else: + exit(2) +except Exception as e: + print(repr(e)) + exit(3) diff --git a/bundles/systemd/items.py b/bundles/systemd/items.py index bc822e8..c8ecbd9 100644 --- a/bundles/systemd/items.py +++ b/bundles/systemd/items.py @@ -72,3 +72,7 @@ svc_systemd['systemd-timesyncd'] = { }, } +# monitoring +files['/usr/local/share/icinga/plugins/check_timesyncd_sync'] = { + 'mode': '0755', +} diff --git a/bundles/systemd/metadata.py b/bundles/systemd/metadata.py index 1a418d9..f8a8ba4 100644 --- a/bundles/systemd/metadata.py +++ b/bundles/systemd/metadata.py @@ -15,6 +15,9 @@ defaults = { 'SYSTEMD-TIMESYNCD STATUS': { 'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_systemd_unit systemd-timesyncd', }, + 'SYSTEMD-TIMESYNCD SYNC': { + 'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_timesyncd_sync', + }, }, }, },