summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2c59754)
raw | patch | inline | side by side (parent: 2c59754)
author | Ruben Kerkhof <ruben@rubenkerkhof.com> | |
Sun, 2 Jul 2017 17:38:12 +0000 (19:38 +0200) | ||
committer | Ruben Kerkhof <ruben@rubenkerkhof.com> | |
Sun, 2 Jul 2017 17:38:12 +0000 (19:38 +0200) |
src/write_mongodb.c | patch | blob | history |
diff --git a/src/write_mongodb.c b/src/write_mongodb.c
index f585a39660b55582ea18b618f708a78558c48938..e90900ec12f8cf2c3f53fbed2830ea97d191ab42 100644 (file)
--- a/src/write_mongodb.c
+++ b/src/write_mongodb.c
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->connected = 0;
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) {
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->connected = 0;
return -1;
}
- snprintf(uri, uri_length, format_string, node->host, node->port);
node->client = mongoc_client_new(uri);
if (!node->client) {
sfree(uri);
return -1;
}
+ sfree(uri);
}
- sfree(uri);
node->database = mongoc_client_get_database(node->client, "collectd");
if (!node->database) {