Last active
March 6, 2024 12:24
-
-
Save gm3dmo/cdc85ffea1e2d4c8e23e526ce44d7227 to your computer and use it in GitHub Desktop.
display clock offset from ntp using chrony
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import shlex | |
import subprocess | |
def run_cmd(): | |
"""chronyc tracking | |
Reference ID : 50484330 (PHC0) | |
Stratum : 1 | |
Ref time (UTC) : Wed Mar 06 12:17:24 2024 | |
System time : 0.000000137 seconds fast of NTP time | |
Last offset : -0.000000099 seconds | |
RMS offset : 0.000004309 seconds | |
Frequency : 1.295 ppm fast | |
Residual freq : +0.000 ppm | |
Skew : 0.137 ppm | |
Root delay : 0.000000001 seconds | |
Root dispersion : 0.000023911 seconds | |
Update interval : 8.0 seconds | |
Leap status : Normal | |
-c makes the output more processable like: | |
chronyc -c tracking | |
50484330,PHC0,1,1709727533.139866748,0.000004315,-0.000003372,0.000002759,1.274,-0.005,0.161,0.000000001,0.000029493,8.0,Normal | |
0 1 2 3 4 | |
""" | |
c = "chronyc -c tracking" | |
cmd = shlex.split(c) | |
output, error = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() | |
return (output, error) | |
def main(): | |
""" | |
Use **chronyc tracking command** on linux show clock offset from it's ntp source. | |
""" | |
#maximum_divergence_tolerated = float(0.1) | |
#maximum_divergence_tolerated = float(0.000001) | |
maximum_divergence_tolerated = float(0.00001) | |
clock_state = run_cmd() | |
clock_data = clock_state[0].decode().split(',') | |
#clock_data = clock_state[0].split(',') | |
clock_offset_from_ntp = float(clock_data[4]) | |
if abs(clock_offset_from_ntp) >= maximum_divergence_tolerated: | |
print('oh no, clock is off by {co:06.12f} max_divergence_tolerated is {mt:06.12f}'.format(co=clock_offset_from_ntp, mt=maximum_divergence_tolerated)) | |
else: | |
print('good clock is {co:06.12f} which is within the tolerance of {mt:06.12f}'.format(co=clock_offset_from_ntp, cd=clock_data[4], mt=maximum_divergence_tolerated)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment