summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f25e41d)
raw | patch | inline | side by side (parent: f25e41d)
author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Mon, 2 Feb 2009 17:40:11 +0000 (18:40 +0100) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Mon, 2 Feb 2009 17:40:11 +0000 (18:40 +0100) |
The `utils_db_query' module now handles config parsing and row handling.
This unifies basically all of the <Query> blocks, so that the `dbi' and
`oracle' plugins behave exactly alike. Porting the `postgresql' plugin
should be possible, too, so that all three database plugins are in line.
This unifies basically all of the <Query> blocks, so that the `dbi' and
`oracle' plugins behave exactly alike. Porting the `postgresql' plugin
should be possible, too, so that all three database plugins are in line.
src/Makefile.am | patch | blob | history | |
src/collectd.conf.in | patch | blob | history | |
src/collectd.conf.pod | patch | blob | history | |
src/dbi.c | patch | blob | history | |
src/utils_db_query.c | [new file with mode: 0644] | patch | blob |
src/utils_db_query.h | [new file with mode: 0644] | patch | blob |
diff --git a/src/Makefile.am b/src/Makefile.am
index 500b389566d72fe33d599c02002abd9e4207de96..58d7dde99eba1b9cc20ea24b3cc140688ee271da 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
if BUILD_PLUGIN_DBI
pkglib_LTLIBRARIES += dbi.la
-dbi_la_SOURCES = dbi.c
+dbi_la_SOURCES = dbi.c \
+ utils_db_query.c utils_db_query.h
dbi_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBDBI_CPPFLAGS)
dbi_la_LDFLAGS = -module -avoid-version $(BUILD_WITH_LIBDBI_LDFLAGS)
dbi_la_LIBADD = $(BUILD_WITH_LIBDBI_LIBS)
diff --git a/src/collectd.conf.in b/src/collectd.conf.in
index 15ae7f7cc4e3a2716b5ae0bbf7d06b7267481d4f..b241c9b6d8700279c552d93da49c363d782d9f7a 100644 (file)
--- a/src/collectd.conf.in
+++ b/src/collectd.conf.in
# Type "gauge"
# InstancesFrom "c_key"
# ValuesFrom "c_value"
-# <Result>
+# </Result>
# </Query>
# <Database "customers_db">
# Driver "mysql"
diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod
index 67fe3b631415bc654ce4f3614309c69a1ce8b521..9b72b437ffb95cc8e4aa0b31d5ee8d82d5f63f10 100644 (file)
--- a/src/collectd.conf.pod
+++ b/src/collectd.conf.pod
Statement "SELECT category, COUNT(*) AS value FROM products WHERE in_stock = 0 GROUP BY category"
<Result>
Type "gauge"
+ InstancePrefix "out_of_stock"
InstancesFrom "category"
ValuesFrom "value"
</Result>
Statement "select station, temperature, humidity from environment"
<Result>
Type "temperature"
+ # InstancePrefix "foo"
InstancesFrom "station"
ValuesFrom "temperature"
</Result>
There must be exactly one B<Type> option inside each B<Result> block.
+=item B<InstancePrefix> I<prefix>
+
+Prepends I<prefix> followed by a dash I<("-")> to the type instance. See
+B<InstancesFrom> on how the rest of the type instance is built.
+
=item B<InstancesFrom> I<column0> [I<column1> ...]
Specifies the columns whose values will be used to create the "TypeInstance"
diff --git a/src/dbi.c b/src/dbi.c
index 9e4f446fe3b879db4ae30eda492f1c2b021f28e6..ee43602309e55cbc8ec6749fb4fe974021c8e5b8 100644 (file)
--- a/src/dbi.c
+++ b/src/dbi.c
#include "common.h"
#include "plugin.h"
#include "configfile.h"
+#include "utils_db_query.h"
#include <dbi/dbi.h>
/*
* Data types
*/
-struct cdbi_driver_option_s
+struct cdbi_driver_option_s /* {{{ */
{
char *key;
char *value;
};
-typedef struct cdbi_driver_option_s cdbi_driver_option_t;
+typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
-struct cdbi_result_s;
-typedef struct cdbi_result_s cdbi_result_t;
-struct cdbi_result_s
-{
- char *type;
- char **instances;
- size_t instances_num;
- char **values;
- size_t values_num;
-
- cdbi_result_t *next;
-};
-
-struct cdbi_query_s
-{
- char *name;
- char *statement;
-
- cdbi_result_t *results;
-};
-typedef struct cdbi_query_s cdbi_query_t;
-
-struct cdbi_database_s
+struct cdbi_database_s /* {{{ */
{
char *name;
char *select_db;
cdbi_driver_option_t *driver_options;
size_t driver_options_num;
- cdbi_query_t **queries;
- size_t queries_num;
+ udb_query_t **queries;
+ size_t queries_num;
dbi_conn connection;
};
-typedef struct cdbi_database_s cdbi_database_t;
+typedef struct cdbi_database_s cdbi_database_t; /* }}} */
/*
* Global variables
*/
-static cdbi_query_t **queries = NULL;
+static udb_query_t **queries = NULL;
static size_t queries_num = 0;
static cdbi_database_t **databases = NULL;
static size_t databases_num = 0;
} /* }}} const char *cdbi_conn_error */
static int cdbi_result_get_field (dbi_result res, /* {{{ */
- const char *name, int dst_type, value_t *ret_value)
+ unsigned int index, char *buffer, size_t buffer_size)
{
- value_t value;
- unsigned int index;
unsigned short src_type;
- dbi_conn connection;
-
- index = dbi_result_get_field_idx (res, name);
- if (index < 1)
- {
- ERROR ("dbi plugin: cdbi_result_get: No such column: %s.", name);
- return (-1);
- }
src_type = dbi_result_get_field_type_idx (res, index);
if (src_type == DBI_TYPE_ERROR)
return (-1);
}
- if ((dst_type != DS_TYPE_COUNTER) && (dst_type != DS_TYPE_GAUGE))
- {
- ERROR ("dbi plugin: cdbi_result_get: Don't know how to handle "
- "destination type %i.", dst_type);
- return (-1);
- }
-
if (src_type == DBI_TYPE_INTEGER)
{
- if (dst_type == DS_TYPE_COUNTER)
- value.counter = dbi_result_get_ulonglong_idx (res, index);
- else
- value.gauge = (gauge_t) dbi_result_get_longlong_idx (res, index);
+ long long value;
+
+ value = dbi_result_get_longlong_idx (res, index);
+ ssnprintf (buffer, buffer_size, "%lli", value);
}
else if (src_type == DBI_TYPE_DECIMAL)
{
- value.gauge = dbi_result_get_double_idx (res, index);
- if (dst_type == DS_TYPE_COUNTER)
- value.counter = (counter_t) round (value.gauge);
+ double value;
+
+ value = dbi_result_get_double_idx (res, index);
+ ssnprintf (buffer, buffer_size, "%63.15g", value);
}
else if (src_type == DBI_TYPE_STRING)
{
- const char *string = dbi_result_get_string_idx (res, index);
- char *endptr = NULL;
-
- if (string == NULL)
- value.gauge = NAN;
- else if (dst_type == DS_TYPE_COUNTER)
- value.counter = (counter_t) strtoll (string, &endptr, 0);
- else
- value.gauge = (gauge_t) strtod (string, &endptr);
-
- if (string == endptr)
- {
- ERROR ("dbi plugin: cdbi_result_get: Can't parse string as number: %s.",
- string);
+ const char *value;
+
+ value = dbi_result_get_string_idx (res, index);
+ if (value == NULL)
+ sstrncpy (buffer, "", buffer_size);
+ else if (strcmp ("ERROR", value) == 0)
return (-1);
- }
+ else
+ sstrncpy (buffer, value, buffer_size);
}
else
{
return (-1);
}
- connection = dbi_result_get_conn (res);
- if (dbi_conn_error (connection, NULL) != 0)
- {
- char errbuf[1024];
- ERROR ("dbi plugin: cdbi_result_get: dbi_result_get_*_idx failed: %s.",
- cdbi_strerror (connection, errbuf, sizeof (errbuf)));
- return (-1);
- }
-
- *ret_value = value;
return (0);
} /* }}} int cdbi_result_get_field */
-static void cdbi_result_free (cdbi_result_t *r) /* {{{ */
-{
- size_t i;
-
- if (r == NULL)
- return;
-
- sfree (r->type);
-
- for (i = 0; i < r->instances_num; i++)
- sfree (r->instances[i]);
- sfree (r->instances);
-
- for (i = 0; i < r->values_num; i++)
- sfree (r->values[i]);
- sfree (r->values);
-
- cdbi_result_free (r->next);
-
- sfree (r);
-} /* }}} void cdbi_result_free */
-
-static void cdbi_query_free (cdbi_query_t *q) /* {{{ */
-{
- if (q == NULL)
- return;
-
- sfree (q->name);
- sfree (q->statement);
-
- cdbi_result_free (q->results);
-
- sfree (q);
-} /* }}} void cdbi_query_free */
-
static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
{
size_t i;
sfree (db);
} /* }}} void cdbi_database_free */
-static void cdbi_submit (cdbi_database_t *db, cdbi_result_t *r, /* {{{ */
- char **instances, value_t *values)
-{
- value_list_t vl = VALUE_LIST_INIT;
-
- vl.values = values;
- vl.values_len = (int) r->values_num;
- vl.time = time (NULL);
- sstrncpy (vl.host, hostname_g, sizeof (vl.host));
- sstrncpy (vl.plugin, "dbi", sizeof (vl.plugin));
- sstrncpy (vl.plugin_instance, db->name, sizeof (vl.type_instance));
- sstrncpy (vl.type, r->type, sizeof (vl.type));
- strjoin (vl.type_instance, sizeof (vl.type_instance),
- instances, r->instances_num, "-");
- vl.type_instance[sizeof (vl.type_instance) - 1] = 0;
-
- plugin_dispatch_values (&vl);
-} /* }}} void cdbi_submit */
-
/* Configuration handling functions {{{
*
* <Plugin dbi>
return (0);
} /* }}} int cdbi_config_set_string */
-static int cdbi_config_add_string (char ***ret_array, /* {{{ */
- size_t *ret_array_len, oconfig_item_t *ci)
-{
- char **array;
- size_t array_len;
- int i;
-
- if (ci->values_num < 1)
- {
- WARNING ("dbi plugin: The `%s' config option "
- "needs at least one argument.", ci->key);
- return (-1);
- }
-
- for (i = 0; i < ci->values_num; i++)
- {
- if (ci->values[i].type != OCONFIG_TYPE_STRING)
- {
- WARNING ("dbi plugin: Argument %i to the `%s' option "
- "is not a string.", i + 1, ci->key);
- return (-1);
- }
- }
-
- array_len = *ret_array_len;
- array = (char **) realloc (*ret_array,
- sizeof (char *) * (array_len + ci->values_num));
- if (array == NULL)
- {
- ERROR ("dbi plugin: realloc failed.");
- return (-1);
- }
- *ret_array = array;
-
- for (i = 0; i < ci->values_num; i++)
- {
- array[array_len] = strdup (ci->values[i].value.string);
- if (array[array_len] == NULL)
- {
- ERROR ("dbi plugin: strdup failed.");
- *ret_array_len = array_len;
- return (-1);
- }
- array_len++;
- }
-
- *ret_array_len = array_len;
- return (0);
-} /* }}} int cdbi_config_add_string */
-
-static int cdbi_config_add_query_result (cdbi_query_t *q, /* {{{ */
- oconfig_item_t *ci)
-{
- cdbi_result_t *r;
- int status;
- int i;
-
- if (ci->values_num != 0)
- {
- WARNING ("dbi plugin: The `Result' block doesn't accept any arguments. "
- "Ignoring %i argument%s.",
- ci->values_num, (ci->values_num == 1) ? "" : "s");
- }
-
- r = (cdbi_result_t *) malloc (sizeof (*r));
- if (r == NULL)
- {
- ERROR ("dbi plugin: malloc failed.");
- return (-1);
- }
- memset (r, 0, sizeof (*r));
- r->type = NULL;
- r->instances = NULL;
- r->values = NULL;
- r->next = NULL;
-
- /* Fill the `cdbi_result_t' structure.. */
- for (i = 0; i < ci->children_num; i++)
- {
- oconfig_item_t *child = ci->children + i;
-
- if (strcasecmp ("Type", child->key) == 0)
- status = cdbi_config_set_string (&r->type, child);
- else if (strcasecmp ("InstancesFrom", child->key) == 0)
- status = cdbi_config_add_string (&r->instances, &r->instances_num, child);
- else if (strcasecmp ("ValuesFrom", child->key) == 0)
- status = cdbi_config_add_string (&r->values, &r->values_num, child);
- else
- {
- WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
- status = -1;
- }
-
- if (status != 0)
- break;
- }
-
- /* Check that all necessary options have been given. */
- while (status == 0)
- {
- if (r->type == NULL)
- {
- WARNING ("dbi plugin: `Type' not given for "
- "result in query `%s'", q->name);
- status = -1;
- }
- if (r->instances == NULL)
- {
- WARNING ("dbi plugin: `InstancesFrom' not given for "
- "result in query `%s'", q->name);
- status = -1;
- }
- if (r->values == NULL)
- {
- WARNING ("dbi plugin: `ValuesFrom' not given for "
- "result in query `%s'", q->name);
- status = -1;
- }
-
- break;
- } /* while (status == 0) */
-
- /* If all went well, add this result to the list of results within the
- * query structure. */
- if (status == 0)
- {
- if (q->results == NULL)
- {
- q->results = r;
- }
- else
- {
- cdbi_result_t *last;
-
- last = q->results;
- while (last->next != NULL)
- last = last->next;
-
- last->next = r;
- }
- }
-
- if (status != 0)
- {
- cdbi_result_free (r);
- return (-1);
- }
-
- return (0);
-} /* }}} int cdbi_config_add_query_result */
-
-static int cdbi_config_add_query (oconfig_item_t *ci) /* {{{ */
-{
- cdbi_query_t *q;
- int status;
- int i;
-
- if ((ci->values_num != 1)
- || (ci->values[0].type != OCONFIG_TYPE_STRING))
- {
- WARNING ("dbi plugin: The `Query' block "
- "needs exactly one string argument.");
- return (-1);
- }
-
- q = (cdbi_query_t *) malloc (sizeof (*q));
- if (q == NULL)
- {
- ERROR ("dbi plugin: malloc failed.");
- return (-1);
- }
- memset (q, 0, sizeof (*q));
-
- status = cdbi_config_set_string (&q->name, ci);
- if (status != 0)
- {
- sfree (q);
- return (status);
- }
-
- /* Fill the `cdbi_query_t' structure.. */
- for (i = 0; i < ci->children_num; i++)
- {
- oconfig_item_t *child = ci->children + i;
-
- if (strcasecmp ("Statement", child->key) == 0)
- status = cdbi_config_set_string (&q->statement, child);
- else if (strcasecmp ("Result", child->key) == 0)
- status = cdbi_config_add_query_result (q, child);
- else
- {
- WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
- status = -1;
- }
-
- if (status != 0)
- break;
- }
-
- /* Check that all necessary options have been given. */
- while (status == 0)
- {
- if (q->statement == NULL)
- {
- WARNING ("dbi plugin: `Statement' not given for query `%s'", q->name);
- status = -1;
- }
- if (q->results == NULL)
- {
- WARNING ("dbi plugin: No (valid) `Result' block given for query `%s'",
- q->name);
- status = -1;
- }
-
- break;
- } /* while (status == 0) */
-
- /* If all went well, add this query to the list of queries within the
- * database structure. */
- if (status == 0)
- {
- cdbi_query_t **temp;
-
- temp = (cdbi_query_t **) realloc (queries,
- sizeof (*queries) * (queries_num + 1));
- if (temp == NULL)
- {
- ERROR ("dbi plugin: realloc failed");
- status = -1;
- }
- else
- {
- queries = temp;
- queries[queries_num] = q;
- queries_num++;
- }
- }
-
- if (status != 0)
- {
- cdbi_query_free (q);
- return (-1);
- }
-
- return (0);
-} /* }}} int cdbi_config_add_query */
-
static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
oconfig_item_t *ci)
{
@@ -604,58 +249,6 @@ static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ *
return (0);
} /* }}} int cdbi_config_add_database_driver_option */
-static int cdbi_config_add_database_query (cdbi_database_t *db, /* {{{ */
- oconfig_item_t *ci)
-{
- cdbi_query_t *q;
- cdbi_query_t **temp;
- size_t i;
-
- if ((ci->values_num != 1)
- || (ci->values[0].type != OCONFIG_TYPE_STRING))
- {
- WARNING ("dbi plugin: The `Query' config option "
- "needs exactly one string argument.");
- return (-1);
- }
-
- q = NULL;
- for (i = 0; i < queries_num; i++)
- {
- if (strcasecmp (queries[i]->name, ci->values[0].value.string) == 0)
- {
- q = queries[i];
- break;
- }
- }
-
- if (q == NULL)
- {
- WARNING ("dbi plugin: Database `%s': Unknown query `%s'. "
- "Please make sure that the <Query \"%s\"> block comes before "
- "the <Database \"%s\"> block.",
- db->name, ci->values[0].value.string,
- ci->values[0].value.string, db->name);
- return (-1);
- }
-
- temp = (cdbi_query_t **) realloc (db->queries,
- sizeof (*db->queries) * (db->queries_num + 1));
- if (temp == NULL)
- {
- ERROR ("dbi plugin: realloc failed");
- return (-1);
- }
- else
- {
- db->queries = temp;
- db->queries[db->queries_num] = q;
- db->queries_num++;
- }
-
- return (0);
-} /* }}} int cdbi_config_add_database_query */
-
static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
{
cdbi_database_t *db;
else if (strcasecmp ("SelectDB", child->key) == 0)
status = cdbi_config_set_string (&db->select_db, child);
else if (strcasecmp ("Query", child->key) == 0)
- status = cdbi_config_add_database_query (db, child);
+ status = udb_query_pick_from_list (child, queries, queries_num,
+ &db->queries, &db->queries_num);
else
{
WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
{
oconfig_item_t *child = ci->children + i;
if (strcasecmp ("Query", child->key) == 0)
- cdbi_config_add_query (child);
+ udb_query_create (&queries, &queries_num, child);
else if (strcasecmp ("Database", child->key) == 0)
cdbi_config_add_database (child);
else
} /* }}} int cdbi_init */
static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
- cdbi_query_t *q)
+ udb_query_t *q)
{
+ const char *statement;
dbi_result res;
+ size_t column_num;
+ char **column_names;
+ char **column_values;
int status;
+ size_t i;
/* Macro that cleans up dynamically allocated memory and returns the
* specified status. */
#define BAIL_OUT(status) \
+ if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
+ if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
if (res != NULL) { dbi_result_free (res); res = NULL; } \
return (status)
- res = dbi_conn_query (db->connection, q->statement);
+ column_names = NULL;
+ column_values = NULL;
+ res = NULL;
+
+ statement = udb_query_get_statement (q);
+ assert (statement != NULL);
+
+ res = dbi_conn_query (db->connection, statement);
if (res == NULL)
{
char errbuf[1024];
ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
"dbi_conn_query failed: %s",
- db->name, q->name,
+ db->name, udb_query_get_name (q),
cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
BAIL_OUT (-1);
}
+ else /* Get the number of columns */
+ {
+ unsigned int db_status;
+
+ db_status = dbi_result_get_numfields (res);
+ if (db_status == DBI_FIELD_ERROR)
+ {
+ char errbuf[1024];
+ ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
+ "dbi_result_get_numfields failed: %s",
+ db->name, udb_query_get_name (q),
+ cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
+ BAIL_OUT (-1);
+ }
+
+ column_num = (size_t) db_status;
+ DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
+ db->name, udb_query_get_name (q), column_num);
+ }
+
+ /* Allocate `column_names' and `column_values'. {{{ */
+ column_names = (char **) calloc (column_num, sizeof (char *));
+ if (column_names == NULL)
+ {
+ ERROR ("dbi plugin: malloc failed.");
+ BAIL_OUT (-1);
+ }
+
+ column_names[0] = (char *) calloc (column_num,
+ DATA_MAX_NAME_LEN * sizeof (char));
+ if (column_names[0] == NULL)
+ {
+ ERROR ("dbi plugin: malloc failed.");
+ BAIL_OUT (-1);
+ }
+ for (i = 1; i < column_num; i++)
+ column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
+
+ column_values = (char **) calloc (column_num, sizeof (char *));
+ if (column_values == NULL)
+ {
+ ERROR ("dbi plugin: malloc failed.");
+ BAIL_OUT (-1);
+ }
+
+ column_values[0] = (char *) calloc (column_num,
+ DATA_MAX_NAME_LEN * sizeof (char));
+ if (column_values[0] == NULL)
+ {
+ ERROR ("dbi plugin: malloc failed.");
+ BAIL_OUT (-1);
+ }
+ for (i = 1; i < column_num; i++)
+ column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
+ /* }}} */
+
+ /* Copy the field names to `column_names' */
+ for (i = 0; i < column_num; i++) /* {{{ */
+ {
+ const char *column_name;
+
+ column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
+ if (column_name == NULL)
+ {
+ ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
+ "Cannot retrieve name of field %zu.",
+ db->name, udb_query_get_name (q), i + 1);
+ BAIL_OUT (-1);
+ }
+
+ sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
+ } /* }}} for (i = 0; i < column_num; i++) */
+
+ udb_query_prepare_result (q, hostname_g, /* plugin = */ "dbi", db->name,
+ column_names, column_num);
/* 0 = error; 1 = success; */
- status = dbi_result_first_row (res);
+ status = dbi_result_first_row (res); /* {{{ */
if (status != 1)
{
char errbuf[1024];
ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
"dbi_result_first_row failed: %s. Maybe the statement didn't "
"return any rows?",
- db->name, q->name,
+ db->name, udb_query_get_name (q),
cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
+ udb_query_finish_result (q);
BAIL_OUT (-1);
- }
+ } /* }}} */
- /* Iterate over all rows and use every result with each row. */
+ /* Iterate over all rows and call `udb_query_handle_result' with each list of
+ * values. */
while (42) /* {{{ */
{
- cdbi_result_t *r;
-
- /* Iterate over all results, get the appropriate data_set, allocate memory
- * for the instance(s) and value(s), copy the values and finally call
- * `cdbi_submit' to create and dispatch a value_list. */
- for (r = q->results; r != NULL; r = r->next) /* {{{ */
+ status = 0;
+ /* Copy the value of the columns to `column_values' */
+ for (i = 0; i < column_num; i++) /* {{{ */
{
- const data_set_t *ds;
- char **instances;
- value_t *values;
- size_t i;
-
- instances = NULL;
- values = NULL;
-
- /* Macro to clean up dynamically allocated memory and continue with the
- * next iteration of the containing loop, i. e. the `for' loop iterating
- * over all `Result' sets. */
-#define BAIL_OUT_CONTINUE \
- if (instances != NULL) { sfree (instances[0]); sfree (instances); } \
- sfree (values); \
- continue
-
- /* Read `ds' and check number of values {{{ */
- ds = plugin_get_ds (r->type);
- if (ds == NULL)
- {
- ERROR ("dbi plugin: cdbi_read_database_query: Query `%s': Type `%s' is not "
- "known by the daemon. See types.db(5) for details.",
- q->name, r->type);
- BAIL_OUT_CONTINUE;
- }
-
- if (((size_t) ds->ds_num) != r->values_num)
- {
- ERROR ("dbi plugin: cdbi_read_database_query: Query `%s': The type `%s' "
- "requires exactly %i value%s, but the configuration specifies %zu.",
- q->name, r->type,
- ds->ds_num, (ds->ds_num == 1) ? "" : "s",
- r->values_num);
- BAIL_OUT_CONTINUE;
- }
- /* }}} */
-
- /* Allocate `instances' and `values' {{{ */
- instances = (char **) malloc (sizeof (*instances) * r->instances_num);
- if (instances == NULL)
- {
- ERROR ("dbi plugin: malloc failed.");
- BAIL_OUT_CONTINUE;
- }
-
- instances[0] = (char *) malloc (r->instances_num * DATA_MAX_NAME_LEN);
- if (instances[0] == NULL)
- {
- ERROR ("dbi plugin: malloc failed.");
- BAIL_OUT_CONTINUE;
- }
- for (i = 1; i < r->instances_num; i++)
- instances[i] = instances[i - 1] + DATA_MAX_NAME_LEN;
-
- values = (value_t *) malloc (sizeof (*values) * r->values_num);
- if (values == NULL)
- {
- ERROR ("dbi plugin: malloc failed.");
- BAIL_OUT_CONTINUE;
- }
- /* }}} */
-
- /* Get instance names and values from the result: */
- for (i = 0; i < r->instances_num; i++) /* {{{ */
- {
- const char *inst;
-
- inst = dbi_result_get_string (res, r->instances[i]);
- if (dbi_conn_error (db->connection, NULL) != 0)
- {
- char errbuf[1024];
- ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
- "dbi_result_get_string (%s) failed: %s",
- db->name, q->name, r->instances[i],
- cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
- status = -1;
- break;
- }
-
- sstrncpy (instances[i], (inst == NULL) ? "" : inst, DATA_MAX_NAME_LEN);
- DEBUG ("dbi plugin: cdbi_read_database_query (%s, %s): "
- "instances[%zu] = %s;",
- db->name, q->name, i, instances[i]);
- } /* }}} for (i = 0; i < q->instances_num; i++) */
+ status = cdbi_result_get_field (res, (unsigned int) (i + 1),
+ column_values[i], DATA_MAX_NAME_LEN);
if (status != 0)
{
- BAIL_OUT_CONTINUE;
+ ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
+ "cdbi_result_get_field (%zu) failed.",
+ db->name, udb_query_get_name (q), i + 1);
+ status = -1;
+ break;
}
+ } /* }}} for (i = 0; i < column_num; i++) */
- for (i = 0; i < r->values_num; i++) /* {{{ */
- {
- status = cdbi_result_get_field (res, r->values[i], ds->ds[i].type,
- values + i);
- if (status != 0)
- {
- break;
- }
-
- if (ds->ds[i].type == DS_TYPE_COUNTER)
- {
- DEBUG ("dbi plugin: cdbi_read_database_query (%s, %s): values[%zu] = %llu;",
- db->name, q->name, i, values[i].counter);
- }
- else
- {
- DEBUG ("dbi plugin: cdbi_read_database_query (%s, %s): values[%zu] = %g;",
- db->name, q->name, i, values[i].gauge);
- }
- } /* }}} for (i = 0; i < q->values_num; i++) */
-
+ /* If all values were copied successfully, call `udb_query_handle_result'
+ * to dispatch the row to the daemon. */
+ if (status == 0) /* {{{ */
+ {
+ status = udb_query_handle_result (q, column_values);
if (status != 0)
{
- BAIL_OUT_CONTINUE;
+ ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
+ "udb_query_handle_result failed.",
+ db->name, udb_query_get_name (q));
}
-
- /* Dispatch this row to the daemon. */
- cdbi_submit (db, r, instances, values);
-
- BAIL_OUT_CONTINUE;
-#undef BAIL_OUT_CONTINUE
- } /* }}} for (r = q->results; r != NULL; r = r->next) */
+ } /* }}} */
/* Get the next row from the database. */
- status = dbi_result_next_row (res);
+ status = dbi_result_next_row (res); /* {{{ */
if (status != 1)
{
if (dbi_conn_error (db->connection, NULL) != 0)
char errbuf[1024];
WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
"dbi_result_next_row failed: %s.",
- db->name, q->name,
+ db->name, udb_query_get_name (q),
cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
}
break;
- }
+ } /* }}} */
} /* }}} while (42) */
+ /* Tell the db query interface that we're done with this query. */
+ udb_query_finish_result (q);
+
/* Clean up and return `status = 0' (success) */
BAIL_OUT (0);
#undef BAIL_OUT
sfree (databases);
databases_num = 0;
- for (i = 0; i < queries_num; i++)
- cdbi_query_free (queries[i]);
- sfree (queries);
+ udb_query_free (queries, queries_num);
+ queries = NULL;
queries_num = 0;
return (0);
diff --git a/src/utils_db_query.c b/src/utils_db_query.c
--- /dev/null
+++ b/src/utils_db_query.c
@@ -0,0 +1,845 @@
+/**
+ * collectd - src/utils_db_query.c
+ * Copyright (C) 2008,2009 Florian octo Forster
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors:
+ * Florian octo Forster <octo at verplant.org>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "configfile.h"
+#include "utils_db_query.h"
+
+/*
+ * Data types
+ */
+struct udb_result_s; /* {{{ */
+typedef struct udb_result_s udb_result_t;
+struct udb_result_s
+{
+ char *type;
+ char *instance_prefix;
+ char **instances;
+ size_t instances_num;
+ char **values;
+ size_t values_num;
+
+ /* Preparation area */
+ const data_set_t *ds;
+ size_t *instances_pos;
+ size_t *values_pos;
+ char **instances_buffer;
+ char **values_buffer;
+
+ udb_result_t *next;
+}; /* }}} */
+
+struct udb_query_s /* {{{ */
+{
+ char *name;
+ char *statement;
+ void *user_data;
+
+ /* Preparation area */
+ size_t column_num;
+ char *host;
+ char *plugin;
+ char *db_name;
+
+ udb_result_t *results;
+}; /* }}} */
+
+/*
+ * Config Private functions
+ */
+static int udb_config_set_string (char **ret_string, /* {{{ */
+ oconfig_item_t *ci)
+{
+ char *string;
+
+ if ((ci->values_num != 1)
+ || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ WARNING ("db query utils: The `%s' config option "
+ "needs exactly one string argument.", ci->key);
+ return (-1);
+ }
+
+ string = strdup (ci->values[0].value.string);
+ if (string == NULL)
+ {
+ ERROR ("db query utils: strdup failed.");
+ return (-1);
+ }
+
+ if (*ret_string != NULL)
+ free (*ret_string);
+ *ret_string = string;
+
+ return (0);
+} /* }}} int udb_config_set_string */
+
+static int udb_config_add_string (char ***ret_array, /* {{{ */
+ size_t *ret_array_len, oconfig_item_t *ci)
+{
+ char **array;
+ size_t array_len;
+ int i;
+
+ if (ci->values_num < 1)
+ {
+ WARNING ("db query utils: The `%s' config option "
+ "needs at least one argument.", ci->key);
+ return (-1);
+ }
+
+ for (i = 0; i < ci->values_num; i++)
+ {
+ if (ci->values[i].type != OCONFIG_TYPE_STRING)
+ {
+ WARNING ("db query utils: Argument %i to the `%s' option "
+ "is not a string.", i + 1, ci->key);
+ return (-1);
+ }
+ }
+
+ array_len = *ret_array_len;
+ array = (char **) realloc (*ret_array,
+ sizeof (char *) * (array_len + ci->values_num));
+ if (array == NULL)
+ {
+ ERROR ("db query utils: realloc failed.");
+ return (-1);
+ }
+ *ret_array = array;
+
+ for (i = 0; i < ci->values_num; i++)
+ {
+ array[array_len] = strdup (ci->values[i].value.string);
+ if (array[array_len] == NULL)
+ {
+ ERROR ("db query utils: strdup failed.");
+ *ret_array_len = array_len;
+ return (-1);
+ }
+ array_len++;
+ }
+
+ *ret_array_len = array_len;
+ return (0);
+} /* }}} int udb_config_add_string */
+
+/*
+ * Result private functions
+ */
+static void udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */
+{
+ value_list_t vl = VALUE_LIST_INIT;
+ size_t i;
+
+ assert (((size_t) r->ds->ds_num) == r->values_num);
+
+ DEBUG ("db query utils: udb_result_submit: r->instance_prefix = %s;",
+ (r->instance_prefix == NULL) ? "NULL" : r->instance_prefix);
+ for (i = 0; i < r->instances_num; i++)
+ {
+ DEBUG ("db query utils: udb_result_submit: r->instances_buffer[%zu] = %s;",
+ i, r->instances_buffer[i]);
+ }
+
+ vl.values = (value_t *) calloc (r->ds->ds_num, sizeof (value_t));
+ if (vl.values == NULL)
+ {
+ ERROR ("db query utils: malloc failed.");
+ return;
+ }
+ vl.values_len = r->ds->ds_num;
+
+ for (i = 0; i < r->values_num; i++)
+ {
+ char *endptr;
+
+ endptr = NULL;
+ errno = 0;
+ if (r->ds->ds[i].type == DS_TYPE_COUNTER)
+ vl.values[i].counter = (counter_t) strtoll (r->values_buffer[i],
+ &endptr, /* base = */ 0);
+ else if (r->ds->ds[i].type == DS_TYPE_GAUGE)
+ vl.values[i].gauge = (gauge_t) strtod (r->values_buffer[i], &endptr);
+ else
+ errno = EINVAL;
+
+ if ((endptr == r->values_buffer[i]) || (errno != 0))
+ {
+ WARNING ("db query utils: udb_result_submit: Parsing `%s' as %s failed.",
+ r->values_buffer[i],
+ (r->ds->ds[i].type == DS_TYPE_COUNTER) ? "counter" : "gauge");
+ vl.values[i].gauge = NAN;
+ }
+ }
+
+ vl.time = time (NULL);
+ sstrncpy (vl.host, q->host, sizeof (vl.host));
+ sstrncpy (vl.plugin, q->plugin, sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, q->db_name, sizeof (vl.type_instance));
+ sstrncpy (vl.type, r->type, sizeof (vl.type));
+
+ if (r->instance_prefix == NULL)
+ {
+ strjoin (vl.type_instance, sizeof (vl.type_instance),
+ r->instances_buffer, r->instances_num, "-");
+ }
+ else
+ {
+ char tmp[DATA_MAX_NAME_LEN];
+
+ strjoin (tmp, sizeof (tmp), r->instances_buffer, r->instances_num, "-");
+ tmp[sizeof (tmp) - 1] = 0;
+
+ snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
+ r->instance_prefix, tmp);
+ }
+ vl.type_instance[sizeof (vl.type_instance) - 1] = 0;
+
+ plugin_dispatch_values (&vl);
+
+ sfree (vl.values);
+} /* }}} void udb_result_submit */
+
+static void udb_result_finish_result (udb_result_t *r) /* {{{ */
+{
+ if (r == NULL)
+ return;
+
+ r->ds = NULL;
+ sfree (r->instances_pos);
+ sfree (r->values_pos);
+ sfree (r->instances_buffer);
+ sfree (r->values_buffer);
+} /* }}} void udb_result_finish_result */
+
+static int udb_result_handle_result (udb_result_t *r, /* {{{ */
+ udb_query_t *q, char **column_values)
+{
+ size_t i;
+
+ for (i = 0; i < r->instances_num; i++)
+ r->instances_buffer[i] = column_values[r->instances_pos[i]];
+
+ for (i = 0; i < r->values_num; i++)
+ r->values_buffer[i] = column_values[r->values_pos[i]];
+
+ udb_result_submit (r, q);
+
+ return (0);
+} /* }}} int udb_result_handle_result */
+
+static int udb_result_prepare_result (udb_result_t *r, /* {{{ */
+ char **column_names, size_t column_num)
+{
+ size_t i;
+
+ if (r == NULL)
+ return (-EINVAL);
+
+#define BAIL_OUT(status) \
+ r->ds = NULL; \
+ sfree (r->instances_pos); \
+ sfree (r->values_pos); \
+ sfree (r->instances_buffer); \
+ sfree (r->values_buffer); \
+ return (status)
+
+ /* Make sure previous preparations are cleaned up. */
+ udb_result_finish_result (r);
+ r->instances_pos = NULL;
+ r->values_pos = NULL;
+
+ /* Read `ds' and check number of values {{{ */
+ r->ds = plugin_get_ds (r->type);
+ if (r->ds == NULL)
+ {
+ ERROR ("db query utils: udb_result_prepare_result: Type `%s' is not "
+ "known by the daemon. See types.db(5) for details.",
+ r->type);
+ BAIL_OUT (-1);
+ }
+
+ if (((size_t) r->ds->ds_num) != r->values_num)
+ {
+ ERROR ("db query utils: udb_result_prepare_result: The type `%s' "
+ "requires exactly %i value%s, but the configuration specifies %zu.",
+ r->type,
+ r->ds->ds_num, (r->ds->ds_num == 1) ? "" : "s",
+ r->values_num);
+ BAIL_OUT (-1);
+ }
+ /* }}} */
+
+ /* Allocate r->instances_pos, r->values_pos, r->instances_buffer, and
+ * r->values_buffer {{{ */
+ r->instances_pos = (size_t *) calloc (r->instances_num, sizeof (size_t));
+ if (r->instances_pos == NULL)
+ {
+ ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
+ BAIL_OUT (-ENOMEM);
+ }
+
+ r->values_pos = (size_t *) calloc (r->values_num, sizeof (size_t));
+ if (r->values_pos == NULL)
+ {
+ ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
+ BAIL_OUT (-ENOMEM);
+ }
+
+ r->instances_buffer = (char **) calloc (r->instances_num, sizeof (char *));
+ if (r->instances_buffer == NULL)
+ {
+ ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
+ BAIL_OUT (-ENOMEM);
+ }
+
+ r->values_buffer = (char **) calloc (r->values_num, sizeof (char *));
+ if (r->values_buffer == NULL)
+ {
+ ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
+ BAIL_OUT (-ENOMEM);
+ }
+ /* }}} */
+
+ /* Determine the position of the instance columns {{{ */
+ for (i = 0; i < r->instances_num; i++)
+ {
+ size_t j;
+
+ for (j = 0; j < column_num; j++)
+ {
+ if (strcasecmp (r->instances[i], column_names[j]) == 0)
+ {
+ r->instances_pos[i] = j;
+ break;
+ }
+ }
+
+ if (j >= column_num)
+ {
+ ERROR ("db query utils: udb_result_prepare_result: "
+ "Column `%s' could not be found.",
+ r->instances[i]);
+ BAIL_OUT (-ENOENT);
+ }
+ } /* }}} for (i = 0; i < r->instances_num; i++) */
+
+ /* Determine the position of the value columns {{{ */
+ for (i = 0; i < r->values_num; i++)
+ {
+ size_t j;
+
+ for (j = 0; j < column_num; j++)
+ {
+ if (strcasecmp (r->values[i], column_names[j]) == 0)
+ {
+ r->values_pos[i] = j;
+ break;
+ }
+ }
+
+ if (j >= column_num)
+ {
+ ERROR ("db query utils: udb_result_prepare_result: "
+ "Column `%s' could not be found.",
+ r->values[i]);
+ BAIL_OUT (-ENOENT);
+ }
+ } /* }}} for (i = 0; i < r->values_num; i++) */
+
+#undef BAIL_OUT
+ return (0);
+} /* }}} int udb_result_prepare_result */
+
+static void udb_result_free (udb_result_t *r) /* {{{ */
+{
+ size_t i;
+
+ if (r == NULL)
+ return;
+
+ sfree (r->type);
+
+ for (i = 0; i < r->instances_num; i++)
+ sfree (r->instances[i]);
+ sfree (r->instances);
+
+ for (i = 0; i < r->values_num; i++)
+ sfree (r->values[i]);
+ sfree (r->values);
+
+ udb_result_free (r->next);
+
+ sfree (r);
+} /* }}} void udb_result_free */
+
+static int udb_result_create (const char *query_name, /* {{{ */
+ udb_result_t **r_head, oconfig_item_t *ci)
+{
+ udb_result_t *r;
+ int status;
+ int i;
+
+ if (ci->values_num != 0)
+ {
+ WARNING ("db query utils: The `Result' block doesn't accept "
+ "any arguments. Ignoring %i argument%s.",
+ ci->values_num, (ci->values_num == 1) ? "" : "s");
+ }
+
+ r = (udb_result_t *) malloc (sizeof (*r));
+ if (r == NULL)
+ {
+ ERROR ("db query utils: malloc failed.");
+ return (-1);
+ }
+ memset (r, 0, sizeof (*r));
+ r->type = NULL;
+ r->instance_prefix = NULL;
+ r->instances = NULL;
+ r->values = NULL;
+ r->next = NULL;
+
+ /* Fill the `udb_result_t' structure.. */
+ status = 0;
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;
+
+ if (strcasecmp ("Type", child->key) == 0)
+ status = udb_config_set_string (&r->type, child);
+ else if (strcasecmp ("InstancePrefix", child->key) == 0)
+ status = udb_config_set_string (&r->instance_prefix, child);
+ else if (strcasecmp ("InstancesFrom", child->key) == 0)
+ status = udb_config_add_string (&r->instances, &r->instances_num, child);
+ else if (strcasecmp ("ValuesFrom", child->key) == 0)
+ status = udb_config_add_string (&r->values, &r->values_num, child);
+ else
+ {
+ WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
+ query_name, child->key);
+ status = -1;
+ }
+
+ if (status != 0)
+ break;
+ }
+
+ /* Check that all necessary options have been given. */
+ while (status == 0)
+ {
+ if (r->type == NULL)
+ {
+ WARNING ("db query utils: `Type' not given for "
+ "result in query `%s'", query_name);
+ status = -1;
+ }
+ if (r->instances == NULL)
+ {
+ WARNING ("db query utils: `InstancesFrom' not given for "
+ "result in query `%s'", query_name);
+ status = -1;
+ }
+ if (r->values == NULL)
+ {
+ WARNING ("db query utils: `ValuesFrom' not given for "
+ "result in query `%s'", query_name);
+ status = -1;
+ }
+
+ break;
+ } /* while (status == 0) */
+
+ if (status != 0)
+ {
+ udb_result_free (r);
+ return (-1);
+ }
+
+ /* If all went well, add this result to the list of results. */
+ if (*r_head == NULL)
+ {
+ *r_head = r;
+ }
+ else
+ {
+ udb_result_t *last;
+
+ last = *r_head;
+ while (last->next != NULL)
+ last = last->next;
+
+ last->next = r;
+ }
+
+ return (0);
+} /* }}} int udb_result_create */
+
+/*
+ * Query private functions
+ */
+void udb_query_free_one (udb_query_t *q) /* {{{ */
+{
+ if (q == NULL)
+ return;
+
+ sfree (q->name);
+ sfree (q->statement);
+
+ udb_result_free (q->results);
+
+ sfree (q);
+} /* }}} void udb_query_free_one */
+
+/*
+ * Query public functions
+ */
+int udb_query_create (udb_query_t ***ret_query_list, /* {{{ */
+ size_t *ret_query_list_len, oconfig_item_t *ci)
+{
+ udb_query_t **query_list;
+ size_t query_list_len;
+
+ udb_query_t *q;
+ int status;
+ int i;
+
+ if ((ret_query_list == NULL) || (ret_query_list_len == NULL))
+ return (-EINVAL);
+ query_list = *ret_query_list;
+ query_list_len = *ret_query_list_len;
+
+ if ((ci->values_num != 1)
+ || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ WARNING ("db query utils: The `Query' block "
+ "needs exactly one string argument.");
+ return (-1);
+ }
+
+ q = (udb_query_t *) malloc (sizeof (*q));
+ if (q == NULL)
+ {
+ ERROR ("db query utils: malloc failed.");
+ return (-1);
+ }
+ memset (q, 0, sizeof (*q));
+
+ status = udb_config_set_string (&q->name, ci);
+ if (status != 0)
+ {
+ sfree (q);
+ return (status);
+ }
+
+ /* Fill the `udb_query_t' structure.. */
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;
+
+ if (strcasecmp ("Statement", child->key) == 0)
+ status = udb_config_set_string (&q->statement, child);
+ else if (strcasecmp ("Result", child->key) == 0)
+ status = udb_result_create (q->name, &q->results, child);
+ else
+ {
+ WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
+ q->name, child->key);
+ status = -1;
+ }
+
+ if (status != 0)
+ break;
+ }
+
+ /* Check that all necessary options have been given. */
+ if (status == 0)
+ {
+ if (q->statement == NULL)
+ {
+ WARNING ("db query utils: Query `%s': No `Statement' given.", q->name);
+ status = -1;
+ }
+ if (q->results == NULL)
+ {
+ WARNING ("db query utils: Query `%s': No (valid) `Result' block given.",
+ q->name);
+ status = -1;
+ }
+ } /* if (status == 0) */
+
+ /* If all went well, add this query to the list of queries within the
+ * database structure. */
+ if (status == 0)
+ {
+ udb_query_t **temp;
+
+ temp = (udb_query_t **) realloc (query_list,
+ sizeof (*query_list) * (query_list_len + 1));
+ if (temp == NULL)
+ {
+ ERROR ("db query utils: realloc failed");
+ status = -1;
+ }
+ else
+ {
+ query_list = temp;
+ query_list[query_list_len] = q;
+ query_list_len++;
+ }
+ }
+
+ if (status != 0)
+ {
+ udb_query_free_one (q);
+ return (-1);
+ }
+
+ *ret_query_list = query_list;
+ *ret_query_list_len = query_list_len;
+
+ return (0);
+} /* }}} int udb_query_create */
+
+void udb_query_free (udb_query_t **query_list, size_t query_list_len) /* {{{ */
+{
+ size_t i;
+
+ if (query_list == NULL)
+ return;
+
+ for (i = 0; i < query_list_len; i++)
+ udb_query_free_one (query_list[i]);
+
+ sfree (query_list);
+} /* }}} void udb_query_free */
+
+int udb_query_pick_from_list (oconfig_item_t *ci, /* {{{ */
+ udb_query_t **src_list, size_t src_list_len,
+ udb_query_t ***dst_list, size_t *dst_list_len)
+{
+ const char *name;
+ udb_query_t *q;
+ udb_query_t **tmp_list;
+ size_t tmp_list_len;
+ size_t i;
+
+ if ((ci == NULL) || (src_list == NULL) || (dst_list == NULL)
+ || (dst_list_len == NULL))
+ {
+ ERROR ("db query utils: Invalid argument.");
+ return (-EINVAL);
+ }
+
+ if ((ci->values_num != 1)
+ || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ ERROR ("db query utils: The `%s' config option "
+ "needs exactly one string argument.", ci->key);
+ return (-1);
+ }
+ name = ci->values[0].value.string;
+
+ q = NULL;
+ for (i = 0; i < src_list_len; i++)
+ if (strcasecmp (name, src_list[i]->name) == 0)
+ {
+ q = src_list[i];
+ break;
+ }
+
+ if (q == NULL)
+ {
+ ERROR ("db query utils: Cannot find query `%s'. Make sure the <%s> "
+ "block is above the database definition!",
+ name, ci->key);
+ return (-ENOENT);
+ }
+
+ tmp_list_len = *dst_list_len;
+ tmp_list = (udb_query_t **) realloc (*dst_list, (tmp_list_len + 1)
+ * sizeof (udb_query_t *));
+ if (tmp_list == NULL)
+ {
+ ERROR ("db query utils: realloc failed.");
+ return (-ENOMEM);
+ }
+ tmp_list[tmp_list_len] = q;
+ tmp_list_len++;
+
+ *dst_list = tmp_list;
+ *dst_list_len = tmp_list_len;
+
+ return (0);
+} /* }}} int udb_query_pick_from_list */
+
+const char *udb_query_get_name (udb_query_t *q) /* {{{ */
+{
+ if (q == NULL)
+ return (NULL);
+
+ return (q->name);
+} /* }}} const char *udb_query_get_name */
+
+const char *udb_query_get_statement (udb_query_t *q) /* {{{ */
+{
+ if (q == NULL)
+ return (NULL);
+
+ return (q->statement);
+} /* }}} const char *udb_query_get_statement */
+
+void udb_query_set_user_data (udb_query_t *q, void *user_data) /* {{{ */
+{
+ if (q == NULL)
+ return;
+
+ q->user_data = user_data;
+} /* }}} void udb_query_set_user_data */
+
+void *udb_query_get_user_data (udb_query_t *q) /* {{{ */
+{
+ if (q == NULL)
+ return (NULL);
+
+ return (q->statement);
+} /* }}} void *udb_query_get_user_data */
+
+void udb_query_finish_result (udb_query_t *q) /* {{{ */
+{
+ udb_result_t *r;
+
+ if (q == NULL)
+ return;
+
+ q->column_num = 0;
+ sfree (q->host);
+ sfree (q->plugin);
+ sfree (q->db_name);
+
+ for (r = q->results; r != NULL; r = r->next)
+ udb_result_finish_result (r);
+} /* }}} void udb_query_finish_result */
+
+int udb_query_handle_result (udb_query_t *q, char **column_values) /* {{{ */
+{
+ udb_result_t *r;
+ int success;
+ int status;
+
+ if (q == NULL)
+ return (-EINVAL);
+
+ if ((q->column_num < 1) || (q->host == NULL) || (q->plugin == NULL)
+ || (q->db_name == NULL))
+ {
+ ERROR ("db query utils: Query `%s': Query is not prepared; "
+ "can't handle result.", q->name);
+ return (-EINVAL);
+ }
+
+#if defined(COLLECT_DEBUG) && COLLECT_DEBUG /* {{{ */
+ do
+ {
+ size_t i;
+
+ for (i = 0; i < q->column_num; i++)
+ {
+ DEBUG ("db query utils: udb_query_handle_result (%s, %s): "
+ "column[%zu] = %s;",
+ q->db_name, q->name, i, column_values[i]);
+ }
+ } while (0);
+#endif /* }}} */
+
+ success = 0;
+ for (r = q->results; r != NULL; r = r->next)
+ {
+ status = udb_result_handle_result (r, q, column_values);
+ if (status == 0)
+ success++;
+ }
+
+ if (success == 0)
+ {
+ ERROR ("db query utils: udb_query_handle_result (%s, %s): "
+ "All results failed.", q->db_name, q->name);
+ return (-1);
+ }
+
+ return (0);
+} /* }}} int udb_query_handle_result */
+
+int udb_query_prepare_result (udb_query_t *q, /* {{{ */
+ const char *host, const char *plugin, const char *db_name,
+ char **column_names, size_t column_num)
+{
+ udb_result_t *r;
+ int status;
+
+ if (q == NULL)
+ return (-EINVAL);
+
+ udb_query_finish_result (q);
+
+ q->column_num = column_num;
+ q->host = strdup (host);
+ q->plugin = strdup (plugin);
+ q->db_name = strdup (db_name);
+
+ if ((q->host == NULL) || (q->plugin == NULL) || (q->db_name == NULL))
+ {
+ ERROR ("db query utils: Query `%s': Prepare failed: Out of memory.", q->name);
+ udb_query_finish_result (q);
+ return (-ENOMEM);
+ }
+
+#if defined(COLLECT_DEBUG) && COLLECT_DEBUG
+ do
+ {
+ size_t i;
+
+ for (i = 0; i < column_num; i++)
+ {
+ DEBUG ("db query utils: udb_query_prepare_result: "
+ "query = %s; column[%zu] = %s;",
+ q->name, i, column_names[i]);
+ }
+ } while (0);
+#endif
+
+ for (r = q->results; r != NULL; r = r->next)
+ {
+ status = udb_result_prepare_result (r, column_names, column_num);
+ if (status != 0)
+ {
+ udb_query_finish_result (q);
+ return (status);
+ }
+ }
+
+ return (0);
+} /* }}} int udb_query_prepare_result */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/src/utils_db_query.h b/src/utils_db_query.h
--- /dev/null
+++ b/src/utils_db_query.h
@@ -0,0 +1,57 @@
+/**
+ * collectd - src/utils_db_query.h
+ * Copyright (C) 2008,2009 Florian octo Forster
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors:
+ * Florian octo Forster <octo at verplant.org>
+ **/
+
+#ifndef UTILS_DB_QUERY_H
+#define UTILS_DB_QUERY_H 1
+
+#include "configfile.h"
+
+/*
+ * Data types
+ */
+struct udb_query_s;
+typedef struct udb_query_s udb_query_t;
+
+/*
+ * Public functions
+ */
+int udb_query_create (udb_query_t ***ret_query_list,
+ size_t *ret_query_list_len, oconfig_item_t *ci);
+void udb_query_free (udb_query_t **query_list, size_t query_list_len);
+
+int udb_query_pick_from_list (oconfig_item_t *ci,
+ udb_query_t **src_list, size_t src_list_len,
+ udb_query_t ***dst_list, size_t *dst_list_len);
+
+const char *udb_query_get_name (udb_query_t *q);
+const char *udb_query_get_statement (udb_query_t *q);
+
+void udb_query_set_user_data (udb_query_t *q, void *user_data);
+void *udb_query_get_user_data (udb_query_t *q);
+
+int udb_query_prepare_result (udb_query_t *q,
+ const char *host, const char *plugin, const char *db_name,
+ char **column_names, size_t column_num);
+int udb_query_handle_result (udb_query_t *q, char **column_values);
+void udb_query_finish_result (udb_query_t *q);
+
+#endif /* UTILS_DB_QUERY_H */
+/* vim: set sw=2 sts=2 et : */