how validate NTP time synchronization

Go To StackoverFlow.com

6

I need create an application that validates NTP time synchronization on each machine. In other words, I need to determine if each machine is my LAN is synched to a common time server (stratum). So, far I came up with two ways.

1) Have the NTP client on each machine generate the statistics file. If I take this approach, which one of the statistics file should I examine to determine if the time on the machine is within some tolerance of the time server? There is a peerstats, clockstats and loopstats. Do any of those files contain information which I can use to determine if the time is synced up?

2) Invoke the ntpq command line argument and pipe its output. Parse the output.

Right now I prefer option 1, but, as I indicated, I am not sure if any of the NTP statistics file contain information which I can use to determine if time is synchronized.

Is there a better approach.

Is there an API which I can use to query the NTP client directly?

Thanks for your help.

2012-04-05 20:23
by user1316216


5

You could run ntpq -c "rv 0 stratum,offset" command and parse the output. If returned stratum is <16 then offset will be the time difference between the ntp pool and you system clock in milliseconds.

There is a also libntpq library in development. It is in early betas, but if you need something badly enough it should be usable.

Update 2016-10-07: It looks like libntpq project is long time defunct but a library with identical name is included with ntp source. It looks useable but it most likely will need to be linked statically. One should be able to tolerate GPL in order to use it in this manner.

2013-03-07 01:04
by dtoux


1

The best way to verify synchronization is to actually verify it, not look at a file generated by the daemon whose work you're checking up on. You should run ntpdate -q or equivalent and verify that the reported offset is within whatever tolerance you require.

2012-04-05 20:32
by Kyle Jones
Note that on some systems (I'm using Debian with a 2.6 kernel on an embedded computer), you must specify the path to the binary, otherwise you get a shell script that takes start|stop|restart|reload|force-reload instead of the -q option - Roger Dueck 2015-09-22 15:13


0

I have the same question. I've been using a combination of two commands: ntptime [and then parse output to confirm no occurrences of the word "ERROR"] ntpq -p [and then confirm it returns a non-error status, as well as at least one "+" sign to indicate a synchronized state]

This works great on my CentOS boxes, but on my FreeBSD servers, it will return an "OK" status even when it is several hours out of sync, as long as the local time is stable, ntpd is running, and the time source servers are returning a consistent time. I'd welcome a better answer. The suggestion to use ntpdate might work, if I can rig something to make ntpdate use the ntp.conf to get a list of time source servers.

2012-05-01 19:33
by JBK


0

I think both will output good results the only difference if you really want to monitor the time for every few minutes then go with second option...ntpq has various options to monitor your whole Ntp sync process...ntpq>pe will give you the status of your all configured peers, ntpq>as will give all additional details about each individual peers and ntpq>rv will give the complete synopsis about each peer...There are other internal commands to debug your ntp process through ntpq. Its worth going through the following page if you need some more clarifications : http://doc.ntp.org/4.1.1/ntpq.htm

For you first option, its a stats report which you can leave on your ntp to keep on generating and updating as per mentioned in ntp.conf file i.e whether you want it to be generated per day or week etc...

Loopstats will show you the date, time, frequency & time offset, jitter,clock discipline time constant etc. Peerstats will inform you about all your peers where present and configured clockstats will inform about clock driver status

Apart from these there are rawstats and sysstats options are also available, Also you can check your syslogs and redirect them to your own Ntp logs through ntp.conf file

you can go through the monitoring section in the above mentioned link and can learn in more details about how to configure ntpstats

Hope This might help you a bit!!

2013-03-07 10:23
by pragati


0

If all you need is to determine if the machine is in sync or not, you can use ntpstat.

SYNOPSIS
   ntpstat

DESCRIPTION
   ntpstat will report the synchronisation state of the NTP daemon 
   running on the local machine.  If the local system is found to be 
   synchronised to a reference time source, ntpstat will report the 
   approximate time accuracy.

RETURNS
   ntpstat returns 0 if clock is synchronised.  
   ntpstat returns 1 if clock is not synchronised.  
   ntpstat returns 2 if clock state is indeterminant, 
   for example if ntpd is not contactable.
2015-01-25 12:17
by Joachim Nilsson
I was trying to use that on Ubuntu 16.04. But it always report 0, even after more than 10 minutes after the network cable was unplugged and eventhough the list return by ntpq -pn was empty. I haven't yet investigated if it is a bug or if I should have waited even longer.. - Huygens 2016-09-21 13:28
ntp is quite smart about it, it knows you clock drift and it also keep track of how you cock behave and how well it was disciplined at the time you pull the cable. And this is correct behavior - your clock does not go out of sync just if cable is disconnected. In fact, if your clock has rubidium oscillator, it can remain stratum 2 clock for more than a month without network. Quite neat :- - dtoux 2018-04-27 01:04
Ads