index 8b3804ba22e36144876efadc1440e1b3314bef40..23d261bacf46baf7b3939f5718fe529c714f98d7 100644 (file)
#include "sysdb.h"
#include "core/plugin.h"
#include "core/store.h"
-#include "core/error.h"
#include "utils/dbi.h"
+#include "utils/error.h"
#include "liboconfig/utils.h"
-#include <dbi/dbi.h>
-
#include <assert.h>
#include <errno.h>
size_t n, sdb_data_t *data,
sdb_object_t __attribute__((unused)) *user_data)
{
- sdb_host_t host = SDB_HOST_INIT;
+ const char *hostname;
+ sdb_time_t timestamp;
int status;
assert((data[0].type == SDB_TYPE_STRING)
&& (data[1].type == SDB_TYPE_DATETIME));
- host._name = strdup(data[0].data.string);
- host._last_update = data[1].data.datetime;
+ hostname = data[0].data.string;
+ timestamp = data[1].data.datetime;
- status = sdb_store_host(&host);
+ status = sdb_store_host(hostname, timestamp);
if (status < 0) {
sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
- "store/update host '%s'.", host._name);
- free(host._name);
+ "store/update host '%s'.", hostname);
return -1;
}
else if (! status)
sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: "
"Added/updated host '%s' (last update timestamp = "
- "%"PRIscTIME").", host._name, host._last_update);
- free(host._name);
+ "%"PRIscTIME").", hostname, timestamp);
return 0;
} /* sdb_puppet_stcfg_get_hosts */
size_t n, sdb_data_t *data,
sdb_object_t __attribute__((unused)) *user_data)
{
- sdb_attribute_t attr = SDB_ATTR_INIT;
-
int status;
+ const char *hostname;
+ const char *key;
+ sdb_data_t value;
+ sdb_time_t last_update;
+
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._name = strdup(data[1].data.string);
- attr.attr_value = strdup(data[2].data.string);
- attr._last_update = data[3].data.datetime;
+ hostname = data[0].data.string;
+ key = data[1].data.string;
+ value.type = SDB_TYPE_STRING;
+ value.data.string = data[2].data.string;
+ last_update = data[3].data.datetime;
- status = sdb_store_attribute(&attr);
+ status = sdb_store_attribute(hostname, key, &value, last_update);
if (status < 0) {
sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
"store/update host attribute '%s' for host '%s'.",
- attr._name, attr.hostname);
- free(attr.hostname);
- free(attr._name);
- free(attr.attr_value);
+ key, hostname);
return -1;
}
- free(attr.hostname);
- free(attr._name);
- free(attr.attr_value);
return 0;
} /* sdb_puppet_stcfg_get_attrs */
return 0;
} /* sdb_puppet_stcfg_init */
+static int
+sdb_puppet_stcfg_shutdown(sdb_object_t *user_data)
+{
+ if (! user_data)
+ return -1;
+
+ sdb_dbi_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
+ SDB_OBJ_WRAPPER(user_data)->data = NULL;
+ return 0;
+} /* sdb_puppet_stcfg_shutdown */
+
static int
sdb_puppet_stcfg_collect(sdb_object_t *user_data)
{
sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
{
char *name = NULL;
- char cb_name[1024];
sdb_object_t *user_data;
sdb_dbi_client_t *client;
return -1;
}
- snprintf(cb_name, sizeof(cb_name), "puppet::storeconfigs::%s", name);
- cb_name[sizeof(cb_name) - 1] = '\0';
-
client = sdb_dbi_client_create(driver, database);
if (! client) {
char errbuf[1024];
sdb_dbi_client_set_options(client, options);
- user_data = sdb_object_create_wrapper(client,
+ user_data = sdb_object_create_wrapper("dbi-client", client,
(void (*)(void *))sdb_dbi_client_destroy);
if (! user_data) {
sdb_dbi_client_destroy(client);
return -1;
}
- sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
- sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
+ sdb_plugin_register_init(name, sdb_puppet_stcfg_init, user_data);
+ sdb_plugin_register_shutdown(name, sdb_puppet_stcfg_shutdown,
+ user_data);
+ sdb_plugin_register_collector(name, sdb_puppet_stcfg_collect,
/* interval */ NULL, user_data);
/* pass control to the list */
{
int i;
+ if (! ci) /* nothing to do to deconfigure this plugin */
+ return 0;
+
for (i = 0; i < ci->children_num; ++i) {
oconfig_item_t *child = ci->children + i;
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) {
- sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: failed to "
- "initialize DBI; possibly you don't have any drivers "
- "installed.");
- return -1;
- }
-
- sdb_plugin_register_config("puppet::store-configs",
- sdb_puppet_stcfg_config);
+ sdb_plugin_register_config(sdb_puppet_stcfg_config);
return 0;
} /* sdb_version_extra */