index 7e4f73a2d20bfab567f116446f5caed1dd37c33b..c0d023ed6b85dc2d2c87320123b0e0a44f968b5b 100644 (file)
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif /* HAVE_CONFIG_H */
+
#include "sysdb.h"
#include "core/plugin.h"
#include "core/store.h"
-#include "core/error.h"
+#include "utils/error.h"
#include "utils/unixsock.h"
#include "liboconfig/utils.h"
typedef struct {
char *current_host;
sdb_time_t current_timestamp;
- int svc_updated;
- int svc_failed;
+ int metrics_updated;
+ int metrics_failed;
} sdb_collectd_state_t;
#define SDB_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
* private helper functions
*/
+/* store the specified host-name (once per iteration) */
static int
-sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
+sdb_collectd_store_host(sdb_collectd_state_t *state,
+ const char *hostname, sdb_time_t last_update)
{
int status;
+ if (last_update > state->current_timestamp)
+ state->current_timestamp = last_update;
+
+ if (state->current_host && (! strcasecmp(state->current_host, hostname)))
+ return 0;
+ /* else: first/new host */
+
+ if (state->current_host) {
+ sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
+ "%i metric%s (%i failed) for host '%s'.",
+ state->metrics_updated, state->metrics_updated == 1 ? "" : "s",
+ state->metrics_failed, state->current_host);
+ state->metrics_updated = state->metrics_failed = 0;
+ free(state->current_host);
+ }
+
+ state->current_host = strdup(hostname);
+ if (! state->current_host) {
+ char errbuf[1024];
+ sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
+ "string buffer: %s",
+ sdb_strerror(errno, errbuf, sizeof(errbuf)));
+ return -1;
+ }
+
status = sdb_store_host(hostname, last_update);
if (status < 0) {
return 0;
sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
- "host '%s' (last update timestamp = %"PRIscTIME").",
+ "host '%s' (last update timestamp = %"PRIsdbTIME").",
hostname, last_update);
return 0;
-} /* sdb_collectd_add_host */
+} /* sdb_collectd_store_host */
static int
-sdb_collectd_add_svc(const char *hostname, const char *plugin,
- const char *type, sdb_time_t last_update)
+sdb_collectd_add_metrics(const char *hostname, char *plugin, char *type,
+ sdb_time_t last_update)
{
- sdb_service_t svc = SDB_SVC_INIT;
- char host[strlen(hostname) + 1];
- char name[strlen(plugin) + strlen(type) + 2];
+ char name[strlen(plugin) + strlen(type) + 2];
+ char *plugin_instance, *type_instance;
- int status;
+ sdb_data_t data = { SDB_TYPE_STRING, { .string = NULL } };
- strncpy(host, hostname, sizeof(host));
- snprintf(name, sizeof(name), "%s/%s", plugin, type);
+ int status;
- svc.hostname = host;
- SDB_OBJ(&svc)->name = name;
- svc._last_update = last_update;
+ snprintf(name, sizeof(name), "%s/%s", plugin, type);
- status = sdb_store_service(&svc);
+ status = sdb_store_metric(hostname, name, NULL, last_update);
if (status < 0) {
sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
- "store/update service '%s/%s'.", host, name);
+ "store/update metric '%s/%s'.", hostname, name);
return -1;
}
+
+ plugin_instance = strchr(plugin, '-');
+ if (plugin_instance) {
+ *plugin_instance = '\0';
+ ++plugin_instance;
+
+ data.data.string = plugin_instance;
+ sdb_store_metric_attr(hostname, name,
+ "plugin_instance", &data, last_update);
+ }
+
+ type_instance = strchr(type, '-');
+ if (type_instance) {
+ *type_instance = '\0';
+ ++type_instance;
+
+ data.data.string = type_instance;
+ sdb_store_metric_attr(hostname, name,
+ "type_instance", &data, last_update);
+ }
+
+ data.data.string = plugin;
+ sdb_store_metric_attr(hostname, name, "plugin", &data, last_update);
+ data.data.string = type;
+ sdb_store_metric_attr(hostname, name, "type", &data, last_update);
return 0;
-} /* sdb_collectd_add_svc */
+} /* sdb_collectd_add_metrics */
static int
sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
size_t n, sdb_data_t *data, sdb_object_t *user_data)
{
sdb_collectd_state_t *state;
+ sdb_data_t last_update;
- const char *hostname;
- const char *plugin;
- const char *type;
- sdb_time_t last_update;
+ char *hostname;
+ char *plugin;
+ char *type;
assert(user_data);
- assert(n == 4);
- assert((data[0].type == SDB_TYPE_DATETIME)
+ /* 0: <last_update> <hostname>
+ * 1: <plugin>
+ * 2: <type> */
+ assert(n == 3);
+ assert((data[0].type == SDB_TYPE_STRING)
&& (data[1].type == SDB_TYPE_STRING)
- && (data[2].type == SDB_TYPE_STRING)
- && (data[3].type == SDB_TYPE_STRING));
+ && (data[2].type == SDB_TYPE_STRING));
- last_update = data[0].data.datetime;
- hostname = data[1].data.string;
- plugin = data[2].data.string;
- type = data[3].data.string;
+ hostname = data[0].data.string;
+ plugin = data[1].data.string;
+ type = data[2].data.string;
- state = SDB_OBJ_WRAPPER(user_data)->data;
-
- if (! state->current_host) {
- state->current_host = strdup(hostname);
- state->current_timestamp = last_update;
+ hostname = strchr(hostname, ' ');
+ if (! hostname) {
+ sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Expected to find "
+ "a space character in the LISTVAL response");
+ return -1;
}
+ *hostname = '\0';
+ ++hostname;
- if (! state->current_host) {
+ if (sdb_data_parse(data[0].data.string, SDB_TYPE_DATETIME, &last_update)) {
char errbuf[1024];
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
- "string buffer: %s",
+ sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
+ "timestamp '%s' returned by LISTVAL: %s", data[0].data.string,
sdb_strerror(errno, errbuf, sizeof(errbuf)));
return -1;
}
- if (! sdb_store_get_host(hostname))
- sdb_collectd_add_host(hostname, last_update);
+ state = SDB_OBJ_WRAPPER(user_data)->data;
+ if (sdb_collectd_store_host(state, hostname, last_update.data.datetime))
+ return -1;
- if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
- ++state->svc_failed;
+ if (sdb_collectd_add_metrics(hostname, plugin, type,
+ last_update.data.datetime))
+ ++state->metrics_failed;
else
- ++state->svc_updated;
-
- if (! strcasecmp(state->current_host, hostname)) {
- if (last_update > state->current_timestamp)
- state->current_timestamp = last_update;
- return 0;
- }
-
- /* new host */
- sdb_collectd_add_host(hostname, last_update);
-
- sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
- "%i service%s (%i failed) for host '%s'.",
- state->svc_updated, state->svc_updated == 1 ? "" : "s",
- state->svc_failed, state->current_host);
- state->svc_updated = state->svc_failed = 0;
-
- free(state->current_host);
- state->current_host = strdup(hostname);
- state->current_timestamp = last_update;
+ ++state->metrics_updated;
return 0;
} /* sdb_collectd_get_data */
static int
sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
{
+ if (! user_data)
+ return -1;
+
+ sdb_unixsock_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
+ SDB_OBJ_WRAPPER(user_data)->data = NULL;
return 0;
} /* sdb_collectd_shutdown */
long int count;
sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
- sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
- /* destructor = */ NULL);
+ sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state);
if (! user_data)
return -1;
}
if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
- SDB_OBJ(&state_obj), count, /* delim */ " /",
- /* column count = */ 4,
- SDB_TYPE_DATETIME, SDB_TYPE_STRING,
- SDB_TYPE_STRING, SDB_TYPE_STRING)) {
+ SDB_OBJ(&state_obj), count, /* delim */ "/",
+ /* column count = */ 3,
+ SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_STRING)) {
sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
"to read response from collectd @ %s.",
sdb_unixsock_client_path(client));
}
if (state.current_host) {
- sdb_collectd_add_host(state.current_host, state.current_timestamp);
sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
- "%i service%s (%i failed) for host '%s'.",
- state.svc_updated, state.svc_updated == 1 ? "" : "s",
- state.svc_failed, state.current_host);
+ "%i metric%s (%i failed) for host '%s'.",
+ state.metrics_updated, state.metrics_updated == 1 ? "" : "s",
+ state.metrics_failed, state.current_host);
+ free(state.current_host);
}
return 0;
} /* sdb_collectd_collect */
char *name = NULL;
char *socket_path = NULL;
- char cb_name[1024];
-
sdb_object_t *user_data;
sdb_unixsock_client_t *client;
return -1;
}
- snprintf(cb_name, sizeof(cb_name), "collectd::unixsock::%s", name);
- cb_name[sizeof(cb_name) - 1] = '\0';
-
client = sdb_unixsock_client_create(socket_path);
if (! client) {
char errbuf[1024];
return -1;
}
- sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
- sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
+ sdb_plugin_register_init(name, sdb_collectd_init, user_data);
+ sdb_plugin_register_shutdown(name, sdb_collectd_shutdown, user_data);
- sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
+ sdb_plugin_register_collector(name, sdb_collectd_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;
int
sdb_module_init(sdb_plugin_info_t *info)
{
- sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd::unixsock");
sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
"backend accessing the system statistics collection daemon "
"throught the UNIXSOCK interface");
sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
- sdb_plugin_register_config("collectd::unixsock", sdb_collectd_config);
+ sdb_plugin_register_config(sdb_collectd_config);
return 0;
} /* sdb_version_extra */