How to check ntp status?

You can add an hotplug script to react when there is an ntpd event.

For my own system I have set a logging line for the initial synchronising and the server stratum events, but not for every periodic update.

Below is example from my build, where I use hotplug to log NTP events to system log:

root@router1:~# cat /etc/hotplug.d/ntp/20-ntpd-logger
[ $ACTION = "step" ]    && logger -t ntpd Time set, stratum=$stratum interval=$poll_interval offset=$offset
[ $ACTION = "stratum" ] && logger -t ntpd Stratum change, stratum=$stratum interval=$poll_interval offset=$offset

The output in system log looks like:

root@router1:~# logread | grep ntp
Tue Mar 24 08:24:51 2020 user.notice ntpd: Time set, stratum=16 interval=32 offset=41280.463320
Tue Mar 24 08:24:52 2020 user.notice ntpd: Stratum change, stratum=3 interval=32 offset=-0.000191
Tue Mar 24 10:17:57 2020 user.notice ntpd: Stratum change, stratum=4 interval=512 offset=0.007282
Tue Mar 24 10:26:39 2020 user.notice ntpd: Stratum change, stratum=3 interval=512 offset=0.002756
Tue Mar 24 11:46:07 2020 user.notice ntpd: Stratum change, stratum=4 interval=1024 offset=0.008404
Tue Mar 24 12:01:00 2020 user.notice ntpd: Stratum change, stratum=3 interval=1024 offset=0.002724




thanks a lot, but i need to check it with C language

To my knowledge the busybox ntpd (that we use) does not offer any query interface.

You might need to create that query interface with C for busybox, author the patch for OpenWrt, and hopefully also upstream the patch to busybox...

Alternatively, use chrony ntpd, which offers a query interface if I remember right.

This alternative might be some crude "fetch time with NTP or http protocol from network and compare that to local time" type of crude check, if your need is merely to find out if the time is roughly ok. Use a C NTP library for that?

1 Like

Fourth alternative:
use the NTP hotplug to write an indicator file to /tmp at the initial NTP time setting ("step" action).
Then you can check the existence of that file from your C program.

Really like this! Exactly what I'm after, as I need to delay WireGuard (uplink) until time sync is done (or the WireGuard handshake fails).

So I created /etc/hotplug.d/ntp/20-ntpd-logger like you note, but not seeing anything in the log. Anything "special" that needs to be done ... file permissions, or registering the file somehow? I do know that time sync has been achieved (based on the date / time :laughing:).


Or do a manual sync, but watch out UDP is blocked. If you want to truly automate this, you need to include htpdate as a last resort

if ntpd -nqN -p -p >/dev/null 2>&1 || ntpd -nqN -p -p >/dev/null 2>&1; then
       #### restart wireguard
1 Like

Sorry, not quite following. When WireGuard is not connected, but it's set up for ... no traffic gets through, right?


Have you got a killswitch activity on the VPN? The above command will run a NTP sync and returns true/false

Right, but if WG is up, with no handshake => the traffic (NTP sync request) just goes in the bit "trash", no? At least I have been seeing this, similar to this post.


Sort of yes. NTP Daemon can be delay to sync on reboot

You can force an manual sync and if successful restart wireguard

1 Like

Got it, thanks!

Sorry, old thread but it came up first on search.

This has bothered me for some time. Where the heck are the $ stratum, $poll_interval, and $offset variables defined?

I used the default busybox ntpd when I first came here, and never could get this hotplug to spit out the supposed values.

Having a local TS, I just set up a cron using

ntpdate -q

server, stratum 2, offset -0.013229, delay 0.02811
 2 Aug 23:21:46 ntpdate[19776]: adjust time server offset -0.013229 sec

As a result of a recent thread, I revisited the NTP wiki series and along with that I reviewed the relevant source openwrt/packages. I cannot find where these variables are either referenced or populated.

I can modify the script to populate values of my choice using

        stratum=$(/usr/sbin/ntpq -c 'rv 0 stratum')
        refid=$(/usr/sbin/ntpq -c 'rv 0 refid')
        clock=$(/usr/sbin/ntpq -c 'rv 0 clock')
        drift=$(/usr/sbin/ntpq -c 'rv 0 frequency')

so the script triggers properly - but why/where are the wiki hotplug script variables not presenting in my syslog?

Busybox source code...

1 Like

Thanks for that. Inquiring minds . . .

So this hotplug only works with busybox ntpd as described. Which brings up the next question.

For time on non-RTC devices we have busybox ntpd, ntpclient, chrony, and ICS ntp suite (ntpd, ntpdate, ntpq/c… Does the wiki require revision??? Should it have a separate section for each?

Feel free to add to wiki.

Note that quite much of the documentation is targeted for "OpenWrt with the default daemon packages" users. Chrony, ICS ntp etc. extra packages users will hopefully get documentation from the package upstream.

ICS ntpd in OpenWRT has a hotplug script that, in theory, should replicate some of the busybox ntpd hotplug functionality -

In my experience, that script does not actually work, and I never figured out why. Rather than debug it, I reimplemented similar functionality for chrony by periodically polling chrony status using chronyc - I'm seeking formal approval from my employer to upstream stuff I've developed "on the clock", but that's taking a lot longer than expected because there are much higher priority things going on.

Some things don't really map well, since it seems like busybox ntpd emits an "offset" event when it changes time by an offset without a stratum change (I'm guessing, I've never actually seen it), while chronyc can't emit events and just provides a "current offset" whenever polled. I was able to implement stratum change by only triggering a stratum hotplug when the stratum was not equal to a previously saved value.

This is a hotplug helper script. It gets called in the /etc/init.d/ntpd and doesn't reside in /etc/hotplug.d/ntp

It sure does! Exactly. In fact here is a snippet from my syslog:

Thu Aug  4 15:48:53 2022 user.notice ntpd: stratum=2 frequency=+18.496 refid= poll=11m clock=e696a125.b97903e7 Thu, Aug 4 2022 15:48:53.724
Thu Aug  4 15:59:53 2022 user.notice ntpd: stratum=2 frequency=+18.496 refid= poll=11m clock=e696a3b9.c5752ab2 Thu, Aug 4 2022 15:59:53.771
Thu Aug  4 16:10:53 2022 user.notice ntpd: stratum=2 frequency=+18.496 refid= poll=11m clock=e696a64d.cdf92dee Thu, Aug 4 2022 16:10:53.804
Thu Aug  4 16:21:53 2022 user.notice ntpd: stratum=3 frequency=+18.435 refid= poll=11m clock=e696a8e1.d6c47445 Thu, Aug 4 2022 16:21:53.838
Thu Aug  4 16:32:53 2022 user.notice ntpd: stratum=3 frequency=+18.424 refid= poll=11m clock=e696ab75.df711467 Thu, Aug 4 2022 16:32:53.872
Thu Aug  4 16:43:53 2022 user.notice ntpd: stratum=3 frequency=+18.424 refid= poll=11m clock=e696ae09.e8465d26 Thu, Aug 4 2022 16:43:53.907
Thu Aug  4 16:54:53 2022 user.notice ntpd: stratum=3 frequency=+18.400 refid= poll=11m clock=e696b09d.f0ee152a Thu, Aug 4 2022 16:54:53.941

Notice the time stamp progression - every 11 minutes or 660 seconds a poll runs. On a side note, you will find your drift file will update every hour.

This is my hack on the busybox ntpd hotplug script 20-ntpd-logger from the wiki to produce the above if you are running real ICS ntpd. This uses the ntpq runtime to query and populate the current variables.

	stratum=$(/usr/sbin/ntpq -c 'rv 0 stratum')
	refid=$(/usr/sbin/ntpq -c 'rv 0 refid')
	clock=$(/usr/sbin/ntpq -c 'rv 0 clock')
	drift=$(/usr/sbin/ntpq -c 'rv 0 frequency')

[ $ACTION = "stratum" ] && logger -t ntpd $stratum $drift $refid poll=$poll_interval $clock

Yes, I know that. Its role is to trigger scripts that reside in /etc/hotplug/ntp - I have been using such a script to restart OpenVPN on stratum change since OpenVPN goes out to lunch if time jumps while it's running.

The problem is - it doesn't do its job. Anything in /etc/hotplug/ntp NEVER runs when using that hotplug helper. As a result, when I tried switching from busybox ntpd to ISC ntpd, the OpenVPN restart script stopped running, leading to OpenVPN going out to lunch on time sync and staying out to lunch. I tried adding the "logger" example to determine why OpenVPN wasn't restarting - and the initial blip on stratum change and subsequent 11 minute polls never run with the included hotplug helper script. The scripts in /etc/hotplug/ntp were not the problem, since they worked with busybox ntpd and also with the hotplug helper I wrote for chrony

I found that chrony performed better in my use case anyway, so wrote a similar hotplug helper for chrony that parses the output of chronyc to set most of the variables. There's no way I can see to trigger ACTION="step", but so far, ACTION="stratum" does OK, even though in theory, OpenVPN should only need a restart on departure from stratum=16 and not any other stratum change - but in most use cases, the only stratum change is going to be 16->somethingelse and then that stratum will be held

you put that where?


1 Like