#define _GNU_SOURCE
#include "collectd.h"
+
#include "plugin.h"
#include "common.h"
-#include "configfile.h"
#include "utils_cache.h"
-#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <inttypes.h>
-#include <pthread.h>
#include <stddef.h>
#include <stdlib.h>
#define SENSU_HOST "localhost"
#define SENSU_PORT "3030"
+#ifdef HAVE_ASPRINTF
+#define my_asprintf asprintf
+#define my_vasprintf vasprintf
+#else
+/*
+ * asprintf() is available from Solaris 10 update 11.
+ * For older versions, use asprintf() portable implementation from
+ * https://github.com/littlstar/asprintf.c/blob/master/
+ * copyright (c) 2014 joseph werle <joseph.werle@gmail.com> under MIT license.
+ */
+
+static int my_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;
+}
+
+static int my_asprintf(char **str, const char *fmt, ...) {
+ int size = 0;
+ va_list args;
+ // init variadic argumens
+ va_start(args, fmt);
+ // format and get size
+ size = my_vasprintf(str, fmt, args);
+ // toss args
+ va_end(args);
+ return size;
+}
+
+#endif
+
struct str_list {
int nb_strs;
char **strs;
static void free_str_list(struct str_list *strs) /* {{{ */
{
- int i;
- for (i=0; i<strs->nb_strs; i++)
+ for (int i=0; i<strs->nb_strs; i++)
free(strs->strs[i]);
free(strs->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;
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,
if (setsockopt(host->s, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger) != 0)
WARNING("write_sensu plugin: failed to set socket close() lingering");
+ set_sock_opts(host->s);
+
// connect the socket
if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
close(host->s);
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) {
ret_str[0] = '\0';
}
- res = asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
+ res = my_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; i<list->nb_strs; i++) {
- res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
+ for (int i=1; i<list->nb_strs; i++) {
+ res = my_asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
free(temp_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
}
temp_str = ret_str;
}
- res = asprintf(&ret_str, "%s]", temp_str);
+ res = my_asprintf(&ret_str, "%s]", temp_str);
free(temp_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
{
- int i;
int len=strlen(orig_name);
- for (i=0; i<len; i++) {
+ for (int i=0; i<len; i++) {
// some plugins like ipmi generate special characters in metric name
switch(orig_name[i]) {
case '(': orig_name[i] = '_'; break;
{
char name_buffer[5 * DATA_MAX_NAME_LEN];
char service_buffer[6 * DATA_MAX_NAME_LEN];
- int i;
char *ret_str;
char *temp_str;
char *value_str;
}
}
else {
- res = asprintf(&ret_str, "%s, %s", part1, handlers_str);
+ res = my_asprintf(&ret_str, "%s, %s", part1, handlers_str);
free(handlers_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
}
// incorporate the plugin name information
- res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
+ res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
ret_str = temp_str;
// incorporate the plugin type
- res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
+ res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// incorporate the plugin instance if any
if (vl->plugin_instance[0] != 0) {
- res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
+ res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// incorporate the plugin type instance if any
if (vl->type_instance[0] != 0) {
- res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
+ res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
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));
- res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
+ res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
}
ret_str = temp_str;
} else {
- res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
+ res = my_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("write_sensu plugin: Unable to alloc memory");
}
// incorporate the data source name
- res = asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
+ res = my_asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
{
char ds_index[DATA_MAX_NAME_LEN];
ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
- res = asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
+ res = my_asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
}
// add key value attributes from config if any
- for (i = 0; i < sensu_attrs_num; i += 2) {
- res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
+ for (size_t i = 0; i < sensu_attrs_num; i += 2) {
+ res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// incorporate sensu tags from config if any
if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
- res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
+ res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// calculate the value and set to a string
if (ds->ds[index].type == DS_TYPE_GAUGE) {
- res = asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
+ res = my_asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
if (res == -1) {
free(ret_str);
ERROR("write_sensu plugin: Unable to alloc memory");
return NULL;
}
} else if (rates != NULL) {
- res = asprintf(&value_str, GAUGE_FORMAT, rates[index]);
+ res = my_asprintf(&value_str, GAUGE_FORMAT, rates[index]);
if (res == -1) {
free(ret_str);
ERROR("write_sensu plugin: Unable to alloc memory");
}
} else {
if (ds->ds[index].type == DS_TYPE_DERIVE) {
- res = asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
+ res = my_asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
if (res == -1) {
free(ret_str);
ERROR("write_sensu plugin: Unable to alloc memory");
}
}
else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
- res = asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
+ res = my_asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
if (res == -1) {
free(ret_str);
ERROR("write_sensu plugin: Unable to alloc memory");
}
}
else {
- res = asprintf(&value_str, "%llu", vl->values[index].counter);
+ res = my_asprintf(&value_str, "%llu", vl->values[index].counter);
if (res == -1) {
free(ret_str);
ERROR("write_sensu plugin: Unable to alloc memory");
in_place_replace_sensu_name_reserved(service_buffer);
// finalize the buffer by setting the output and closing curly bracket
- res = asprintf(&temp_str, "%s, \"output\": \"%s %s %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time));
+ res = my_asprintf(&temp_str, "%s, \"output\": \"%s %s %lld\"}\n",ret_str, service_buffer, value_str, (long long)CDTIME_T_TO_TIME_T(vl->time));
free(ret_str);
free(value_str);
if (res == -1) {
} else
retlen = strlen(str);
- ret = malloc(retlen + 1);
+ ret = calloc(1, retlen + 1);
if (ret == NULL)
return NULL;
// added to original: not optimized, but keeps valgrind happy.
- memset(ret, 0, retlen + 1);
r = ret;
p = str;
{
char service_buffer[6 * DATA_MAX_NAME_LEN];
char const *severity;
- notification_meta_t *meta;
char *ret_str;
char *temp_str;
int status;
- int i;
+ size_t i;
int res;
// add the severity/status
switch (n->severity) {
severity = "UNKNOWN";
status = 3;
}
- res = asprintf(&temp_str, "{\"status\": %d", status);
+ res = my_asprintf(&temp_str, "{\"status\": %d", status);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
return NULL;
ret_str = temp_str;
// incorporate the timestamp
- res = asprintf(&temp_str, "%s, \"timestamp\": %ld", ret_str, CDTIME_T_TO_TIME_T(n->time));
+ res = my_asprintf(&temp_str, "%s, \"timestamp\": %lld", ret_str, (long long)CDTIME_T_TO_TIME_T(n->time));
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
}
// incorporate the handlers
if (strlen(handlers_str) != 0) {
- res = asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
+ res = my_asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
free(ret_str);
free(handlers_str);
if (res == -1) {
// incorporate the plugin name information if any
if (n->plugin[0] != 0) {
- res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
+ res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// incorporate the plugin type if any
if (n->type[0] != 0) {
- res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
+ res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// incorporate the plugin instance if any
if (n->plugin_instance[0] != 0) {
- res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
+ res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// incorporate the plugin type instance if any
if (n->type_instance[0] != 0) {
- res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
+ res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// add key value attributes from config if any
for (i = 0; i < sensu_attrs_num; i += 2) {
- res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
+ res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
// incorporate sensu tags from config if any
if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
- res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
+ res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
n->type, n->type_instance, host->separator);
// replace sensu event name chars that are considered illegal
in_place_replace_sensu_name_reserved(service_buffer);
- res = asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
+ res = my_asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
free(ret_str);
if (res == -1) {
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);
+ res = my_asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str, severity, msg);
free(ret_str);
free(msg);
if (res == -1) {
}
// 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);
+ res = my_asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
ret_str = temp_str;
}
if (meta->type == NM_TYPE_STRING) {
- res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
+ res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
}
// close the curly bracket
- res = asprintf(&temp_str, "%s}\n", ret_str);
+ res = my_asprintf(&temp_str, "%s}\n", ret_str);
free(ret_str);
if (res == -1) {
ERROR("write_sensu plugin: Unable to alloc memory");
int statuses[vl->values_len];
struct sensu_host *host = ud->data;
gauge_t *rates = NULL;
- int i;
char *msg;
pthread_mutex_lock(&host->lock);
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);
{
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;
if ((host = calloc(1, sizeof(*host))) == NULL) {
ERROR("write_sensu plugin: calloc failed.");
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;
}
ssnprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
- ud.data = host;
- ud.free_func = sensu_free;
+
+ user_data_t ud = {
+ .data = host,
+ .free_func = sensu_free
+ };
pthread_mutex_lock(&host->lock);
static int sensu_config(oconfig_item_t *ci) /* {{{ */
{
- int i;
oconfig_item_t *child;
int status;
struct str_list sensu_tags_arr;
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) {