Merge branch 'collectd-5.1' into collectd-5.2
[collectd.git] / src / ntpd.c
index d1ce621..bbc455f 100644 (file)
@@ -322,6 +322,18 @@ static void ntpd_submit (char *type, char *type_inst, double value)
        plugin_dispatch_values (&vl);
 }
 
+/* Each time a peer is polled, ntpd shifts the reach register to the left and
+ * sets the LSB based on whether the peer was reachable. If the LSB is zero,
+ * the values are out of date. */
+static void ntpd_submit_reach (char *type, char *type_inst, uint8_t reach,
+               double value)
+{
+       if (!(reach & 1))
+               value = NAN;
+
+       ntpd_submit (type, type_inst, value);
+}
+
 static int ntpd_connect (void)
 {
        char *host;
@@ -785,7 +797,7 @@ static uint32_t ntpd_get_refclock_id (struct info_peer_summary const *peer_info)
 }
 
 static int ntpd_get_name_from_address (char *buffer, size_t buffer_size,
-               struct info_peer_summary *peer_info, _Bool do_reverse_lookup)
+               struct info_peer_summary const *peer_info, _Bool do_reverse_lookup)
 {
        struct sockaddr_storage sa;
        socklen_t sa_len;
@@ -846,7 +858,7 @@ static int ntpd_get_name_from_address (char *buffer, size_t buffer_size,
 } /* ntpd_get_name_from_address */
 
 static int ntpd_get_name_refclock (char *buffer, size_t buffer_size,
-               struct info_peer_summary *peer_info)
+               struct info_peer_summary const *peer_info)
 {
        uint32_t refclock_id = ntpd_get_refclock_id (peer_info);
        uint32_t unit_id = ntohl (peer_info->srcadr) & 0x00FF;
@@ -866,7 +878,7 @@ static int ntpd_get_name_refclock (char *buffer, size_t buffer_size,
 } /* int ntpd_get_name_refclock */
 
 static int ntpd_get_name (char *buffer, size_t buffer_size,
-               struct info_peer_summary *peer_info)
+               struct info_peer_summary const *peer_info)
 {
        uint32_t addr = ntohl (peer_info->srcadr);
 
@@ -971,6 +983,7 @@ static int ntpd_read (void)
                DEBUG ("peer %i:\n"
                                "  peername   = %s\n"
                                "  srcadr     = 0x%08x\n"
+                               "  reach      = 0%03o\n"
                                "  delay      = %f\n"
                                "  offset_int = %i\n"
                                "  offset_frc = %i\n"
@@ -979,6 +992,7 @@ static int ntpd_read (void)
                                i,
                                peername,
                                ntohl (ptr->srcadr),
+                               ptr->reach,
                                ntpd_read_fp (ptr->delay),
                                ntohl (ptr->offset_int),
                                ntohl (ptr->offset_frc),
@@ -986,10 +1000,13 @@ static int ntpd_read (void)
                                ntpd_read_fp (ptr->dispersion));
 
                if (refclock_id != 1) /* not the system clock (offset will always be zero.. */
-                       ntpd_submit ("time_offset", peername, offset);
-               ntpd_submit ("time_dispersion", peername, ntpd_read_fp (ptr->dispersion));
+                       ntpd_submit_reach ("time_offset", peername, ptr->reach,
+                                       offset);
+               ntpd_submit_reach ("time_dispersion", peername, ptr->reach,
+                               ntpd_read_fp (ptr->dispersion));
                if (refclock_id == 0) /* not a reference clock */
-                       ntpd_submit ("delay", peername, ntpd_read_fp (ptr->delay));
+                       ntpd_submit_reach ("delay", peername, ptr->reach,
+                                       ntpd_read_fp (ptr->delay));
        }
 
        free (ps);