write_kafka: Fix misleading indentation
[collectd.git] / src / write_sensu.c
index 706ef46..0d568fe 100644 (file)
@@ -24,6 +24,8 @@
  *   Fabrice A. Marie <fabrice at kibinlabs.com>
  */
 
+#define _GNU_SOURCE
+
 #include "collectd.h"
 #include "plugin.h"
 #include "common.h"
 #include <stddef.h>
 
 #include <stdlib.h>
-#ifndef HAVE_ASPRINTF
-/*
- * Uses asprintf() portable implementation from
- * https://github.com/littlstar/asprintf.c/blob/master/
- * copyright (c) 2014 joseph werle <joseph.werle@gmail.com> under MIT license.
- */
-#include <stdio.h>
-#include <stdarg.h>
-
-int vasprintf(char **str, const char *fmt, va_list args) {
-  int size = 0;
-  va_list tmpa;
-  // copy
-  va_copy(tmpa, args);
-  // apply variadic arguments to
-  // sprintf with format to get size
-  size = vsnprintf(NULL, size, fmt, tmpa);
-  // toss args
-  va_end(tmpa);
-  // return -1 to be compliant if
-  // size is less than 0
-  if (size < 0) { return -1; }
-  // alloc with size plus 1 for `\0'
-  *str = (char *) malloc(size + 1);
-  // return -1 to be compliant
-  // if pointer is `NULL'
-  if (NULL == *str) { return -1; }
-  // format string with original
-  // variadic arguments and set new size
-  size = vsprintf(*str, fmt, args);
-  return size;
-}
-
-int asprintf(char **str, const char *fmt, ...) {
-  int size = 0;
-  va_list args;
-  // init variadic argumens
-  va_start(args, fmt);
-  // format and get size
-  size = vasprintf(str, fmt, args);
-  // toss args
-  va_end(args);
-  return size;
-}
-
-#endif
-
 #define SENSU_HOST             "localhost"
 #define SENSU_PORT             "3030"
 
@@ -113,10 +68,9 @@ struct sensu_host {
        int                          reference_count;
 };
 
-static char    *sensu_tags;
-static char    **sensu_attrs;
+static char    *sensu_tags = NULL;
+static char    **sensu_attrs = NULL;
 static size_t sensu_attrs_num;
-static const char *alloc_err ="write_sensu plugin: Unable to alloc memory";
 
 static int add_str_to_list(struct str_list *strs,
                const char *str_to_add) /* {{{ */
@@ -124,14 +78,14 @@ static int add_str_to_list(struct str_list *strs,
        char **old_strs_ptr = strs->strs;
        char *newstr = strdup(str_to_add);
        if (newstr == NULL) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return -1;
        }
        strs->strs = realloc(strs->strs, sizeof(char *) *(strs->nb_strs + 1));
        if (strs->strs == NULL) {
                strs->strs = old_strs_ptr;
                free(newstr);
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return -1;
        }
        strs->strs[strs->nb_strs] = newstr;
@@ -230,7 +184,7 @@ static char *build_json_str_list(const char *tag, struct str_list const *list) /
        if (list->nb_strs == 0) {
                ret_str = malloc(sizeof(char));
                if (ret_str == NULL) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str[0] = '\0';
@@ -238,14 +192,14 @@ static char *build_json_str_list(const char *tag, struct str_list const *list) /
 
        res = asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        for (i=1; i<list->nb_strs; i++) {
                res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
                free(temp_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                temp_str = ret_str;
@@ -253,57 +207,57 @@ static char *build_json_str_list(const char *tag, struct str_list const *list) /
        res = asprintf(&ret_str, "%s]", temp_str);
        free(temp_str);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
 
        return ret_str;
 } /* }}} char *build_json_str_list*/
 
-int format_name2(char *ret, int ret_len,
-                const char *hostname,
-                const char *plugin, const char *plugin_instance,
-                const char *type, const char *type_instance,
-                const char *separator)
+static int sensu_format_name2(char *ret, int ret_len,
+               const char *hostname,
+               const char *plugin, const char *plugin_instance,
+               const char *type, const char *type_instance,
+               const char *separator)
 {
-  char *buffer;
-  size_t buffer_size;
+       char *buffer;
+       size_t buffer_size;
 
-  buffer = ret;
-  buffer_size = (size_t) ret_len;
+       buffer = ret;
+       buffer_size = (size_t) ret_len;
 
 #define APPEND(str) do {          \
-  size_t l = strlen (str);        \
-  if (l >= buffer_size)           \
-    return (ENOBUFS);             \
-  memcpy (buffer, (str), l);      \
-  buffer += l; buffer_size -= l;  \
+       size_t l = strlen (str);        \
+       if (l >= buffer_size)           \
+               return (ENOBUFS);             \
+       memcpy (buffer, (str), l);      \
+       buffer += l; buffer_size -= l;  \
 } while (0)
 
-  assert (plugin != NULL);
-  assert (type != NULL);
-
-  APPEND (hostname);
-  APPEND (separator);
-  APPEND (plugin);
-  if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
-  {
-    APPEND ("-");
-    APPEND (plugin_instance);
-  }
-  APPEND (separator);
-  APPEND (type);
-  if ((type_instance != NULL) && (type_instance[0] != 0))
-  {
-    APPEND ("-");
-    APPEND (type_instance);
-  }
-  assert (buffer_size > 0);
-  buffer[0] = 0;
+       assert (plugin != NULL);
+       assert (type != NULL);
+
+       APPEND (hostname);
+       APPEND (separator);
+       APPEND (plugin);
+       if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
+       {
+               APPEND ("-");
+               APPEND (plugin_instance);
+       }
+       APPEND (separator);
+       APPEND (type);
+       if ((type_instance != NULL) && (type_instance[0] != 0))
+       {
+               APPEND ("-");
+               APPEND (type_instance);
+       }
+       assert (buffer_size > 0);
+       buffer[0] = 0;
 
 #undef APPEND
-  return (0);
-} /* int format_name2 */
+       return (0);
+} /* int sensu_format_name2 */
 
 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
 {
@@ -340,7 +294,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
 
        char *handlers_str = build_json_str_list("handlers", &(host->metric_handlers));
        if (handlers_str == NULL) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
 
@@ -349,7 +303,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
                free(handlers_str);
                ret_str = strdup(part1);
                if (ret_str == NULL) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
        }
@@ -357,7 +311,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
                res = asprintf(&ret_str, "%s, %s", part1, handlers_str);
                free(handlers_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
        }
@@ -366,7 +320,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
        res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
        free(ret_str);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        ret_str = temp_str;
@@ -375,7 +329,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
        res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
        free(ret_str);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        ret_str = temp_str;
@@ -385,7 +339,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -396,7 +350,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -409,7 +363,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -417,7 +371,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -427,7 +381,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
        res = asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
        free(ret_str);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        ret_str = temp_str;
@@ -439,7 +393,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -450,18 +404,18 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
        }
 
        // incorporate sensu tags from config if any
-       if (strlen(sensu_tags) != 0) {
+       if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
                res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -469,39 +423,48 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
 
        // calculate the value and set to a string
        if (ds->ds[index].type == DS_TYPE_GAUGE) {
-               double tmp_v = (double) vl->values[index].gauge;
-               res = asprintf(&value_str, "%.8f", tmp_v, sensu_tags);
+               res = asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
                if (res == -1) {
                        free(ret_str);
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
        } else if (rates != NULL) {
-               double tmp_v = (double) rates[index];
-               res = asprintf(&value_str, "%.8f", tmp_v, sensu_tags);
+               res = asprintf(&value_str, GAUGE_FORMAT, rates[index]);
                if (res == -1) {
                        free(ret_str);
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
        } else {
-               int64_t tmp_v;
-               if (ds->ds[index].type == DS_TYPE_DERIVE)
-                       tmp_v = (int64_t) vl->values[index].derive;
-               else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
-                       tmp_v = (int64_t) vl->values[index].absolute;
-               else
-                       tmp_v = (int64_t) vl->values[index].counter;
-               res = asprintf(&value_str, "%lld", tmp_v, sensu_tags);
-               if (res == -1) {
-                       free(ret_str);
-                       ERROR(alloc_err);
-                       return NULL;
+               if (ds->ds[index].type == DS_TYPE_DERIVE) {
+                       res = asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
+                       if (res == -1) {
+                               free(ret_str);
+                               ERROR("write_sensu plugin: Unable to alloc memory");
+                               return NULL;
+                       }
+               }
+               else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
+                       res = asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
+                       if (res == -1) {
+                               free(ret_str);
+                               ERROR("write_sensu plugin: Unable to alloc memory");
+                               return NULL;
+                       }
+               }
+               else {
+                       res = asprintf(&value_str, "%llu", vl->values[index].counter);
+                       if (res == -1) {
+                               free(ret_str);
+                               ERROR("write_sensu plugin: Unable to alloc memory");
+                               return NULL;
+                       }
                }
        }
 
        // Generate the full service name
-       format_name2(name_buffer, sizeof(name_buffer),
+       sensu_format_name2(name_buffer, sizeof(name_buffer),
                vl->host, vl->plugin, vl->plugin_instance,
                vl->type, vl->type_instance, host->separator);
        if (host->always_append_ds || (ds->ds_num > 1)) {
@@ -527,7 +490,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
        free(ret_str);
        free(value_str);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        ret_str = temp_str;
@@ -543,14 +506,14 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
  * http://creativeandcritical.net/str-replace-c/
  * copyright (c) Laird Shaw, under public domain.
  */
-char *replace_str(const char *str, const char *old, /* {{{ */
+static char *replace_str(const char *str, const char *old, /* {{{ */
                const char *new)
 {
        char *ret, *r;
        const char *p, *q;
        size_t oldlen = strlen(old);
        size_t count = strlen(new);
-       size_t retlen = count;
+       size_t retlen;
        size_t newlen = count;
        int samesize = (oldlen == newlen);
 
@@ -606,19 +569,19 @@ static char *replace_json_reserved(const char *message) /* {{{ */
 {
        char *msg = replace_str(message, "\\", "\\\\");
        if (msg == NULL) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        char *tmp = replace_str(msg, "\"", "\\\"");
        free(msg);
        if (tmp == NULL) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        msg = replace_str(tmp, "\n", "\\\n");
        free(tmp);
        if (msg == NULL) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        return msg;
@@ -655,7 +618,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
        }
        res = asprintf(&temp_str, "{\"status\": %d", status);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        ret_str = temp_str;
@@ -664,14 +627,15 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
        res = asprintf(&temp_str, "%s, \"timestamp\": %ld", ret_str, CDTIME_T_TO_TIME_T(n->time));
        free(ret_str);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        ret_str = temp_str;
 
        char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers));
        if (handlers_str == NULL) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
+               free(ret_str);
                return NULL;
        }
        // incorporate the handlers
@@ -680,7 +644,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                free(ret_str);
                free(handlers_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -693,7 +657,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -704,7 +668,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -715,7 +679,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -726,7 +690,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -737,25 +701,25 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
        }
 
        // incorporate sensu tags from config if any
-       if (strlen(sensu_tags) != 0) {
+       if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
                res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
                free(ret_str);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
        }
 
        // incorporate the service name
-       format_name2(service_buffer, sizeof(service_buffer),
+       sensu_format_name2(service_buffer, sizeof(service_buffer),
                                /* host */ "", n->plugin, n->plugin_instance,
                                n->type, n->type_instance, host->separator);
        // replace sensu event name chars that are considered illegal
@@ -763,7 +727,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
        res = asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
        free(ret_str);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        ret_str = temp_str;
@@ -772,14 +736,15 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
        if (n->message[0] != 0) {
                char *msg = replace_json_reserved(n->message);
                if (msg == NULL) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
+                       free(ret_str);
                        return NULL;
                }
                res = asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str, severity, msg);
                free(ret_str);
                free(msg);
                if (res == -1) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return NULL;
                }
                ret_str = temp_str;
@@ -791,7 +756,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                        res = asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double);
                        free(ret_str);
                        if (res == -1) {
-                               ERROR(alloc_err);
+                               ERROR("write_sensu plugin: Unable to alloc memory");
                                return NULL;
                        }
                        ret_str = temp_str;
@@ -800,7 +765,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                        res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
                        free(ret_str);
                        if (res == -1) {
-                               ERROR(alloc_err);
+                               ERROR("write_sensu plugin: Unable to alloc memory");
                                return NULL;
                        }
                        ret_str = temp_str;
@@ -811,7 +776,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
        res = asprintf(&temp_str, "%s}\n", ret_str);
        free(ret_str);
        if (res == -1) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                return NULL;
        }
        ret_str = temp_str;
@@ -879,6 +844,7 @@ static int sensu_write(const data_set_t *ds, /* {{{ */
        char *msg;
 
        pthread_mutex_lock(&host->lock);
+       memset(statuses, 0, vl->values_len * sizeof(*statuses));
 
        if (host->store_rates) {
                rates = uc_get_rate(ds, vl);
@@ -991,7 +957,7 @@ static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
        host->notification_handlers.strs = NULL;
        host->separator = strdup("/");
        if (host->separator == NULL) {
-               ERROR(alloc_err);
+               ERROR("write_sensu plugin: Unable to alloc memory");
                sensu_free(host);
                return -1;
        }
@@ -1151,12 +1117,6 @@ static int sensu_config(oconfig_item_t *ci) /* {{{ */
 
        sensu_tags_arr.nb_strs = 0;
        sensu_tags_arr.strs = NULL;
-       sensu_tags = malloc(sizeof(char));
-       if (sensu_tags == NULL) {
-               ERROR(alloc_err);
-               return -1;
-       }
-       sensu_tags[0] = '\0';
 
        for (i = 0; i < ci->children_num; i++)  {
                child = &ci->children[i];
@@ -1164,35 +1124,22 @@ static int sensu_config(oconfig_item_t *ci) /* {{{ */
                if (strcasecmp("Node", child->key) == 0) {
                        sensu_config_node(child);
                } else if (strcasecmp(child->key, "attribute") == 0) {
-                       char *key = NULL;
-                       char *val = NULL;
-
                        if (child->values_num != 2) {
                                WARNING("sensu attributes need both a key and a value.");
-                               free(sensu_tags);
-                               return -1;
+                               continue;
                        }
                        if (child->values[0].type != OCONFIG_TYPE_STRING ||
-                           child->values[1].type != OCONFIG_TYPE_STRING) {
+                                       child->values[1].type != OCONFIG_TYPE_STRING) {
                                WARNING("sensu attribute needs string arguments.");
-                               free(sensu_tags);
-                               return -1;
-                       }
-                       if ((key = strdup(child->values[0].value.string)) == NULL) {
-                               ERROR(alloc_err);
-                               free(sensu_tags);
-                               return -1;
-                       }
-                       if ((val = strdup(child->values[1].value.string)) == NULL) {
-                               free(sensu_tags);
-                               ERROR(alloc_err);
-                               return -1;
+                               continue;
                        }
-                       strarray_add(&sensu_attrs, &sensu_attrs_num, key);
-                       strarray_add(&sensu_attrs, &sensu_attrs_num, val);
-                       DEBUG("write_sensu: got attr: %s => %s", key, val);
-                       sfree(key);
-                       sfree(val);
+
+                       strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[0].value.string);
+                       strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[1].value.string);
+
+                       DEBUG("write_sensu plugin: New attribute: %s => %s",
+                                       child->values[0].value.string,
+                                       child->values[1].value.string);
                } else if (strcasecmp(child->key, "tag") == 0) {
                        char *tmp = NULL;
                        status = cf_util_get_string(child, &tmp);
@@ -1211,11 +1158,11 @@ static int sensu_config(oconfig_item_t *ci) /* {{{ */
                }
        }
        if (sensu_tags_arr.nb_strs > 0) {
-               free(sensu_tags);
+               sfree (sensu_tags);
                sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
                free_str_list(&sensu_tags_arr);
                if (sensu_tags == NULL) {
-                       ERROR(alloc_err);
+                       ERROR("write_sensu plugin: Unable to alloc memory");
                        return -1;
                }
        }