Rewrite to use timedatectl

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-08-11 21:24:13 +02:00
parent a9fbb0d125
commit e7e4ca66c7
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 66 additions and 50 deletions

View file

@ -1,50 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
import subprocess
import sys
# ╰─○ chronyc tracking
# Reference ID : 192.53.103.108 (ptbtime1.ptb.de)
# Stratum : 2
# Ref time (UTC) : Mon Oct 30 09:48:39 2017
# System time : 0.000000000 seconds fast of NTP time
# Last offset : +0.000054065 seconds
# RMS offset : 42.973133087 seconds
# Frequency : 421.567 ppm fast
# Residual freq : +0.013 ppm
# Skew : 0.218 ppm
# Root delay : 0.017120 seconds
# Root dispersion : 0.001280 seconds
# Update interval : 238.4 seconds
# Leap status : Normal
KEY_SYSTEM_TIME = 'System time'
KEY_PARSED_OFFSET = 'Parsed offset'
def get_stats():
stats = {}
for line in subprocess.check_output(['/usr/bin/chronyc', 'tracking']).decode('utf-8').split('\n'):
line_parts = line.split(' : ')
if len(line_parts) != 2:
continue
desc, cont = line_parts
stats[desc.strip()] = cont.strip()
offset_parts = stats[KEY_SYSTEM_TIME].split(' ')
offset = float(offset_parts[0]) * (1 if offset_parts[2] == 'fast' else -1)
stats[KEY_PARSED_OFFSET] = offset
return stats
def main():
stats = get_stats()
if math.fabs(stats[KEY_PARSED_OFFSET]) > 1.0:
print('{:.2f}s diff'.format(stats[KEY_PARSED_OFFSET]))
return 0
if __name__ == '__main__':
sys.exit(main())

66
bin/tmux-timediff Executable file
View file

@ -0,0 +1,66 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
import re
import subprocess
import sys
# # timedatectl timesync-status
# Server: 2001:638:610:be01::108 (ptbtime1.ptb.de)
# Poll interval: 17min 4s (min: 32s; max 34min 8s)
# Leap: normal
# Version: 4
# Stratum: 1
# Reference: PTB
# Precision: 1us (-20)
# Root distance: 167us (max: 5s)
# Offset: +1.086ms
# Delay: 11.524ms
# Jitter: 445us
# Packet count: 5
# Frequency: +99.341ppm
KEY_OFFSET = 'Offset'
KEY_PARSED_OFFSET = 'Parsed Offset'
mult = {
's': 1.0,
'ms': 1.0 / 1000.0,
'us': 1.0 / 1000000.0,
}
def get_stats():
stats = {}
for line in subprocess.check_output(['timedatectl', 'timesync-status']).decode('utf-8').split('\n'):
line_parts = line.split(': ', 2)
if len(line_parts) != 2:
continue
desc, cont = line_parts
stats[desc.strip()] = cont.strip()
groups = re.search(r'^([+-])([0-9.]+)([a-z]+)$',
stats[KEY_OFFSET]).groups()
offset = float(groups[1])
offset = offset * mult[groups[2]]
if groups[0] == '-':
offset = offset * -1
stats[KEY_PARSED_OFFSET] = offset
return stats
def main():
stats = get_stats()
if math.fabs(stats[KEY_PARSED_OFFSET]) > 1.0:
print('{:.2f}s diff'.format(stats[KEY_PARSED_OFFSET]))
return 0
if __name__ == '__main__':
sys.exit(main())