How to determine NTP communication status

Daniel Griscom picture Daniel Griscom · Apr 22, 2014 · Viewed 17.2k times · Source

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.

Answer

Ciano picture Ciano · Apr 24, 2014

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