/*
* 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,
- 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;
- bson record;
- gauge_t *rates = NULL;
- if (node->store_rates)
+ ret = bson_create ();
+ 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 (-1);
+ return (NULL);
}
}
+ else
+ {
+ rates = NULL;
+ }
- ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
-
- bson_init(&record);
- bson_append_date (&record, "time", (bson_date_t) CDTIME_T_TO_MS (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);
+ bson_init (ret);
+ 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++)
{
+ char key[16];
+
+ ssnprintf (key, sizeof (key), "%i", i);
+
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]);
+ 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(&record, ds->ds[i].name, vl->values[i].counter);
+ bson_append_long(ret, key, 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);
+ bson_append_long(ret, key, vl->values[i].derive);
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);
}
- /* We must finish the record, other wise the insert will fail */
- bson_finish(&record);
+ bson_append_finish_array (ret); /* }}} values */
+
+ bson_append_start_array (ret, "dstypes"); /* {{{ */
+ for (i = 0; i < ds->ds_num; i++)
+ {
+ char key[16];
+
+ ssnprintf (key, sizeof (key), "%i", i);
+
+ 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++)
+ {
+ 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);
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);
/* Assert if the connection has been established */
assert (node->connected == 1);
- DEBUG ( "write_mongodb plugin: writing record");
- /* bson_print(&record); */
-
- status = mongo_insert(node->conn,collection_name,&record);
-
+ status = mongo_insert (node->conn, collection_name, bson_record);
if(status != MONGO_OK)
{
ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
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);
+ else if (bson_record->err)
+ ERROR ("write_mongodb plugin: %s", bson_record->errstr);
}
pthread_mutex_unlock (&node->lock);
+
/* free our resource as not to leak memory */
- bson_destroy(&record);
+ bson_dispose (bson_record);
return (0);
} /* }}} int wm_write */