Merge branch 'collectd-4.5'
[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         db->conn = NULL;
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                 PQclear (res);
431                 return -1;
432         }
433
434         for (i = 0; i < rows; ++i) {
435                 int j;
436
437                 for (j = 0; j < cols; ++j) {
438                         c_psql_col_t col = query->cols[j];
439
440                         char *value = PQgetvalue (res, i, j);
441
442                         if (col.ds_type == DS_TYPE_COUNTER)
443                                 submit_counter (db, col.type, col.type_instance, value);
444                         else if (col.ds_type == DS_TYPE_GAUGE)
445                                 submit_gauge (db, col.type, col.type_instance, value);
446                 }
447         }
448         PQclear (res);
449         return 0;
450 } /* c_psql_exec_query */
451
452 static int c_psql_read (void)
453 {
454         int success = 0;
455         int i;
456
457         for (i = 0; i < databases_num; ++i) {
458                 c_psql_database_t *db = databases + i;
459
460                 int j;
461
462                 assert (NULL != db->database);
463
464                 if (0 != c_psql_check_connection (db))
465                         continue;
466
467                 for (j = 0; j < db->queries_num; ++j)
468                         c_psql_exec_query (db, j);
469
470                 ++success;
471         }
472
473         if (! success)
474                 return -1;
475         return 0;
476 } /* c_psql_read */
477
478 static int c_psql_shutdown (void)
479 {
480         int i;
481
482         if ((NULL == databases) || (0 == databases_num))
483                 return 0;
484
485         plugin_unregister_read ("postgresql");
486         plugin_unregister_shutdown ("postgresql");
487
488         for (i = 0; i < databases_num; ++i) {
489                 c_psql_database_t *db = databases + i;
490                 c_psql_database_delete (db);
491         }
492
493         sfree (databases);
494         databases_num = 0;
495
496         for (i = 0; i < queries_num; ++i) {
497                 c_psql_query_t *query = queries + i;
498                 c_psql_query_delete (query);
499         }
500
501         sfree (queries);
502         queries_num = 0;
503         return 0;
504 } /* c_psql_shutdown */
505
506 static int c_psql_init (void)
507 {
508         int i;
509
510         if ((NULL == databases) || (0 == databases_num))
511                 return 0;
512
513         for (i = 0; i < queries_num; ++i) {
514                 c_psql_query_t *query = queries + i;
515                 int j;
516
517                 for (j = 0; j < query->cols_num; ++j) {
518                         c_psql_col_t     *col = query->cols + j;
519                         const data_set_t *ds;
520
521                         ds = plugin_get_ds (col->type);
522                         if (NULL == ds) {
523                                 log_err ("Column: Unknown type \"%s\".", col->type);
524                                 c_psql_shutdown ();
525                                 return -1;
526                         }
527
528                         if (1 != ds->ds_num) {
529                                 log_err ("Column: Invalid type \"%s\" - types defining "
530                                                 "one data source are supported only (got: %i).",
531                                                 col->type, ds->ds_num);
532                                 c_psql_shutdown ();
533                                 return -1;
534                         }
535
536                         col->ds_type = ds->ds[0].type;
537                 }
538         }
539
540         for (i = 0; i < databases_num; ++i) {
541                 c_psql_database_t *db = databases + i;
542
543                 char  conninfo[4096];
544                 char *buf     = conninfo;
545                 int   buf_len = sizeof (conninfo);
546                 int   status;
547
548                 char *server_host;
549                 int   server_version;
550
551                 int j;
552
553                 /* this will happen during reinitialization */
554                 if (NULL != db->conn) {
555                         c_psql_check_connection (db);
556                         continue;
557                 }
558
559                 status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
560                 if (0 < status) {
561                         buf     += status;
562                         buf_len -= status;
563                 }
564
565                 C_PSQL_PAR_APPEND (buf, buf_len, "host",       db->host);
566                 C_PSQL_PAR_APPEND (buf, buf_len, "port",       db->port);
567                 C_PSQL_PAR_APPEND (buf, buf_len, "user",       db->user);
568                 C_PSQL_PAR_APPEND (buf, buf_len, "password",   db->password);
569                 C_PSQL_PAR_APPEND (buf, buf_len, "sslmode",    db->sslmode);
570                 C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
571                 C_PSQL_PAR_APPEND (buf, buf_len, "service",    db->service);
572
573                 db->conn = PQconnectdb (conninfo);
574                 if (0 != c_psql_check_connection (db))
575                         continue;
576
577                 db->proto_version = PQprotocolVersion (db->conn);
578
579                 server_host    = PQhost (db->conn);
580                 server_version = PQserverVersion (db->conn);
581                 log_info ("Sucessfully connected to database %s (user %s) "
582                                 "at server %s%s%s (server version: %d.%d.%d, "
583                                 "protocol version: %d, pid: %d)",
584                                 PQdb (db->conn), PQuser (db->conn),
585                                 C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
586                                 C_PSQL_SERVER_VERSION3 (server_version),
587                                 db->proto_version, PQbackendPID (db->conn));
588
589                 if (3 > db->proto_version)
590                         log_warn ("Protocol version %d does not support parameters.",
591                                         db->proto_version);
592
593                 /* Now that we know the PostgreSQL server version, we can get the
594                  * right version of each query definition. */
595                 for (j = 0; j < db->queries_num; ++j) {
596                         c_psql_query_t *tmp;
597
598                         tmp = c_psql_query_get (db->queries[j]->name, server_version);
599
600                         if (tmp == db->queries[j])
601                                 continue;
602
603                         if (NULL == tmp) {
604                                 log_err ("Query \"%s\" not found for server version %i - "
605                                                 "please check your configuration.",
606                                                 db->queries[j]->name, server_version);
607
608                                 if (db->queries_num - j - 1 > 0)
609                                         memmove (db->queries + j, db->queries + j + 1,
610                                                         (db->queries_num - j - 1) * sizeof (*db->queries));
611
612                                 --db->queries_num;
613                                 --j;
614                                 continue;
615                         }
616
617                         db->queries[j] = tmp;
618                 }
619         }
620
621         plugin_register_read ("postgresql", c_psql_read);
622         plugin_register_shutdown ("postgresql", c_psql_shutdown);
623         return 0;
624 } /* c_psql_init */
625
626 static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
627 {
628         if ((0 != ci->children_num) || (1 != ci->values_num)
629                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
630                 log_err ("%s expects a single string argument.", name);
631                 return 1;
632         }
633
634         sfree (*var);
635         *var = sstrdup (ci->values[0].value.string);
636         return 0;
637 } /* config_set_s */
638
639 static int config_set_i (char *name, int *var, const oconfig_item_t *ci)
640 {
641         if ((0 != ci->children_num) || (1 != ci->values_num)
642                         || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
643                 log_err ("%s expects a single number argument.", name);
644                 return 1;
645         }
646
647         *var = (int)ci->values[0].value.number;
648         return 0;
649 } /* config_set_i */
650
651 static int config_set_param (c_psql_query_t *query, const oconfig_item_t *ci)
652 {
653         c_psql_param_t param;
654         char          *param_str;
655
656         if ((0 != ci->children_num) || (1 != ci->values_num)
657                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
658                 log_err ("Param expects a single string argument.");
659                 return 1;
660         }
661
662         param_str = ci->values[0].value.string;
663         if (0 == strcasecmp (param_str, "hostname"))
664                 param = C_PSQL_PARAM_HOST;
665         else if (0 == strcasecmp (param_str, "database"))
666                 param = C_PSQL_PARAM_DB;
667         else if (0 == strcasecmp (param_str, "username"))
668                 param = C_PSQL_PARAM_USER;
669         else {
670                 log_err ("Invalid parameter \"%s\".", param_str);
671                 return 1;
672         }
673
674         ++query->params_num;
675         if (NULL == (query->params = (c_psql_param_t *)realloc (query->params,
676                                 query->params_num * sizeof (*query->params)))) {
677                 log_err ("Out of memory.");
678                 exit (5);
679         }
680
681         query->params[query->params_num - 1] = param;
682         return 0;
683 } /* config_set_param */
684
685 static int config_set_column (c_psql_query_t *query, const oconfig_item_t *ci)
686 {
687         c_psql_col_t *col;
688
689         int i;
690
691         if ((0 != ci->children_num)
692                         || (1 > ci->values_num) || (2 < ci->values_num)) {
693                 log_err ("Column expects either one or two arguments.");
694                 return 1;
695         }
696
697         for (i = 0; i < ci->values_num; ++i) {
698                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
699                         log_err ("Column expects either one or two string arguments.");
700                         return 1;
701                 }
702         }
703
704         ++query->cols_num;
705         if (NULL == (query->cols = (c_psql_col_t *)realloc (query->cols,
706                                 query->cols_num * sizeof (*query->cols)))) {
707                 log_err ("Out of memory.");
708                 exit (5);
709         }
710
711         col = query->cols + query->cols_num - 1;
712
713         col->ds_type = -1;
714
715         col->type = sstrdup (ci->values[0].value.string);
716         col->type_instance = (2 == ci->values_num)
717                 ? sstrdup (ci->values[1].value.string) : NULL;
718         return 0;
719 } /* config_set_column */
720
721 static int set_query (c_psql_database_t *db, const char *name)
722 {
723         c_psql_query_t *query;
724
725         query = c_psql_query_get (name, -1);
726         if (NULL == query) {
727                 log_err ("Query \"%s\" not found - please check your configuration.",
728                                 name);
729                 return 1;
730         }
731
732         ++db->queries_num;
733         if (NULL == (db->queries = (c_psql_query_t **)realloc (db->queries,
734                                 db->queries_num * sizeof (*db->queries)))) {
735                 log_err ("Out of memory.");
736                 exit (5);
737         }
738
739         if (query->params_num > db->max_params_num)
740                 db->max_params_num = query->params_num;
741
742         db->queries[db->queries_num - 1] = query;
743         return 0;
744 } /* set_query */
745
746 static int config_set_query (c_psql_database_t *db, const oconfig_item_t *ci)
747 {
748         if ((0 != ci->children_num) || (1 != ci->values_num)
749                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
750                 log_err ("Query expects a single string argument.");
751                 return 1;
752         }
753         return set_query (db, ci->values[0].value.string);
754 } /* config_set_query */
755
756 static int c_psql_config_query (oconfig_item_t *ci)
757 {
758         c_psql_query_t *query;
759
760         int i;
761
762         if ((1 != ci->values_num)
763                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
764                 log_err ("<Query> expects a single string argument.");
765                 return 1;
766         }
767
768         query = c_psql_query_new (ci->values[0].value.string);
769
770         for (i = 0; i < ci->children_num; ++i) {
771                 oconfig_item_t *c = ci->children + i;
772
773                 if (0 == strcasecmp (c->key, "Query"))
774                         config_set_s ("Query", &query->query, c);
775                 else if (0 == strcasecmp (c->key, "Param"))
776                         config_set_param (query, c);
777                 else if (0 == strcasecmp (c->key, "Column"))
778                         config_set_column (query, c);
779                 else if (0 == strcasecmp (c->key, "MinPGVersion"))
780                         config_set_i ("MinPGVersion", &query->min_pg_version, c);
781                 else if (0 == strcasecmp (c->key, "MaxPGVersion"))
782                         config_set_i ("MaxPGVersion", &query->max_pg_version, c);
783                 else
784                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
785         }
786
787         for (i = 0; i < queries_num - 1; ++i) {
788                 c_psql_query_t *q = queries + i;
789
790                 if ((0 == strcasecmp (q->name, query->name))
791                                 && (q->min_pg_version <= query->max_pg_version)
792                                 && (query->min_pg_version <= q->max_pg_version)) {
793                         log_err ("Ignoring redefinition (with overlapping version ranges) "
794                                         "of query \"%s\".", query->name);
795                         c_psql_query_delete (query);
796                         --queries_num;
797                         return 1;
798                 }
799         }
800
801         if (query->min_pg_version > query->max_pg_version) {
802                 log_err ("Query \"%s\": MinPGVersion > MaxPGVersion.",
803                                 query->name);
804                 c_psql_query_delete (query);
805                 --queries_num;
806                 return 1;
807         }
808
809         if (NULL == query->query) {
810                 log_err ("Query \"%s\" does not include an SQL query string - "
811                                 "please check your configuration.", query->name);
812                 c_psql_query_delete (query);
813                 --queries_num;
814                 return 1;
815         }
816         return 0;
817 } /* c_psql_config_query */
818
819 static int c_psql_config_database (oconfig_item_t *ci)
820 {
821         c_psql_database_t *db;
822
823         int i;
824
825         if ((1 != ci->values_num)
826                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
827                 log_err ("<Database> expects a single string argument.");
828                 return 1;
829         }
830
831         db = c_psql_database_new (ci->values[0].value.string);
832
833         for (i = 0; i < ci->children_num; ++i) {
834                 oconfig_item_t *c = ci->children + i;
835
836                 if (0 == strcasecmp (c->key, "Host"))
837                         config_set_s ("Host", &db->host, c);
838                 else if (0 == strcasecmp (c->key, "Port"))
839                         config_set_s ("Port", &db->port, c);
840                 else if (0 == strcasecmp (c->key, "User"))
841                         config_set_s ("User", &db->user, c);
842                 else if (0 == strcasecmp (c->key, "Password"))
843                         config_set_s ("Password", &db->password, c);
844                 else if (0 == strcasecmp (c->key, "SSLMode"))
845                         config_set_s ("SSLMode", &db->sslmode, c);
846                 else if (0 == strcasecmp (c->key, "KRBSrvName"))
847                         config_set_s ("KRBSrvName", &db->krbsrvname, c);
848                 else if (0 == strcasecmp (c->key, "Service"))
849                         config_set_s ("Service", &db->service, c);
850                 else if (0 == strcasecmp (c->key, "Query"))
851                         config_set_query (db, c);
852                 else
853                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
854         }
855
856         if (NULL == db->queries) {
857                 for (i = 0; i < def_queries_num; ++i)
858                         set_query (db, def_queries[i]);
859         }
860         return 0;
861 }
862
863 static int c_psql_config (oconfig_item_t *ci)
864 {
865         static int have_def_config = 0;
866
867         int i;
868
869         if (0 == have_def_config) {
870                 oconfig_item_t *c;
871
872                 have_def_config = 1;
873
874                 c = oconfig_parse_file (C_PSQL_DEFAULT_CONF);
875                 if (NULL == c)
876                         log_err ("Failed to read default config ("C_PSQL_DEFAULT_CONF").");
877                 else
878                         c_psql_config (c);
879
880                 if (NULL == queries)
881                         log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
882                                         "any queries - please check your installation.");
883         }
884
885         for (i = 0; i < ci->children_num; ++i) {
886                 oconfig_item_t *c = ci->children + i;
887
888                 if (0 == strcasecmp (c->key, "Query"))
889                         c_psql_config_query (c);
890                 else if (0 == strcasecmp (c->key, "Database"))
891                         c_psql_config_database (c);
892                 else
893                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
894         }
895         return 0;
896 } /* c_psql_config */
897
898 void module_register (void)
899 {
900         plugin_register_complex_config ("postgresql", c_psql_config);
901         plugin_register_init ("postgresql", c_psql_init);
902 } /* module_register */
903
904 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
905