From c7ad61ce3f92b3430983e10e2d47e66ce1e44f26 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 16 Feb 2009 18:17:53 +0100 Subject: [PATCH] src/utils_db_query.c: Make `InstancePrefix' and `InstancesFrom' optional. This is required by the default PostgreSQL queries. --- src/collectd.conf.pod | 20 +++++++----- src/utils_db_query.c | 72 ++++++++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index a1c2b073..e323c36c 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -653,20 +653,24 @@ There must be exactly one B option inside each B block. =item B I -Prepends I followed by a dash I<("-")> to the type instance. See -B on how the rest of the type instance is built. +Prepends I to the type instance. If B (see below) is not +given, the string is simply copied. If B is given, I and +all strings returned in the appropriate columns are concatenated together, +separated by dahes I<("-")>. =item B I [I ...] -Specifies the columns whose values will be used to create the "TypeInstance" -for each row. You need to specify at least one column for each query. If you -specify more than one column, the value of all columns will be join together -with the hyphen as separation character. +Specifies the columns whose values will be used to create the "type-instance" +for each row. If you specify more than one column, the value of all columns +will be join together with the dahes I<("-")> as separation character. The plugin itself does not check whether or not all built instances are -different. It's your responsibility to assure that each is unique. +different. It's your responsibility to assure that each is unique. This is +especially true, if you do not specify B: B have to make +sure that only one row is returned in this case. -There must be at least one B option inside each B block. +If neither B nor B is given, the type-instance +will be empty. =item B I [I ...] diff --git a/src/utils_db_query.c b/src/utils_db_query.c index 33107649..c2897c7c 100644 --- a/src/utils_db_query.c +++ b/src/utils_db_query.c @@ -361,14 +361,6 @@ static void udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */ assert (r->ds != NULL); 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) { @@ -405,22 +397,35 @@ static void udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */ sstrncpy (vl.plugin_instance, q->db_name, sizeof (vl.type_instance)); sstrncpy (vl.type, r->type, sizeof (vl.type)); - if (r->instance_prefix == NULL) + /* Set vl.type_instance {{{ */ + if (r->instances_num <= 0) { - strjoin (vl.type_instance, sizeof (vl.type_instance), - r->instances_buffer, r->instances_num, "-"); + if (r->instance_prefix == NULL) + vl.type_instance[0] = 0; + else + sstrncpy (vl.type_instance, r->instance_prefix, + sizeof (vl.type_instance)); } - else + else /* if ((r->instances_num > 0) */ { - char tmp[DATA_MAX_NAME_LEN]; + 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; + 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); + 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); @@ -517,12 +522,22 @@ static int udb_result_prepare_result (udb_result_t *r, /* {{{ */ /* 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) + if (r->instances_num > 0) { - ERROR ("db query utils: udb_result_prepare_result: malloc failed."); - BAIL_OUT (-ENOMEM); - } + 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->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); + } + } /* if (r->instances_num > 0) */ r->values_pos = (size_t *) calloc (r->values_num, sizeof (size_t)); if (r->values_pos == NULL) @@ -531,13 +546,6 @@ static int udb_result_prepare_result (udb_result_t *r, /* {{{ */ 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) { @@ -679,12 +687,6 @@ static int udb_result_create (const char *query_name, /* {{{ */ "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 " -- 2.30.2