We have to convert CDTime to seconds since epoch
[collectd.git] / src / write_mongodb.c
index 900fb87..f13cdc3 100644 (file)
 #endif
 #include <mongo.h>
 
-/*
-struct mongo_options
-{
-  char *host;
-  int port;
-  int timeout;
-};
-typedef struct mongo_options mongo_options;
-*/
-
 struct wm_node_s
 {
   char name[DATA_MAX_NAME_LEN];
@@ -60,7 +50,6 @@ struct wm_node_s
   int connected;
 
   mongo conn[1];
-/*  mongo_options opts[1]; */
   pthread_mutex_t lock;
 };
 typedef struct wm_node_s wm_node_t;
@@ -77,12 +66,11 @@ static int wm_write (const data_set_t *ds, /* {{{ */
   int status;
   int i;
   bson record;
-  /*bson_data record_buf; */
 
   ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
 
   bson_init(&record);
-  bson_append_time_t(&record,"ts",vl->time);
+  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);
@@ -101,7 +89,7 @@ static int wm_write (const data_set_t *ds, /* {{{ */
     else
       assert (23 == 42);
   }
-
+  bson_finish(&record);
 
   pthread_mutex_lock (&node->lock);
 
@@ -137,11 +125,15 @@ static int wm_write (const data_set_t *ds, /* {{{ */
 
   if(status != MONGO_OK)
   {
-    ERROR ( "write_mongodb plugin: error inserting record: ");
-    if(node->conn->err == MONGO_BSON_INVALID) 
+    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);
     }
+
   }