Code

postgresql plugin: Pass an array of data-source types to writers.
authorSebastian Harl <sh@tokkee.org>
Sun, 19 Aug 2012 12:14:03 +0000 (14:14 +0200)
committerSebastian Harl <sh@tokkee.org>
Sun, 19 Aug 2012 12:14:03 +0000 (14:14 +0200)
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
src/postgresql.c

index 6a1cdd2cad75f2a32103d787cb8335dbe642bc6f..ca4dcd53fbfc32baebecc60b6537f2d88c021817 100644 (file)
@@ -3487,7 +3487,8 @@ L<http://www.postgresql.org/docs/manuals/>.
     </Query>
 
     <Writer sqlstore>
-      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
     </Writer>
 
     <Database foo>
@@ -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.E<nbsp>e., the type of the data
+sources of the submitted value-list; C<counter>, C<gauge>, ...). Note, that if
+B<StoreRates> is enabled (which is the default, see below), all types will be
+C<gauge>.
+
+=item B<$9>
+
 An array of the submitted values. The dimensions of the value name and value
 arrays match.
 
index 62a54da4380d24c95f5d17079a83e9677ed56f60..34ce527e8a76f216e71a9c5f1d20a6ab8fd9a820 100644 (file)
@@ -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,