Add chrony timediff into tmux status bar

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-10-30 11:35:22 +01:00
parent 1c6a0b562f
commit 1f27f72990
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 51 additions and 1 deletions

View file

@ -37,7 +37,7 @@ set -g status-justify left
set -g status-left-length 40
set -g status-right-length 140
set -g status-left '#[fg=green]#H#[default] '
set -g status-right '#[fg=white,bg=red]#(tmux-reboot-required)#[default] #[fg=red,dim,bg=default]#(uptime | cut -f 4-5 -d " " | cut -f 1 -d ",") #[fg=white,bg=default]%a, %H:%M:%S#[default] #[fg=blue,bright]%Y-%m-%d (KW %V) '
set -g status-right '#[fg=red,bg=default]#(tmux-chrony-timediff)#[default] #[fg=white,bg=red]#(tmux-reboot-required)#[default] #[fg=red,dim,bg=default]#(uptime | cut -f 4-5 -d " " | cut -f 1 -d ",") #[fg=white,bg=default]%a, %H:%M:%S#[default] #[fg=blue,bright]%Y-%m-%d (KW %V) '
# C-b is not acceptable -- Vim uses it
set-option -g prefix C-a

50
bin/tmux-chrony-timediff Executable file
View file

@ -0,0 +1,50 @@
#!/usr/bin/env python
# -*- 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']).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 time diff'.format(stats[KEY_PARSED_OFFSET]))
return 0
if __name__ == '__main__':
sys.exit(main())