X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_mongodb.c;h=9a6fdf2697da4b52c7ba7d5fd5f8f39ed96c73c6;hb=d486225f89ea52d8ed2b4242eba2ad94c409f837;hp=4f866bc84fc9158a2a266518fe2942a63dfa52f1;hpb=47d9b018d30223078ea44dee5305dfa2225ce95b;p=collectd.git diff --git a/src/write_mongodb.c b/src/write_mongodb.c index 4f866bc8..9cddc916 100644 --- a/src/write_mongodb.c +++ b/src/write_mongodb.c @@ -1,8 +1,9 @@ /** * collectd - src/write_mongodb.c - * Copyright (C) 2010-2012 Florian Forster + * Copyright (C) 2010-2013 Florian Forster * Copyright (C) 2010 Akkarit Sangpetch * Copyright (C) 2012 Chris Lundquist + * Copyright (C) 2017 Saikrishna Arcot * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -23,39 +24,37 @@ * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian Forster + * Florian Forster * Akkarit Sangpetch * Chris Lundquist + * Saikrishna Arcot **/ #include "collectd.h" -#include "plugin.h" + #include "common.h" -#include "configfile.h" +#include "plugin.h" #include "utils_cache.h" -#include +#include -#if HAVE_STDINT_H -# define MONGO_HAVE_STDINT 1 -#else -# define MONGO_USE_LONG_LONG_INT 1 -#endif -#include - -struct wm_node_s -{ +struct wm_node_s { char name[DATA_MAX_NAME_LEN]; char *host; int port; int timeout; - int connected; + /* Authentication information */ + char *db; + char *user; + char *passwd; - _Bool store_rates; + bool store_rates; + bool connected; - mongo conn[1]; + mongoc_client_t *client; + mongoc_database_t *database; pthread_mutex_t lock; }; typedef struct wm_node_s wm_node_t; @@ -63,212 +62,345 @@ typedef struct wm_node_s wm_node_t; /* * Functions */ -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]; - int status; - 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); +static bson_t *wm_create_bson(const data_set_t *ds, /* {{{ */ + const value_list_t *vl, bool store_rates) { + bson_t *ret; + bson_t subarray; + gauge_t *rates; + + ret = bson_new(); + if (!ret) { + ERROR("write_mongodb plugin: bson_new failed."); + return NULL; + } + + if (store_rates) { + rates = uc_get_rate(ds, vl); + if (rates == NULL) { + ERROR("write_mongodb plugin: uc_get_rate() failed."); + bson_destroy(ret); + return NULL; } + } else { + rates = NULL; } - ssnprintf(collection_name, sizeof (collection_name), "collectd.%s", vl->plugin); + BSON_APPEND_DATE_TIME(ret, "timestamp", CDTIME_T_TO_MS(vl->time)); + BSON_APPEND_UTF8(ret, "host", vl->host); + BSON_APPEND_UTF8(ret, "plugin", vl->plugin); + BSON_APPEND_UTF8(ret, "plugin_instance", vl->plugin_instance); + BSON_APPEND_UTF8(ret, "type", vl->type); + BSON_APPEND_UTF8(ret, "type_instance", vl->type_instance); + + BSON_APPEND_ARRAY_BEGIN(ret, "values", &subarray); /* {{{ */ + for (size_t i = 0; i < ds->ds_num; i++) { + char key[16]; - 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); + snprintf(key, sizeof(key), "%" PRIsz, i); - for (i = 0; i < ds->ds_num; 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(&subarray, key, vl->values[i].gauge); + else if (store_rates) + BSON_APPEND_DOUBLE(&subarray, 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_INT64(&subarray, 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_INT64(&subarray, 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_INT64(&subarray, key, vl->values[i].absolute); + else { + ERROR("write_mongodb plugin: Unknown ds_type %d for index %" PRIsz, + ds->ds[i].type, i); + bson_destroy(ret); + return NULL; + } + } + bson_append_array_end(ret, &subarray); /* }}} values */ + + BSON_APPEND_ARRAY_BEGIN(ret, "dstypes", &subarray); /* {{{ */ + for (size_t i = 0; i < ds->ds_num; i++) { + char key[16]; + + snprintf(key, sizeof(key), "%" PRIsz, i); + + if (store_rates) + BSON_APPEND_UTF8(&subarray, key, "gauge"); else - assert (23 == 42); + BSON_APPEND_UTF8(&subarray, key, DS_TYPE_TO_STRING(ds->ds[i].type)); + } + bson_append_array_end(ret, &subarray); /* }}} dstypes */ + + BSON_APPEND_ARRAY_BEGIN(ret, "dsnames", &subarray); /* {{{ */ + for (size_t i = 0; i < ds->ds_num; i++) { + char key[16]; + + snprintf(key, sizeof(key), "%" PRIsz, i); + BSON_APPEND_UTF8(&subarray, key, ds->ds[i].name); + } + bson_append_array_end(ret, &subarray); /* }}} dsnames */ + + sfree(rates); + + size_t error_location; + if (!bson_validate(ret, BSON_VALIDATE_UTF8, &error_location)) { + ERROR("write_mongodb plugin: Error in generated BSON document " + "at byte %" PRIsz, + error_location); + bson_destroy(ret); + return NULL; } - /* 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) - { - 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 : MONGO_DEFAULT_PORT); - mongo_destroy(node->conn); - pthread_mutex_unlock (&node->lock); - return (-1); + + return ret; +} /* }}} bson *wm_create_bson */ + +static int wm_initialize(wm_node_t *node) /* {{{ */ +{ + char *uri; + + if (node->connected) + return 0; + + INFO("write_mongodb plugin: Connecting to [%s]:%d", node->host, node->port); + + if ((node->db != NULL) && (node->user != NULL) && (node->passwd != NULL)) { + uri = ssnprintf_alloc("mongodb://%s:%s@%s:%d/?authSource=%s", node->user, + node->passwd, node->host, node->port, node->db); + if (uri == NULL) { + ERROR("write_mongodb plugin: Not enough memory to assemble " + "authentication string."); + mongoc_client_destroy(node->client); + node->client = NULL; + node->connected = false; + return -1; } - if (node->timeout > 0) { - 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); - } + node->client = mongoc_client_new(uri); + if (!node->client) { + ERROR("write_mongodb plugin: Authenticating to [%s]:%d for database " + "\"%s\" as user \"%s\" failed.", + node->host, node->port, node->db, node->user); + node->connected = false; + sfree(uri); + return -1; + } + } else { + uri = ssnprintf_alloc("mongodb://%s:%d", node->host, node->port); + if (uri == NULL) { + ERROR("write_mongodb plugin: Not enough memory to assemble " + "authentication string."); + mongoc_client_destroy(node->client); + node->client = NULL; + node->connected = false; + return -1; } - node->connected = 1; + node->client = mongoc_client_new(uri); + if (!node->client) { + ERROR("write_mongodb plugin: Connecting to [%s]:%d failed.", node->host, + node->port); + node->connected = false; + sfree(uri); + return -1; + } + sfree(uri); + } + + node->database = mongoc_client_get_database(node->client, "collectd"); + if (!node->database) { + ERROR("write_mongodb plugin: error creating/getting database"); + mongoc_client_destroy(node->client); + node->client = NULL; + node->connected = false; + return -1; } - /* Assert if the connection has been established */ - assert (node->connected == 1); + node->connected = true; + return 0; +} /* }}} int wm_initialize */ - DEBUG ( "write_mongodb plugin: writing record"); - /* bson_print(&record); */ +static int wm_write(const data_set_t *ds, /* {{{ */ + const value_list_t *vl, user_data_t *ud) { + wm_node_t *node = ud->data; + mongoc_collection_t *collection = NULL; + bson_t *bson_record; + bson_error_t error; + int status; + + bson_record = wm_create_bson(ds, vl, node->store_rates); + if (!bson_record) { + ERROR("write_mongodb plugin: error making insert bson"); + return -1; + } + + pthread_mutex_lock(&node->lock); + if (wm_initialize(node) < 0) { + ERROR("write_mongodb plugin: error making connection to server"); + pthread_mutex_unlock(&node->lock); + bson_destroy(bson_record); + return -1; + } - status = mongo_insert(node->conn,collection_name,&record); + collection = + mongoc_client_get_collection(node->client, "collectd", vl->plugin); + if (!collection) { + ERROR("write_mongodb plugin: error creating/getting collection"); + mongoc_database_destroy(node->database); + mongoc_client_destroy(node->client); + node->database = NULL; + node->client = NULL; + node->connected = false; + pthread_mutex_unlock(&node->lock); + bson_destroy(bson_record); + return -1; + } - 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); + status = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, bson_record, + NULL, &error); + + if (!status) { + ERROR("write_mongodb plugin: error inserting record: %s", error.message); + mongoc_database_destroy(node->database); + mongoc_client_destroy(node->client); + node->database = NULL; + node->client = NULL; + node->connected = false; + pthread_mutex_unlock(&node->lock); + bson_destroy(bson_record); + mongoc_collection_destroy(collection); + return -1; } - pthread_mutex_unlock (&node->lock); /* free our resource as not to leak memory */ - bson_destroy(&record); + mongoc_collection_destroy(collection); + + pthread_mutex_unlock(&node->lock); + + bson_destroy(bson_record); - return (0); + 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 (node->connected != 0) - { - mongo_destroy(node->conn); - node->connected = 0; - } + mongoc_database_destroy(node->database); + mongoc_client_destroy(node->client); + node->database = NULL; + node->client = NULL; + node->connected = false; - 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; - int i; - node = malloc (sizeof (*node)); + node = calloc(1, sizeof(*node)); if (node == NULL) - return (ENOMEM); - memset (node, 0, sizeof (*node)); - node->host = NULL; - node->store_rates = 1; - pthread_mutex_init (&node->lock, /* attr = */ NULL); + return ENOMEM; + mongoc_init(); + node->host = strdup("localhost"); + if (node->host == NULL) { + sfree(node); + return ENOMEM; + } + node->port = MONGOC_DEFAULT_PORT; + node->store_rates = true; + 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); - return (status); + if (status != 0) { + sfree(node->host); + sfree(node); + return status; } - for (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("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 (status == 0) - { - char cb_name[DATA_MAX_NAME_LEN]; - user_data_t ud; + 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); + } + } - ssnprintf (cb_name, sizeof (cb_name), "write_mongodb/%s", node->name); + if (status == 0) { + char cb_name[sizeof("write_mongodb/") + DATA_MAX_NAME_LEN]; - ud.data = node; - ud.free_func = wm_config_free; + snprintf(cb_name, sizeof(cb_name), "write_mongodb/%s", node->name); - 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); + return status; } /* }}} int wm_config_node */ -static int wm_config (oconfig_item_t *ci) /* {{{ */ +static int wm_config(oconfig_item_t *ci) /* {{{ */ { - int i; - - for (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); + 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 : */