src/plugin.c: Report an error if ctx.interval is not set.
[collectd.git] / src / write_mongodb.c
index 2b80921..4deb24d 100644 (file)
@@ -1,6 +1,8 @@
 /**
  * collectd - src/write_mongodb.c
- * Copyright (C) 2010  Florian Forster, Akkarit Sangpetch
+ * Copyright (C) 2010  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"),
@@ -22,7 +24,8 @@
  *
  * 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"
@@ -49,8 +52,7 @@ struct wm_node_s
 
   int connected;
 
-  mongo_connection conn[1];
-  mongo_connection_options opts[1];
+  mongo conn[1];
   pthread_mutex_t lock;
 };
 typedef struct wm_node_s wm_node_t;
@@ -67,46 +69,41 @@ static int wm_write (const data_set_t *ds, /* {{{ */
   int status;
   int i;
   bson record;
-  bson_buffer record_buf;
 
   ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin);
 
-  bson_buffer_init(&record_buf);
-  bson_append_time_t(&record_buf,"ts",vl->time);
-  bson_append_string(&record_buf,"h",vl->host);
-  bson_append_string(&record_buf,"i",vl->plugin_instance);
-  bson_append_string(&record_buf,"t",vl->type);
-  bson_append_string(&record_buf,"ti",vl->type_instance);
+  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);
 
   for (i = 0; i < ds->ds_num; i++)
   {
     if (ds->ds[i].type == DS_TYPE_COUNTER)
-      bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].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_buf, ds->ds[i].name, vl->values[i].gauge);
+      bson_append_double(&record, ds->ds[i].name, vl->values[i].gauge);
     else if (ds->ds[i].type == DS_TYPE_DERIVE)
-      bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].derive);
+      bson_append_long(&record, ds->ds[i].name, vl->values[i].derive);
     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
-      bson_append_long(&record_buf, ds->ds[i].name, vl->values[i].absolute);
+      bson_append_long(&record, ds->ds[i].name, vl->values[i].absolute);
     else
       assert (23 == 42);
   }
-
-  bson_from_buffer(&record,&record_buf);
+  /* We must finish the record, other wise the insert will fail */
+  bson_finish(&record);
 
   pthread_mutex_lock (&node->lock);
 
   if (node->connected == 0)
   {
-    sstrncpy(node->opts->host, node->host,
-        sizeof (node->opts->host));
-    node->opts->port = node->port;
-
-    status = mongo_connect(node->conn,node->opts);
-    if (status!=mongo_conn_success) {
+    status = mongo_connect(node->conn, node->host, node->port);
+    if (status != MONGO_OK) {
       ERROR ("write_mongodb plugin: Connecting to host \"%s\" (port %i) failed.",
           (node->host != NULL) ? node->host : "localhost",
-          (node->port != 0) ? node->port : 27017);
+          (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
       mongo_destroy(node->conn);
       pthread_mutex_unlock (&node->lock);
       return (-1);
@@ -118,11 +115,23 @@ static int wm_write (const data_set_t *ds, /* {{{ */
   /* Assert if the connection has been established */
   assert (node->connected == 1);
 
-  mongo_insert(node->conn,collection_name,&record);
+  DEBUG ( "write_mongodb plugin: writing record");
+  /* bson_print(&record); */
 
-  pthread_mutex_unlock (&node->lock);
+  status = mongo_insert(node->conn,collection_name,&record);
 
-  bson_buffer_destroy(&record_buf);
+  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);
+  }
+
+  pthread_mutex_unlock (&node->lock);
+  /* free our resource as not to leak memory */
+  bson_destroy(&record);
 
   return (0);
 } /* }}} int wm_write */