X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_sensu.c;h=4d0e473757174e7bdf79b9662bbd258fa049355f;hb=2761915bed8c6caea41018be3e675aa712cc0b0a;hp=f7803e86a24cf1194dd7ee2d1651b1a4d8628694;hpb=add25ce6f3129cb7138e96e5f55d11de1e5cf6de;p=collectd.git diff --git a/src/write_sensu.c b/src/write_sensu.c index f7803e86..63e9aabb 100644 --- a/src/write_sensu.c +++ b/src/write_sensu.c @@ -24,67 +24,21 @@ * Fabrice A. Marie */ +#define _GNU_SOURCE + #include "collectd.h" + #include "plugin.h" #include "common.h" #include "configfile.h" #include "utils_cache.h" -#include #include #include #include #include -#include #include #include -#ifndef HAVE_ASPRINTF -/* - * Uses asprintf() portable implementation from - * https://github.com/littlstar/asprintf.c/blob/master/ - * copyright (c) 2014 joseph werle under MIT license. - */ -#include -#include - -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" @@ -141,8 +95,7 @@ static int add_str_to_list(struct str_list *strs, static void free_str_list(struct str_list *strs) /* {{{ */ { - int i; - for (i=0; inb_strs; i++) + for (int i=0; inb_strs; i++) free(strs->strs[i]); free(strs->strs); } @@ -151,25 +104,24 @@ static void free_str_list(struct str_list *strs) /* {{{ */ static int sensu_connect(struct sensu_host *host) /* {{{ */ { int e; - struct addrinfo *ai, hints; char const *node; char const *service; // Resolve the target if we haven't done already if (!(host->flags & F_READY)) { - memset(&hints, 0, sizeof(hints)); memset(&service, 0, sizeof(service)); host->res = NULL; - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; -#ifdef AI_ADDRCONFIG - hints.ai_flags |= AI_ADDRCONFIG; -#endif node = (host->node != NULL) ? host->node : SENSU_HOST; service = (host->service != NULL) ? host->service : SENSU_PORT; - if ((e = getaddrinfo(node, service, &hints, &(host->res))) != 0) { + struct addrinfo ai_hints = { + .ai_family = AF_INET, + .ai_flags = AI_ADDRCONFIG, + .ai_socktype = SOCK_STREAM + }; + + if ((e = getaddrinfo(node, service, &ai_hints, &(host->res))) != 0) { ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s", node, gai_strerror(e)); return -1; @@ -181,7 +133,7 @@ static int sensu_connect(struct sensu_host *host) /* {{{ */ struct linger so_linger; host->s = -1; - for (ai = host->res; ai != NULL; ai = ai->ai_next) { + for (struct addrinfo *ai = host->res; ai != NULL; ai = ai->ai_next) { // create the socket if ((host->s = socket(ai->ai_family, ai->ai_socktype, @@ -223,9 +175,8 @@ static void sensu_close_socket(struct sensu_host *host) /* {{{ */ static char *build_json_str_list(const char *tag, struct str_list const *list) /* {{{ */ { int res; - char *ret_str; + char *ret_str = NULL; char *temp_str; - int i; if (list->nb_strs == 0) { ret_str = malloc(sizeof(char)); if (ret_str == NULL) { @@ -238,9 +189,10 @@ 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("write_sensu plugin: Unable to alloc memory"); + free(ret_str); return NULL; } - for (i=1; inb_strs; i++) { + for (int i=1; inb_strs; i++) { res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]); free(temp_str); if (res == -1) { @@ -259,7 +211,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, @@ -306,9 +258,8 @@ int sensu_format_name2(char *ret, int ret_len, static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */ { - int i; int len=strlen(orig_name); - for (i=0; iseverity) { @@ -680,6 +628,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 +701,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 +730,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); @@ -794,7 +744,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ } // Pull in values from threshold and add extra attributes - for (meta = n->meta; meta != NULL; meta = meta->next) { + for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) { if (strcasecmp("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE) { res = asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double); free(ret_str); @@ -883,7 +833,6 @@ static int sensu_write(const data_set_t *ds, /* {{{ */ int statuses[vl->values_len]; struct sensu_host *host = ud->data; gauge_t *rates = NULL; - int i; char *msg; pthread_mutex_lock(&host->lock); @@ -897,7 +846,7 @@ static int sensu_write(const data_set_t *ds, /* {{{ */ return -1; } } - for (i = 0; i < (size_t) vl->values_len; i++) { + for (size_t i = 0; i < vl->values_len; i++) { msg = sensu_value_to_json(host, ds, vl, (int) i, rates, statuses[i]); if (msg == NULL) { sfree(rates); @@ -977,7 +926,6 @@ static int sensu_config_node(oconfig_item_t *ci) /* {{{ */ { struct sensu_host *host = NULL; int status = 0; - int i; oconfig_item_t *child; char callback_name[DATA_MAX_NAME_LEN]; user_data_t ud; @@ -1012,7 +960,7 @@ static int sensu_config_node(oconfig_item_t *ci) /* {{{ */ return -1; } - for (i = 0; i < ci->children_num; i++) { + for (int i = 0; i < ci->children_num; i++) { child = &ci->children[i]; status = 0; @@ -1153,7 +1101,6 @@ static int sensu_config_node(oconfig_item_t *ci) /* {{{ */ static int sensu_config(oconfig_item_t *ci) /* {{{ */ { - int i; oconfig_item_t *child; int status; struct str_list sensu_tags_arr; @@ -1161,7 +1108,7 @@ static int sensu_config(oconfig_item_t *ci) /* {{{ */ sensu_tags_arr.nb_strs = 0; sensu_tags_arr.strs = NULL; - for (i = 0; i < ci->children_num; i++) { + for (int i = 0; i < ci->children_num; i++) { child = &ci->children[i]; if (strcasecmp("Node", child->key) == 0) {