Skip to content

Commit a9f9e10

Browse files
committed
combine systemd-timesyncd, ntp-client, and ntp-server.py
1 parent ad01567 commit a9f9e10

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

snmp/ntp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/sh
2+
# Please make sure the paths below are correct.
3+
# Alternatively you can put them in $0.conf, meaning if you've named
4+
# this script ntp-client then it must go in ntp-client.conf .
5+
#
6+
# If you are unsure, which to set, run this script and make sure that
7+
# the JSON output variables match that in "ntpq -c rv".
8+
#
9+
# `--ntpsec` for ntpsec, `--systemd-timesyncd` for `systemd-timesyncd`
10+
#
11+
################################################################
12+
# Don't change anything unless you know what are you doing #
13+
################################################################
14+
BIN_GREP='/usr/bin/env grep'
15+
BIN_AWK='/usr/bin/env awk'
16+
17+
if [ $# -gt 0 ]; then
18+
SEQUENCE=$1
19+
else
20+
if dpkg-query -W -f='${status}' ntpsec | grep -q "ok installed"; then
21+
SEQUENCE="--ntpsec"
22+
elif dpkg-query -W -f='${status}' systemd-timesyncd | grep -q "ok installed"; then
23+
SEQUENCE="--systemd-timesyncd"
24+
fi
25+
fi
26+
27+
case $SEQUENCE in
28+
--ntpsec)
29+
BIN_NTPQ='/usr/bin/env ntpq'
30+
31+
CONFIG=$0".conf"
32+
if [ -f "$CONFIG" ]; then
33+
# shellcheck disable=SC1090
34+
. "$CONFIG"
35+
fi
36+
37+
NTP_RV=$($BIN_NTPQ -c rv 127.0.0.1)
38+
NTP_IOSTAT=$($BIN_NTPQ -c iostat 127.0.0.1 2> /dev/null)
39+
40+
NTP_STRATUM=$(echo $NTP_RV | $BIN_AWK -Fstratum= '{print $2}' | $BIN_AWK -F, '{print $1}')
41+
NTP_OFFSET=$(echo $NTP_RV | $BIN_AWK -Foffset= '{print $2}' | $BIN_AWK -F, '{print $1}')
42+
NTP_FREQUENCY=$(echo $NTP_RV | $BIN_AWK -Ffrequency= '{print $2}' | $BIN_AWK -F, '{print $1}')
43+
NTP_SYS_JITTER=$(echo $NTP_RV | $BIN_AWK -Fsys_jitter= '{print $2}' | $BIN_AWK -F, '{print $1}')
44+
NTP_CLK_JITTER=$(echo $NTP_RV | $BIN_AWK -Fclk_jitter= '{print $2}' | $BIN_AWK -F, '{print $1}')
45+
NTP_WANDER=$(echo $NTP_RV | $BIN_AWK -Fclk_wander= '{print $2}' | $BIN_AWK -F, '{print $1}')
46+
NTP_RESET=$(echo $NTP_IOSTAT | $BIN_GREP "reset" | sed 's/.*reset://' | grep -oE '[0-9:]+' | head -n 1 )
47+
NTP_RESET=$(date -u -d "1970-01-01 $NTP_RESET" +"%s")
48+
NTP_FREE_BUFFER=$(echo $NTP_IOSTAT | sed 's/.*free receive buffers://' | grep -oE '[0-9:]+' | head -n 1 )
49+
NTP_USED_BUFFER=$(echo $NTP_IOSTAT | sed 's/.*used receive buffers://' | grep -oE '[0-9:]+' | head -n 1 )
50+
NTP_BUFFERS=$(($NTP_USED_BUFFER + $NTP_FREE_BUFFER))
51+
NTP_DROP_PACK=$(echo $NTP_IOSTAT | sed 's/.*dropped packets://' | grep -oE '[0-9]+' | head -n 1)
52+
NTP_IGNOR_PACK=$(echo $NTP_IOSTAT | sed 's/.*ignored packets://' | grep -oE '[0-9]+' | head -n 1)
53+
NTP_RCV_PACK=$(echo $NTP_IOSTAT | sed 's/.*received packets://' | grep -oE '[0-9]+' | head -n 1)
54+
NTP_SENT_PACK=$(echo $NTP_IOSTAT | sed 's/.*packets sent://' | grep -oE '[0-9]+' | head -n 1)
55+
56+
echo '{"data":{"stratum":"'"$NTP_STRATUM"'","offset":"'"$NTP_OFFSET"'","frequency":"'"$NTP_FREQUENCY"'","sys_jitter":"'"$NTP_SYS_JITTER"'","clk_jitter":"'"$NTP_CLK_JITTER"'","clk_wander":"'"$NTP_WANDER"'","time_since_reset":"'"$NTP_RESET"'","receive_buffers":"'"$NTP_BUFFERS"'","free_receive_buffers":"'"$NTP_FREE_BUFFER"'","used_receive_buffers":"'"$NTP_USED_BUFFER"'","dropped_packets":"'"$NTP_DROP_PACK"'","ignored_packets":"'"$NTP_IGNOR_PACK"'","received_packets":"'"$NTP_RCV_PACK"'","packets_sent":"'"$NTP_SENT_PACK"'"},"version":"1","error":"0","errorString":""}'
57+
;;
58+
59+
--systemd-timesyncd)
60+
BIN_TIMEDATECTL='/usr/bin/env timedatectl'
61+
NTP_TIMESYNCD=$($BIN_TIMEDATECTL timesync-status)
62+
NTP_OFFSET=$(echo $NTP_TIMESYNCD | sed 's/.*Offset://' | grep -oE '[0-9.]+' | head -n 1)
63+
NTP_FREQUENCY=$(echo $NTP_TIMESYNCD | sed 's/.*Frequency://' | grep -oE '[0-9.]+' | head -n 1)
64+
NTP_JITTER=$(echo $NTP_TIMESYNCD | sed 's/.*Jitter://' | grep -oE '[0-9.]+' | head -n 1)
65+
NTP_DELAY=$(echo $NTP_TIMESYNCD | sed 's/.*Delay://' | grep -oE '[0-9.]+' | head -n 1)
66+
67+
echo '{"data":{"offset":"'"$NTP_OFFSET"'","frequency":"'"$NTP_FREQUENCY"'","jitter":"'"$NTP_JITTER"'","delay":"'"$NTP_DELAY"'"},"version":"1","error":"0","errorString":""}'
68+
;;
69+
esac
70+
71+
exit 0

0 commit comments

Comments
 (0)