write_kafka: Fix misleading indentation
[collectd.git] / src / write_sensu.c
index 7a3e4f4..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,8 +68,8 @@ 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 int add_str_to_list(struct str_list *strs,
@@ -259,7 +214,7 @@ static char *build_json_str_list(const char *tag, struct str_list const *list) /
        return ret_str;
 } /* }}} char *build_json_str_list*/
 
-int sensu_format_name2(char *ret, int ret_len,
+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,
@@ -456,7 +411,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
        }
 
        // 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) {
@@ -551,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);
 
@@ -680,6 +635,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
        char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers));
        if (handlers_str == NULL) {
                ERROR("write_sensu plugin: Unable to alloc memory");
+               free(ret_str);
                return NULL;
        }
        // incorporate the handlers
@@ -752,7 +708,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
        }
 
        // 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) {
@@ -781,6 +737,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
                char *msg = replace_json_reserved(n->message);
                if (msg == NULL) {
                        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);
@@ -1160,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("write_sensu plugin: Unable to alloc memory");
-               return -1;
-       }
-       sensu_tags[0] = '\0';
 
        for (i = 0; i < ci->children_num; i++)  {
                child = &ci->children[i];
@@ -1173,36 +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("write_sensu plugin: Unable to alloc memory");
-                               free(sensu_tags);
-                               return -1;
-                       }
-                       if ((val = strdup(child->values[1].value.string)) == NULL) {
-                               free(sensu_tags);
-                               free(key);
-                               ERROR("write_sensu plugin: Unable to alloc memory");
-                               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);
@@ -1221,7 +1158,7 @@ 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) {