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