X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_sensu.c;h=791acb307c54612f3e78de98f322f7c48e6b0458;hb=0fdd51138abce6c305ecb94c0b655578d9f26667;hp=cb0c2fe2dc4ce8cac97f7cb7fc7ef7f82bdb21a5;hpb=27894a14bcc5437d1e9131ea602c66c0e28fd9d1;p=collectd.git diff --git a/src/write_sensu.c b/src/write_sensu.c index cb0c2fe2..791acb30 100644 --- a/src/write_sensu.c +++ b/src/write_sensu.c @@ -113,8 +113,8 @@ struct sensu_host { int reference_count; }; -static char *sensu_tags; -static char **sensu_attrs; +static char *sensu_tags = NULL; +static char **sensu_attrs = NULL; static size_t sensu_attrs_num; static int add_str_to_list(struct str_list *strs, @@ -329,7 +329,7 @@ static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */ { char name_buffer[5 * DATA_MAX_NAME_LEN]; char service_buffer[6 * DATA_MAX_NAME_LEN]; - int i; + size_t i; char *ret_str; char *temp_str; char *value_str; @@ -468,16 +468,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) { - double tmp_v = (double) vl->values[index].gauge; - res = asprintf(&value_str, "%.8f", tmp_v); + res = 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) { - double tmp_v = (double) rates[index]; - res = asprintf(&value_str, "%.8f", tmp_v); + res = asprintf(&value_str, GAUGE_FORMAT, rates[index]); if (res == -1) { free(ret_str); ERROR("write_sensu plugin: Unable to alloc memory"); @@ -560,7 +558,7 @@ char *replace_str(const char *str, const char *old, /* {{{ */ const char *p, *q; size_t oldlen = strlen(old); size_t count = strlen(new); - size_t retlen = count; + size_t retlen; size_t newlen = count; int samesize = (oldlen == newlen); @@ -643,7 +641,7 @@ static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */ char *ret_str; char *temp_str; int status; - int i; + size_t i; int res; // add the severity/status switch (n->severity) { @@ -885,7 +883,7 @@ 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; + size_t i; char *msg; pthread_mutex_lock(&host->lock); @@ -899,7 +897,7 @@ static int sensu_write(const data_set_t *ds, /* {{{ */ return -1; } } - for (i = 0; i < (size_t) vl->values_len; i++) { + for (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); @@ -1162,12 +1160,6 @@ static int sensu_config(oconfig_item_t *ci) /* {{{ */ sensu_tags_arr.nb_strs = 0; sensu_tags_arr.strs = NULL; - sensu_tags = malloc(sizeof(char)); - if (sensu_tags == NULL) { - ERROR("write_sensu plugin: Unable to alloc memory"); - return -1; - } - sensu_tags[0] = '\0'; for (i = 0; i < ci->children_num; i++) { child = &ci->children[i]; @@ -1175,36 +1167,22 @@ static int sensu_config(oconfig_item_t *ci) /* {{{ */ if (strcasecmp("Node", child->key) == 0) { sensu_config_node(child); } else if (strcasecmp(child->key, "attribute") == 0) { - char *key = NULL; - char *val = NULL; - if (child->values_num != 2) { WARNING("sensu attributes need both a key and a value."); - free(sensu_tags); - return -1; + continue; } if (child->values[0].type != OCONFIG_TYPE_STRING || - child->values[1].type != OCONFIG_TYPE_STRING) { + child->values[1].type != OCONFIG_TYPE_STRING) { WARNING("sensu attribute needs string arguments."); - free(sensu_tags); - return -1; - } - if ((key = strdup(child->values[0].value.string)) == NULL) { - ERROR("write_sensu plugin: Unable to alloc memory"); - free(sensu_tags); - return -1; - } - if ((val = strdup(child->values[1].value.string)) == NULL) { - free(sensu_tags); - free(key); - ERROR("write_sensu plugin: Unable to alloc memory"); - return -1; + continue; } - strarray_add(&sensu_attrs, &sensu_attrs_num, key); - strarray_add(&sensu_attrs, &sensu_attrs_num, val); - DEBUG("write_sensu: got attr: %s => %s", key, val); - sfree(key); - sfree(val); + + strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[0].value.string); + strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[1].value.string); + + DEBUG("write_sensu plugin: New attribute: %s => %s", + child->values[0].value.string, + child->values[1].value.string); } else if (strcasecmp(child->key, "tag") == 0) { char *tmp = NULL; status = cf_util_get_string(child, &tmp); @@ -1223,7 +1201,7 @@ static int sensu_config(oconfig_item_t *ci) /* {{{ */ } } if (sensu_tags_arr.nb_strs > 0) { - free(sensu_tags); + sfree (sensu_tags); sensu_tags = build_json_str_list("tags", &sensu_tags_arr); free_str_list(&sensu_tags_arr); if (sensu_tags == NULL) {