Merge tag 'collectd-5.9.2'
[collectd.git] / src / chrony.c
index a4dd59f..079a335 100644 (file)
@@ -28,8 +28,8 @@
 
 #include "collectd.h"
 
-#include "common.h" /* auxiliary functions */
-#include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
+#include "plugin.h"              /* plugin_register_*, plugin_dispatch_values */
+#include "utils/common/common.h" /* auxiliary functions */
 
 #if HAVE_NETDB_H
 #include <netdb.h> /* struct addrinfo */
 #include <arpa/inet.h> /* ntohs/ntohl */
 #endif
 
+/* AIX doesn't have MSG_DONTWAIT */
+#ifndef MSG_DONTWAIT
+#define MSG_DONTWAIT MSG_NONBLOCK
+#endif
+
 #define CONFIG_KEY_HOST "Host"
 #define CONFIG_KEY_PORT "Port"
 #define CONFIG_KEY_TIMEOUT "Timeout"
@@ -106,7 +111,9 @@ typedef enum {
 #define ATTRIB_PACKED
 #endif
 
-typedef struct ATTRIB_PACKED { int32_t value; } tFloat;
+typedef struct ATTRIB_PACKED {
+  int32_t value;
+} tFloat;
 
 typedef struct ATTRIB_PACKED {
   uint32_t tv_sec_high;
@@ -143,7 +150,9 @@ typedef struct ATTRIB_PACKED {
                            Amplification) */
 } tChrony_Req_Tracking;
 
-typedef struct ATTRIB_PACKED { uint32_t f_n_sources; } tChrony_Req_N_Sources;
+typedef struct ATTRIB_PACKED {
+  uint32_t f_n_sources;
+} tChrony_Req_N_Sources;
 
 typedef struct ATTRIB_PACKED {
   int32_t f_index;
@@ -178,7 +187,9 @@ typedef struct ATTRIB_PACKED {
 } tChrony_Request;
 
 /* Chrony daemon response packets */
-typedef struct ATTRIB_PACKED { uint32_t f_n_sources; } tChrony_Resp_N_Sources;
+typedef struct ATTRIB_PACKED {
+  uint32_t f_n_sources;
+} tChrony_Resp_N_Sources;
 
 typedef struct ATTRIB_PACKED {
   union {
@@ -348,10 +359,10 @@ static char *niptoha(const tChrony_IPAddr *addr, char *p_buf,
 }
 
 static void nreftostr(uint32_t nrefid, char *p_buf, size_t p_buf_size) {
-  int i, j, c;
+  size_t j = 0;
 
-  for (i = j = 0; i < 4; i++) {
-    c = ntohl(nrefid) << i * 8 >> 24;
+  for (int i = 0; i < 4; i++) {
+    int c = ntohl(nrefid) << i * 8 >> 24;
     if (!isalnum(c) || j + 1 >= p_buf_size)
       continue;
     p_buf[j++] = c;
@@ -440,6 +451,15 @@ static int chrony_recv_response(tChrony_Response *p_resp,
   }
 }
 
+static void chrony_flush_recv_queue(void) {
+  char buf[1];
+
+  if (g_chrony_is_connected) {
+    while (recv(g_chrony_socket, buf, sizeof(buf), MSG_DONTWAIT) > 0)
+      ;
+  }
+}
+
 static int chrony_query(const int p_command, tChrony_Request *p_req,
                         tChrony_Response *p_resp, size_t *p_resp_size) {
   /* Check connection. We simply perform one try as collectd already handles
@@ -892,6 +912,9 @@ static int chrony_request_source_data(int p_src_idx, char *src_addr,
                          ntohs(chrony_resp.body.source_data.f_reachability));
   chrony_push_data_valid("clock_last_meas", src_addr, is_reachable,
                          ntohl(chrony_resp.body.source_data.f_since_sample));
+  chrony_push_data_valid(
+      "time_offset", src_addr, is_reachable,
+      ntohf(chrony_resp.body.source_data.f_origin_latest_meas));
 
   return CHRONY_RC_OK;
 }
@@ -903,12 +926,11 @@ static int chrony_request_source_stats(int p_src_idx, const char *src_addr,
   size_t chrony_resp_size;
   tChrony_Request chrony_req;
   tChrony_Response chrony_resp;
-  double skew_ppm, frequency_error, time_offset;
+  double skew_ppm, frequency_error;
 
   if (*p_is_reachable == 0) {
     skew_ppm = 0;
     frequency_error = 0;
-    time_offset = 0;
   } else {
     chrony_init_req(&chrony_req);
     chrony_req.body.source_stats.f_index = htonl(p_src_idx);
@@ -923,7 +945,6 @@ static int chrony_request_source_stats(int p_src_idx, const char *src_addr,
 
     skew_ppm = ntohf(chrony_resp.body.source_stats.f_skew_ppm);
     frequency_error = ntohf(chrony_resp.body.source_stats.f_rtc_gain_rate_ppm);
-    time_offset = ntohf(chrony_resp.body.source_stats.f_est_offset);
 
     DEBUG(PLUGIN_NAME
           ": Source[%d] stat: .addr = %s, .ref_id= %u, .n_samples = %u, "
@@ -935,7 +956,8 @@ static int chrony_request_source_stats(int p_src_idx, const char *src_addr,
           ntohl(chrony_resp.body.source_stats.f_n_runs),
           ntohl(chrony_resp.body.source_stats.f_span_seconds),
           ntohf(chrony_resp.body.source_stats.f_rtc_seconds_fast),
-          frequency_error, skew_ppm, time_offset,
+          frequency_error, skew_ppm,
+          ntohf(chrony_resp.body.source_stats.f_est_offset),
           ntohf(chrony_resp.body.source_stats.f_est_offset_err));
 
   } /* if (*is_reachable) */
@@ -944,8 +966,6 @@ static int chrony_request_source_stats(int p_src_idx, const char *src_addr,
   chrony_push_data_valid("clock_skew_ppm", src_addr, *p_is_reachable, skew_ppm);
   chrony_push_data_valid("frequency_error", src_addr, *p_is_reachable,
                          frequency_error); /* unit: ppm */
-  chrony_push_data_valid("time_offset", src_addr, *p_is_reachable,
-                         time_offset); /* unit: s */
 
   return CHRONY_RC_OK;
 }
@@ -964,6 +984,9 @@ static int chrony_read(void) {
     g_chrony_seq_is_initialized = 1;
   }
 
+  /* Ignore late responses that may have been received */
+  chrony_flush_recv_queue();
+
   /* Get daemon stats */
   rc = chrony_request_daemon_stats();
   if (rc != CHRONY_RC_OK)