Updated the TODO file.
[collectd.git] / src / postgresql.c
1 /**
2  * collectd - src/postgresql.c
3  * Copyright (C) 2008  Sebastian Harl
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Sebastian Harl <sh at tokkee.org>
20  **/
21
22 /*
23  * This module collects PostgreSQL database statistics.
24  */
25
26 #include "collectd.h"
27 #include "common.h"
28
29 #include "configfile.h"
30 #include "plugin.h"
31
32 #include "utils_complain.h"
33
34 #include <pg_config_manual.h>
35 #include <libpq-fe.h>
36
37 #define log_err(...) ERROR ("postgresql: " __VA_ARGS__)
38 #define log_warn(...) WARNING ("postgresql: " __VA_ARGS__)
39 #define log_info(...) INFO ("postgresql: " __VA_ARGS__)
40
41 #ifndef C_PSQL_DEFAULT_CONF
42 # define C_PSQL_DEFAULT_CONF PKGDATADIR "/postgresql_default.conf"
43 #endif
44
45 /* Appends the (parameter, value) pair to the string
46  * pointed to by 'buf' suitable to be used as argument
47  * for PQconnectdb(). If value equals NULL, the pair
48  * is ignored. */
49 #define C_PSQL_PAR_APPEND(buf, buf_len, parameter, value) \
50         if ((0 < (buf_len)) && (NULL != (value)) && ('\0' != *(value))) { \
51                 int s = ssnprintf (buf, buf_len, " %s = '%s'", parameter, value); \
52                 if (0 < s) { \
53                         buf     += s; \
54                         buf_len -= s; \
55                 } \
56         }
57
58 /* Returns the tuple (major, minor, patchlevel)
59  * for the given version number. */
60 #define C_PSQL_SERVER_VERSION3(server_version) \
61         (server_version) / 10000, \
62         (server_version) / 100 - (int)((server_version) / 10000) * 100, \
63         (server_version) - (int)((server_version) / 100) * 100
64
65 /* Returns true if the given host specifies a
66  * UNIX domain socket. */
67 #define C_PSQL_IS_UNIX_DOMAIN_SOCKET(host) \
68         ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host)))
69
70 /* Returns the tuple (host, delimiter, port) for a
71  * given (host, port) pair. Depending on the value of
72  * 'host' a UNIX domain socket or a TCP socket is
73  * assumed. */
74 #define C_PSQL_SOCKET3(host, port) \
75         ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, \
76         C_PSQL_IS_UNIX_DOMAIN_SOCKET (host) ? "/.s.PGSQL." : ":", \
77         port
78
79 typedef enum {
80         C_PSQL_PARAM_HOST = 1,
81         C_PSQL_PARAM_DB,
82         C_PSQL_PARAM_USER,
83 } c_psql_param_t;
84
85 typedef struct {
86         char *type;
87         char *type_instance;
88         int   ds_type;
89 } c_psql_col_t;
90
91 typedef struct {
92         char *name;
93         char *query;
94
95         c_psql_param_t *params;
96         int             params_num;
97
98         c_psql_col_t *cols;
99         int           cols_num;
100
101         int min_pg_version;
102         int max_pg_version;
103 } c_psql_query_t;
104
105 typedef struct {
106         PGconn      *conn;
107         c_complain_t conn_complaint;
108
109         int proto_version;
110
111         int max_params_num;
112
113         /* user configuration */
114         c_psql_query_t **queries;
115         int              queries_num;
116
117         char *host;
118         char *port;
119         char *database;
120         char *user;
121         char *password;
122
123         char *sslmode;
124
125         char *krbsrvname;
126
127         char *service;
128 } c_psql_database_t;
129
130 static char *def_queries[] = {
131         "backends",
132         "transactions",
133         "queries",
134         "query_plans",
135         "table_states",
136         "disk_io",
137         "disk_usage"
138 };
139 static int def_queries_num = STATIC_ARRAY_SIZE (def_queries);
140
141 static c_psql_query_t *queries          = NULL;
142 static int             queries_num      = 0;
143
144 static c_psql_database_t *databases     = NULL;
145 static int                databases_num = 0;
146
147 static c_psql_query_t *c_psql_query_new (const char *name)
148 {
149         c_psql_query_t *query;
150
151         ++queries_num;
152         if (NULL == (queries = (c_psql_query_t *)realloc (queries,
153                                 queries_num * sizeof (*queries)))) {
154                 log_err ("Out of memory.");
155                 exit (5);
156         }
157         query = queries + queries_num - 1;
158
159         query->name  = sstrdup (name);
160         query->query = NULL;
161
162         query->params     = NULL;
163         query->params_num = 0;
164
165         query->cols     = NULL;
166         query->cols_num = 0;
167
168         query->min_pg_version = 0;
169         query->max_pg_version = INT_MAX;
170         return query;
171 } /* c_psql_query_new */
172
173 static void c_psql_query_delete (c_psql_query_t *query)
174 {
175         int i;
176
177         sfree (query->name);
178         sfree (query->query);
179
180         sfree (query->params);
181         query->params_num = 0;
182
183         for (i = 0; i < query->cols_num; ++i) {
184                 sfree (query->cols[i].type);
185                 sfree (query->cols[i].type_instance);
186         }
187         sfree (query->cols);
188         query->cols_num = 0;
189         return;
190 } /* c_psql_query_delete */
191
192 static c_psql_query_t *c_psql_query_get (const char *name, int server_version)
193 {
194         int i;
195
196         for (i = 0; i < queries_num; ++i)
197                 if (0 == strcasecmp (name, queries[i].name)
198                                 && ((-1 == server_version)
199                                         || ((queries[i].min_pg_version <= server_version)
200                                                 && (server_version <= queries[i].max_pg_version))))
201                         return queries + i;
202         return NULL;
203 } /* c_psql_query_get */
204
205 static c_psql_database_t *c_psql_database_new (const char *name)
206 {
207         c_psql_database_t *db;
208
209         ++databases_num;
210         if (NULL == (databases = (c_psql_database_t *)realloc (databases,
211                                 databases_num * sizeof (*databases)))) {
212                 log_err ("Out of memory.");
213                 exit (5);
214         }
215
216         db = databases + (databases_num - 1);
217
218         db->conn = NULL;
219
220         C_COMPLAIN_INIT (&db->conn_complaint);
221
222         db->proto_version = 0;
223
224         db->max_params_num = 0;
225
226         db->queries     = NULL;
227         db->queries_num = 0;
228
229         db->database   = sstrdup (name);
230         db->host       = NULL;
231         db->port       = NULL;
232         db->user       = NULL;
233         db->password   = NULL;
234
235         db->sslmode    = NULL;
236
237         db->krbsrvname = NULL;
238
239         db->service    = NULL;
240         return db;
241 } /* c_psql_database_new */
242
243 static void c_psql_database_delete (c_psql_database_t *db)
244 {
245         PQfinish (db->conn);
246
247         sfree (db->queries);
248         db->queries_num = 0;
249
250         sfree (db->database);
251         sfree (db->host);
252         sfree (db->port);
253         sfree (db->user);
254         sfree (db->password);
255
256         sfree (db->sslmode);
257
258         sfree (db->krbsrvname);
259
260         sfree (db->service);
261         return;
262 } /* c_psql_database_delete */
263
264 static void submit (const c_psql_database_t *db,
265                 const char *type, const char *type_instance,
266                 value_t *values, size_t values_len)
267 {
268         value_list_t vl = VALUE_LIST_INIT;
269
270         vl.values     = values;
271         vl.values_len = values_len;
272         vl.time       = time (NULL);
273
274         if (C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
275                         || (0 == strcmp (db->host, "localhost")))
276                 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
277         else
278                 sstrncpy (vl.host, db->host, sizeof (vl.host));
279
280         sstrncpy (vl.plugin, "postgresql", sizeof (vl.plugin));
281         sstrncpy (vl.plugin_instance, db->database, sizeof (vl.plugin_instance));
282
283         sstrncpy (vl.type, type, sizeof (vl.type));
284
285         if (NULL != type_instance)
286                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
287
288         plugin_dispatch_values (&vl);
289         return;
290 } /* submit */
291
292 static void submit_counter (const c_psql_database_t *db,
293                 const char *type, const char *type_instance,
294                 const char *value)
295 {
296         value_t values[1];
297
298         if ((NULL == value) || ('\0' == *value))
299                 return;
300
301         values[0].counter = atoll (value);
302         submit (db, type, type_instance, values, 1);
303         return;
304 } /* submit_counter */
305
306 static void submit_gauge (const c_psql_database_t *db,
307                 const char *type, const char *type_instance,
308                 const char *value)
309 {
310         value_t values[1];
311
312         if ((NULL == value) || ('\0' == *value))
313                 return;
314
315         values[0].gauge = atof (value);
316         submit (db, type, type_instance, values, 1);
317         return;
318 } /* submit_gauge */
319
320 static int c_psql_check_connection (c_psql_database_t *db)
321 {
322         /* "ping" */
323         PQclear (PQexec (db->conn, "SELECT 42;"));
324
325         if (CONNECTION_OK != PQstatus (db->conn)) {
326                 PQreset (db->conn);
327
328                 /* trigger c_release() */
329                 if (0 == db->conn_complaint.interval)
330                         db->conn_complaint.interval = 1;
331
332                 if (CONNECTION_OK != PQstatus (db->conn)) {
333                         c_complain (LOG_ERR, &db->conn_complaint,
334                                         "Failed to connect to database %s: %s",
335                                         db->database, PQerrorMessage (db->conn));
336                         return -1;
337                 }
338
339                 db->proto_version = PQprotocolVersion (db->conn);
340                 if (3 > db->proto_version)
341                         log_warn ("Protocol version %d does not support parameters.",
342                                         db->proto_version);
343         }
344
345         c_release (LOG_INFO, &db->conn_complaint,
346                         "Successfully reconnected to database %s", PQdb (db->conn));
347         return 0;
348 } /* c_psql_check_connection */
349
350 static PGresult *c_psql_exec_query_params (c_psql_database_t *db,
351                 c_psql_query_t *query)
352 {
353         char *params[db->max_params_num];
354         int   i;
355
356         assert (db->max_params_num >= query->params_num);
357
358         for (i = 0; i < query->params_num; ++i) {
359                 switch (query->params[i]) {
360                         case C_PSQL_PARAM_HOST:
361                                 params[i] = C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
362                                         ? "localhost" : db->host;
363                                 break;
364                         case C_PSQL_PARAM_DB:
365                                 params[i] = db->database;
366                                 break;
367                         case C_PSQL_PARAM_USER:
368                                 params[i] = db->user;
369                                 break;
370                         default:
371                                 assert (0);
372                 }
373         }
374
375         return PQexecParams (db->conn, query->query, query->params_num, NULL,
376                         (const char *const *)((0 == query->params_num) ? NULL : params),
377                         NULL, NULL, /* return text data */ 0);
378 } /* c_psql_exec_query_params */
379
380 static PGresult *c_psql_exec_query_noparams (c_psql_database_t *db,
381                 c_psql_query_t *query)
382 {
383         return PQexec (db->conn, query->query);
384 } /* c_psql_exec_query_noparams */
385
386 static int c_psql_exec_query (c_psql_database_t *db, int idx)
387 {
388         c_psql_query_t *query;
389         PGresult       *res;
390
391         int rows, cols;
392         int i;
393
394         if (idx >= db->queries_num)
395                 return -1;
396
397         query = db->queries[idx];
398
399         if (3 <= db->proto_version)
400                 res = c_psql_exec_query_params (db, query);
401         else if (0 == query->params_num)
402                 res = c_psql_exec_query_noparams (db, query);
403         else {
404                 log_err ("Connection to database \"%s\" does not support parameters "
405                                 "(protocol version %d) - cannot execute query \"%s\".",
406                                 db->database, db->proto_version, query->name);
407                 return -1;
408         }
409
410         if (PGRES_TUPLES_OK != PQresultStatus (res)) {
411                 log_err ("Failed to execute SQL query: %s",
412                                 PQerrorMessage (db->conn));
413                 log_info ("SQL query was: %s", query->query);
414                 PQclear (res);
415                 return -1;
416         }
417
418         rows = PQntuples (res);
419         if (1 > rows)
420                 return 0;
421
422         cols = PQnfields (res);
423         if (query->cols_num != cols) {
424                 log_err ("SQL query returned wrong number of fields "
425                                 "(expected: %i, got: %i)", query->cols_num, cols);
426                 log_info ("SQL query was: %s", query->query);
427                 return -1;
428         }
429
430         for (i = 0; i < rows; ++i) {
431                 int j;
432
433                 for (j = 0; j < cols; ++j) {
434                         c_psql_col_t col = query->cols[j];
435
436                         char *value = PQgetvalue (res, i, j);
437
438                         if (col.ds_type == DS_TYPE_COUNTER)
439                                 submit_counter (db, col.type, col.type_instance, value);
440                         else if (col.ds_type == DS_TYPE_GAUGE)
441                                 submit_gauge (db, col.type, col.type_instance, value);
442                 }
443         }
444         return 0;
445 } /* c_psql_exec_query */
446
447 static int c_psql_read (void)
448 {
449         int success = 0;
450         int i;
451
452         for (i = 0; i < databases_num; ++i) {
453                 c_psql_database_t *db = databases + i;
454
455                 int j;
456
457                 assert (NULL != db->database);
458
459                 if (0 != c_psql_check_connection (db))
460                         continue;
461
462                 for (j = 0; j < db->queries_num; ++j)
463                         c_psql_exec_query (db, j);
464
465                 ++success;
466         }
467
468         if (! success)
469                 return -1;
470         return 0;
471 } /* c_psql_read */
472
473 static int c_psql_shutdown (void)
474 {
475         int i;
476
477         if ((NULL == databases) || (0 == databases_num))
478                 return 0;
479
480         plugin_unregister_read ("postgresql");
481         plugin_unregister_shutdown ("postgresql");
482
483         for (i = 0; i < databases_num; ++i) {
484                 c_psql_database_t *db = databases + i;
485                 c_psql_database_delete (db);
486         }
487
488         sfree (databases);
489         databases_num = 0;
490
491         for (i = 0; i < queries_num; ++i) {
492                 c_psql_query_t *query = queries + i;
493                 c_psql_query_delete (query);
494         }
495
496         sfree (queries);
497         queries_num = 0;
498         return 0;
499 } /* c_psql_shutdown */
500
501 static int c_psql_init (void)
502 {
503         int i;
504
505         if ((NULL == databases) || (0 == databases_num))
506                 return 0;
507
508         for (i = 0; i < queries_num; ++i) {
509                 c_psql_query_t *query = queries + i;
510                 int j;
511
512                 for (j = 0; j < query->cols_num; ++j) {
513                         c_psql_col_t     *col = query->cols + j;
514                         const data_set_t *ds;
515
516                         ds = plugin_get_ds (col->type);
517                         if (NULL == ds) {
518                                 log_err ("Column: Unknown type \"%s\".", col->type);
519                                 c_psql_shutdown ();
520                                 return -1;
521                         }
522
523                         if (1 != ds->ds_num) {
524                                 log_err ("Column: Invalid type \"%s\" - types defining "
525                                                 "one data source are supported only (got: %i).",
526                                                 col->type, ds->ds_num);
527                                 c_psql_shutdown ();
528                                 return -1;
529                         }
530
531                         col->ds_type = ds->ds[0].type;
532                 }
533         }
534
535         for (i = 0; i < databases_num; ++i) {
536                 c_psql_database_t *db = databases + i;
537
538                 char  conninfo[4096];
539                 char *buf     = conninfo;
540                 int   buf_len = sizeof (conninfo);
541                 int   status;
542
543                 char *server_host;
544                 int   server_version;
545
546                 int j;
547
548                 status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
549                 if (0 < status) {
550                         buf     += status;
551                         buf_len -= status;
552                 }
553
554                 C_PSQL_PAR_APPEND (buf, buf_len, "host",       db->host);
555                 C_PSQL_PAR_APPEND (buf, buf_len, "port",       db->port);
556                 C_PSQL_PAR_APPEND (buf, buf_len, "user",       db->user);
557                 C_PSQL_PAR_APPEND (buf, buf_len, "password",   db->password);
558                 C_PSQL_PAR_APPEND (buf, buf_len, "sslmode",    db->sslmode);
559                 C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
560                 C_PSQL_PAR_APPEND (buf, buf_len, "service",    db->service);
561
562                 db->conn = PQconnectdb (conninfo);
563                 if (0 != c_psql_check_connection (db))
564                         continue;
565
566                 db->proto_version = PQprotocolVersion (db->conn);
567
568                 server_host    = PQhost (db->conn);
569                 server_version = PQserverVersion (db->conn);
570                 log_info ("Sucessfully connected to database %s (user %s) "
571                                 "at server %s%s%s (server version: %d.%d.%d, "
572                                 "protocol version: %d, pid: %d)",
573                                 PQdb (db->conn), PQuser (db->conn),
574                                 C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
575                                 C_PSQL_SERVER_VERSION3 (server_version),
576                                 db->proto_version, PQbackendPID (db->conn));
577
578                 if (3 > db->proto_version)
579                         log_warn ("Protocol version %d does not support parameters.",
580                                         db->proto_version);
581
582                 /* Now that we know the PostgreSQL server version, we can get the
583                  * right version of each query definition. */
584                 for (j = 0; j < db->queries_num; ++j) {
585                         c_psql_query_t *tmp;
586
587                         tmp = c_psql_query_get (db->queries[j]->name, server_version);
588
589                         if (tmp == db->queries[j])
590                                 continue;
591
592                         if (NULL == tmp) {
593                                 log_err ("Query \"%s\" not found for server version %i - "
594                                                 "please check your configuration.",
595                                                 db->queries[j]->name, server_version);
596
597                                 if (db->queries_num - j - 1 > 0)
598                                         memmove (db->queries + j, db->queries + j + 1,
599                                                         (db->queries_num - j - 1) * sizeof (*db->queries));
600
601                                 --db->queries_num;
602                                 --j;
603                                 continue;
604                         }
605
606                         db->queries[j] = tmp;
607                 }
608         }
609
610         plugin_register_read ("postgresql", c_psql_read);
611         plugin_register_shutdown ("postgresql", c_psql_shutdown);
612         return 0;
613 } /* c_psql_init */
614
615 static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
616 {
617         if ((0 != ci->children_num) || (1 != ci->values_num)
618                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
619                 log_err ("%s expects a single string argument.", name);
620                 return 1;
621         }
622
623         sfree (*var);
624         *var = sstrdup (ci->values[0].value.string);
625         return 0;
626 } /* config_set_s */
627
628 static int config_set_i (char *name, int *var, const oconfig_item_t *ci)
629 {
630         if ((0 != ci->children_num) || (1 != ci->values_num)
631                         || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
632                 log_err ("%s expects a single number argument.", name);
633                 return 1;
634         }
635
636         *var = (int)ci->values[0].value.number;
637         return 0;
638 } /* config_set_i */
639
640 static int config_set_param (c_psql_query_t *query, const oconfig_item_t *ci)
641 {
642         c_psql_param_t param;
643         char          *param_str;
644
645         if ((0 != ci->children_num) || (1 != ci->values_num)
646                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
647                 log_err ("Param expects a single string argument.");
648                 return 1;
649         }
650
651         param_str = ci->values[0].value.string;
652         if (0 == strcasecmp (param_str, "hostname"))
653                 param = C_PSQL_PARAM_HOST;
654         else if (0 == strcasecmp (param_str, "database"))
655                 param = C_PSQL_PARAM_DB;
656         else if (0 == strcasecmp (param_str, "username"))
657                 param = C_PSQL_PARAM_USER;
658         else {
659                 log_err ("Invalid parameter \"%s\".", param_str);
660                 return 1;
661         }
662
663         ++query->params_num;
664         if (NULL == (query->params = (c_psql_param_t *)realloc (query->params,
665                                 query->params_num * sizeof (*query->params)))) {
666                 log_err ("Out of memory.");
667                 exit (5);
668         }
669
670         query->params[query->params_num - 1] = param;
671         return 0;
672 } /* config_set_param */
673
674 static int config_set_column (c_psql_query_t *query, const oconfig_item_t *ci)
675 {
676         c_psql_col_t *col;
677
678         int i;
679
680         if ((0 != ci->children_num)
681                         || (1 > ci->values_num) || (2 < ci->values_num)) {
682                 log_err ("Column expects either one or two arguments.");
683                 return 1;
684         }
685
686         for (i = 0; i < ci->values_num; ++i) {
687                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
688                         log_err ("Column expects either one or two string arguments.");
689                         return 1;
690                 }
691         }
692
693         ++query->cols_num;
694         if (NULL == (query->cols = (c_psql_col_t *)realloc (query->cols,
695                                 query->cols_num * sizeof (*query->cols)))) {
696                 log_err ("Out of memory.");
697                 exit (5);
698         }
699
700         col = query->cols + query->cols_num - 1;
701
702         col->ds_type = -1;
703
704         col->type = sstrdup (ci->values[0].value.string);
705         col->type_instance = (2 == ci->values_num)
706                 ? sstrdup (ci->values[1].value.string) : NULL;
707         return 0;
708 } /* config_set_column */
709
710 static int set_query (c_psql_database_t *db, const char *name)
711 {
712         c_psql_query_t *query;
713
714         query = c_psql_query_get (name, -1);
715         if (NULL == query) {
716                 log_err ("Query \"%s\" not found - please check your configuration.",
717                                 name);
718                 return 1;
719         }
720
721         ++db->queries_num;
722         if (NULL == (db->queries = (c_psql_query_t **)realloc (db->queries,
723                                 db->queries_num * sizeof (*db->queries)))) {
724                 log_err ("Out of memory.");
725                 exit (5);
726         }
727
728         if (query->params_num > db->max_params_num)
729                 db->max_params_num = query->params_num;
730
731         db->queries[db->queries_num - 1] = query;
732         return 0;
733 } /* set_query */
734
735 static int config_set_query (c_psql_database_t *db, const oconfig_item_t *ci)
736 {
737         if ((0 != ci->children_num) || (1 != ci->values_num)
738                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
739                 log_err ("Query expects a single string argument.");
740                 return 1;
741         }
742         return set_query (db, ci->values[0].value.string);
743 } /* config_set_query */
744
745 static int c_psql_config_query (oconfig_item_t *ci)
746 {
747         c_psql_query_t *query;
748
749         int i;
750
751         if ((1 != ci->values_num)
752                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
753                 log_err ("<Query> expects a single string argument.");
754                 return 1;
755         }
756
757         query = c_psql_query_new (ci->values[0].value.string);
758
759         for (i = 0; i < ci->children_num; ++i) {
760                 oconfig_item_t *c = ci->children + i;
761
762                 if (0 == strcasecmp (c->key, "Query"))
763                         config_set_s ("Query", &query->query, c);
764                 else if (0 == strcasecmp (c->key, "Param"))
765                         config_set_param (query, c);
766                 else if (0 == strcasecmp (c->key, "Column"))
767                         config_set_column (query, c);
768                 else if (0 == strcasecmp (c->key, "MinPGVersion"))
769                         config_set_i ("MinPGVersion", &query->min_pg_version, c);
770                 else if (0 == strcasecmp (c->key, "MaxPGVersion"))
771                         config_set_i ("MaxPGVersion", &query->max_pg_version, c);
772                 else
773                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
774         }
775
776         for (i = 0; i < queries_num - 1; ++i) {
777                 c_psql_query_t *q = queries + i;
778
779                 if ((0 == strcasecmp (q->name, query->name))
780                                 && (q->min_pg_version <= query->max_pg_version)
781                                 && (query->min_pg_version <= q->max_pg_version)) {
782                         log_err ("Ignoring redefinition (with overlapping version ranges) "
783                                         "of query \"%s\".", query->name);
784                         c_psql_query_delete (query);
785                         --queries_num;
786                         return 1;
787                 }
788         }
789
790         if (query->min_pg_version > query->max_pg_version) {
791                 log_err ("Query \"%s\": MinPGVersion > MaxPGVersion.",
792                                 query->name);
793                 c_psql_query_delete (query);
794                 --queries_num;
795                 return 1;
796         }
797
798         if (NULL == query->query) {
799                 log_err ("Query \"%s\" does not include an SQL query string - "
800                                 "please check your configuration.", query->name);
801                 c_psql_query_delete (query);
802                 --queries_num;
803                 return 1;
804         }
805         return 0;
806 } /* c_psql_config_query */
807
808 static int c_psql_config_database (oconfig_item_t *ci)
809 {
810         c_psql_database_t *db;
811
812         int i;
813
814         if ((1 != ci->values_num)
815                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
816                 log_err ("<Database> expects a single string argument.");
817                 return 1;
818         }
819
820         db = c_psql_database_new (ci->values[0].value.string);
821
822         for (i = 0; i < ci->children_num; ++i) {
823                 oconfig_item_t *c = ci->children + i;
824
825                 if (0 == strcasecmp (c->key, "Host"))
826                         config_set_s ("Host", &db->host, c);
827                 else if (0 == strcasecmp (c->key, "Port"))
828                         config_set_s ("Port", &db->port, c);
829                 else if (0 == strcasecmp (c->key, "User"))
830                         config_set_s ("User", &db->user, c);
831                 else if (0 == strcasecmp (c->key, "Password"))
832                         config_set_s ("Password", &db->password, c);
833                 else if (0 == strcasecmp (c->key, "SSLMode"))
834                         config_set_s ("SSLMode", &db->sslmode, c);
835                 else if (0 == strcasecmp (c->key, "KRBSrvName"))
836                         config_set_s ("KRBSrvName", &db->krbsrvname, c);
837                 else if (0 == strcasecmp (c->key, "Service"))
838                         config_set_s ("Service", &db->service, c);
839                 else if (0 == strcasecmp (c->key, "Query"))
840                         config_set_query (db, c);
841                 else
842                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
843         }
844
845         if (NULL == db->queries) {
846                 for (i = 0; i < def_queries_num; ++i)
847                         set_query (db, def_queries[i]);
848         }
849         return 0;
850 }
851
852 static int c_psql_config (oconfig_item_t *ci)
853 {
854         static int have_def_config = 0;
855
856         int i;
857
858         if (0 == have_def_config) {
859                 oconfig_item_t *c;
860
861                 have_def_config = 1;
862
863                 c = oconfig_parse_file (C_PSQL_DEFAULT_CONF);
864                 if (NULL == c)
865                         log_err ("Failed to read default config ("C_PSQL_DEFAULT_CONF").");
866                 else
867                         c_psql_config (c);
868
869                 if (NULL == queries)
870                         log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
871                                         "any queries - please check your installation.");
872         }
873
874         for (i = 0; i < ci->children_num; ++i) {
875                 oconfig_item_t *c = ci->children + i;
876
877                 if (0 == strcasecmp (c->key, "Query"))
878                         c_psql_config_query (c);
879                 else if (0 == strcasecmp (c->key, "Database"))
880                         c_psql_config_database (c);
881                 else
882                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
883         }
884         return 0;
885 } /* c_psql_config */
886
887 void module_register (void)
888 {
889         plugin_register_complex_config ("postgresql", c_psql_config);
890         plugin_register_init ("postgresql", c_psql_init);
891 } /* module_register */
892
893 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
894