]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Add TimePrecision configuration option.
authorcarlospeon <carlospeon@gmail.com>
Fri, 26 Mar 2021 11:40:22 +0000 (12:40 +0100)
committerCarlos Peón Costa <carlospec@inditex.com>
Tue, 24 Aug 2021 15:11:53 +0000 (17:11 +0200)
src/collectd.conf.in
src/collectd.conf.pod
src/write_influxdb_udp.c

index 562a55d9d4e5b6f97f280e3ac110c7eb7bc4310b..579a4f0a1921cce31f891afcf34cb314f3b2f3e4 100644 (file)
 
 #<Plugin write_influxdb_udp>
 #  Server "localhost"
+#  TimePrecision "ms"
 #  StoreRates true
 #  MaxPacketSize 32768
 #  TimeToLive 128
index 3a4554b96fdaf0613a4c49b8aa2f55591320b6f7..82c9532d499f0ad4fd10c1682673a1322375a440 100644 (file)
@@ -11209,6 +11209,7 @@ miliseconds while plugin instance, type and type instance are sent as tags.
 
  <Plugin "write_influxdb_udp">
    Server "influxdb.internal.tld"
+   TimePrecision "ms"
    StoreRates "yes"
  </Plugin>
 
@@ -11222,6 +11223,14 @@ The argument I<Host> may be a hostname, an IPv4 address or an IPv6 address. The
 optional second argument specifies a port number or a service name. If not
 given, the default, B<8089>, is used.
 
+=item B<TimePrecision> I<ms>|I<us>|I<ns>
+
+The B<TimePrecision> option sets the precision of the timestamps sent to 
+InfluxDB. It must match the precision set in InfluxDB line protocol 
+configuration.
+
+The defaut value is I<ms>. Note that InfluxDB default may differ.
+
 =item B<TimeToLive> I<1-255>
 
 Set the time-to-live of sent packets. This applies to all, unicast and
index fe786fd353d4889d6dc6b3c92dc460fd6bff9670..6f63ced14c98583be143eb6b54ac89c798d704ee 100644 (file)
@@ -65,6 +65,12 @@ typedef struct sockent {
 #define NET_DEFAULT_PACKET_SIZE 1452
 #define NET_DEFAULT_PORT "8089"
 
+typedef enum {
+  NS,
+  US,
+  MS,
+} wifxudp_time_precision_t;
+
 /*
  * Private variables
  */
@@ -72,6 +78,7 @@ typedef struct sockent {
 static int wifxudp_config_ttl;
 static size_t wifxudp_config_packet_size = NET_DEFAULT_PACKET_SIZE;
 static bool wifxudp_config_store_rates;
+static wifxudp_time_precision_t wifxudp_config_time_precision = MS;
 
 static sockent_t *sending_socket;
 
@@ -463,7 +470,20 @@ static int write_influxdb_point(char *buffer, int buffer_len,
   if (!have_values)
     return 0;
 
-  BUFFER_ADD(" %" PRIu64 "\n", CDTIME_T_TO_MS(vl->time));
+  uint64_t influxdb_time = 0;
+  switch (wifxudp_config_time_precision) {
+  case NS:
+    influxdb_time = CDTIME_T_TO_NS(vl->time);
+    break;
+  case US:
+    influxdb_time = CDTIME_T_TO_US(vl->time);
+    break;
+  case MS:
+    influxdb_time = CDTIME_T_TO_MS(vl->time);
+    break;
+  }
+
+  BUFFER_ADD(" %" PRIu64 "\n", influxdb_time);
 
 #undef BUFFER_ADD_ESCAPE
 #undef BUFFER_ADD
@@ -559,16 +579,41 @@ static int wifxudp_config_set_server(const oconfig_item_t *ci) {
   return 0;
 } /* int wifxudp_config_set_server */
 
+static int wifxudp_config_set_time_precision(const oconfig_item_t *ci) {
+  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
+    ERROR("write_influxdb_udp plugin: The `%s' config option needs "
+          "one string arguments.",
+          ci->key);
+    return -1;
+  }
+  if (strcasecmp("ns", ci->values[0].value.string) == 0)
+    wifxudp_config_time_precision = NS;
+  else if (strcasecmp("us", ci->values[0].value.string) == 0)
+    wifxudp_config_time_precision = US;
+  else if (strcasecmp("ms", ci->values[0].value.string) == 0)
+    wifxudp_config_time_precision = MS;
+  else {
+    WARNING("write_influxdb_udp plugin: "
+            "The `TimePrecision' option must be `ns', `us' or `ms`.");
+    return -1;
+  }
+
+  return 0;
+} /* int wifxudp_config_set_time_precision */
+
 static int write_influxdb_udp_config(oconfig_item_t *ci) {
+
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
 
     if (strcasecmp("Server", child->key) == 0)
       wifxudp_config_set_server(child);
-    else if (strcasecmp("TimeToLive", child->key) == 0) {
+    else if (strcasecmp("TimeToLive", child->key) == 0)
       wifxudp_config_set_ttl(child);
-    else if (strcasecmp("MaxPacketSize", child->key) == 0)
+    else if (strcasecmp("MaxPacketSize", child->key) == 0)
       wifxudp_config_set_buffer_size(child);
+    else if (strcasecmp("TimePrecision", child->key) == 0)
+      wifxudp_config_set_time_precision(child);
     else if (strcasecmp("StoreRates", child->key) == 0)
       cf_util_get_boolean(child, &wifxudp_config_store_rates);
     else {