- wm_node_t *node = ud->data;
- char collection_name[512];
- bson *bson_record;
- int status;
-
- 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);
+ char *uri;
+
+ 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)) {
+ 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;
+ return -1;