9959690a1ad8553c1f40567a8dc14778f3052ac7
[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         db->conn_complaint.last     = 0;
221         db->conn_complaint.interval = 0;
222
223         db->proto_version = 0;
224
225         db->max_params_num = 0;
226
227         db->queries     = NULL;
228         db->queries_num = 0;
229
230         db->database   = sstrdup (name);
231         db->host       = NULL;
232         db->port       = NULL;
233         db->user       = NULL;
234         db->password   = NULL;
235
236         db->sslmode    = NULL;
237
238         db->krbsrvname = NULL;
239
240         db->service    = NULL;
241         return db;
242 } /* c_psql_database_new */
243
244 static void c_psql_database_delete (c_psql_database_t *db)
245 {
246         PQfinish (db->conn);
247
248         sfree (db->queries);
249         db->queries_num = 0;
250
251         sfree (db->database);
252         sfree (db->host);
253         sfree (db->port);
254         sfree (db->user);
255         sfree (db->password);
256
257         sfree (db->sslmode);
258
259         sfree (db->krbsrvname);
260
261         sfree (db->service);
262         return;
263 } /* c_psql_database_delete */
264
265 static void submit (const c_psql_database_t *db,
266                 const char *type, const char *type_instance,
267                 value_t *values, size_t values_len)
268 {
269         value_list_t vl = VALUE_LIST_INIT;
270
271         vl.values     = values;
272         vl.values_len = values_len;
273         vl.time       = time (NULL);
274
275         if (C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
276                         || (0 == strcmp (db->host, "localhost")))
277                 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
278         else
279                 sstrncpy (vl.host, db->host, sizeof (vl.host));
280
281         sstrncpy (vl.plugin, "postgresql", sizeof (vl.plugin));
282         sstrncpy (vl.plugin_instance, db->database, sizeof (vl.plugin_instance));
283
284         sstrncpy (vl.type, type, sizeof (vl.type));
285
286         if (NULL != type_instance)
287                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
288
289         plugin_dispatch_values (&vl);
290         return;
291 } /* submit */
292
293 static void submit_counter (const c_psql_database_t *db,
294                 const char *type, const char *type_instance,
295                 const char *value)
296 {
297         value_t values[1];
298
299         if ((NULL == value) || ('\0' == *value))
300                 return;
301
302         values[0].counter = atoll (value);
303         submit (db, type, type_instance, values, 1);
304         return;
305 } /* submit_counter */
306
307 static void submit_gauge (const c_psql_database_t *db,
308                 const char *type, const char *type_instance,
309                 const char *value)
310 {
311         value_t values[1];
312
313         if ((NULL == value) || ('\0' == *value))
314                 return;
315
316         values[0].gauge = atof (value);
317         submit (db, type, type_instance, values, 1);
318         return;
319 } /* submit_gauge */
320
321 static int c_psql_check_connection (c_psql_database_t *db)
322 {
323         /* "ping" */
324         PQclear (PQexec (db->conn, "SELECT 42;"));
325
326         if (CONNECTION_OK != PQstatus (db->conn)) {
327                 PQreset (db->conn);
328
329                 /* trigger c_release() */
330                 if (0 == db->conn_complaint.interval)
331                         db->conn_complaint.interval = 1;
332
333                 if (CONNECTION_OK != PQstatus (db->conn)) {
334                         c_complain (LOG_ERR, &db->conn_complaint,
335                                         "Failed to connect to database %s: %s",
336                                         db->database, PQerrorMessage (db->conn));
337                         return -1;
338                 }
339
340                 db->proto_version = PQprotocolVersion (db->conn);
341                 if (3 > db->proto_version)
342                         log_warn ("Protocol version %d does not support parameters.",
343                                         db->proto_version);
344         }
345
346         c_release (LOG_INFO, &db->conn_complaint,
347                         "Successfully reconnected to database %s", PQdb (db->conn));
348         return 0;
349 } /* c_psql_check_connection */
350
351 static PGresult *c_psql_exec_query_params (c_psql_database_t *db,
352                 c_psql_query_t *query)
353 {
354         char *params[db->max_params_num];
355         int   i;
356
357         assert (db->max_params_num >= query->params_num);
358
359         for (i = 0; i < query->params_num; ++i) {
360                 switch (query->params[i]) {
361                         case C_PSQL_PARAM_HOST:
362                                 params[i] = C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
363                                         ? "localhost" : db->host;
364                                 break;
365                         case C_PSQL_PARAM_DB:
366                                 params[i] = db->database;
367                                 break;
368                         case C_PSQL_PARAM_USER:
369                                 params[i] = db->user;
370                                 break;
371                         default:
372                                 assert (0);
373                 }
374         }
375
376         return PQexecParams (db->conn, query->query, query->params_num, NULL,
377                         (const char *const *)((0 == query->params_num) ? NULL : params),
378                         NULL, NULL, /* return text data */ 0);
379 } /* c_psql_exec_query_params */
380
381 static PGresult *c_psql_exec_query_noparams (c_psql_database_t *db,
382                 c_psql_query_t *query)
383 {
384         return PQexec (db->conn, query->query);
385 } /* c_psql_exec_query_noparams */
386
387 static int c_psql_exec_query (c_psql_database_t *db, int idx)
388 {
389         c_psql_query_t *query;
390         PGresult       *res;
391
392         int rows, cols;
393         int i;
394
395         if (idx >= db->queries_num)
396                 return -1;
397
398         query = db->queries[idx];
399
400         if (3 <= db->proto_version)
401                 res = c_psql_exec_query_params (db, query);
402         else if (0 == query->params_num)
403                 res = c_psql_exec_query_noparams (db, query);
404         else {
405                 log_err ("Connection to database \"%s\" does not support parameters "
406                                 "(protocol version %d) - cannot execute query \"%s\".",
407                                 db->database, db->proto_version, query->name);
408                 return -1;
409         }
410
411         if (PGRES_TUPLES_OK != PQresultStatus (res)) {
412                 log_err ("Failed to execute SQL query: %s",
413                                 PQerrorMessage (db->conn));
414                 log_info ("SQL query was: %s", query->query);
415                 PQclear (res);
416                 return -1;
417         }
418
419         rows = PQntuples (res);
420         if (1 > rows) {
421                 PQclear (res);
422                 return 0;
423         }
424
425         cols = PQnfields (res);
426         if (query->cols_num != cols) {
427                 log_err ("SQL query returned wrong number of fields "
428                                 "(expected: %i, got: %i)", query->cols_num, cols);
429                 log_info ("SQL query was: %s", query->query);
430                 return -1;
431         }
432
433         for (i = 0; i < rows; ++i) {
434                 int j;
435
436                 for (j = 0; j < cols; ++j) {
437                         c_psql_col_t col = query->cols[j];
438
439                         char *value = PQgetvalue (res, i, j);
440
441                         if (col.ds_type == DS_TYPE_COUNTER)
442                                 submit_counter (db, col.type, col.type_instance, value);
443                         else if (col.ds_type == DS_TYPE_GAUGE)
444                                 submit_gauge (db, col.type, col.type_instance, value);
445                 }
446         }
447         PQclear (res);
448         return 0;
449 } /* c_psql_exec_query */
450
451 static int c_psql_read (void)
452 {
453         int success = 0;
454         int i;
455
456         for (i = 0; i < databases_num; ++i) {
457                 c_psql_database_t *db = databases + i;
458
459                 int j;
460
461                 assert (NULL != db->database);
462
463                 if (0 != c_psql_check_connection (db))
464                         continue;
465
466                 for (j = 0; j < db->queries_num; ++j)
467                         c_psql_exec_query (db, j);
468
469                 ++success;
470         }
471
472         if (! success)
473                 return -1;
474         return 0;
475 } /* c_psql_read */
476
477 static int c_psql_shutdown (void)
478 {
479         int i;
480
481         if ((NULL == databases) || (0 == databases_num))
482                 return 0;
483
484         plugin_unregister_read ("postgresql");
485         plugin_unregister_shutdown ("postgresql");
486
487         for (i = 0; i < databases_num; ++i) {
488                 c_psql_database_t *db = databases + i;
489                 c_psql_database_delete (db);
490         }
491
492         sfree (databases);
493         databases_num = 0;
494
495         for (i = 0; i < queries_num; ++i) {
496                 c_psql_query_t *query = queries + i;
497                 c_psql_query_delete (query);
498         }
499
500         sfree (queries);
501         queries_num = 0;
502         return 0;
503 } /* c_psql_shutdown */
504
505 static int c_psql_init (void)
506 {
507         int i;
508
509         if ((NULL == databases) || (0 == databases_num))
510                 return 0;
511
512         for (i = 0; i < queries_num; ++i) {
513                 c_psql_query_t *query = queries + i;
514                 int j;
515
516                 for (j = 0; j < query->cols_num; ++j) {
517                         c_psql_col_t     *col = query->cols + j;
518                         const data_set_t *ds;
519
520                         ds = plugin_get_ds (col->type);
521                         if (NULL == ds) {
522                                 log_err ("Column: Unknown type \"%s\".", col->type);
523                                 c_psql_shutdown ();
524                                 return -1;
525                         }
526
527                         if (1 != ds->ds_num) {
528                                 log_err ("Column: Invalid type \"%s\" - types defining "
529                                                 "one data source are supported only (got: %i).",
530                                                 col->type, ds->ds_num);
531                                 c_psql_shutdown ();
532                                 return -1;
533                         }
534
535                         col->ds_type = ds->ds[0].type;
536                 }
537         }
538
539         for (i = 0; i < databases_num; ++i) {
540                 c_psql_database_t *db = databases + i;
541
542                 char  conninfo[4096];
543                 char *buf     = conninfo;
544                 int   buf_len = sizeof (conninfo);
545                 int   status;
546
547                 char *server_host;
548                 int   server_version;
549
550                 int j;
551
552                 status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
553                 if (0 < status) {
554                         buf     += status;
555                         buf_len -= status;
556                 }
557
558                 C_PSQL_PAR_APPEND (buf, buf_len, "host",       db->host);
559                 C_PSQL_PAR_APPEND (buf, buf_len, "port",       db->port);
560                 C_PSQL_PAR_APPEND (buf, buf_len, "user",       db->user);
561                 C_PSQL_PAR_APPEND (buf, buf_len, "password",   db->password);
562                 C_PSQL_PAR_APPEND (buf, buf_len, "sslmode",    db->sslmode);
563                 C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
564                 C_PSQL_PAR_APPEND (buf, buf_len, "service",    db->service);
565
566                 db->conn = PQconnectdb (conninfo);
567                 if (0 != c_psql_check_connection (db))
568                         continue;
569
570                 db->proto_version = PQprotocolVersion (db->conn);
571
572                 server_host    = PQhost (db->conn);
573                 server_version = PQserverVersion (db->conn);
574                 log_info ("Sucessfully connected to database %s (user %s) "
575                                 "at server %s%s%s (server version: %d.%d.%d, "
576                                 "protocol version: %d, pid: %d)",
577                                 PQdb (db->conn), PQuser (db->conn),
578                                 C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
579                                 C_PSQL_SERVER_VERSION3 (server_version),
580                                 db->proto_version, PQbackendPID (db->conn));
581
582                 if (3 > db->proto_version)
583                         log_warn ("Protocol version %d does not support parameters.",
584                                         db->proto_version);
585
586                 /* Now that we know the PostgreSQL server version, we can get the
587                  * right version of each query definition. */
588                 for (j = 0; j < db->queries_num; ++j) {
589                         c_psql_query_t *tmp;
590
591                         tmp = c_psql_query_get (db->queries[j]->name, server_version);
592
593                         if (tmp == db->queries[j])
594                                 continue;
595
596                         if (NULL == tmp) {
597                                 log_err ("Query \"%s\" not found for server version %i - "
598                                                 "please check your configuration.",
599                                                 db->queries[j]->name, server_version);
600
601                                 if (db->queries_num - j - 1 > 0)
602                                         memmove (db->queries + j, db->queries + j + 1,
603                                                         (db->queries_num - j - 1) * sizeof (*db->queries));
604
605                                 --db->queries_num;
606                                 --j;
607                                 continue;
608                         }
609
610                         db->queries[j] = tmp;
611                 }
612         }
613
614         plugin_register_read ("postgresql", c_psql_read);
615         plugin_register_shutdown ("postgresql", c_psql_shutdown);
616         return 0;
617 } /* c_psql_init */
618
619 static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
620 {
621         if ((0 != ci->children_num) || (1 != ci->values_num)
622                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
623                 log_err ("%s expects a single string argument.", name);
624                 return 1;
625         }
626
627         sfree (*var);
628         *var = sstrdup (ci->values[0].value.string);
629         return 0;
630 } /* config_set_s */
631
632 static int config_set_i (char *name, int *var, const oconfig_item_t *ci)
633 {
634         if ((0 != ci->children_num) || (1 != ci->values_num)
635                         || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
636                 log_err ("%s expects a single number argument.", name);
637                 return 1;
638         }
639
640         *var = (int)ci->values[0].value.number;
641         return 0;
642 } /* config_set_i */
643
644 static int config_set_param (c_psql_query_t *query, const oconfig_item_t *ci)
645 {
646         c_psql_param_t param;
647         char          *param_str;
648
649         if ((0 != ci->children_num) || (1 != ci->values_num)
650                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
651                 log_err ("Param expects a single string argument.");
652                 return 1;
653         }
654
655         param_str = ci->values[0].value.string;
656         if (0 == strcasecmp (param_str, "hostname"))
657                 param = C_PSQL_PARAM_HOST;
658         else if (0 == strcasecmp (param_str, "database"))
659                 param = C_PSQL_PARAM_DB;
660         else if (0 == strcasecmp (param_str, "username"))
661                 param = C_PSQL_PARAM_USER;
662         else {
663                 log_err ("Invalid parameter \"%s\".", param_str);
664                 return 1;
665         }
666
667         ++query->params_num;
668         if (NULL == (query->params = (c_psql_param_t *)realloc (query->params,
669                                 query->params_num * sizeof (*query->params)))) {
670                 log_err ("Out of memory.");
671                 exit (5);
672         }
673
674         query->params[query->params_num - 1] = param;
675         return 0;
676 } /* config_set_param */
677
678 static int config_set_column (c_psql_query_t *query, const oconfig_item_t *ci)
679 {
680         c_psql_col_t *col;
681
682         int i;
683
684         if ((0 != ci->children_num)
685                         || (1 > ci->values_num) || (2 < ci->values_num)) {
686                 log_err ("Column expects either one or two arguments.");
687                 return 1;
688         }
689
690         for (i = 0; i < ci->values_num; ++i) {
691                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
692                         log_err ("Column expects either one or two string arguments.");
693                         return 1;
694                 }
695         }
696
697         ++query->cols_num;
698         if (NULL == (query->cols = (c_psql_col_t *)realloc (query->cols,
699                                 query->cols_num * sizeof (*query->cols)))) {
700                 log_err ("Out of memory.");
701                 exit (5);
702         }
703
704         col = query->cols + query->cols_num - 1;
705
706         col->ds_type = -1;
707
708         col->type = sstrdup (ci->values[0].value.string);
709         col->type_instance = (2 == ci->values_num)
710                 ? sstrdup (ci->values[1].value.string) : NULL;
711         return 0;
712 } /* config_set_column */
713
714 static int set_query (c_psql_database_t *db, const char *name)
715 {
716         c_psql_query_t *query;
717
718         query = c_psql_query_get (name, -1);
719         if (NULL == query) {
720                 log_err ("Query \"%s\" not found - please check your configuration.",
721                                 name);
722                 return 1;
723         }
724
725         ++db->queries_num;
726         if (NULL == (db->queries = (c_psql_query_t **)realloc (db->queries,
727                                 db->queries_num * sizeof (*db->queries)))) {
728                 log_err ("Out of memory.");
729                 exit (5);
730         }
731
732         if (query->params_num > db->max_params_num)
733                 db->max_params_num = query->params_num;
734
735         db->queries[db->queries_num - 1] = query;
736         return 0;
737 } /* set_query */
738
739 static int config_set_query (c_psql_database_t *db, const oconfig_item_t *ci)
740 {
741         if ((0 != ci->children_num) || (1 != ci->values_num)
742                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
743                 log_err ("Query expects a single string argument.");
744                 return 1;
745         }
746         return set_query (db, ci->values[0].value.string);
747 } /* config_set_query */
748
749 static int c_psql_config_query (oconfig_item_t *ci)
750 {
751         c_psql_query_t *query;
752
753         int i;
754
755         if ((1 != ci->values_num)
756                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
757                 log_err ("<Query> expects a single string argument.");
758                 return 1;
759         }
760
761         query = c_psql_query_new (ci->values[0].value.string);
762
763         for (i = 0; i < ci->children_num; ++i) {
764                 oconfig_item_t *c = ci->children + i;
765
766                 if (0 == strcasecmp (c->key, "Query"))
767                         config_set_s ("Query", &query->query, c);
768                 else if (0 == strcasecmp (c->key, "Param"))
769                         config_set_param (query, c);
770                 else if (0 == strcasecmp (c->key, "Column"))
771                         config_set_column (query, c);
772                 else if (0 == strcasecmp (c->key, "MinPGVersion"))
773                         config_set_i ("MinPGVersion", &query->min_pg_version, c);
774                 else if (0 == strcasecmp (c->key, "MaxPGVersion"))
775                         config_set_i ("MaxPGVersion", &query->max_pg_version, c);
776                 else
777                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
778         }
779
780         for (i = 0; i < queries_num - 1; ++i) {
781                 c_psql_query_t *q = queries + i;
782
783                 if ((0 == strcasecmp (q->name, query->name))
784                                 && (q->min_pg_version <= query->max_pg_version)
785                                 && (query->min_pg_version <= q->max_pg_version)) {
786                         log_err ("Ignoring redefinition (with overlapping version ranges) "
787                                         "of query \"%s\".", query->name);
788                         c_psql_query_delete (query);
789                         --queries_num;
790                         return 1;
791                 }
792         }
793
794         if (query->min_pg_version > query->max_pg_version) {
795                 log_err ("Query \"%s\": MinPGVersion > MaxPGVersion.",
796                                 query->name);
797                 c_psql_query_delete (query);
798                 --queries_num;
799                 return 1;
800         }
801
802         if (NULL == query->query) {
803                 log_err ("Query \"%s\" does not include an SQL query string - "
804                                 "please check your configuration.", query->name);
805                 c_psql_query_delete (query);
806                 --queries_num;
807                 return 1;
808         }
809         return 0;
810 } /* c_psql_config_query */
811
812 static int c_psql_config_database (oconfig_item_t *ci)
813 {
814         c_psql_database_t *db;
815
816         int i;
817
818         if ((1 != ci->values_num)
819                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
820                 log_err ("<Database> expects a single string argument.");
821                 return 1;
822         }
823
824         db = c_psql_database_new (ci->values[0].value.string);
825
826         for (i = 0; i < ci->children_num; ++i) {
827                 oconfig_item_t *c = ci->children + i;
828
829                 if (0 == strcasecmp (c->key, "Host"))
830                         config_set_s ("Host", &db->host, c);
831                 else if (0 == strcasecmp (c->key, "Port"))
832                         config_set_s ("Port", &db->port, c);
833                 else if (0 == strcasecmp (c->key, "User"))
834                         config_set_s ("User", &db->user, c);
835                 else if (0 == strcasecmp (c->key, "Password"))
836                         config_set_s ("Password", &db->password, c);
837                 else if (0 == strcasecmp (c->key, "SSLMode"))
838                         config_set_s ("SSLMode", &db->sslmode, c);
839                 else if (0 == strcasecmp (c->key, "KRBSrvName"))
840                         config_set_s ("KRBSrvName", &db->krbsrvname, c);
841                 else if (0 == strcasecmp (c->key, "Service"))
842                         config_set_s ("Service", &db->service, c);
843                 else if (0 == strcasecmp (c->key, "Query"))
844                         config_set_query (db, c);
845                 else
846                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
847         }
848
849         if (NULL == db->queries) {
850                 for (i = 0; i < def_queries_num; ++i)
851                         set_query (db, def_queries[i]);
852         }
853         return 0;
854 }
855
856 static int c_psql_config (oconfig_item_t *ci)
857 {
858         static int have_def_config = 0;
859
860         int i;
861
862         if (0 == have_def_config) {
863                 oconfig_item_t *c;
864
865                 have_def_config = 1;
866
867                 c = oconfig_parse_file (C_PSQL_DEFAULT_CONF);
868                 if (NULL == c)
869                         log_err ("Failed to read default config ("C_PSQL_DEFAULT_CONF").");
870                 else
871                         c_psql_config (c);
872
873                 if (NULL == queries)
874                         log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
875                                         "any queries - please check your installation.");
876         }
877
878         for (i = 0; i < ci->children_num; ++i) {
879                 oconfig_item_t *c = ci->children + i;
880
881                 if (0 == strcasecmp (c->key, "Query"))
882                         c_psql_config_query (c);
883                 else if (0 == strcasecmp (c->key, "Database"))
884                         c_psql_config_database (c);
885                 else
886                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
887         }
888         return 0;
889 } /* c_psql_config */
890
891 void module_register (void)
892 {
893         plugin_register_complex_config ("postgresql", c_psql_config);
894         plugin_register_init ("postgresql", c_psql_init);
895 } /* module_register */
896
897 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
898