Replace zu with PRIu64 and llu with new macro, PRIsz, which will make it easier to...
[collectd.git] / src / write_tsdb.c
index 6d8fe60..777f4cc 100644 (file)
@@ -45,8 +45,8 @@
 
 #include "common.h"
 #include "plugin.h"
-
 #include "utils_cache.h"
+#include "utils_random.h"
 
 #include <netdb.h>
 
 #define WT_SEND_BUF_SIZE 1428
 #endif
 
-/* Default configuration */
-
-/* WRITE_TSDB_DEFAULT_DNS_TTL is the time we keep the dns cached info
- * (seconds)
- */
-#define WRITE_TSDB_DEFAULT_DNS_TTL 600
-
-/* WRITE_TSDB_DEFAULT_DNS_RANDOM_TTL helps define the max random
- * time we keep the dns cached info :
- * min = 0
- * max = WRITE_TSDB_DEFAULT_DNS_RANDOM_TTL * get_plugin_interval()
- */
-#define WRITE_TSDB_DEFAULT_DNS_RANDOM_TTL 15
-
 /*
  * Private variables
  */
@@ -108,8 +94,8 @@ struct wt_callback {
   cdtime_t next_random_ttl;
 };
 
-static cdtime_t dnsttl = TIME_T_TO_CDTIME_T_STATIC(WRITE_TSDB_DEFAULT_DNS_TTL);
-static cdtime_t dnsrandomttl = 0;
+static cdtime_t resolve_interval = 0;
+static cdtime_t resolve_jitter = 0;
 
 /*
  * Functions
@@ -125,7 +111,7 @@ static int wt_send_buffer(struct wt_callback *cb) {
   ssize_t status = 0;
 
   status = swrite(cb->sock_fd, cb->send_buf, strlen(cb->send_buf));
-  if (status < 0) {
+  if (status != 0) {
     char errbuf[1024];
     ERROR("write_tsdb plugin: send failed with status %zi (%s)", status,
           sstrerror(errno, errbuf, sizeof(errbuf)));
@@ -144,7 +130,7 @@ static int wt_flush_nolock(cdtime_t timeout, struct wt_callback *cb) {
   int status;
 
   DEBUG("write_tsdb plugin: wt_flush_nolock: timeout = %.3f; "
-        "send_buf_fill = %zu;",
+        "send_buf_fill = %" PRIsz ";",
         (double)timeout, cb->send_buf_fill);
 
   /* timeout == 0  => flush unconditionally */
@@ -168,12 +154,10 @@ static int wt_flush_nolock(cdtime_t timeout, struct wt_callback *cb) {
 }
 
 static cdtime_t new_random_ttl() {
-  if (dnsrandomttl == 0)
+  if (resolve_jitter == 0)
     return 0;
 
-  time_t ttl = (time_t)(CDTIME_T_TO_DOUBLE(dnsrandomttl) * ((double)random()) /
-                        (((double)RAND_MAX) + 1.0));
-  return TIME_T_TO_CDTIME_T(ttl);
+  return (cdtime_t)cdrand_range(0, (long)resolve_jitter);
 }
 
 static int wt_callback_init(struct wt_callback *cb) {
@@ -194,7 +178,7 @@ static int wt_callback_init(struct wt_callback *cb) {
      * If there is no more attempts, we need to flush the cache.
      */
 
-    if ((cb->ai_last_update + dnsttl + cb->next_random_ttl) < now) {
+    if ((cb->ai_last_update + resolve_interval + cb->next_random_ttl) < now) {
       cb->next_random_ttl = new_random_ttl();
       if (cb->connect_dns_failed_attempts_remaining > 0) {
         /* Warning : this is run under send_lock mutex.
@@ -210,10 +194,10 @@ static int wt_callback_init(struct wt_callback *cb) {
   }
 
   if (cb->ai == NULL) {
-    if ((cb->ai_last_update + dnsttl + cb->next_random_ttl) >= now) {
+    if ((cb->ai_last_update + resolve_interval + cb->next_random_ttl) >= now) {
       DEBUG("write_tsdb plugin: too many getaddrinfo(%s, %s) failures", node,
             service);
-      return (-1);
+      return -1;
     }
     cb->ai_last_update = now;
     cb->next_random_ttl = new_random_ttl();
@@ -341,7 +325,7 @@ static int wt_format_values(char *ret, size_t ret_len, int ds_num,
 
 #define BUFFER_ADD(...)                                                        \
   do {                                                                         \
-    status = ssnprintf(ret + offset, ret_len - offset, __VA_ARGS__);           \
+    status = snprintf(ret + offset, ret_len - offset, __VA_ARGS__);            \
     if (status < 1) {                                                          \
       sfree(rates);                                                            \
       return -1;                                                               \
@@ -364,7 +348,7 @@ static int wt_format_values(char *ret, size_t ret_len, int ds_num,
     }
     BUFFER_ADD(GAUGE_FORMAT, rates[ds_num]);
   } else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
-    BUFFER_ADD("%llu", vl->values[ds_num].counter);
+    BUFFER_ADD("%" PRIu64, (uint64_t)vl->values[ds_num].counter);
   else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
     BUFFER_ADD("%" PRIi64, vl->values[ds_num].derive);
   else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
@@ -404,36 +388,36 @@ static int wt_format_name(char *ret, int ret_len, const value_list_t *vl,
   if (ds_name != NULL) {
     if (vl->plugin_instance[0] == '\0') {
       if (vl->type_instance[0] == '\0') {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin, vl->type,
-                  ds_name);
+        snprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin, vl->type,
+                 ds_name);
       } else {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin, vl->type,
-                  vl->type_instance, ds_name);
+        snprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin, vl->type,
+                 vl->type_instance, ds_name);
       }
     } else { /* vl->plugin_instance != "" */
       if (vl->type_instance[0] == '\0') {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
-                  vl->plugin_instance, vl->type, ds_name);
+        snprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
+                 vl->plugin_instance, vl->type, ds_name);
       } else {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s.%s.%s", prefix, vl->plugin,
-                  vl->plugin_instance, vl->type, vl->type_instance, ds_name);
+        snprintf(ret, ret_len, "%s%s.%s.%s.%s.%s", prefix, vl->plugin,
+                 vl->plugin_instance, vl->type, vl->type_instance, ds_name);
       }
     }
   } else { /* ds_name == NULL */
     if (vl->plugin_instance[0] == '\0') {
       if (vl->type_instance[0] == '\0') {
-        ssnprintf(ret, ret_len, "%s%s.%s", prefix, vl->plugin, vl->type);
+        snprintf(ret, ret_len, "%s%s.%s", prefix, vl->plugin, vl->type);
       } else {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
-                  vl->type_instance, vl->type);
+        snprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
+                 vl->type_instance, vl->type);
       }
     } else { /* vl->plugin_instance != "" */
       if (vl->type_instance[0] == '\0') {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
-                  vl->plugin_instance, vl->type);
+        snprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
+                 vl->plugin_instance, vl->type);
       } else {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
-                  vl->plugin_instance, vl->type, vl->type_instance);
+        snprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
+                 vl->plugin_instance, vl->type, vl->type_instance);
       }
     }
   }
@@ -472,8 +456,8 @@ static int wt_send_message(const char *key, const char *value, cdtime_t time,
   }
 
   status =
-      ssnprintf(message, sizeof(message), "put %s %.0f %s fqdn=%s %s %s\r\n",
-                key, CDTIME_T_TO_DOUBLE(time), value, host, tags, host_tags);
+      snprintf(message, sizeof(message), "put %s %.0f %s fqdn=%s %s %s\r\n",
+               key, CDTIME_T_TO_DOUBLE(time), value, host, tags, host_tags);
   sfree(temp);
   if (status < 0)
     return -1;
@@ -481,7 +465,7 @@ static int wt_send_message(const char *key, const char *value, cdtime_t time,
 
   if (message_len >= sizeof(message)) {
     ERROR("write_tsdb plugin: message buffer too small: "
-          "Need %zu bytes.",
+          "Need %" PRIsz " bytes.",
           message_len + 1);
     return -1;
   }
@@ -514,8 +498,8 @@ static int wt_send_message(const char *key, const char *value, cdtime_t time,
   cb->send_buf_fill += message_len;
   cb->send_buf_free -= message_len;
 
-  DEBUG("write_tsdb plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"", cb->node,
-        cb->service, cb->send_buf_fill, sizeof(cb->send_buf),
+  DEBUG("write_tsdb plugin: [%s]:%s buf %" PRIsz "/%" PRIsz " (%.1f %%) \"%s\"",
+        cb->node, cb->service, cb->send_buf_fill, sizeof(cb->send_buf),
         100.0 * ((double)cb->send_buf_fill) / ((double)sizeof(cb->send_buf)),
         message);
 
@@ -623,9 +607,9 @@ static int wt_config_tsd(oconfig_item_t *ci) {
     }
   }
 
-  ssnprintf(callback_name, sizeof(callback_name), "write_tsdb/%s/%s",
-            cb->node != NULL ? cb->node : WT_DEFAULT_NODE,
-            cb->service != NULL ? cb->service : WT_DEFAULT_SERVICE);
+  snprintf(callback_name, sizeof(callback_name), "write_tsdb/%s/%s",
+           cb->node != NULL ? cb->node : WT_DEFAULT_NODE,
+           cb->service != NULL ? cb->service : WT_DEFAULT_SERVICE);
 
   user_data_t user_data = {.data = cb, .free_func = wt_callback_free};
 
@@ -638,7 +622,8 @@ static int wt_config_tsd(oconfig_item_t *ci) {
 }
 
 static int wt_config(oconfig_item_t *ci) {
-  _Bool config_random_ttl = 0;
+  if ((resolve_interval == 0) && (resolve_jitter == 0))
+    resolve_interval = resolve_jitter = plugin_get_interval();
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
@@ -646,26 +631,19 @@ static int wt_config(oconfig_item_t *ci) {
     if (strcasecmp("Node", child->key) == 0)
       wt_config_tsd(child);
     else if (strcasecmp("ResolveInterval", child->key) == 0)
-      cf_util_get_cdtime(child, &dnsttl);
-    else if (strcasecmp("ResolveJitter", child->key) == 0) {
-      config_random_ttl = 1;
-      cf_util_get_cdtime(child, &dnsrandomttl);
-    } else {
+      cf_util_get_cdtime(child, &resolve_interval);
+    else if (strcasecmp("ResolveJitter", child->key) == 0)
+      cf_util_get_cdtime(child, &resolve_jitter);
+    else {
       ERROR("write_tsdb plugin: Invalid configuration "
             "option: %s.",
             child->key);
     }
   }
 
-  if (!config_random_ttl)
-    dnsrandomttl = CDTIME_T_TO_DOUBLE(WRITE_TSDB_DEFAULT_DNS_RANDOM_TTL *
-                                      plugin_get_interval());
-
   return 0;
 }
 
 void module_register(void) {
   plugin_register_complex_config("write_tsdb", wt_config);
 }
-
-/* vim: set sw=4 ts=4 sts=4 tw=78 et : */