X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_sensu.c;h=77069c08a8d1f7532019d20de92fdedbac785e5e;hb=43954e07f30d05b2da8319749400c9bf4d01c23c;hp=63e9aabbcdeaf31eec10419f1a7d2fd52b8c1f41;hpb=f8e1e81d433c5b4e06792c2617abf0e6ec9e76d9;p=collectd.git diff --git a/src/write_sensu.c b/src/write_sensu.c index 63e9aabb..77069c08 100644 --- a/src/write_sensu.c +++ b/src/write_sensu.c @@ -42,6 +42,55 @@ #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 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; @@ -147,6 +196,8 @@ static int sensu_connect(struct sensu_host *host) /* {{{ */ 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); @@ -186,14 +237,14 @@ static char *build_json_str_list(const char *tag, struct str_list const *list) / 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 (int i=1; inb_strs; i++) { - res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->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"); @@ -201,7 +252,7 @@ static char *build_json_str_list(const char *tag, struct str_list const *list) / } 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"); @@ -303,7 +354,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ } } 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"); @@ -312,7 +363,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ } // 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"); @@ -321,7 +372,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ 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"); @@ -331,7 +382,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ // 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"); @@ -342,7 +393,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ // 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"); @@ -355,7 +406,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ 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"); @@ -363,7 +414,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ } 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"); @@ -373,7 +424,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ } // 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"); @@ -385,7 +436,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ { 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"); @@ -396,7 +447,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ // add key value attributes from config if any for (size_t 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"); @@ -407,7 +458,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ // 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"); @@ -418,14 +469,14 @@ 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) { - 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"); @@ -433,7 +484,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ } } 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"); @@ -441,7 +492,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ } } 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"); @@ -449,7 +500,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ } } 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"); @@ -481,7 +532,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ 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 %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time)); free(ret_str); free(value_str); if (res == -1) { @@ -609,7 +660,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ 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; @@ -617,7 +668,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ 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\": %ld", ret_str, CDTIME_T_TO_TIME_T(n->time)); free(ret_str); if (res == -1) { ERROR("write_sensu plugin: Unable to alloc memory"); @@ -633,7 +684,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ } // 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) { @@ -647,7 +698,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ // 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"); @@ -658,7 +709,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ // 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"); @@ -669,7 +720,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ // 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"); @@ -680,7 +731,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ // 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"); @@ -691,7 +742,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ // 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"); @@ -702,7 +753,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ // 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"); @@ -717,7 +768,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ 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"); @@ -733,7 +784,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ 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) { @@ -746,7 +797,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ // Pull in values from threshold and add extra attributes 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"); @@ -755,7 +806,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ 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"); @@ -766,7 +817,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ } // 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");