- assert (23 == 42);
- }
- bson_finish(&record);
-
- pthread_mutex_lock (&node->lock);
-
- if (node->connected == 0)
- {
- status = mongo_connect(node->conn, node->host, node->port);
- if (status != MONGO_OK) {
- ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %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);
- } else {
- node->connected = 1;
+ BSON_APPEND_UTF8(&subarray, key, DS_TYPE_TO_STRING(ds->ds[i].type));
+ }
+ bson_append_array_end(ret, &subarray); /* }}} dstypes */
+
+ BSON_APPEND_ARRAY_BEGIN(ret, "dsnames", &subarray); /* {{{ */
+ for (size_t i = 0; i < ds->ds_num; i++) {
+ char key[16];
+
+ snprintf(key, sizeof(key), "%" PRIsz, i);
+ BSON_APPEND_UTF8(&subarray, key, ds->ds[i].name);
+ }
+ bson_append_array_end(ret, &subarray); /* }}} dsnames */
+
+ sfree(rates);
+
+ size_t error_location;
+ if (!bson_validate(ret, BSON_VALIDATE_UTF8, &error_location)) {
+ ERROR("write_mongodb plugin: Error in generated BSON document "
+ "at byte %" PRIsz,
+ error_location);
+ bson_destroy(ret);
+ return NULL;
+ }
+
+ return ret;
+} /* }}} bson *wm_create_bson */
+
+static int wm_initialize(wm_node_t *node) /* {{{ */
+{
+ 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 = false;
+ return -1;
+ }
+
+ 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 = false;
+ sfree(uri);
+ return -1;
+ }
+ } else {
+ 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 = false;
+ return -1;
+ }
+
+ node->client = mongoc_client_new(uri);
+ if (!node->client) {
+ ERROR("write_mongodb plugin: Connecting to [%s]:%d failed.", node->host,
+ node->port);
+ node->connected = false;
+ sfree(uri);
+ return -1;