diff --git a/src/write_sensu.c b/src/write_sensu.c
index f7803e86a24cf1194dd7ee2d1651b1a4d8628694..0d568fe6118d8b9808710e22f10db2ff7d548d59 100644 (file)
--- a/src/write_sensu.c
+++ b/src/write_sensu.c
* 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"
@@ -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,
}
// 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) {
* 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;
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
}
// 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) {
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);