bundles/systemd: add monitoring for systemd-timesyncd
This commit is contained in:
parent
e12b9e6c12
commit
eaab905735
3 changed files with 61 additions and 0 deletions
54
bundles/systemd/files/check_timesyncd_sync
Normal file
54
bundles/systemd/files/check_timesyncd_sync
Normal file
|
@ -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)
|
|
@ -72,3 +72,7 @@ svc_systemd['systemd-timesyncd'] = {
|
|||
},
|
||||
}
|
||||
|
||||
# monitoring
|
||||
files['/usr/local/share/icinga/plugins/check_timesyncd_sync'] = {
|
||||
'mode': '0755',
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue