Code

Moved sdb_strerror() from utils/string.h to utils/error.h.
[sysdb.git] / src / backend / puppet-storeconfigs.c
index 0d02ba0ecb70ed52de2cd55d3b9aafca0a250b60..5549f777f07967e1b2129021463b6734e7474daa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * syscollector - src/backend/puppet-storeconfigs.c
+ * SysDB - src/backend/puppet-storeconfigs.c
  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
  * All rights reserved.
  *
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "syscollector.h"
+#include "sysdb.h"
 #include "core/plugin.h"
 #include "core/store.h"
+#include "utils/error.h"
 #include "utils/dbi.h"
-#include "utils/string.h"
 
 #include "liboconfig/utils.h"
 
 #include <string.h>
 #include <strings.h>
 
-SC_PLUGIN_MAGIC;
+SDB_PLUGIN_MAGIC;
 
 /*
  * private helper functions
  */
 
 static int
-sc_puppet_stcfg_get_data(sc_dbi_client_t __attribute__((unused)) *client,
-               size_t n, sc_dbi_data_t *data)
+sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
+               size_t n, sdb_data_t *data,
+               sdb_object_t __attribute__((unused)) *user_data)
 {
-       sc_host_t host = SC_HOST_INIT;
+       sdb_host_t host = SDB_HOST_INIT;
 
        int status;
 
        assert(n == 2);
-       assert((data[0].type == DBI_TYPE_STRING)
-                       && (data[1].type == DBI_TYPE_DATETIME));
+       assert((data[0].type == SDB_TYPE_STRING)
+                       && (data[1].type == SDB_TYPE_DATETIME));
 
        host.host_name = strdup(data[0].data.string);
        host.host_last_update = data[1].data.datetime;
 
-       status = sc_store_host(&host);
+       status = sdb_store_host(&host);
 
        if (status < 0) {
-               fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
-                               "host '%s'.\n", host.host_name);
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
+                               "store/update host '%s'.\n", host.host_name);
                free(host.host_name);
                return -1;
        }
        else if (! status)
-               fprintf(stderr, "puppet storeconfigs backend: Added/updated host '%s' "
-                               "(last update timestamp = %"PRIscTIME").\n",
-                               host.host_name, host.host_last_update);
+               sdb_log(SDB_LOG_DEBUG, "puppet storeconfigs backend: "
+                               "Added/updated host '%s' (last update timestamp = "
+                               "%"PRIscTIME").\n", host.host_name, host.host_last_update);
        free(host.host_name);
        return 0;
-} /* sc_puppet_stcfg_get_data */
+} /* sdb_puppet_stcfg_get_hosts */
+
+static int
+sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
+               size_t n, sdb_data_t *data,
+               sdb_object_t __attribute__((unused)) *user_data)
+{
+       sdb_attribute_t attr = SDB_ATTR_INIT;
+
+       int status;
+
+       assert(n == 4);
+       assert((data[0].type == SDB_TYPE_STRING)
+                       && (data[1].type == SDB_TYPE_STRING)
+                       && (data[2].type == SDB_TYPE_STRING)
+                       && (data[3].type == SDB_TYPE_DATETIME));
+
+       attr.hostname = strdup(data[0].data.string);
+       attr.attr_name = strdup(data[1].data.string);
+       attr.attr_value = strdup(data[2].data.string);
+       attr.attr_last_update = data[3].data.datetime;
+
+       status = sdb_store_attribute(&attr);
+
+       if (status < 0) {
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
+                               "store/update host attribute '%s' for host '%s'.\n",
+                               attr.attr_name, attr.hostname);
+               free(attr.hostname);
+               free(attr.attr_name);
+               free(attr.attr_value);
+               return -1;
+       }
+
+       free(attr.hostname);
+       free(attr.attr_name);
+       free(attr.attr_value);
+       return 0;
+} /* sdb_puppet_stcfg_get_attrs */
 
 /*
  * plugin API
  */
 
 static int
-sc_puppet_stcfg_init(sc_object_t *user_data)
+sdb_puppet_stcfg_init(sdb_object_t *user_data)
 {
-       sc_dbi_client_t *client;
+       sdb_dbi_client_t *client;
 
        if (! user_data)
                return -1;
 
-       client = SC_OBJ_WRAPPER(user_data)->data;
-       if (sc_dbi_client_connect(client)) {
-               fprintf(stderr, "puppet storeconfigs backend: "
+       client = SDB_OBJ_WRAPPER(user_data)->data;
+       if (sdb_dbi_client_connect(client)) {
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
                                "Failed to connect to the storeconfigs DB.\n");
                return -1;
        }
 
-       fprintf(stderr, "puppet storeconfigs backend: Successfully "
+       sdb_log(SDB_LOG_INFO, "puppet storeconfigs backend: Successfully "
                        "connected to the storeconfigs DB.\n");
        return 0;
-} /* sc_collectd_init */
+} /* sdb_puppet_stcfg_init */
 
 static int
-sc_puppet_stcfg_collect(sc_object_t *user_data)
+sdb_puppet_stcfg_collect(sdb_object_t *user_data)
 {
-       sc_dbi_client_t *client;
+       sdb_dbi_client_t *client;
 
        if (! user_data)
                return -1;
 
-       client = SC_OBJ_WRAPPER(user_data)->data;
-       if (sc_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
-                               sc_puppet_stcfg_get_data, /* #columns = */ 2,
-                               /* col types = */ DBI_TYPE_STRING, DBI_TYPE_DATETIME)) {
-               fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
-                               "hosts from the storeconfigs DB.\n");
+       client = SDB_OBJ_WRAPPER(user_data)->data;
+       if (sdb_dbi_client_check_conn(client)) {
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                               "Connection to storeconfigs DB failed.\n");
+               return -1;
+       }
+
+       if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
+                               sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
+                               /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
+                               "retrieve hosts from the storeconfigs DB.\n");
+               return -1;
+       }
+
+       if (sdb_dbi_exec_query(client, "SELECT "
+                                       "hosts.name AS hostname, "
+                                       "fact_names.name AS name, "
+                                       "fact_values.value AS value, "
+                                       "fact_values.updated_at AS updated_at "
+                               "FROM fact_values "
+                               "INNER JOIN hosts "
+                                       "ON fact_values.host_id = hosts.id "
+                               "INNER JOIN fact_names "
+                                       "ON fact_values.fact_name_id = fact_names.id;",
+                               sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
+                               /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
+                               SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
+                               "retrieve host attributes from the storeconfigs DB.\n");
                return -1;
        }
        return 0;
-} /* sc_collectd_collect */
+} /* sdb_collectd_collect */
 
 static int
-sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
+sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
 {
        char *name = NULL;
        char cb_name[1024];
 
-       sc_object_t *user_data;
-       sc_dbi_client_t *client;
-       sc_dbi_options_t *options = NULL;
+       sdb_object_t *user_data;
+       sdb_dbi_client_t *client;
+       sdb_dbi_options_t *options = NULL;
 
        char *driver = NULL;
        char *database = NULL;
@@ -137,8 +200,9 @@ sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
        int i;
 
        if (oconfig_get_string(ci, &name)) {
-               fprintf(stderr, "puppet storeconfigs backend: Connection requires a "
-                               "single string argument\n\tUsage: <Connection NAME>\n");
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: Connection "
+                               "requires a single string argument\n"
+                               "\tUsage: <Connection NAME>\n");
                return -1;
        }
 
@@ -150,8 +214,8 @@ sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
 
                if (! strcasecmp(child->key, "DBAdapter")) {
                        if (oconfig_get_string(child, &driver)) {
-                               fprintf(stderr, "puppet storeconfigs backend: DBAdapter "
-                                               "requires a single string argument inside "
+                               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                               "DBAdapter requires a single string argument inside "
                                                "<Connection %s>\n\tUsage: DBAdapter NAME\n",
                                                name);
                        }
@@ -159,8 +223,8 @@ sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
                }
                else if (! strcasecmp(child->key, "DBName")) {
                        if (oconfig_get_string(child, &database)) {
-                               fprintf(stderr, "puppet storeconfigs backend: DBName"
-                                               "requires a single string argument inside "
+                               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                               "DBName requires a single string argument inside "
                                                "<Connection %s>\n\tUsage: DBName NAME\n",
                                                name);
                        }
@@ -186,10 +250,10 @@ sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
                        if ((child->values_num != 2)
                                        || (child->values[0].type != OCONFIG_TYPE_STRING)
                                        || (child->values[1].type != OCONFIG_TYPE_STRING)) {
-                               fprintf(stderr, "puppet storeconfigs backend: DBIOption "
-                                               "requires exactly two string arguments inside "
-                                               "<Connection %s>\n\tUsage: DBIOption KEY VALUE\n",
-                                               name);
+                               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                               "DBIOption requires exactly two string arguments "
+                                               "inside <Connection %s>\n"
+                                               "\tUsage: DBIOption KEY VALUE\n", name);
                                continue;
                        }
 
@@ -198,15 +262,15 @@ sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
                        value = child->values[1].value.string;
                }
                else {
-                       fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
-                                       "config option '%s' inside <Connection %s>.\n",
-                                       child->key, name);
+                       sdb_log(SDB_LOG_WARNING, "puppet storeconfigs backend: "
+                                       "Ignoring unknown config option '%s' inside "
+                                       "<Connection %s>.\n", child->key, name);
                        continue;
                }
 
                if (status) {
-                       fprintf(stderr, "puppet storeconfigs backend: Option '%s' "
-                                       "requires a single string argument inside "
+                       sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: Option "
+                                       "'%s' requires a single string argument inside "
                                        "<Connection %s>\n\tUsage: DBAdapter NAME\n",
                                        child->key, name);
                        continue;
@@ -215,69 +279,70 @@ sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
                assert(key && value);
 
                if (! options) {
-                       if (! (options = sc_dbi_options_create())) {
+                       if (! (options = sdb_dbi_options_create())) {
                                char errmsg[1024];
-                               fprintf(stderr, "puppet storeconfigs backend: Failed to "
-                                               "create DBI options object: %s\n",
-                                               sc_strerror(errno, errmsg, sizeof(errmsg)));
+                               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                               "Failed to create DBI options object: %s\n",
+                                               sdb_strerror(errno, errmsg, sizeof(errmsg)));
                                continue;
                        }
                }
 
-               if (sc_dbi_options_add(options, key, value)) {
+               if (sdb_dbi_options_add(options, key, value)) {
                        char errmsg[1024];
-                       fprintf(stderr, "puppet storeconfigs backend: Failed to add "
-                                       "option '%s': %s\n", key,
-                                       sc_strerror(errno, errmsg, sizeof(errmsg)));
+                       sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                       "Failed to add option '%s': %s\n", key,
+                                       sdb_strerror(errno, errmsg, sizeof(errmsg)));
                        continue;
                }
        }
 
        if (! driver) {
-               fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
-                               "missing the 'DBAdapter' option.\n", name);
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                               "Connection '%s' " "missing the 'DBAdapter' option.\n",
+                               name);
                return -1;
        }
        if (! database) {
-               fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
-                               "missing the 'DBName' option.\n", name);
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                               "Connection '%s' missing the 'DBName' option.\n", name);
                return -1;
        }
 
        snprintf(cb_name, sizeof(cb_name), "puppet-storeconfigs-%s", name);
        cb_name[sizeof(cb_name) - 1] = '\0';
 
-       client = sc_dbi_client_create(driver, database);
+       client = sdb_dbi_client_create(driver, database);
        if (! client) {
                char errbuf[1024];
-               fprintf(stderr, "puppet storeconfigs backend: "
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
                                "Failed to create DBI client: %s\n",
-                               sc_strerror(errno, errbuf, sizeof(errbuf)));
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
        }
 
-       sc_dbi_client_set_options(client, options);
+       sdb_dbi_client_set_options(client, options);
 
-       user_data = sc_object_create_wrapper(client,
-                       (void (*)(void *))sc_dbi_client_destroy);
+       user_data = sdb_object_create_wrapper(client,
+                       (void (*)(void *))sdb_dbi_client_destroy);
        if (! user_data) {
-               sc_dbi_client_destroy(client);
-               fprintf(stderr, "puppet storeconfigs backend: "
-                               "Failed to allocate sc_object_t\n");
+               sdb_dbi_client_destroy(client);
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                               "Failed to allocate sdb_object_t\n");
                return -1;
        }
 
-       sc_plugin_register_init(cb_name, sc_puppet_stcfg_init, user_data);
-       sc_plugin_register_collector(cb_name, sc_puppet_stcfg_collect,
+       sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
+       sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
                        /* interval */ NULL, user_data);
 
        /* pass control to the list */
-       sc_object_deref(user_data);
+       sdb_object_deref(user_data);
        return 0;
-} /* sc_puppet_stcfg_config_conn */
+} /* sdb_puppet_stcfg_config_conn */
 
 static int
-sc_puppet_stcfg_config(oconfig_item_t *ci)
+sdb_puppet_stcfg_config(oconfig_item_t *ci)
 {
        int i;
 
@@ -285,35 +350,37 @@ sc_puppet_stcfg_config(oconfig_item_t *ci)
                oconfig_item_t *child = ci->children + i;
 
                if (! strcasecmp(child->key, "Connection"))
-                       sc_puppet_stcfg_config_conn(child);
+                       sdb_puppet_stcfg_config_conn(child);
                else
-                       fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
-                                       "config option '%s'.\n", child->key);
+                       sdb_log(SDB_LOG_WARNING, "puppet storeconfigs backend: "
+                                       "Ignoring unknown config option '%s'.\n", child->key);
        }
        return 0;
-} /* sc_puppet_stcfg_config */
+} /* sdb_puppet_stcfg_config */
 
 int
-sc_module_init(sc_plugin_info_t *info)
+sdb_module_init(sdb_plugin_info_t *info)
 {
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "puppet-storeconfigs");
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet-storeconfigs");
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
                        "backend accessing the Puppet stored configuration database");
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
                        "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
 
        if (dbi_initialize(/* driver dir = */ NULL) < 0) {
-               fprintf(stderr, "puppet storeconfigs backend: failed to initialize "
-                               "DBI; possibly you don't have any drivers installed.\n");
+               sdb_log(SDB_LOG_ERR, "puppet storeconfigs backend: failed to "
+                               "initialize DBI; possibly you don't have any drivers "
+                               "installed.\n");
                return -1;
        }
 
-       sc_plugin_register_config("puppet-storeconfigs", sc_puppet_stcfg_config);
+       sdb_plugin_register_config("puppet-storeconfigs",
+                       sdb_puppet_stcfg_config);
        return 0;
-} /* sc_version_extra */
+} /* sdb_version_extra */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */