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 **/
22 /*
23 * This module collects PostgreSQL database statistics.
24 */
26 #include "collectd.h"
27 #include "common.h"
29 #include "configfile.h"
30 #include "plugin.h"
32 #include "utils_complain.h"
34 #include <pg_config_manual.h>
35 #include <libpq-fe.h>
37 #define log_err(...) ERROR ("postgresql: " __VA_ARGS__)
38 #define log_warn(...) WARNING ("postgresql: " __VA_ARGS__)
39 #define log_info(...) INFO ("postgresql: " __VA_ARGS__)
41 #ifndef C_PSQL_DEFAULT_CONF
42 # define C_PSQL_DEFAULT_CONF PKGDATADIR "/postgresql_default.conf"
43 #endif
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 }
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
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)))
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
79 typedef enum {
80 C_PSQL_PARAM_HOST = 1,
81 C_PSQL_PARAM_DB,
82 C_PSQL_PARAM_USER,
83 } c_psql_param_t;
85 typedef struct {
86 char *type;
87 char *type_instance;
88 int ds_type;
89 } c_psql_col_t;
91 typedef struct {
92 char *name;
93 char *query;
95 c_psql_param_t *params;
96 int params_num;
98 c_psql_col_t *cols;
99 int cols_num;
101 int min_pg_version;
102 int max_pg_version;
103 } c_psql_query_t;
105 typedef struct {
106 PGconn *conn;
107 c_complain_t conn_complaint;
109 int proto_version;
111 int max_params_num;
113 /* user configuration */
114 c_psql_query_t **queries;
115 int queries_num;
117 char *host;
118 char *port;
119 char *database;
120 char *user;
121 char *password;
123 char *sslmode;
125 char *krbsrvname;
127 char *service;
128 } c_psql_database_t;
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);
141 static c_psql_query_t *queries = NULL;
142 static int queries_num = 0;
144 static c_psql_database_t *databases = NULL;
145 static int databases_num = 0;
147 static c_psql_query_t *c_psql_query_new (const char *name)
148 {
149 c_psql_query_t *query;
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;
159 query->name = sstrdup (name);
160 query->query = NULL;
162 query->params = NULL;
163 query->params_num = 0;
165 query->cols = NULL;
166 query->cols_num = 0;
168 query->min_pg_version = 0;
169 query->max_pg_version = INT_MAX;
170 return query;
171 } /* c_psql_query_new */
173 static void c_psql_query_delete (c_psql_query_t *query)
174 {
175 int i;
177 sfree (query->name);
178 sfree (query->query);
180 sfree (query->params);
181 query->params_num = 0;
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 */
192 static c_psql_query_t *c_psql_query_get (const char *name, int server_version)
193 {
194 int i;
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 */
205 static c_psql_database_t *c_psql_database_new (const char *name)
206 {
207 c_psql_database_t *db;
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 }
216 db = databases + (databases_num - 1);
218 db->conn = NULL;
220 db->conn_complaint.last = 0;
221 db->conn_complaint.interval = 0;
223 db->proto_version = 0;
225 db->max_params_num = 0;
227 db->queries = NULL;
228 db->queries_num = 0;
230 db->database = sstrdup (name);
231 db->host = NULL;
232 db->port = NULL;
233 db->user = NULL;
234 db->password = NULL;
236 db->sslmode = NULL;
238 db->krbsrvname = NULL;
240 db->service = NULL;
241 return db;
242 } /* c_psql_database_new */
244 static void c_psql_database_delete (c_psql_database_t *db)
245 {
246 PQfinish (db->conn);
248 sfree (db->queries);
249 db->queries_num = 0;
251 sfree (db->database);
252 sfree (db->host);
253 sfree (db->port);
254 sfree (db->user);
255 sfree (db->password);
257 sfree (db->sslmode);
259 sfree (db->krbsrvname);
261 sfree (db->service);
262 return;
263 } /* c_psql_database_delete */
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;
271 vl.values = values;
272 vl.values_len = values_len;
273 vl.time = time (NULL);
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));
281 sstrncpy (vl.plugin, "postgresql", sizeof (vl.plugin));
282 sstrncpy (vl.plugin_instance, db->database, sizeof (vl.plugin_instance));
284 sstrncpy (vl.type, type, sizeof (vl.type));
286 if (NULL != type_instance)
287 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
289 plugin_dispatch_values (&vl);
290 return;
291 } /* submit */
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];
299 if ((NULL == value) || ('\0' == *value))
300 return;
302 values[0].counter = atoll (value);
303 submit (db, type, type_instance, values, 1);
304 return;
305 } /* submit_counter */
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];
313 if ((NULL == value) || ('\0' == *value))
314 return;
316 values[0].gauge = atof (value);
317 submit (db, type, type_instance, values, 1);
318 return;
319 } /* submit_gauge */
321 static int c_psql_check_connection (c_psql_database_t *db)
322 {
323 /* "ping" */
324 PQclear (PQexec (db->conn, "SELECT 42;"));
326 if (CONNECTION_OK != PQstatus (db->conn)) {
327 PQreset (db->conn);
329 /* trigger c_release() */
330 if (0 == db->conn_complaint.interval)
331 db->conn_complaint.interval = 1;
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 }
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 }
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 */
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;
357 assert (db->max_params_num >= query->params_num);
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 }
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 */
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 */
387 static int c_psql_exec_query (c_psql_database_t *db, int idx)
388 {
389 c_psql_query_t *query;
390 PGresult *res;
392 int rows, cols;
393 int i;
395 if (idx >= db->queries_num)
396 return -1;
398 query = db->queries[idx];
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 }
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 }
419 rows = PQntuples (res);
420 if (1 > rows)
421 return 0;
423 cols = PQnfields (res);
424 if (query->cols_num != cols) {
425 log_err ("SQL query returned wrong number of fields "
426 "(expected: %i, got: %i)", query->cols_num, cols);
427 log_info ("SQL query was: %s", query->query);
428 return -1;
429 }
431 for (i = 0; i < rows; ++i) {
432 int j;
434 for (j = 0; j < cols; ++j) {
435 c_psql_col_t col = query->cols[j];
437 char *value = PQgetvalue (res, i, j);
439 if (col.ds_type == DS_TYPE_COUNTER)
440 submit_counter (db, col.type, col.type_instance, value);
441 else if (col.ds_type == DS_TYPE_GAUGE)
442 submit_gauge (db, col.type, col.type_instance, value);
443 }
444 }
445 return 0;
446 } /* c_psql_exec_query */
448 static int c_psql_read (void)
449 {
450 int success = 0;
451 int i;
453 for (i = 0; i < databases_num; ++i) {
454 c_psql_database_t *db = databases + i;
456 int j;
458 assert (NULL != db->database);
460 if (0 != c_psql_check_connection (db))
461 continue;
463 for (j = 0; j < db->queries_num; ++j)
464 c_psql_exec_query (db, j);
466 ++success;
467 }
469 if (! success)
470 return -1;
471 return 0;
472 } /* c_psql_read */
474 static int c_psql_shutdown (void)
475 {
476 int i;
478 if ((NULL == databases) || (0 == databases_num))
479 return 0;
481 plugin_unregister_read ("postgresql");
482 plugin_unregister_shutdown ("postgresql");
484 for (i = 0; i < databases_num; ++i) {
485 c_psql_database_t *db = databases + i;
486 c_psql_database_delete (db);
487 }
489 sfree (databases);
490 databases_num = 0;
492 for (i = 0; i < queries_num; ++i) {
493 c_psql_query_t *query = queries + i;
494 c_psql_query_delete (query);
495 }
497 sfree (queries);
498 queries_num = 0;
499 return 0;
500 } /* c_psql_shutdown */
502 static int c_psql_init (void)
503 {
504 int i;
506 if ((NULL == databases) || (0 == databases_num))
507 return 0;
509 for (i = 0; i < queries_num; ++i) {
510 c_psql_query_t *query = queries + i;
511 int j;
513 for (j = 0; j < query->cols_num; ++j) {
514 c_psql_col_t *col = query->cols + j;
515 const data_set_t *ds;
517 ds = plugin_get_ds (col->type);
518 if (NULL == ds) {
519 log_err ("Column: Unknown type \"%s\".", col->type);
520 c_psql_shutdown ();
521 return -1;
522 }
524 if (1 != ds->ds_num) {
525 log_err ("Column: Invalid type \"%s\" - types defining "
526 "one data source are supported only (got: %i).",
527 col->type, ds->ds_num);
528 c_psql_shutdown ();
529 return -1;
530 }
532 col->ds_type = ds->ds[0].type;
533 }
534 }
536 for (i = 0; i < databases_num; ++i) {
537 c_psql_database_t *db = databases + i;
539 char conninfo[4096];
540 char *buf = conninfo;
541 int buf_len = sizeof (conninfo);
542 int status;
544 char *server_host;
545 int server_version;
547 int j;
549 status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
550 if (0 < status) {
551 buf += status;
552 buf_len -= status;
553 }
555 C_PSQL_PAR_APPEND (buf, buf_len, "host", db->host);
556 C_PSQL_PAR_APPEND (buf, buf_len, "port", db->port);
557 C_PSQL_PAR_APPEND (buf, buf_len, "user", db->user);
558 C_PSQL_PAR_APPEND (buf, buf_len, "password", db->password);
559 C_PSQL_PAR_APPEND (buf, buf_len, "sslmode", db->sslmode);
560 C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
561 C_PSQL_PAR_APPEND (buf, buf_len, "service", db->service);
563 db->conn = PQconnectdb (conninfo);
564 if (0 != c_psql_check_connection (db))
565 continue;
567 db->proto_version = PQprotocolVersion (db->conn);
569 server_host = PQhost (db->conn);
570 server_version = PQserverVersion (db->conn);
571 log_info ("Sucessfully connected to database %s (user %s) "
572 "at server %s%s%s (server version: %d.%d.%d, "
573 "protocol version: %d, pid: %d)",
574 PQdb (db->conn), PQuser (db->conn),
575 C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
576 C_PSQL_SERVER_VERSION3 (server_version),
577 db->proto_version, PQbackendPID (db->conn));
579 if (3 > db->proto_version)
580 log_warn ("Protocol version %d does not support parameters.",
581 db->proto_version);
583 /* Now that we know the PostgreSQL server version, we can get the
584 * right version of each query definition. */
585 for (j = 0; j < db->queries_num; ++j) {
586 c_psql_query_t *tmp;
588 tmp = c_psql_query_get (db->queries[j]->name, server_version);
590 if (tmp == db->queries[j])
591 continue;
593 if (NULL == tmp) {
594 log_err ("Query \"%s\" not found for server version %i - "
595 "please check your configuration.",
596 db->queries[j]->name, server_version);
598 if (db->queries_num - j - 1 > 0)
599 memmove (db->queries + j, db->queries + j + 1,
600 (db->queries_num - j - 1) * sizeof (*db->queries));
602 --db->queries_num;
603 --j;
604 continue;
605 }
607 db->queries[j] = tmp;
608 }
609 }
611 plugin_register_read ("postgresql", c_psql_read);
612 plugin_register_shutdown ("postgresql", c_psql_shutdown);
613 return 0;
614 } /* c_psql_init */
616 static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
617 {
618 if ((0 != ci->children_num) || (1 != ci->values_num)
619 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
620 log_err ("%s expects a single string argument.", name);
621 return 1;
622 }
624 sfree (*var);
625 *var = sstrdup (ci->values[0].value.string);
626 return 0;
627 } /* config_set_s */
629 static int config_set_i (char *name, int *var, const oconfig_item_t *ci)
630 {
631 if ((0 != ci->children_num) || (1 != ci->values_num)
632 || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
633 log_err ("%s expects a single number argument.", name);
634 return 1;
635 }
637 *var = (int)ci->values[0].value.number;
638 return 0;
639 } /* config_set_i */
641 static int config_set_param (c_psql_query_t *query, const oconfig_item_t *ci)
642 {
643 c_psql_param_t param;
644 char *param_str;
646 if ((0 != ci->children_num) || (1 != ci->values_num)
647 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
648 log_err ("Param expects a single string argument.");
649 return 1;
650 }
652 param_str = ci->values[0].value.string;
653 if (0 == strcasecmp (param_str, "hostname"))
654 param = C_PSQL_PARAM_HOST;
655 else if (0 == strcasecmp (param_str, "database"))
656 param = C_PSQL_PARAM_DB;
657 else if (0 == strcasecmp (param_str, "username"))
658 param = C_PSQL_PARAM_USER;
659 else {
660 log_err ("Invalid parameter \"%s\".", param_str);
661 return 1;
662 }
664 ++query->params_num;
665 if (NULL == (query->params = (c_psql_param_t *)realloc (query->params,
666 query->params_num * sizeof (*query->params)))) {
667 log_err ("Out of memory.");
668 exit (5);
669 }
671 query->params[query->params_num - 1] = param;
672 return 0;
673 } /* config_set_param */
675 static int config_set_column (c_psql_query_t *query, const oconfig_item_t *ci)
676 {
677 c_psql_col_t *col;
679 int i;
681 if ((0 != ci->children_num)
682 || (1 > ci->values_num) || (2 < ci->values_num)) {
683 log_err ("Column expects either one or two arguments.");
684 return 1;
685 }
687 for (i = 0; i < ci->values_num; ++i) {
688 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
689 log_err ("Column expects either one or two string arguments.");
690 return 1;
691 }
692 }
694 ++query->cols_num;
695 if (NULL == (query->cols = (c_psql_col_t *)realloc (query->cols,
696 query->cols_num * sizeof (*query->cols)))) {
697 log_err ("Out of memory.");
698 exit (5);
699 }
701 col = query->cols + query->cols_num - 1;
703 col->ds_type = -1;
705 col->type = sstrdup (ci->values[0].value.string);
706 col->type_instance = (2 == ci->values_num)
707 ? sstrdup (ci->values[1].value.string) : NULL;
708 return 0;
709 } /* config_set_column */
711 static int set_query (c_psql_database_t *db, const char *name)
712 {
713 c_psql_query_t *query;
715 query = c_psql_query_get (name, -1);
716 if (NULL == query) {
717 log_err ("Query \"%s\" not found - please check your configuration.",
718 name);
719 return 1;
720 }
722 ++db->queries_num;
723 if (NULL == (db->queries = (c_psql_query_t **)realloc (db->queries,
724 db->queries_num * sizeof (*db->queries)))) {
725 log_err ("Out of memory.");
726 exit (5);
727 }
729 if (query->params_num > db->max_params_num)
730 db->max_params_num = query->params_num;
732 db->queries[db->queries_num - 1] = query;
733 return 0;
734 } /* set_query */
736 static int config_set_query (c_psql_database_t *db, const oconfig_item_t *ci)
737 {
738 if ((0 != ci->children_num) || (1 != ci->values_num)
739 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
740 log_err ("Query expects a single string argument.");
741 return 1;
742 }
743 return set_query (db, ci->values[0].value.string);
744 } /* config_set_query */
746 static int c_psql_config_query (oconfig_item_t *ci)
747 {
748 c_psql_query_t *query;
750 int i;
752 if ((1 != ci->values_num)
753 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
754 log_err ("<Query> expects a single string argument.");
755 return 1;
756 }
758 query = c_psql_query_new (ci->values[0].value.string);
760 for (i = 0; i < ci->children_num; ++i) {
761 oconfig_item_t *c = ci->children + i;
763 if (0 == strcasecmp (c->key, "Query"))
764 config_set_s ("Query", &query->query, c);
765 else if (0 == strcasecmp (c->key, "Param"))
766 config_set_param (query, c);
767 else if (0 == strcasecmp (c->key, "Column"))
768 config_set_column (query, c);
769 else if (0 == strcasecmp (c->key, "MinPGVersion"))
770 config_set_i ("MinPGVersion", &query->min_pg_version, c);
771 else if (0 == strcasecmp (c->key, "MaxPGVersion"))
772 config_set_i ("MaxPGVersion", &query->max_pg_version, c);
773 else
774 log_warn ("Ignoring unknown config key \"%s\".", c->key);
775 }
777 if (NULL == query->query) {
778 log_err ("Query \"%s\" does not include an SQL query string - "
779 "please check your configuration.", query->name);
780 c_psql_query_delete (query);
781 --queries_num;
782 return 1;
783 }
784 return 0;
785 } /* c_psql_config_query */
787 static int c_psql_config_database (oconfig_item_t *ci)
788 {
789 c_psql_database_t *db;
791 int i;
793 if ((1 != ci->values_num)
794 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
795 log_err ("<Database> expects a single string argument.");
796 return 1;
797 }
799 db = c_psql_database_new (ci->values[0].value.string);
801 for (i = 0; i < ci->children_num; ++i) {
802 oconfig_item_t *c = ci->children + i;
804 if (0 == strcasecmp (c->key, "Host"))
805 config_set_s ("Host", &db->host, c);
806 else if (0 == strcasecmp (c->key, "Port"))
807 config_set_s ("Port", &db->port, c);
808 else if (0 == strcasecmp (c->key, "User"))
809 config_set_s ("User", &db->user, c);
810 else if (0 == strcasecmp (c->key, "Password"))
811 config_set_s ("Password", &db->password, c);
812 else if (0 == strcasecmp (c->key, "SSLMode"))
813 config_set_s ("SSLMode", &db->sslmode, c);
814 else if (0 == strcasecmp (c->key, "KRBSrvName"))
815 config_set_s ("KRBSrvName", &db->krbsrvname, c);
816 else if (0 == strcasecmp (c->key, "Service"))
817 config_set_s ("Service", &db->service, c);
818 else if (0 == strcasecmp (c->key, "Query"))
819 config_set_query (db, c);
820 else
821 log_warn ("Ignoring unknown config key \"%s\".", c->key);
822 }
824 if (NULL == db->queries) {
825 for (i = 0; i < def_queries_num; ++i)
826 set_query (db, def_queries[i]);
827 }
828 return 0;
829 }
831 static int c_psql_config (oconfig_item_t *ci)
832 {
833 static int have_def_config = 0;
835 int i;
837 if (0 == have_def_config) {
838 oconfig_item_t *c;
840 have_def_config = 1;
842 c = oconfig_parse_file (C_PSQL_DEFAULT_CONF);
843 if (NULL == c)
844 log_err ("Failed to read default config ("C_PSQL_DEFAULT_CONF").");
845 else
846 c_psql_config (c);
848 if (NULL == queries)
849 log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
850 "any queries - please check your installation.");
851 }
853 for (i = 0; i < ci->children_num; ++i) {
854 oconfig_item_t *c = ci->children + i;
856 if (0 == strcasecmp (c->key, "Query"))
857 c_psql_config_query (c);
858 else if (0 == strcasecmp (c->key, "Database"))
859 c_psql_config_database (c);
860 else
861 log_warn ("Ignoring unknown config key \"%s\".", c->key);
862 }
863 return 0;
864 } /* c_psql_config */
866 void module_register (void)
867 {
868 plugin_register_complex_config ("postgresql", c_psql_config);
869 plugin_register_init ("postgresql", c_psql_init);
870 } /* module_register */
872 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */