Skip to content

Commit f403504

Browse files
committed
combine systemd-timesyncd and ntp-client
1 parent 0861af2 commit f403504

3 files changed

Lines changed: 45 additions & 46 deletions

File tree

snmp/ntp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
################################################################
10+
# Don't change anything unless you know what are you doing #
11+
################################################################
12+
BIN_GREP='/usr/bin/env grep'
13+
BIN_AWK='/usr/bin/env awk'
14+
15+
if dpkg-query -W -f='${status}' ntpsec | grep -q "ok installed"; then
16+
17+
BIN_NTPQ='/usr/bin/env ntpq'
18+
19+
CONFIG=$0".conf"
20+
if [ -f "$CONFIG" ]; then
21+
# shellcheck disable=SC1090
22+
. "$CONFIG"
23+
fi
24+
25+
NTP_OFFSET=$($BIN_NTPQ -c rv 127.0.0.1 | $BIN_GREP "offset" | $BIN_AWK -Foffset= '{print $2}' | $BIN_AWK -F, '{print $1}')
26+
NTP_FREQUENCY=$($BIN_NTPQ -c rv 127.0.0.1 | $BIN_GREP "frequency" | $BIN_AWK -Ffrequency= '{print $2}' | $BIN_AWK -F, '{print $1}')
27+
NTP_SYS_JITTER=$($BIN_NTPQ -c rv 127.0.0.1 | $BIN_GREP "sys_jitter" | $BIN_AWK -Fsys_jitter= '{print $2}' | $BIN_AWK -F, '{print $1}')
28+
NTP_CLK_JITTER=$($BIN_NTPQ -c rv 127.0.0.1 | $BIN_GREP "clk_jitter" | $BIN_AWK -Fclk_jitter= '{print $2}' | $BIN_AWK -F, '{print $1}')
29+
NTP_WANDER=$($BIN_NTPQ -c rv 127.0.0.1 | $BIN_GREP "clk_wander" | $BIN_AWK -Fclk_wander= '{print $2}' | $BIN_AWK -F, '{print $1}')
30+
31+
echo '{"data":{"offset":"'"$NTP_OFFSET"'","frequency":"'"$NTP_FREQUENCY"'","sys_jitter":"'"$NTP_SYS_JITTER"'","clk_jitter":"'"$NTP_CLK_JITTER"'","clk_wander":"'"$NTP_WANDER"'"},"version":"1","error":"0","errorString":""}'
32+
fi
33+
34+
if dpkg-query -W -f='${status}' systemd-timesyncd | grep -q "ok installed"; then
35+
BIN_TIMEDATECTL='/usr/bin/env timedatectl'
36+
37+
NTP_OFFSET=$($BIN_TIMEDATECTL timesync-status | $BIN_GREP "Offset" | $BIN_AWK -F: '{print $2 + 0}')
38+
NTP_FREQUENCY=$($BIN_TIMEDATECTL timesync-status | $BIN_GREP "Frequency" | $BIN_AWK -F: '{print $2 + 0}')
39+
NTP_JITTER=$($BIN_TIMEDATECTL timesync-status | $BIN_GREP "Jitter" | $BIN_AWK -F: '{print $2 + 0}')
40+
NTP_DELAY=$($BIN_TIMEDATECTL timesync-status | $BIN_GREP "Delay" | $BIN_AWK -F: '{print $2 + 0}')
41+
42+
echo '{"data":{"offset":"'"$NTP_OFFSET"'","frequency":"'"$NTP_FREQUENCY"'","jitter":"'"$NTP_JITTER"'","delay":"'"$NTP_DELAY"'"},"version":"1","error":"0","errorString":""}'
43+
fi
44+
45+
exit 0

snmp/ntp-client

Lines changed: 0 additions & 30 deletions
This file was deleted.

snmp/systemd-timesyncd

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)