#include "collectd.h"
-#include "common.h"
#include "plugin.h"
+#include "utils/common/common.h"
#include "utils_cache.h"
#include <mongoc.h>
char *user;
char *passwd;
- _Bool store_rates;
- _Bool connected;
+ bool store_rates;
+ bool connected;
mongoc_client_t *client;
mongoc_database_t *database;
* Functions
*/
static bson_t *wm_create_bson(const data_set_t *ds, /* {{{ */
- const value_list_t *vl, _Bool store_rates) {
+ const value_list_t *vl, bool store_rates) {
bson_t *ret;
bson_t subarray;
gauge_t *rates;
BSON_APPEND_UTF8(ret, "type_instance", vl->type_instance);
BSON_APPEND_ARRAY_BEGIN(ret, "values", &subarray); /* {{{ */
- for (int i = 0; i < ds->ds_num; i++) {
+ for (size_t i = 0; i < ds->ds_num; i++) {
char key[16];
- ssnprintf(key, sizeof(key), "%i", i);
+ snprintf(key, sizeof(key), "%" PRIsz, i);
if (ds->ds[i].type == DS_TYPE_GAUGE)
BSON_APPEND_DOUBLE(&subarray, key, vl->values[i].gauge);
else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
BSON_APPEND_INT64(&subarray, key, vl->values[i].absolute);
else {
- ERROR("write_mongodb plugin: Unknown ds_type %d for index %d",
+ ERROR("write_mongodb plugin: Unknown ds_type %d for index %" PRIsz,
ds->ds[i].type, i);
bson_destroy(ret);
return NULL;
bson_append_array_end(ret, &subarray); /* }}} values */
BSON_APPEND_ARRAY_BEGIN(ret, "dstypes", &subarray); /* {{{ */
- for (int i = 0; i < ds->ds_num; i++) {
+ for (size_t i = 0; i < ds->ds_num; i++) {
char key[16];
- ssnprintf(key, sizeof(key), "%i", i);
+ snprintf(key, sizeof(key), "%" PRIsz, i);
if (store_rates)
BSON_APPEND_UTF8(&subarray, key, "gauge");
bson_append_array_end(ret, &subarray); /* }}} dstypes */
BSON_APPEND_ARRAY_BEGIN(ret, "dsnames", &subarray); /* {{{ */
- for (int i = 0; i < ds->ds_num; i++) {
+ for (size_t i = 0; i < ds->ds_num; i++) {
char key[16];
- ssnprintf(key, sizeof(key), "%i", i);
+ snprintf(key, sizeof(key), "%" PRIsz, i);
BSON_APPEND_UTF8(&subarray, key, ds->ds[i].name);
}
bson_append_array_end(ret, &subarray); /* }}} dsnames */
size_t error_location;
if (!bson_validate(ret, BSON_VALIDATE_UTF8, &error_location)) {
ERROR("write_mongodb plugin: Error in generated BSON document "
- "at byte %zu",
+ "at byte %" PRIsz,
error_location);
bson_destroy(ret);
return NULL;
static int wm_initialize(wm_node_t *node) /* {{{ */
{
char *uri;
- size_t uri_length;
- char const *format_string;
- if (node->connected) {
+ if (node->connected)
return 0;
- }
INFO("write_mongodb plugin: Connecting to [%s]:%d", node->host, node->port);
if ((node->db != NULL) && (node->user != NULL) && (node->passwd != NULL)) {
- format_string = "mongodb://%s:%s@%s:%d/?authSource=%s";
- uri_length = strlen(format_string) + strlen(node->user) +
- strlen(node->passwd) + strlen(node->host) + 5 +
- strlen(node->db) + 1;
- if ((uri = calloc(1, uri_length)) == NULL) {
+ uri = ssnprintf_alloc("mongodb://%s:%s@%s:%d/?authSource=%s", node->user,
+ node->passwd, node->host, node->port, node->db);
+ if (uri == NULL) {
ERROR("write_mongodb plugin: Not enough memory to assemble "
"authentication string.");
mongoc_client_destroy(node->client);
node->client = NULL;
- node->connected = 0;
+ node->connected = false;
return -1;
}
- ssnprintf(uri, uri_length, format_string, node->user, node->passwd,
- node->host, node->port, node->db);
node->client = mongoc_client_new(uri);
if (!node->client) {
ERROR("write_mongodb plugin: Authenticating to [%s]:%d for database "
"\"%s\" as user \"%s\" failed.",
node->host, node->port, node->db, node->user);
- node->connected = 0;
+ node->connected = false;
sfree(uri);
return -1;
}
} else {
- format_string = "mongodb://%s:%d";
- uri_length = strlen(format_string) + strlen(node->host) + 5 + 1;
- if ((uri = calloc(1, uri_length)) == NULL) {
+ uri = ssnprintf_alloc("mongodb://%s:%d", node->host, node->port);
+ if (uri == NULL) {
ERROR("write_mongodb plugin: Not enough memory to assemble "
"authentication string.");
mongoc_client_destroy(node->client);
node->client = NULL;
- node->connected = 0;
+ node->connected = false;
return -1;
}
- snprintf(uri, uri_length, format_string, node->host, node->port);
node->client = mongoc_client_new(uri);
if (!node->client) {
ERROR("write_mongodb plugin: Connecting to [%s]:%d failed.", node->host,
node->port);
- node->connected = 0;
+ node->connected = false;
sfree(uri);
return -1;
}
+ sfree(uri);
}
- sfree(uri);
node->database = mongoc_client_get_database(node->client, "collectd");
if (!node->database) {
ERROR("write_mongodb plugin: error creating/getting database");
mongoc_client_destroy(node->client);
node->client = NULL;
- node->connected = 0;
+ node->connected = false;
return -1;
}
- node->connected = 1;
+ node->connected = true;
return 0;
} /* }}} int wm_initialize */
mongoc_client_destroy(node->client);
node->database = NULL;
node->client = NULL;
- node->connected = 0;
+ node->connected = false;
pthread_mutex_unlock(&node->lock);
bson_destroy(bson_record);
return -1;
mongoc_client_destroy(node->client);
node->database = NULL;
node->client = NULL;
- node->connected = 0;
+ node->connected = false;
pthread_mutex_unlock(&node->lock);
bson_destroy(bson_record);
mongoc_collection_destroy(collection);
mongoc_client_destroy(node->client);
node->database = NULL;
node->client = NULL;
- node->connected = 0;
+ node->connected = false;
sfree(node->host);
sfree(node);
return ENOMEM;
}
node->port = MONGOC_DEFAULT_PORT;
- node->store_rates = 1;
+ node->store_rates = true;
pthread_mutex_init(&node->lock, /* attr = */ NULL);
status = cf_util_get_string_buffer(ci, node->name, sizeof(node->name));
}
if (status == 0) {
- char cb_name[DATA_MAX_NAME_LEN];
+ char cb_name[sizeof("write_mongodb/") + DATA_MAX_NAME_LEN];
- ssnprintf(cb_name, sizeof(cb_name), "write_mongodb/%s", node->name);
+ snprintf(cb_name, sizeof(cb_name), "write_mongodb/%s", node->name);
- status =
- plugin_register_write(cb_name, wm_write,
- &(user_data_t){
- .data = node, .free_func = wm_config_free,
- });
+ status = plugin_register_write(cb_name, wm_write,
+ &(user_data_t){
+ .data = node,
+ .free_func = wm_config_free,
+ });
INFO("write_mongodb plugin: registered write plugin %s %d", cb_name,
status);
}