char **values;
size_t values_num;
- /* Legacy data */
- int legacy_mode;
- size_t legacy_position;
- /* When in legacy mode:
- * - type/ds hold the format of the data
- * - instance_prefix is used as type-instance if non-NULL
- * - legacy_position holds the index of the column to use as value.
- */
-
udb_result_t *next;
}; /* }}} */
char *statement;
void *user_data;
- int legacy_mode;
-
unsigned int min_version;
unsigned int max_version;
} /* }}} int udb_config_set_uint */
/*
- * Legacy result private functions
- */
-static void udb_legacy_result_finish_result (const udb_result_t const *r, /* {{{ */
- udb_result_preparation_area_t *prep_area)
-{
- if ((r == NULL) || (prep_area))
- return;
-
- assert (r->legacy_mode == 1);
-
- prep_area->ds = NULL;
-} /* }}} void udb_legacy_result_finish_result */
-
-static int udb_legacy_result_handle_result (udb_result_t *r, /* {{{ */
- udb_query_preparation_area_t *q_area,
- udb_result_preparation_area_t *r_area,
- const udb_query_t const *q, char **column_values)
-{
- value_list_t vl = VALUE_LIST_INIT;
- value_t value;
- char *value_str;
-
- assert (r->legacy_mode == 1);
- assert (r_area->ds != NULL);
- assert (r_area->ds->ds_num == 1);
-
- vl.values = &value;
- vl.values_len = 1;
-
- value_str = column_values[r->legacy_position];
- if (0 != parse_value (value_str, &vl.values[0], r_area->ds->ds[0].type))
- {
- ERROR ("db query utils: udb_legacy_result_handle_result: "
- "Parsing `%s' as %s failed.", value_str,
- DS_TYPE_TO_STRING (r_area->ds->ds[0].type));
- errno = EINVAL;
- return (-1);
- }
-
- if (q_area->interval > 0)
- vl.interval = q_area->interval;
-
- sstrncpy (vl.host, q_area->host, sizeof (vl.host));
- sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin));
- sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.type_instance));
- sstrncpy (vl.type, r->type, sizeof (vl.type));
-
- if (r->instance_prefix != NULL)
- sstrncpy (vl.type_instance, r->instance_prefix,
- sizeof (vl.type_instance));
-
- plugin_dispatch_values (&vl);
-
- return (0);
-} /* }}} int udb_legacy_result_handle_result */
-
-static int udb_legacy_result_prepare_result (const udb_result_t const *r, /* {{{ */
- udb_result_preparation_area_t *prep_area,
- char **column_names, size_t column_num)
-{
- if (r == NULL)
- return (-EINVAL);
-
- assert (r->legacy_mode == 1);
-
- /* Make sure previous preparations are cleaned up. */
- udb_legacy_result_finish_result (r, prep_area);
-
- if (r->legacy_position >= column_num)
- {
- ERROR ("db query utils: The legacy configuration specified (at least) "
- "%zu `Column's, but the query returned only %zu columns!",
- r->legacy_position + 1, column_num);
- return (-ENOENT);
- }
-
- /* Read `ds' and check number of values {{{ */
- prep_area->ds = plugin_get_ds (r->type);
- if (prep_area->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);
- return (-1);
- }
-
- if (prep_area->ds->ds_num != 1)
- {
- ERROR ("db query utils: udb_result_prepare_result: The type `%s' "
- "requires exactly %i values, but the legacy configuration "
- "requires exactly one!",
- r->type,
- prep_area->ds->ds_num);
- return (-1);
- }
- /* }}} */
-
- return (0);
-} /* }}} int udb_legacy_result_prepare_result */
-
-static int udb_legacy_result_create (const char *query_name, /* {{{ */
- udb_result_t **r_head, oconfig_item_t *ci, size_t position)
-{
- udb_result_t *r;
-
- if ((ci->values_num < 1) || (ci->values_num > 2)
- || (ci->values[0].type != OCONFIG_TYPE_STRING)
- || ((ci->values_num == 2)
- && (ci->values[1].type != OCONFIG_TYPE_STRING)))
- {
- WARNING ("db query utils: The `Column' block needs either one or two "
- "string arguments.");
- return (-1);
- }
-
- r = (udb_result_t *) malloc (sizeof (*r));
- if (r == NULL)
- {
- ERROR ("db query utils: malloc failed.");
- return (-1);
- }
- memset (r, 0, sizeof (*r));
-
- r->legacy_mode = 1;
- r->legacy_position = position;
-
- r->type = strdup (ci->values[0].value.string);
- if (r->type == NULL)
- {
- ERROR ("db query utils: strdup failed.");
- free (r);
- return (-1);
- }
-
- r->instance_prefix = NULL;
- if (ci->values_num == 2)
- {
- r->instance_prefix = strdup (ci->values[1].value.string);
- if (r->instance_prefix == NULL)
- {
- ERROR ("db query utils: strdup failed.");
- free (r->type);
- 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_legacy_result_create */
-
-/*
* Result private functions
*/
static int udb_result_submit (udb_result_t *r, /* {{{ */
size_t i;
assert (r != NULL);
- assert (r->legacy_mode == 0);
assert (r_area->ds != NULL);
assert (((size_t) r_area->ds->ds_num) == r->values_num);
if ((r == NULL) || (prep_area == NULL))
return;
- if (r->legacy_mode == 1)
- {
- udb_legacy_result_finish_result (r, prep_area);
- return;
- }
-
- assert (r->legacy_mode == 0);
-
prep_area->ds = NULL;
sfree (prep_area->instances_pos);
sfree (prep_area->values_pos);
assert (r && q_area && r_area);
- if (r->legacy_mode == 1)
- return (udb_legacy_result_handle_result (r, q_area, r_area,
- q, column_values));
-
- assert (r->legacy_mode == 0);
-
for (i = 0; i < r->instances_num; i++)
r_area->instances_buffer[i] = column_values[r_area->instances_pos[i]];
if ((r == NULL) || (prep_area == NULL))
return (-EINVAL);
- if (r->legacy_mode == 1)
- return (udb_legacy_result_prepare_result (r, prep_area,
- column_names, column_num));
-
- assert (r->legacy_mode == 0);
-
#define BAIL_OUT(status) \
prep_area->ds = NULL; \
sfree (prep_area->instances_pos); \
*/
int udb_query_create (udb_query_t ***ret_query_list, /* {{{ */
size_t *ret_query_list_len, oconfig_item_t *ci,
- udb_query_create_callback_t cb, int legacy_mode)
+ udb_query_create_callback_t cb)
{
udb_query_t **query_list;
size_t query_list_len;
int status;
int i;
- size_t legacy_position;
-
if ((ret_query_list == NULL) || (ret_query_list_len == NULL))
return (-EINVAL);
query_list = *ret_query_list;
return (-1);
}
memset (q, 0, sizeof (*q));
- q->legacy_mode = legacy_mode;
q->min_version = 0;
q->max_version = UINT_MAX;
- legacy_position = 0;
-
status = udb_config_set_string (&q->name, ci);
if (status != 0)
{
else if (strcasecmp ("MaxVersion", child->key) == 0)
status = udb_config_set_uint (&q->max_version, child);
- /* PostgreSQL compatibility code */
- else if ((strcasecmp ("Query", child->key) == 0)
- && (q->legacy_mode == 1))
- {
- WARNING ("db query utils: Query `%s': The `Query' option is "
- "deprecated. Please use `Statement' instead.",
- q->name);
- status = udb_config_set_string (&q->statement, child);
- }
- else if ((strcasecmp ("Column", child->key) == 0)
- && (q->legacy_mode == 1))
- {
- WARNING ("db query utils: Query `%s': The `Column' option is "
- "deprecated. Please use the new syntax instead.",
- q->name);
- status = udb_legacy_result_create (q->name, &q->results, child,
- legacy_position);
- legacy_position++;
- }
- else if ((strcasecmp ("MinPGVersion", child->key) == 0)
- && (q->legacy_mode == 1))
- {
- WARNING ("db query utils: Query `%s': The `MinPGVersion' option is "
- "deprecated. Please use `MinVersion' instead.",
- q->name);
- status = udb_config_set_uint (&q->min_version, child);
- }
- else if ((strcasecmp ("MaxPGVersion", child->key) == 0)
- && (q->legacy_mode == 1))
- {
- WARNING ("db query utils: Query `%s': The `MaxPGVersion' option is "
- "deprecated. Please use `MaxVersion' instead.",
- q->name);
- status = udb_config_set_uint (&q->max_version, child);
- }
-
/* Call custom callbacks */
else if (cb != NULL)
{