More autoconf work
[collectd.git] / src / write_mongodb.c
index 01ce64a..68e1177 100644 (file)
 
 #include "collectd.h"
 
-#include "plugin.h"
 #include "common.h"
+#include "plugin.h"
 #include "utils_cache.h"
 
-#if HAVE_STDINT_H
-# define MONGO_HAVE_STDINT 1
-#else
-# define MONGO_USE_LONG_LONG_INT 1
-#endif
+#define MONGO_HAVE_STDINT 1
 #include <mongo.h>
 
 #if (MONGO_MAJOR == 0) && (MONGO_MINOR < 8)
-# define bson_alloc()    bson_create()
-# define bson_dealloc(b) bson_dispose(b)
+#define bson_alloc() bson_create()
+#define bson_dealloc(b) bson_dispose(b)
 #endif
 
-struct wm_node_s
-{
+struct wm_node_s {
   char name[DATA_MAX_NAME_LEN];
 
   char *host;
@@ -69,53 +64,45 @@ typedef struct wm_node_s wm_node_t;
 /*
  * Functions
  */
-static bson *wm_create_bson (const data_set_t *ds, /* {{{ */
-    const value_list_t *vl,
-    _Bool store_rates)
-{
+static bson *wm_create_bson(const data_set_t *ds, /* {{{ */
+                            const value_list_t *vl, _Bool store_rates) {
   bson *ret;
   gauge_t *rates;
 
-  ret = bson_alloc (); /* matched by bson_dealloc() */
-  if (ret == NULL)
-  {
-    ERROR ("write_mongodb plugin: bson_create failed.");
+  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.");
+  if (store_rates) {
+    rates = uc_get_rate(ds, vl);
+    if (rates == NULL) {
+      ERROR("write_mongodb plugin: uc_get_rate() failed.");
       return (NULL);
     }
-  }
-  else
-  {
+  } else {
     rates = NULL;
   }
 
-  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 (int i = 0; i < ds->ds_num; i++)
-  {
+  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 (int i = 0; i < ds->ds_num; i++) {
     char key[16];
 
-    ssnprintf (key, sizeof (key), "%i", i);
+    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]);
+      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)
@@ -123,255 +110,239 @@ static bson *wm_create_bson (const data_set_t *ds, /* {{{ */
     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
       bson_append_long(ret, key, vl->values[i].absolute);
     else
-      assert (23 == 42);
+      assert(23 == 42);
   }
-  bson_append_finish_array (ret); /* }}} values */
+  bson_append_finish_array(ret); /* }}} values */
 
-  bson_append_start_array (ret, "dstypes"); /* {{{ */
-  for (int i = 0; i < ds->ds_num; i++)
-  {
+  bson_append_start_array(ret, "dstypes"); /* {{{ */
+  for (int i = 0; i < ds->ds_num; i++) {
     char key[16];
 
-    ssnprintf (key, sizeof (key), "%i", i);
+    ssnprintf(key, sizeof(key), "%i", i);
 
     if (store_rates)
-      bson_append_string (ret, key, "gauge");
+      bson_append_string(ret, key, "gauge");
     else
-      bson_append_string (ret, key, DS_TYPE_TO_STRING (ds->ds[i].type));
+      bson_append_string(ret, key, DS_TYPE_TO_STRING(ds->ds[i].type));
   }
-  bson_append_finish_array (ret); /* }}} dstypes */
+  bson_append_finish_array(ret); /* }}} dstypes */
 
-  bson_append_start_array (ret, "dsnames"); /* {{{ */
-  for (int i = 0; i < ds->ds_num; i++)
-  {
+  bson_append_start_array(ret, "dsnames"); /* {{{ */
+  for (int 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);
+    ssnprintf(key, sizeof(key), "%i", i);
+    bson_append_string(ret, key, ds->ds[i].name);
   }
-  bson_append_finish_array (ret); /* }}} dsnames */
+  bson_append_finish_array(ret); /* }}} dsnames */
 
-  bson_finish (ret);
+  bson_finish(ret);
 
-  sfree (rates);
+  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)
-{
+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);
+  ssnprintf(collection_name, sizeof(collection_name), "collectd.%s",
+            vl->plugin);
 
-  bson_record = wm_create_bson (ds, vl, node->store_rates);
+  bson_record = wm_create_bson(ds, vl, node->store_rates);
   if (bson_record == NULL)
     return (ENOMEM);
 
-  pthread_mutex_lock (&node->lock);
+  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 (!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) {
-      ERROR ("write_mongodb plugin: Connecting to [%s]:%i failed.",
-          (node->host != NULL) ? node->host : "localhost",
-          (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
-      mongo_destroy (node->conn);
-      pthread_mutex_unlock (&node->lock);
+      ERROR("write_mongodb plugin: Connecting to [%s]:%i failed.",
+            (node->host != NULL) ? node->host : "localhost",
+            (node->port != 0) ? node->port : MONGO_DEFAULT_PORT);
+      mongo_destroy(node->conn);
+      pthread_mutex_unlock(&node->lock);
       return (-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);
+    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);
+      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);
+        WARNING("write_mongodb plugin: mongo_set_op_timeout(%i) failed: %s",
+                node->timeout, node->conn->errstr);
       }
     }
   }
 
   /* Assert if the connection has been established */
-  assert (mongo_is_connected (node->conn));
-
-  #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
-
-  if (status != MONGO_OK)
-  {
-    ERROR ( "write_mongodb plugin: error inserting record: %d", node->conn->err);
+  assert(mongo_is_connected(node->conn));
+
+#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
+
+  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);
+      ERROR("write_mongodb plugin: %s", node->conn->errstr);
     else
-      ERROR ("write_mongodb plugin: Invalid BSON structure, error = %#x",
-          (unsigned int) bson_record->err);
+      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);
+    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() */
+  bson_destroy(bson_record); /* matches bson_init() */
+  bson_dealloc(bson_record); /* matches bson_alloc() */
 
   return (0);
 } /* }}} int wm_write */
 
-static void wm_config_free (void *ptr) /* {{{ */
+static void wm_config_free(void *ptr) /* {{{ */
 {
   wm_node_t *node = ptr;
 
   if (node == NULL)
     return;
 
-  if (mongo_is_connected (node->conn))
-    mongo_destroy (node->conn);
+  if (mongo_is_connected(node->conn))
+    mongo_destroy(node->conn);
 
-  sfree (node->host);
-  sfree (node);
+  sfree(node->host);
+  sfree(node);
 } /* }}} void wm_config_free */
 
-static int wm_config_node (oconfig_item_t *ci) /* {{{ */
+static int wm_config_node(oconfig_item_t *ci) /* {{{ */
 {
   wm_node_t *node;
   int status;
 
-  node = calloc (1, sizeof (*node));
+  node = calloc(1, sizeof(*node));
   if (node == NULL)
     return (ENOMEM);
-  mongo_init (node->conn);
+  mongo_init(node->conn);
   node->host = NULL;
   node->store_rates = 1;
-  pthread_mutex_init (&node->lock, /* attr = */ NULL);
+  pthread_mutex_init(&node->lock, /* attr = */ NULL);
 
-  status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name));
+  status = cf_util_get_string_buffer(ci, node->name, sizeof(node->name));
 
-  if (status != 0)
-  {
-    sfree (node);
+  if (status != 0) {
+    sfree(node);
     return (status);
   }
 
-  for (int i = 0; i < ci->children_num; i++)
-  {
+  for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
 
-    if (strcasecmp ("Host", child->key) == 0)
-      status = cf_util_get_string (child, &node->host);
-    else if (strcasecmp ("Port", child->key) == 0)
-    {
-      status = cf_util_get_port_number (child);
-      if (status > 0)
-      {
+    if (strcasecmp("Host", child->key) == 0)
+      status = cf_util_get_string(child, &node->host);
+    else if (strcasecmp("Port", child->key) == 0) {
+      status = cf_util_get_port_number(child);
+      if (status > 0) {
         node->port = status;
         status = 0;
       }
-    }
-    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 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);
+      WARNING("write_mongodb plugin: Ignoring unknown config option \"%s\".",
+              child->key);
 
     if (status != 0)
       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 "
+  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);
+      sfree(node->db);
+      sfree(node->user);
+      sfree(node->passwd);
     }
   }
 
-  if (status == 0)
-  {
+  if (status == 0) {
     char cb_name[DATA_MAX_NAME_LEN];
-    user_data_t ud;
 
-    ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name);
+    ssnprintf(cb_name, sizeof(cb_name), "write_mongodb/%s", node->name);
 
-    ud.data = node;
-    ud.free_func = wm_config_free;
-
-    status = plugin_register_write (cb_name, wm_write, &ud);
-    INFO ("write_mongodb plugin: registered write plugin %s %d",cb_name,status);
+    status = plugin_register_write(
+        cb_name, wm_write, &(user_data_t){
+                               .data = node, .free_func = wm_config_free,
+                           });
+    INFO("write_mongodb plugin: registered write plugin %s %d", cb_name,
+         status);
   }
 
   if (status != 0)
-    wm_config_free (node);
+    wm_config_free(node);
 
   return (status);
 } /* }}} int wm_config_node */
 
-static int wm_config (oconfig_item_t *ci) /* {{{ */
+static int wm_config(oconfig_item_t *ci) /* {{{ */
 {
-  for (int i = 0; i < ci->children_num; i++)
-  {
+  for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
 
-    if (strcasecmp ("Node", child->key) == 0)
-      wm_config_node (child);
+    if (strcasecmp("Node", child->key) == 0)
+      wm_config_node(child);
     else
-      WARNING ("write_mongodb plugin: Ignoring unknown "
-          "configuration option \"%s\" at top level.", child->key);
+      WARNING("write_mongodb plugin: Ignoring unknown "
+              "configuration option \"%s\" at top level.",
+              child->key);
   }
 
   return (0);
 } /* }}} int wm_config */
 
-void module_register (void)
-{
-  plugin_register_complex_config ("write_mongodb", wm_config);
+void module_register(void) {
+  plugin_register_complex_config("write_mongodb", wm_config);
 }
 
 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */