Question

I'm working on an embedded OpenSUSE 12.3 system that lets the user configure the (single) NTP server address. I need to be able to give the user feedback on how things are going in terms of communicating with the specified server. Is the server address valid? Has NTP been unable to contact the server? Is it querying the server? Or, are we fully synchronized? (Note: once NTP thinks it's synchronized then I don't care just how synchronized it is.)

Right now I've written a bash script to parse the output of ntpq -np:

  • If the output contains "ntpq: read: Connection refused" then ntpd isn't running
  • If the output contains "No association ID's returned" then the server address is invalid
  • If the output contains ".INIT." then ntpd hasn't yet connected to the server
  • If the output does NOT contain a line starting with "*" then we're querying the server
  • Otherwise we're synchronized.

Clearly, this is a pain in the rear and highly platform-dependent, and I'd love to have a better solution.

I understand that there's a libntpq.a inside of a built ntp source tree that can be used by external applications, but until there's an official release then that's just switching to a new flavor of platform-dependence.

Is there an official way to determine the current communication status of an NTP client? Note again that only a single server will be configured.

Was it helpful?

Solution

How about ntpstat, would that work for you? Here are the exit/return values from the command when it's run:

  • exit status 0 - Clock is synchronised.
  • exit status 1 - Clock is not synchronised.
  • exit status 2 - If clock state is indeterminant, for example if ntpd is not contactable

Check out the page Verify that my NTP is working

OTHER TIPS

Another option is to use the ntp-wait command. ntp-wait is normally used in startup scripts that need the system time to be synced before carrying on with other tasks. If you pass the verbose option to ntp-wait it will print out the status and or error if syncing fails. You can also tell ntp-wait how long to wait for the sync before returning an error.

Unlike ntpstat ntp-wait is provided by the ntp reference implementation so relying on it is more portable than expecting ntpstat to be installed/available.

I found that the best thing to do is to restart the service. On Debian:

systemctl restart ntp
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top