X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_mongodb.c;h=6d5f37943ea330c369fc6f871777aa8f7887e6e3;hb=4979d8dcd6f097eb8fd4661086e28accd31ff905;hp=30e261c9eee919e582f1d4da06ff52aced103a56;hpb=a320c4a23fee0e900a7cd2310c771b8dbd2a2d82;p=collectd.git diff --git a/src/write_mongodb.c b/src/write_mongodb.c index 30e261c9..6d5f3794 100644 --- a/src/write_mongodb.c +++ b/src/write_mongodb.c @@ -29,13 +29,11 @@ **/ #include "collectd.h" + #include "plugin.h" #include "common.h" -#include "configfile.h" #include "utils_cache.h" -#include - #if HAVE_STDINT_H # define MONGO_HAVE_STDINT 1 #else @@ -43,6 +41,11 @@ #endif #include +#if (MONGO_MAJOR == 0) && (MONGO_MINOR < 8) +# define bson_alloc() bson_create() +# define bson_dealloc(b) bson_dispose(b) +#endif + struct wm_node_s { char name[DATA_MAX_NAME_LEN]; @@ -72,9 +75,8 @@ static bson *wm_create_bson (const data_set_t *ds, /* {{{ */ { bson *ret; gauge_t *rates; - int i; - ret = bson_create (); + ret = bson_alloc (); /* matched by bson_dealloc() */ if (ret == NULL) { ERROR ("write_mongodb plugin: bson_create failed."); @@ -95,7 +97,7 @@ static bson *wm_create_bson (const data_set_t *ds, /* {{{ */ rates = NULL; } - bson_init (ret); + bson_init (ret); /* matched by bson_destroy() */ bson_append_date (ret, "time", (bson_date_t) CDTIME_T_TO_MS (vl->time)); bson_append_string (ret, "host", vl->host); bson_append_string (ret, "plugin", vl->plugin); @@ -104,7 +106,7 @@ static bson *wm_create_bson (const data_set_t *ds, /* {{{ */ bson_append_string (ret, "type_instance", vl->type_instance); bson_append_start_array (ret, "values"); /* {{{ */ - for (i = 0; i < ds->ds_num; i++) + for (int i = 0; i < ds->ds_num; i++) { char key[16]; @@ -126,7 +128,7 @@ static bson *wm_create_bson (const data_set_t *ds, /* {{{ */ bson_append_finish_array (ret); /* }}} values */ bson_append_start_array (ret, "dstypes"); /* {{{ */ - for (i = 0; i < ds->ds_num; i++) + for (int i = 0; i < ds->ds_num; i++) { char key[16]; @@ -140,7 +142,7 @@ static bson *wm_create_bson (const data_set_t *ds, /* {{{ */ bson_append_finish_array (ret); /* }}} dstypes */ bson_append_start_array (ret, "dsnames"); /* {{{ */ - for (i = 0; i < ds->ds_num; i++) + for (int i = 0; i < ds->ds_num; i++) { char key[16]; @@ -243,7 +245,8 @@ static int wm_write (const data_set_t *ds, /* {{{ */ pthread_mutex_unlock (&node->lock); /* free our resource as not to leak memory */ - bson_dispose (bson_record); + bson_destroy (bson_record); /* matches bson_init() */ + bson_dealloc (bson_record); /* matches bson_alloc() */ return (0); } /* }}} int wm_write */ @@ -266,12 +269,10 @@ static int wm_config_node (oconfig_item_t *ci) /* {{{ */ { wm_node_t *node; int status; - int i; - node = malloc (sizeof (*node)); + node = calloc (1, sizeof (*node)); if (node == NULL) return (ENOMEM); - memset (node, 0, sizeof (*node)); mongo_init (node->conn); node->host = NULL; node->store_rates = 1; @@ -285,7 +286,7 @@ static int wm_config_node (oconfig_item_t *ci) /* {{{ */ return (status); } - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; @@ -335,12 +336,13 @@ static int wm_config_node (oconfig_item_t *ci) /* {{{ */ if (status == 0) { char cb_name[DATA_MAX_NAME_LEN]; - user_data_t ud; ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name); - ud.data = node; - ud.free_func = wm_config_free; + user_data_t ud = { + .data = node, + .free_func = wm_config_free + }; status = plugin_register_write (cb_name, wm_write, &ud); INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status); @@ -354,9 +356,7 @@ static int wm_config_node (oconfig_item_t *ci) /* {{{ */ static int wm_config (oconfig_item_t *ci) /* {{{ */ { - int i; - - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i;