- ssnprintf (collection_name, sizeof (collection_name), "collectd.%s",
- vl->plugin);
-
- bson_record = wm_create_bson (ds, vl, node->store_rates);
- if (bson_record == NULL)
- return (ENOMEM);
-
- pthread_mutex_lock (&node->lock);
-
- if (!mongo_is_connected (node->conn))
- {
- INFO ("write_mongodb plugin: Connecting to [%s]:%i",
- (node->host != NULL) ? node->host : "localhost",
- (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
- status = mongo_connect (node->conn, node->host, node->port);
- if (status != MONGO_OK) {
- ERROR ("write_mongodb plugin: Connecting to [%s]:%i failed.",
- (node->host != NULL) ? node->host : "localhost",
- (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
- mongo_destroy (node->conn);
- pthread_mutex_unlock (&node->lock);
- return (-1);
+ if (node->connected) {
+ return 0;
+ }
+
+ INFO("write_mongodb plugin: Connecting to [%s]:%i",
+ (node->host != NULL) ? node->host : "localhost",
+ (node->port != 0) ? node->port : MONGOC_DEFAULT_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) {
+ ERROR("write_mongodb plugin: Not enough memory to assemble "
+ "authentication string.");
+ mongoc_client_destroy(node->client);
+ node->client = NULL;
+ node->connected = 0;
+ return -1;