How to check ntpd is still running and synced

2.4k Views Asked by At

I'm calling ntp_gettime() and it is performing as expected however if I kill ntpd I'm still getting the correct behaviour with my return value showing no issues. This suggests ntp_gettime() does not call through to ntpd, which is what I believe was happening.

I'm trying to check that ntpd is still running correctly and that it still has a valid connection. I now assume that ntpd updates the system based on the defined interval and the ntp_gettime() call is calling just the system.

My question is can ntp_gettime() be used to determine if ntpd is running and that the server connection is still valid and have I just made a mistake somewhere? If not, is there a way to do this?

3

There are 3 best solutions below

0
On BEST ANSWER

The way I ended up doing it is by using a pipe to make a ntpstat call and processing the output. That is I check for unsynchronised, synchronised, and the absence of synchronised (after checking for unsynchronised) and act accordingly based on the results above.

If there is a better way to get the same result, because I think this is messy, please let me know.

3
On

The answer to your question is "no". This is not the interface to the NTP server, and cannot be used to determine the status of the NTP server. This is something completely different. According to the glibc documentation:

The ntp_gettime and ntp_adjtime functions provide an interface to monitor and manipulate the system clock to maintain high accuracy time. For example, you can fine tune the speed of the clock or synchronize it with another time source.

A typical use of these functions is by a server implementing the Network Time Protocol to synchronize the clocks of multiple systems and high precision clocks.

In other words, this is what the NTP server itself uses to talk to the kernel, to take care of business.

0
On

Checking the daemon operation

ntpctl is a program to display information about the running ntpd daemon, the below command will give you info about how many servers the daemon is syncing with, the status of the system clock if it's synced/unsynced and the clock offset. For further information see the manpage of ntpctl https://man.openbsd.org/ntpctl. Openntpd package brings you the ntpctl program.

ntpctl -s all