Replace zu with PRIu64 and llu with new macro, PRIsz, which will make it easier to...
[collectd.git] / src / write_riemann.c
index 10f8958..16533b7 100644 (file)
 #include "collectd.h"
 
 #include "common.h"
-#include "configfile.h"
 #include "plugin.h"
 #include "utils_cache.h"
 #include "utils_complain.h"
 #include "write_riemann_threshold.h"
 
-#include <errno.h>
 #include <riemann/riemann-client.h>
 
 #define RIEMANN_HOST "localhost"
@@ -101,6 +99,7 @@ static int wrr_connect(struct riemann_host *host) /* {{{ */
                node, port);
     return -1;
   }
+#if RCC_VERSION_NUMBER >= 0x010800
   if (host->timeout.tv_sec != 0) {
     if (riemann_client_set_timeout(host->client, &host->timeout) != 0) {
       riemann_client_free(host->client);
@@ -111,6 +110,9 @@ static int wrr_connect(struct riemann_host *host) /* {{{ */
       return -1;
     }
   }
+#endif
+
+  set_sock_opts(riemann_client_get_fd(host->client));
 
   c_release(LOG_INFO, &host->init_complaint,
             "write_riemann plugin: Successfully connected to %s:%d", node,
@@ -123,12 +125,12 @@ static int wrr_connect(struct riemann_host *host) /* {{{ */
 static int wrr_disconnect(struct riemann_host *host) /* {{{ */
 {
   if (!host->client)
-    return (0);
+    return 0;
 
   riemann_client_free(host->client);
   host->client = NULL;
 
-  return (0);
+  return 0;
 } /* }}} int wrr_disconnect */
 
 /**
@@ -212,6 +214,11 @@ wrr_notification_to_message(struct riemann_host *host, /* {{{ */
       RIEMANN_EVENT_FIELD_SERVICE, &service_buffer[1],
       RIEMANN_EVENT_FIELD_NONE);
 
+#if RCC_VERSION_NUMBER >= 0x010A00
+  riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS,
+                    (int64_t)CDTIME_T_TO_US(n->time));
+#endif
+
   if (n->host[0] != 0)
     riemann_event_string_attribute_add(event, "host", n->host);
   if (n->plugin[0] != 0)
@@ -257,13 +264,13 @@ wrr_notification_to_message(struct riemann_host *host, /* {{{ */
   if (msg == NULL) {
     ERROR("write_riemann plugin: riemann_message_create_with_events() failed.");
     riemann_event_free(event);
-    return (NULL);
+    return NULL;
   }
 
   DEBUG("write_riemann plugin: Successfully created message for notification: "
         "host = \"%s\", service = \"%s\", state = \"%s\"",
         event->host, event->service, event->state);
-  return (msg);
+  return msg;
 } /* }}} riemann_message_t *wrr_notification_to_message */
 
 static riemann_event_t *
@@ -278,7 +285,7 @@ wrr_value_to_event(struct riemann_host const *host, /* {{{ */
   event = riemann_event_new();
   if (event == NULL) {
     ERROR("write_riemann plugin: riemann_event_new() failed.");
-    return (NULL);
+    return NULL;
   }
 
   format_name(name_buffer, sizeof(name_buffer),
@@ -286,18 +293,17 @@ wrr_value_to_event(struct riemann_host const *host, /* {{{ */
               vl->type_instance);
   if (host->always_append_ds || (ds->ds_num > 1)) {
     if (host->event_service_prefix == NULL)
-      ssnprintf(service_buffer, sizeof(service_buffer), "%s/%s",
-                &name_buffer[1], ds->ds[index].name);
+      snprintf(service_buffer, sizeof(service_buffer), "%s/%s", &name_buffer[1],
+               ds->ds[index].name);
     else
-      ssnprintf(service_buffer, sizeof(service_buffer), "%s%s/%s",
-                host->event_service_prefix, &name_buffer[1],
-                ds->ds[index].name);
+      snprintf(service_buffer, sizeof(service_buffer), "%s%s/%s",
+               host->event_service_prefix, &name_buffer[1], ds->ds[index].name);
   } else {
     if (host->event_service_prefix == NULL)
       sstrncpy(service_buffer, &name_buffer[1], sizeof(service_buffer));
     else
-      ssnprintf(service_buffer, sizeof(service_buffer), "%s%s",
-                host->event_service_prefix, &name_buffer[1]);
+      snprintf(service_buffer, sizeof(service_buffer), "%s%s",
+               host->event_service_prefix, &name_buffer[1]);
   }
 
   riemann_event_set(
@@ -308,6 +314,11 @@ wrr_value_to_event(struct riemann_host const *host, /* {{{ */
       vl->type, "ds_name", ds->ds[index].name, NULL,
       RIEMANN_EVENT_FIELD_SERVICE, service_buffer, RIEMANN_EVENT_FIELD_NONE);
 
+#if RCC_VERSION_NUMBER >= 0x010A00
+  riemann_event_set(event, RIEMANN_EVENT_FIELD_TIME_MICROS,
+                    (int64_t)CDTIME_T_TO_US(vl->time));
+#endif
+
   if (host->check_thresholds) {
     const char *state = NULL;
 
@@ -340,8 +351,8 @@ wrr_value_to_event(struct riemann_host const *host, /* {{{ */
   if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
     char ds_type[DATA_MAX_NAME_LEN];
 
-    ssnprintf(ds_type, sizeof(ds_type), "%s:rate",
-              DS_TYPE_TO_STRING(ds->ds[index].type));
+    snprintf(ds_type, sizeof(ds_type), "%s:rate",
+             DS_TYPE_TO_STRING(ds->ds[index].type));
     riemann_event_string_attribute_add(event, "ds_type", ds_type);
   } else {
     riemann_event_string_attribute_add(event, "ds_type",
@@ -351,7 +362,7 @@ wrr_value_to_event(struct riemann_host const *host, /* {{{ */
   {
     char ds_index[DATA_MAX_NAME_LEN];
 
-    ssnprintf(ds_index, sizeof(ds_index), "%zu", index);
+    snprintf(ds_index, sizeof(ds_index), "%" PRIsz, index);
     riemann_event_string_attribute_add(event, "ds_index", ds_index);
   }
 
@@ -386,7 +397,7 @@ wrr_value_to_event(struct riemann_host const *host, /* {{{ */
   DEBUG("write_riemann plugin: Successfully created message for metric: "
         "host = \"%s\", service = \"%s\"",
         event->host, event->service);
-  return (event);
+  return event;
 } /* }}} riemann_event_t *wrr_value_to_event */
 
 static riemann_message_t *
@@ -401,7 +412,7 @@ wrr_value_list_to_message(struct riemann_host const *host, /* {{{ */
   msg = riemann_message_new();
   if (msg == NULL) {
     ERROR("write_riemann plugin: riemann_message_new failed.");
-    return (NULL);
+    return NULL;
   }
 
   if (host->store_rates) {
@@ -409,7 +420,7 @@ wrr_value_list_to_message(struct riemann_host const *host, /* {{{ */
     if (rates == NULL) {
       ERROR("write_riemann plugin: uc_get_rate failed.");
       riemann_message_free(msg);
-      return (NULL);
+      return NULL;
     }
   }
 
@@ -420,13 +431,13 @@ wrr_value_list_to_message(struct riemann_host const *host, /* {{{ */
     if (event == NULL) {
       riemann_message_free(msg);
       sfree(rates);
-      return (NULL);
+      return NULL;
     }
     riemann_message_append_events(msg, event, NULL);
   }
 
   sfree(rates);
-  return (msg);
+  return msg;
 } /* }}} riemann_message_t *wrr_value_list_to_message */
 
 /*
@@ -457,7 +468,7 @@ static int wrr_batch_flush(cdtime_t timeout,
   int status;
 
   if (user_data == NULL)
-    return (-EINVAL);
+    return -EINVAL;
 
   host = user_data->data;
   pthread_mutex_lock(&host->lock);
@@ -537,7 +548,7 @@ static int wrr_notification(const notification_t *n, user_data_t *ud) /* {{{ */
    */
   msg = wrr_notification_to_message(host, n);
   if (msg == NULL)
-    return (-1);
+    return -1;
 
   status = wrr_send(host, msg);
   if (status != 0)
@@ -550,7 +561,7 @@ static int wrr_notification(const notification_t *n, user_data_t *ud) /* {{{ */
               "write_riemann plugin: riemann_client_send succeeded");
 
   riemann_message_free(msg);
-  return (status);
+  return status;
 } /* }}} int wrr_notification */
 
 static int wrr_write(const data_set_t *ds, /* {{{ */
@@ -573,7 +584,7 @@ static int wrr_write(const data_set_t *ds, /* {{{ */
   } else {
     msg = wrr_value_list_to_message(host, ds, vl, statuses);
     if (msg == NULL)
-      return (-1);
+      return -1;
 
     status = wrr_send(host, msg);
 
@@ -610,7 +621,6 @@ static int wrr_config_node(oconfig_item_t *ci) /* {{{ */
   int i;
   oconfig_item_t *child;
   char callback_name[DATA_MAX_NAME_LEN];
-  user_data_t ud;
 
   if ((host = calloc(1, sizeof(*host))) == NULL) {
     ERROR("write_riemann plugin: calloc failed.");
@@ -679,9 +689,14 @@ static int wrr_config_node(oconfig_item_t *ci) /* {{{ */
       if (status != 0)
         break;
     } else if (strcasecmp("Timeout", child->key) == 0) {
+#if RCC_VERSION_NUMBER >= 0x010800
       status = cf_util_get_int(child, (int *)&host->timeout.tv_sec);
       if (status != 0)
         break;
+#else
+      WARNING("write_riemann plugin: The Timeout option is not supported. "
+              "Please upgrade the Riemann client to at least 1.8.0.");
+#endif
     } else if (strcasecmp("Port", child->key) == 0) {
       host->port = cf_util_get_port_number(child);
       if (host->port == -1) {
@@ -786,10 +801,10 @@ static int wrr_config_node(oconfig_item_t *ci) /* {{{ */
     return status;
   }
 
-  ssnprintf(callback_name, sizeof(callback_name), "write_riemann/%s",
-            host->name);
-  ud.data = host;
-  ud.free_func = wrr_free;
+  snprintf(callback_name, sizeof(callback_name), "write_riemann/%s",
+           host->name);
+
+  user_data_t ud = {.data = host, .free_func = wrr_free};
 
   pthread_mutex_lock(&host->lock);
 
@@ -821,7 +836,7 @@ static int wrr_config_node(oconfig_item_t *ci) /* {{{ */
      * holding a reference. */
     pthread_mutex_unlock(&host->lock);
     wrr_free(host);
-    return (-1);
+    return -1;
   }
 
   host->reference_count--;
@@ -847,21 +862,21 @@ static int wrr_config(oconfig_item_t *ci) /* {{{ */
 
       if (child->values_num != 2) {
         WARNING("riemann attributes need both a key and a value.");
-        return (-1);
+        return -1;
       }
       if (child->values[0].type != OCONFIG_TYPE_STRING ||
           child->values[1].type != OCONFIG_TYPE_STRING) {
         WARNING("riemann attribute needs string arguments.");
-        return (-1);
+        return -1;
       }
       if ((key = strdup(child->values[0].value.string)) == NULL) {
         WARNING("cannot allocate memory for attribute key.");
-        return (-1);
+        return -1;
       }
       if ((val = strdup(child->values[1].value.string)) == NULL) {
         WARNING("cannot allocate memory for attribute value.");
         sfree(key);
-        return (-1);
+        return -1;
       }
       strarray_add(&riemann_attrs, &riemann_attrs_num, key);
       strarray_add(&riemann_attrs, &riemann_attrs_num, val);
@@ -883,11 +898,9 @@ static int wrr_config(oconfig_item_t *ci) /* {{{ */
               child->key);
     }
   }
-  return (0);
+  return 0;
 } /* }}} int wrr_config */
 
 void module_register(void) {
   plugin_register_complex_config("write_riemann", wrr_config);
 }
-
-/* vim: set sw=8 sts=8 ts=8 noet : */