#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;
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; i<list->nb_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");
}
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");
}
}
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 (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");
// 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 %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time));
free(ret_str);
free(value_str);
if (res == -1) {
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\": %ld", ret_str, 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 (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");