/**
* collectd - src/write_mongodb.c
- * Copyright (C) 2010 Florian Forster, Akkarit Sangpetch
+ * Copyright (C) 2010-2012 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"),
*
* Authors:
* Florian Forster <ff at octo.it>
- * Akkarit Sangpetch <asangpet@andrew.cmu.edu>
+ * 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 "utils_cache.h"
#include <pthread.h>
int connected;
+ _Bool store_rates;
+
mongo conn[1];
pthread_mutex_t lock;
};
int i;
bson record;
+ gauge_t *rates = NULL;
+ if (node->store_rates)
+ {
+ rates = uc_get_rate (ds, vl);
+ if (rates == NULL)
+ {
+ ERROR ("write_mongodb plugin: uc_get_rate() failed.");
+ return (-1);
+ }
+ }
+
ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
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_append_double (&record, "time", CDTIME_T_TO_DOUBLE (vl->time));
+ bson_append_string (&record, "host", vl->host);
+ bson_append_string (&record, "plugin_instance", vl->plugin_instance);
+ bson_append_string (&record, "type", vl->type);
+ bson_append_string (&record, "type_instance", vl->type_instance);
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)
+ if (ds->ds[i].type == DS_TYPE_GAUGE)
bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
+ else if (node->store_rates)
+ bson_append_double(&record, ds->ds[i].name, (double) rates[i]);
+ else 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_DERIVE)
bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
else
assert (23 == 42);
}
+ /* We must finish the record, other wise the insert will fail */
bson_finish(&record);
+ sfree (rates);
+
pthread_mutex_lock (&node->lock);
if (node->connected == 0)
if(status != MONGO_OK)
{
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 if (record.err)
+ ERROR ("write_mongodb plugin: %s", record.errstr);
}
-
pthread_mutex_unlock (&node->lock);
+ /* free our resource as not to leak memory */
+ bson_destroy(&record);
return (0);
} /* }}} int wm_write */
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));
}
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
WARNING ("write_mongodb plugin: Ignoring unknown config option \"%s\".",
child->key);