From 6813fcc6b2fa76e47668fc7e70d2a99ad4d327ca Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 19 Aug 2012 14:14:03 +0200 Subject: [PATCH] postgresql plugin: Pass an array of data-source types to writers. The array includes the stringified version of the data-source type, i.e., 'gauge', 'counter', etc. This will enable the writer to treat different types differently. However, when having 'StoreRates' enabled (the default) all types will be 'gauge'. So, in most cases, this won't be used but it has been added for completeness and to be safe for the future. --- src/collectd.conf.pod | 14 ++++++++--- src/postgresql.c | 58 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 6a1cdd2c..ca4dcd53 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -3487,7 +3487,8 @@ L. - Statement "SELECT collectd_insert($1, $2, $3, $4, $5, $6, $7, $8);" + Statement "SELECT collectd_insert($1, $2, $3, $4, $5, $6, $7, $8, $9);" + StoreRates true @@ -3686,8 +3687,8 @@ This mandatory option specifies the SQL statement that will be executed for each submitted value. A single SQL statement is allowed only. Anything after the first semicolon will be ignored. -Eight parameters will be passed to the statement and should be specified as -tokens B<$1>, B<$2>, through B<$8> in the statement string. The following +Nine parameters will be passed to the statement and should be specified as +tokens B<$1>, B<$2>, through B<$9> in the statement string. The following values are made available through those parameters: =over 4 @@ -3725,6 +3726,13 @@ sources of the submitted value-list). =item B<$8> +An array of types for the submitted values (i.Ee., the type of the data +sources of the submitted value-list; C, C, ...). Note, that if +B is enabled (which is the default, see below), all types will be +C. + +=item B<$9> + An array of the submitted values. The dimensions of the value name and value arrays match. diff --git a/src/postgresql.c b/src/postgresql.c index 62a54da4..34ce527e 100644 --- a/src/postgresql.c +++ b/src/postgresql.c @@ -598,6 +598,53 @@ static char *values_name_to_sqlarray (const data_set_t *ds, return string; } /* values_name_to_sqlarray */ +static char *values_type_to_sqlarray (const data_set_t *ds, + char *string, size_t string_len, _Bool store_rates) +{ + char *str_ptr; + size_t str_len; + + int i; + + str_ptr = string; + str_len = string_len; + + for (i = 0; i < ds->ds_num; ++i) { + int status; + + if (store_rates) + status = ssnprintf(str_ptr, str_len, ",'gauge'"); + else + status = ssnprintf(str_ptr, str_len, ",'%s'", + DS_TYPE_TO_STRING (ds->ds[i].type)); + + if (status < 1) { + str_len = 0; + break; + } + else if ((size_t)status >= str_len) { + str_len = 0; + break; + } + else { + str_ptr += status; + str_len -= (size_t)status; + } + } + + if (str_len <= 2) { + log_err ("c_psql_write: Failed to stringify value types"); + return NULL; + } + + /* overwrite the first comma */ + string[0] = '{'; + str_ptr[0] = '}'; + str_ptr[1] = '\0'; + + return string; +} /* values_type_to_sqlarray */ + static char *values_to_sqlarray (const data_set_t *ds, const value_list_t *vl, char *string, size_t string_len, _Bool store_rates) { @@ -685,9 +732,10 @@ static int c_psql_write (const data_set_t *ds, const value_list_t *vl, char time_str[32]; char values_name_str[1024]; + char values_type_str[1024]; char values_str[1024]; - const char *params[8]; + const char *params[9]; int success = 0; int i; @@ -735,12 +783,18 @@ static int c_psql_write (const data_set_t *ds, const value_list_t *vl, writer = db->writers[i]; + if (values_type_to_sqlarray (ds, + values_type_str, sizeof (values_type_str), + writer->store_rates) == NULL) + return -1; + if (values_to_sqlarray (ds, vl, values_str, sizeof (values_str), writer->store_rates) == NULL) return -1; - params[7] = values_str; + params[7] = values_type_str; + params[8] = values_str; res = PQexecParams (db->conn, writer->statement, STATIC_ARRAY_SIZE (params), NULL, -- 2.30.2