X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_mongodb.c;h=30e261c9eee919e582f1d4da06ff52aced103a56;hb=aee87d9c1665ca8823c7489bfc9900ff12e0e177;hp=9939c914e124e4c66a2868bd54453156c3b8e4f4;hpb=8c08b696bfab56a7040ac569ce1d1c7a0e126d14;p=collectd.git diff --git a/src/write_mongodb.c b/src/write_mongodb.c index 9939c914..30e261c9 100644 --- a/src/write_mongodb.c +++ b/src/write_mongodb.c @@ -1,6 +1,6 @@ /** * collectd - src/write_mongodb.c - * Copyright (C) 2010-2012 Florian Forster + * Copyright (C) 2010-2013 Florian Forster * Copyright (C) 2010 Akkarit Sangpetch * Copyright (C) 2012 Chris Lundquist * @@ -23,7 +23,7 @@ * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian Forster + * Florian Forster * Akkarit Sangpetch * Chris Lundquist **/ @@ -51,7 +51,10 @@ struct wm_node_s int port; int timeout; - int connected; + /* Authentication information */ + char *db; + char *user; + char *passwd; _Bool store_rates; @@ -170,18 +173,38 @@ static int wm_write (const data_set_t *ds, /* {{{ */ pthread_mutex_lock (&node->lock); - if (node->connected == 0) + if (!mongo_is_connected (node->conn)) { - status = mongo_connect(node->conn, node->host, node->port); + 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 host \"%s\" (port %i) failed.", + 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); + mongo_destroy (node->conn); pthread_mutex_unlock (&node->lock); return (-1); } + if ((node->db != NULL) && (node->user != NULL) && (node->passwd != NULL)) + { + status = mongo_cmd_authenticate (node->conn, + node->db, node->user, node->passwd); + if (status != MONGO_OK) + { + ERROR ("write_mongodb plugin: Authenticating to [%s]%i for database " + "\"%s\" as user \"%s\" failed.", + (node->host != NULL) ? node->host : "localhost", + (node->port != 0) ? node->port : MONGO_DEFAULT_PORT, + node->db, node->user); + mongo_destroy (node->conn); + pthread_mutex_unlock (&node->lock); + return (-1); + } + } + if (node->timeout > 0) { status = mongo_set_op_timeout (node->conn, node->timeout); if (status != MONGO_OK) { @@ -189,21 +212,32 @@ static int wm_write (const data_set_t *ds, /* {{{ */ node->timeout, node->conn->errstr); } } - - node->connected = 1; } /* Assert if the connection has been established */ - assert (node->connected == 1); + assert (mongo_is_connected (node->conn)); - status = mongo_insert (node->conn, collection_name, bson_record); - if(status != MONGO_OK) + #if MONGO_MINOR >= 6 + /* There was an API change in 0.6.0 as linked below */ + /* https://github.com/mongodb/mongo-c-driver/blob/master/HISTORY.md */ + status = mongo_insert (node->conn, collection_name, bson_record, NULL); + #else + status = mongo_insert (node->conn, collection_name, bson_record); + #endif + + if (status != MONGO_OK) { ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err); - if (node->conn->err == MONGO_BSON_INVALID) + if (node->conn->err != MONGO_BSON_INVALID) ERROR ("write_mongodb plugin: %s", node->conn->errstr); - else if (bson_record->err) - ERROR ("write_mongodb plugin: %s", bson_record->errstr); + else + ERROR ("write_mongodb plugin: Invalid BSON structure, error = %#x", + (unsigned int) bson_record->err); + + /* Disconnect except on data errors. */ + if ((node->conn->err != MONGO_BSON_INVALID) + && (node->conn->err != MONGO_BSON_NOT_FINISHED)) + mongo_destroy (node->conn); } pthread_mutex_unlock (&node->lock); @@ -221,11 +255,8 @@ static void wm_config_free (void *ptr) /* {{{ */ if (node == NULL) return; - if (node->connected != 0) - { - mongo_destroy(node->conn); - node->connected = 0; - } + if (mongo_is_connected (node->conn)) + mongo_destroy (node->conn); sfree (node->host); sfree (node); @@ -241,6 +272,7 @@ static int wm_config_node (oconfig_item_t *ci) /* {{{ */ if (node == NULL) return (ENOMEM); memset (node, 0, sizeof (*node)); + mongo_init (node->conn); node->host = NULL; node->store_rates = 1; pthread_mutex_init (&node->lock, /* attr = */ NULL); @@ -272,6 +304,12 @@ static int wm_config_node (oconfig_item_t *ci) /* {{{ */ status = cf_util_get_int (child, &node->timeout); else if (strcasecmp ("StoreRates", child->key) == 0) status = cf_util_get_boolean (child, &node->store_rates); + else if (strcasecmp ("Database", child->key) == 0) + status = cf_util_get_string (child, &node->db); + else if (strcasecmp ("User", child->key) == 0) + status = cf_util_get_string (child, &node->user); + else if (strcasecmp ("Password", child->key) == 0) + status = cf_util_get_string (child, &node->passwd); else WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".", child->key); @@ -280,6 +318,20 @@ static int wm_config_node (oconfig_item_t *ci) /* {{{ */ break; } /* for (i = 0; i < ci->children_num; i++) */ + if ((node->db != NULL) || (node->user != NULL) || (node->passwd != NULL)) + { + if ((node->db == NULL) || (node->user == NULL) || (node->passwd == NULL)) + { + WARNING ("write_mongodb plugin: Authentication requires the " + "\"Database\", \"User\" and \"Password\" options to be specified, " + "but at last one of them is missing. Authentication will NOT be " + "used."); + sfree (node->db); + sfree (node->user); + sfree (node->passwd); + } + } + if (status == 0) { char cb_name[DATA_MAX_NAME_LEN];