How to check NTP time synchronization

I need to create an application that checks the NTP time synchronization on each machine. In other words, I need to determine if each machine is synchronized with my local network with a shared time server (stratum). So I came up with two ways.

1) Ask the NTP client on each machine to generate a statistics file. If I take this approach, which of the statistics files should I check to determine if the time on the machine is within a certain time server tolerance? There are peerstats, clockstats and loopstats. Does any of these files contain information that I can use to determine if time is synchronized?

2) Call the ntpq command line argument and print its output. Perform an output analysis.

Right now I prefer option 1, but as I said, I'm not sure if any NTP statistics file contains information that I can use to determine if the time is synchronized.

Is there a better approach.

Is there an API that I can use to directly request an NTP client?

Thanks for your help.

+6
source share
5 answers

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

There is also a libntpq library in libntpq . This is in the early beta versions, but if you need something bad enough, it should be used.

Update 2016-10-07 . The libntpq project does not seem to work for a long time, but a library with the same name is included in the ntp source. It looks applicable, but most likely it will be linked statically. You must be able to port the GPL in order to use it that way.

+5
source

The best way to check the synchronization is to check it, and not look at the file created by the daemon whose work you are checking. You must run ntpdate -q or the equivalent and make sure that the reported offset is within any tolerance required.

+1
source

I have the same question. I used a combination of two commands: ntptime [and then parse the output to confirm that the word ERROR was not found] ntpq -p [and then confirm that it returns no error status, as well as at least one โ€œ+โ€ sign to indicate the status sync]

This works fine on my CentOS windows, but on my FreeBSD servers it will return โ€œOKโ€ status even if synchronization is disabled for several hours, if the local time is stable, ntpd is running, and servers with a temporary source code return a consistent time. I would welcome a better answer. The suggestion to use ntpdate might work if I can install something so that ntpdate uses ntp.conf to get a list of servers with the original time.

0
source

I think that both produce good results only with the difference if you really want to control the time for every few minutes, and then go with the second option ... ntpq has various options for monitoring the entire Ntp synchronization process ... ntpq> pe will provide you will see the status of all configured peers, ntpq>, as well as provide all additional information about each individual peer, and ntpq> rv will provide a complete synopsis about each partner ... There are other internal commands for debugging your ntp process via ntpq, itโ€™s worth reading the following If you need further clarification: http://doc.ntp.org/4.1.1/ntpq.htm

For the first option, this is a statistics report that you can leave on your ntp to continue to generate and update, as indicated in the ntp.conf file, i.e. Do you want it to be created per day or week, etc ...

Loopstats will show you the date, time, frequency and time offset, jitter, time constant in the timeline, etc. Peerstats will inform you of all your peers, where they are represented and configured. Clockstats report the status of the clock driver

In addition, rawstats and sysstats options are available. You can also check your system logs and redirect them to your own Ntp logs via the ntp.conf file.

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

Hope this helps you a bit!

0
source

If you only need to determine if the computer 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. 
0
source

Source: https://habr.com/ru/post/912533/


All Articles