0

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?

Aazarus
  • 125
  • 1
  • 12
  • Try `nttpq -pcrv` that queries the status of the server and returns many different things you could check against. If you need more details please let me know – user3788685 Dec 16 '19 at 11:13

3 Answers3

0

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.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Great thanks. My question isn’t about the server specifically it’s about the ntp client. If ntp_gettime can’t let me know if ntpd is running and synced is there another way to do it? – Aazarus Dec 15 '19 at 15:14
  • You can tell if the ntp is running by checking if something is bound to its port, via `/proc/net/udp`. As far as the status goes, you'll need to dig into the `ntpdc`, or `ntpclient` command-line tool that does this, and see how it gets that info. The source is freely available, after all... – Sam Varshavchik Dec 15 '19 at 15:17
  • Thanks for trying Sam, appreciate the effort. Will take a look at your suggestion. – Aazarus Dec 15 '19 at 15:21
0

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
wuseman
  • 1,259
  • 12
  • 20
0

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.

Aazarus
  • 125
  • 1
  • 12