diff --git a/src/write_mongodb.c b/src/write_mongodb.c
index f13cdc332b5155cbb9a0893cf87345d79921cacf..24151ceb720a3a29a055a0a24e7cdc6993661d45 100644 (file)
--- a/src/write_mongodb.c
+++ b/src/write_mongodb.c
/**
* collectd - src/write_mongodb.c
/**
* collectd - src/write_mongodb.c
- * Copyright (C) 2010 Florian Forster, Akkarit Sangpetch
+ * Copyright (C) 2010-2013 Florian Forster
+ * Copyright (C) 2010 Akkarit Sangpetch
+ * Copyright (C) 2012 Chris Lundquist
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* DEALINGS IN THE SOFTWARE.
*
* Authors:
* DEALINGS IN THE SOFTWARE.
*
* Authors:
- * Florian Forster <ff at octo.it>
- * Akkarit Sangpetch <asangpet@andrew.cmu.edu>
+ * Florian Forster <octo at collectd.org>
+ * Akkarit Sangpetch <asangpet at andrew.cmu.edu>
+ * Chris Lundquist <clundquist at bluebox.net>
**/
#include "collectd.h"
#include "plugin.h"
#include "common.h"
#include "configfile.h"
**/
#include "collectd.h"
#include "plugin.h"
#include "common.h"
#include "configfile.h"
+#include "utils_cache.h"
#include <pthread.h>
#include <pthread.h>
#endif
#include <mongo.h>
#endif
#include <mongo.h>
+#if (MONGO_MAJOR == 0) && (MONGO_MINOR < 8)
+# define bson_alloc() bson_create()
+# define bson_dealloc(b) bson_dispose(b)
+#endif
+
struct wm_node_s
{
char name[DATA_MAX_NAME_LEN];
struct wm_node_s
{
char name[DATA_MAX_NAME_LEN];
int port;
int timeout;
int port;
int timeout;
- int connected;
+ /* Authentication information */
+ char *db;
+ char *user;
+ char *passwd;
+
+ _Bool store_rates;
mongo conn[1];
pthread_mutex_t lock;
mongo conn[1];
pthread_mutex_t lock;
/*
* Functions
*/
/*
* Functions
*/
-static int wm_write (const data_set_t *ds, /* {{{ */
+static bson *wm_create_bson (const data_set_t *ds, /* {{{ */
const value_list_t *vl,
const value_list_t *vl,
- user_data_t *ud)
+ _Bool store_rates)
{
{
- wm_node_t *node = ud->data;
- char collection_name[512];
- int status;
+ bson *ret;
+ gauge_t *rates;
int i;
int i;
- bson record;
- ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
+ ret = bson_alloc (); /* matched by bson_dealloc() */
+ if (ret == NULL)
+ {
+ ERROR ("write_mongodb plugin: bson_create failed.");
+ return (NULL);
+ }
+
+ if (store_rates)
+ {
+ rates = uc_get_rate (ds, vl);
+ if (rates == NULL)
+ {
+ ERROR ("write_mongodb plugin: uc_get_rate() failed.");
+ return (NULL);
+ }
+ }
+ else
+ {
+ rates = NULL;
+ }
- bson_init(&record);
- bson_append_time_t(&record,"ts",CDTIME_T_TO_TIME_T(vl->time));
- bson_append_string(&record,"h",vl->host);
- bson_append_string(&record,"i",vl->plugin_instance);
- bson_append_string(&record,"t",vl->type);
- bson_append_string(&record,"ti",vl->type_instance);
+ bson_init (ret); /* matched by bson_destroy() */
+ bson_append_date (ret, "time", (bson_date_t) CDTIME_T_TO_MS (vl->time));
+ bson_append_string (ret, "host", vl->host);
+ bson_append_string (ret, "plugin", vl->plugin);
+ bson_append_string (ret, "plugin_instance", vl->plugin_instance);
+ bson_append_string (ret, "type", vl->type);
+ bson_append_string (ret, "type_instance", vl->type_instance);
+ bson_append_start_array (ret, "values"); /* {{{ */
for (i = 0; i < ds->ds_num; i++)
{
for (i = 0; i < ds->ds_num; i++)
{
- if (ds->ds[i].type == DS_TYPE_COUNTER)
- bson_append_long(&record, ds->ds[i].name, vl->values[i].counter);
- else if (ds->ds[i].type == DS_TYPE_GAUGE)
- bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
+ char key[16];
+
+ ssnprintf (key, sizeof (key), "%i", i);
+
+ if (ds->ds[i].type == DS_TYPE_GAUGE)
+ bson_append_double(ret, key, vl->values[i].gauge);
+ else if (store_rates)
+ bson_append_double(ret, key, (double) rates[i]);
+ else if (ds->ds[i].type == DS_TYPE_COUNTER)
+ bson_append_long(ret, key, vl->values[i].counter);
else if (ds->ds[i].type == DS_TYPE_DERIVE)
else if (ds->ds[i].type == DS_TYPE_DERIVE)
- bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
+ bson_append_long(ret, key, vl->values[i].derive);
else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
- bson_append_long(&record, ds->ds[i].name, vl->values[i].absolute);
+ bson_append_long(ret, key, vl->values[i].absolute);
else
assert (23 == 42);
}
else
assert (23 == 42);
}
- bson_finish(&record);
+ bson_append_finish_array (ret); /* }}} values */
- pthread_mutex_lock (&node->lock);
+ bson_append_start_array (ret, "dstypes"); /* {{{ */
+ for (i = 0; i < ds->ds_num; i++)
+ {
+ char key[16];
+
+ ssnprintf (key, sizeof (key), "%i", i);
- if (node->connected == 0)
+ if (store_rates)
+ bson_append_string (ret, key, "gauge");
+ else
+ bson_append_string (ret, key, DS_TYPE_TO_STRING (ds->ds[i].type));
+ }
+ bson_append_finish_array (ret); /* }}} dstypes */
+
+ bson_append_start_array (ret, "dsnames"); /* {{{ */
+ for (i = 0; i < ds->ds_num; i++)
{
{
-/*
- sstrncpy(node->opts->host, node->host,
- sizeof (node->opts->host));
- node->opts->port = node->port;
-*/
+ char key[16];
+
+ ssnprintf (key, sizeof (key), "%i", i);
+ bson_append_string (ret, key, ds->ds[i].name);
+ }
+ bson_append_finish_array (ret); /* }}} dsnames */
+
+ bson_finish (ret);
- /* status = mongo_connect(node->conn,node->opts->host, node->opts->port);*/
- status = mongo_connect(node->conn, node->host, node->port);
+ sfree (rates);
+ return (ret);
+} /* }}} bson *wm_create_bson */
+
+static int wm_write (const data_set_t *ds, /* {{{ */
+ const value_list_t *vl,
+ user_data_t *ud)
+{
+ 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) {
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);
(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);
pthread_mutex_unlock (&node->lock);
return (-1);
- } else {
- node->connected = 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) {
+ WARNING ("write_mongodb plugin: mongo_set_op_timeout(%i) failed: %s",
+ node->timeout, node->conn->errstr);
+ }
}
}
/* Assert if the connection has been established */
}
}
/* Assert if the connection has been established */
- assert (node->connected == 1);
+ assert (mongo_is_connected (node->conn));
- DEBUG ( "write_mongodb plugin: writing record");
- /* bson_print(&record); */
+ #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
- status = mongo_insert(node->conn,collection_name,&record);
-
- if(status != MONGO_OK)
+ if (status != MONGO_OK)
{
ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
{
ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
- if(node->conn->err == MONGO_BSON_INVALID)
- {
- ERROR (node->conn->errstr);
- } else if ( record.err)
- {
- ERROR (record.errstr);
- }
+ if (node->conn->err != MONGO_BSON_INVALID)
+ ERROR ("write_mongodb plugin: %s", node->conn->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);
pthread_mutex_unlock (&node->lock);
+ /* free our resource as not to leak memory */
+ bson_destroy (bson_record); /* matches bson_init() */
+ bson_dealloc (bson_record); /* matches bson_alloc() */
+
return (0);
} /* }}} int wm_write */
return (0);
} /* }}} int wm_write */
if (node == NULL)
return;
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);
sfree (node->host);
sfree (node);
if (node == NULL)
return (ENOMEM);
memset (node, 0, sizeof (*node));
if (node == NULL)
return (ENOMEM);
memset (node, 0, sizeof (*node));
+ mongo_init (node->conn);
node->host = NULL;
node->host = NULL;
- node->port = 0;
- node->timeout = 1000;
- node->connected = 0;
+ node->store_rates = 1;
pthread_mutex_init (&node->lock, /* attr = */ NULL);
status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
pthread_mutex_init (&node->lock, /* attr = */ NULL);
status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
}
else if (strcasecmp ("Timeout", child->key) == 0)
status = cf_util_get_int (child, &node->timeout);
}
else if (strcasecmp ("Timeout", child->key) == 0)
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);
else
WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
child->key);
break;
} /* for (i = 0; i < ci->children_num; i++) */
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];
if (status == 0)
{
char cb_name[DATA_MAX_NAME_LEN];