index 29d7802fa83689b13c9b8646139e7e4ce3c64253..d5d732197bc519de94fe0eafc585a964233bfdf4 100644 (file)
/*
* SysDB - src/plugins/backend/collectd/unixsock.c
- * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2012-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
SDB_PLUGIN_MAGIC;
* private data types
*/
+typedef struct {
+ sdb_unixsock_client_t *client;
+
+ char *ts_type;
+ char *ts_base;
+} user_data_t;
+#define UD(obj) ((user_data_t *)(obj))
+
typedef struct {
char *current_host;
sdb_time_t current_timestamp;
int metrics_updated;
int metrics_failed;
-} sdb_collectd_state_t;
-#define SDB_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
+
+ user_data_t *ud;
+} state_t;
+#define STATE_INIT { NULL, 0, 0, 0, NULL }
/*
* private helper functions
*/
+static void
+user_data_destroy(void *obj)
+{
+ user_data_t *ud = UD(obj);
+
+ if (! obj)
+ return;
+
+ if (ud->client)
+ sdb_unixsock_client_destroy(ud->client);
+ ud->client = NULL;
+
+ if (ud->ts_type)
+ free(ud->ts_type);
+ if (ud->ts_base)
+ free(ud->ts_base);
+ ud->ts_type = ud->ts_base = NULL;
+
+ free(ud);
+} /* user_data_destroy */
+
/* store the specified host-name (once per iteration) */
static int
-sdb_collectd_store_host(sdb_collectd_state_t *state,
- const char *hostname, sdb_time_t last_update)
+store_host(state_t *state, const char *hostname, sdb_time_t last_update)
{
int status;
/* 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'.",
+ sdb_log(SDB_LOG_DEBUG, "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;
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_log(SDB_LOG_ERR, "Failed to allocate string buffer: %s",
sdb_strerror(errno, errbuf, sizeof(errbuf)));
return -1;
}
- status = sdb_store_host(hostname, last_update);
+ status = sdb_plugin_store_host(hostname, last_update);
if (status < 0) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
- "store/update host '%s'.", hostname);
+ sdb_log(SDB_LOG_ERR, "Failed to store/update host '%s'.", hostname);
return -1;
}
else if (status > 0) /* value too old */
return 0;
- sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
- "host '%s' (last update timestamp = %"PRIsdbTIME").",
+ sdb_log(SDB_LOG_DEBUG, "Added/updated host '%s' "
+ "(last update timestamp = %"PRIsdbTIME").",
hostname, last_update);
return 0;
-} /* sdb_collectd_store_host */
+} /* store_host */
static int
-sdb_collectd_add_metrics(const char *hostname, char *plugin, char *type,
- sdb_time_t last_update)
+add_metrics(const char *hostname, char *plugin, char *type,
+ sdb_time_t last_update, user_data_t *ud)
{
char name[strlen(plugin) + strlen(type) + 2];
char *plugin_instance, *type_instance;
+ char metric_id[(ud->ts_base ? strlen(ud->ts_base) : 0)
+ + strlen(hostname) + sizeof(name) + 7];
+ sdb_metric_store_t store = { ud->ts_type, metric_id, NULL, last_update };
+
sdb_data_t data = { SDB_TYPE_STRING, { .string = NULL } };
- int status;
+ int status;
snprintf(name, sizeof(name), "%s/%s", plugin, type);
- status = sdb_store_metric(hostname, name, NULL, last_update);
+ if (ud->ts_base) {
+ snprintf(metric_id, sizeof(metric_id), "%s/%s/%s.rrd",
+ ud->ts_base, hostname, name);
+ status = sdb_plugin_store_metric(hostname, name, &store, last_update);
+ }
+ else
+ status = sdb_plugin_store_metric(hostname, name, NULL, last_update);
if (status < 0) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
- "store/update metric '%s/%s'.", hostname, name);
+ sdb_log(SDB_LOG_ERR, "Failed to store/update metric '%s/%s'.", hostname, name);
return -1;
}
++plugin_instance;
data.data.string = plugin_instance;
- sdb_store_metric_attr(hostname, name,
+ sdb_plugin_store_metric_attribute(hostname, name,
"plugin_instance", &data, last_update);
}
++type_instance;
data.data.string = type_instance;
- sdb_store_metric_attr(hostname, name,
+ sdb_plugin_store_metric_attribute(hostname, name,
"type_instance", &data, last_update);
}
data.data.string = plugin;
- sdb_store_metric_attr(hostname, name, "plugin", &data, last_update);
+ sdb_plugin_store_metric_attribute(hostname, name, "plugin", &data, last_update);
data.data.string = type;
- sdb_store_metric_attr(hostname, name, "type", &data, last_update);
+ sdb_plugin_store_metric_attribute(hostname, name, "type", &data, last_update);
return 0;
-} /* sdb_collectd_add_metrics */
+} /* add_metrics */
static int
-sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
+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;
+ state_t *state;
sdb_data_t last_update;
char *hostname;
hostname = strchr(hostname, ' ');
if (! hostname) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Expected to find "
- "a space character in the LISTVAL response");
+ sdb_log(SDB_LOG_ERR, "Expected to find a space character "
+ "in the LISTVAL response");
return -1;
}
*hostname = '\0';
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 parse "
- "timestamp '%s' returned by LISTVAL: %s", data[0].data.string,
+ sdb_log(SDB_LOG_ERR, "Failed to parse timestamp '%s' "
+ "returned by LISTVAL: %s", data[0].data.string,
sdb_strerror(errno, errbuf, sizeof(errbuf)));
return -1;
}
state = SDB_OBJ_WRAPPER(user_data)->data;
- if (sdb_collectd_store_host(state, hostname, last_update.data.datetime))
+ if (store_host(state, hostname, last_update.data.datetime))
return -1;
- if (sdb_collectd_add_metrics(hostname, plugin, type,
- last_update.data.datetime))
+ if (add_metrics(hostname, plugin, type,
+ last_update.data.datetime, state->ud))
++state->metrics_failed;
else
++state->metrics_updated;
return 0;
-} /* sdb_collectd_get_data */
+} /* get_data */
/*
* plugin API
static int
sdb_collectd_init(sdb_object_t *user_data)
{
- sdb_unixsock_client_t *client;
+ user_data_t *ud;
if (! user_data)
return -1;
- client = SDB_OBJ_WRAPPER(user_data)->data;
- if (sdb_unixsock_client_connect(client)) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
- "Failed to connect to collectd.");
+ ud = SDB_OBJ_WRAPPER(user_data)->data;
+ if (sdb_unixsock_client_connect(ud->client)) {
+ sdb_log(SDB_LOG_ERR, "Failed to connect to collectd.");
return -1;
}
- sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
- "connected to collectd @ %s.",
- sdb_unixsock_client_path(client));
+ sdb_log(SDB_LOG_INFO, "Successfully connected to collectd @ %s.",
+ sdb_unixsock_client_path(ud->client));
return 0;
} /* sdb_collectd_init */
-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 */
-
static int
sdb_collectd_collect(sdb_object_t *user_data)
{
- sdb_unixsock_client_t *client;
+ user_data_t *ud;
char buffer[1024];
char *line;
char *endptr = NULL;
long int count;
- sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
+ state_t state = STATE_INIT;
sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state);
if (! user_data)
return -1;
- client = SDB_OBJ_WRAPPER(user_data)->data;
+ ud = SDB_OBJ_WRAPPER(user_data)->data;
+ state.ud = ud;
- if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
- "LISTVAL command to collectd @ %s.",
- sdb_unixsock_client_path(client));
+ if (sdb_unixsock_client_send(ud->client, "LISTVAL") <= 0) {
+ sdb_log(SDB_LOG_ERR, "Failed to send LISTVAL command to collectd @ %s.",
+ sdb_unixsock_client_path(ud->client));
return -1;
}
- line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
+ line = sdb_unixsock_client_recv(ud->client, buffer, sizeof(buffer));
if (! line) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
- "status of LISTVAL command from collectd @ %s.",
- sdb_unixsock_client_path(client));
+ sdb_log(SDB_LOG_ERR, "Failed to read status of LISTVAL command from collectd @ %s.",
+ sdb_unixsock_client_path(ud->client));
return -1;
}
errno = 0;
count = strtol(line, &endptr, /* base */ 0);
if (errno || (line == endptr)) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
- "status of LISTVAL command from collectd @ %s.",
- sdb_unixsock_client_path(client));
+ sdb_log(SDB_LOG_ERR, "Failed to parse status of LISTVAL command from collectd @ %s.",
+ sdb_unixsock_client_path(ud->client));
return -1;
}
if (count < 0) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
- "value list from collectd @ %s: %s",
- sdb_unixsock_client_path(client),
+ sdb_log(SDB_LOG_ERR, "Failed to get value list from collectd @ %s: %s",
+ sdb_unixsock_client_path(ud->client),
msg ? msg : line);
return -1;
}
- if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
+ if (sdb_unixsock_client_process_lines(ud->client, get_data,
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));
+ sdb_log(SDB_LOG_ERR, "Failed to read response from collectd @ %s.",
+ sdb_unixsock_client_path(ud->client));
return -1;
}
if (state.current_host) {
- sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
- "%i metric%s (%i failed) for host '%s'.",
+ sdb_log(SDB_LOG_DEBUG, "Added/updated %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 */
+} /* collect */
static int
sdb_collectd_config_instance(oconfig_item_t *ci)
char *socket_path = NULL;
sdb_object_t *user_data;
- sdb_unixsock_client_t *client;
+ user_data_t *ud;
int i;
if (oconfig_get_string(ci, &name)) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
- "a single string argument\n\tUsage: <Instance NAME>");
+ sdb_log(SDB_LOG_ERR, "Instance requires a single string argument\n"
+ "\tUsage: <Instance NAME>");
+ return -1;
+ }
+
+ ud = calloc(1, sizeof(*ud));
+ if (! ud) {
+ char errbuf[1024];
+ sdb_log(SDB_LOG_ERR, "Failed to allocate user-data object: %s",
+ sdb_strerror(errno, errbuf, sizeof(errbuf)));
return -1;
}
if (! strcasecmp(child->key, "Socket"))
oconfig_get_string(child, &socket_path);
+ else if (! strcasecmp(child->key, "TimeseriesBackend"))
+ oconfig_get_string(child, &ud->ts_type);
+ else if (! strcasecmp(child->key, "TimeseriesBaseURL"))
+ oconfig_get_string(child, &ud->ts_base);
else
- sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
- "unknown config option '%s' inside <Instance %s>.",
- child->key, name);
+ sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s' "
+ "inside <Instance %s>.", child->key, name);
+ }
+
+ if ((ud->ts_type == NULL) != (ud->ts_base == NULL)) {
+ sdb_log(SDB_LOG_ERR, "Both options, TimeseriesBackend and TimeseriesBaseURL, "
+ "have to be specified.");
+ ud->ts_type = ud->ts_base = NULL;
+ user_data_destroy(ud);
+ return -1;
+ }
+
+ if (ud->ts_type) {
+ /* TODO: add support for other backend types
+ * -> will require different ID generation */
+ if (strcasecmp(ud->ts_type, "rrdtool")
+ && strcasecmp(ud->ts_type, "rrdcached")) {
+ sdb_log(SDB_LOG_ERR, "TimeseriesBackend '%s' is not supported - "
+ "use 'rrdtool' instead.", ud->ts_type);
+ ud->ts_type = ud->ts_base = NULL;
+ user_data_destroy(ud);
+ return -1;
+ }
+
+ ud->ts_type = strdup(ud->ts_type);
+ ud->ts_base = strdup(ud->ts_base);
+ if ((! ud->ts_type) || (! ud->ts_base)) {
+ sdb_log(SDB_LOG_ERR, "Failed to duplicate a string");
+ user_data_destroy(ud);
+ return -1;
+ }
}
if (! socket_path) {
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
- "missing the 'Socket' option.", name);
+ sdb_log(SDB_LOG_ERR, "Instance '%s' missing the 'Socket' option.", name);
+ user_data_destroy(ud);
return -1;
}
- client = sdb_unixsock_client_create(socket_path);
- if (! client) {
+ ud->client = sdb_unixsock_client_create(socket_path);
+ if (! ud->client) {
char errbuf[1024];
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
- "unixsock client: %s",
+ sdb_log(SDB_LOG_ERR, "Failed to create unixsock client: %s",
sdb_strerror(errno, errbuf, sizeof(errbuf)));
+ user_data_destroy(ud);
return -1;
}
- user_data = sdb_object_create_wrapper("unixsock-client", client,
- (void (*)(void *))sdb_unixsock_client_destroy);
+ user_data = sdb_object_create_wrapper("collectd-userdata", ud,
+ user_data_destroy);
if (! user_data) {
- sdb_unixsock_client_destroy(client);
- sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
- "sdb_object_t");
+ sdb_log(SDB_LOG_ERR, "Failed to allocate user-data wrapper object");
+ user_data_destroy(ud);
return -1;
}
sdb_plugin_register_init(name, sdb_collectd_init, user_data);
- sdb_plugin_register_shutdown(name, sdb_collectd_shutdown, user_data);
-
sdb_plugin_register_collector(name, sdb_collectd_collect,
/* interval */ NULL, user_data);
if (! strcasecmp(child->key, "Instance"))
sdb_collectd_config_instance(child);
else
- sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
- "unknown config option '%s'.", child->key);
+ sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s'.", child->key);
}
return 0;
} /* sdb_collectd_config */