chrony: fix conversion of very small floating-poing values
[collectd.git] / src / chrony.c
index bf4da4a..6cf2914 100644 (file)
@@ -584,9 +584,10 @@ static double ntohf(tFloat p_float) {
   uint32_t uval;
 
   uval = ntohl(p_float.value);
-  exp = (uval >> FLOAT_COEF_BITS) - FLOAT_COEF_BITS;
+  exp = (uval >> FLOAT_COEF_BITS);
   if (exp >= 1 << (FLOAT_EXP_BITS - 1))
     exp -= 1 << FLOAT_EXP_BITS;
+  exp -= FLOAT_COEF_BITS;
 
   /* coef = (x << FLOAT_EXP_BITS) >> FLOAT_EXP_BITS; */
   coef = uval % (1U << FLOAT_COEF_BITS);
@@ -598,21 +599,13 @@ static double ntohf(tFloat p_float) {
 
 static void chrony_push_data(const char *p_type, const char *p_type_inst,
                              double p_value) {
-  value_t values[1];
   value_list_t vl = VALUE_LIST_INIT;
 
-  values[0].gauge =
-      p_value; /* TODO: Check type??? (counter, gauge, derive, absolute) */
-
-  vl.values = values;
+  vl.values = &(value_t){.gauge = p_value};
   vl.values_len = 1;
 
   /* XXX: Shall g_chrony_host/g_chrony_port be reflected in the plugin's output?
    */
-  /* hostname_g is set in daemon/collectd.c (from config, via gethostname or by
-   * resolving localhost) */
-  /* defined as: char hostname_g[DATA_MAX_NAME_LEN]; (never NULL) */
-  sstrncpy(vl.host, hostname_g, sizeof(vl.host));
   sstrncpy(vl.plugin, PLUGIN_NAME_SHORT, sizeof(vl.plugin));
   if (g_chrony_plugin_instance != NULL) {
     sstrncpy(vl.plugin_instance, g_chrony_plugin_instance,
@@ -786,10 +779,9 @@ static int chrony_request_daemon_stats(void) {
   chrony_push_data("time_ref", DAEMON_NAME, time_ref); /* unit: s */
   chrony_push_data(
       "time_offset_ntp", DAEMON_NAME,
-      ntohf(
-          chrony_resp.body.tracking.f_current_correction)); /* Offset between
-                                                               system time and
-                                                               NTP, unit: s */
+      ntohf(chrony_resp.body.tracking.f_current_correction)); /* Offset between
+                                                                 system time and
+                                                                 NTP, unit: s */
   chrony_push_data(
       "time_offset", DAEMON_NAME,
       ntohf(
@@ -807,10 +799,9 @@ static int chrony_request_daemon_stats(void) {
                    ntohf(chrony_resp.body.tracking.f_skew_ppm));
   chrony_push_data(
       "root_delay", DAEMON_NAME,
-      ntohf(
-          chrony_resp.body.tracking.f_root_delay)); /* Network latency between
-                                                       local daemon and the
-                                                       current source */
+      ntohf(chrony_resp.body.tracking.f_root_delay)); /* Network latency between
+                                                         local daemon and the
+                                                         current source */
   chrony_push_data("root_dispersion", DAEMON_NAME,
                    ntohf(chrony_resp.body.tracking.f_root_dispersion));
   chrony_push_data("clock_last_update", DAEMON_NAME,
@@ -891,7 +882,7 @@ static int chrony_request_source_data(int p_src_idx, int *p_is_reachable) {
   chrony_push_data_valid("clock_reachability", src_addr, is_reachable,
                          ntohs(chrony_resp.body.source_data.f_reachability));
   chrony_push_data_valid("clock_last_meas", src_addr, is_reachable,
-                         ntohs(chrony_resp.body.source_data.f_since_sample));
+                         ntohl(chrony_resp.body.source_data.f_since_sample));
 
   return CHRONY_RC_OK;
 }