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 PQclear (res);
422 return 0;
423 }
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 }
433 for (i = 0; i < rows; ++i) {
434 int j;
436 for (j = 0; j < cols; ++j) {
437 c_psql_col_t col = query->cols[j];
439 char *value = PQgetvalue (res, i, j);
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 */
451 static int c_psql_read (void)
452 {
453 int success = 0;
454 int i;
456 for (i = 0; i < databases_num; ++i) {
457 c_psql_database_t *db = databases + i;
459 int j;
461 assert (NULL != db->database);
463 if (0 != c_psql_check_connection (db))
464 continue;
466 for (j = 0; j < db->queries_num; ++j)
467 c_psql_exec_query (db, j);
469 ++success;
470 }
472 if (! success)
473 return -1;
474 return 0;
475 } /* c_psql_read */
477 static int c_psql_shutdown (void)
478 {
479 int i;
481 if ((NULL == databases) || (0 == databases_num))
482 return 0;
484 plugin_unregister_read ("postgresql");
485 plugin_unregister_shutdown ("postgresql");
487 for (i = 0; i < databases_num; ++i) {
488 c_psql_database_t *db = databases + i;
489 c_psql_database_delete (db);
490 }
492 sfree (databases);
493 databases_num = 0;
495 for (i = 0; i < queries_num; ++i) {
496 c_psql_query_t *query = queries + i;
497 c_psql_query_delete (query);
498 }
500 sfree (queries);
501 queries_num = 0;
502 return 0;
503 } /* c_psql_shutdown */
505 static int c_psql_init (void)
506 {
507 int i;
509 if ((NULL == databases) || (0 == databases_num))
510 return 0;
512 for (i = 0; i < queries_num; ++i) {
513 c_psql_query_t *query = queries + i;
514 int j;
516 for (j = 0; j < query->cols_num; ++j) {
517 c_psql_col_t *col = query->cols + j;
518 const data_set_t *ds;
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 }
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 }
535 col->ds_type = ds->ds[0].type;
536 }
537 }
539 for (i = 0; i < databases_num; ++i) {
540 c_psql_database_t *db = databases + i;
542 char conninfo[4096];
543 char *buf = conninfo;
544 int buf_len = sizeof (conninfo);
545 int status;
547 char *server_host;
548 int server_version;
550 int j;
552 status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
553 if (0 < status) {
554 buf += status;
555 buf_len -= status;
556 }
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);
566 db->conn = PQconnectdb (conninfo);
567 if (0 != c_psql_check_connection (db))
568 continue;
570 db->proto_version = PQprotocolVersion (db->conn);
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));
582 if (3 > db->proto_version)
583 log_warn ("Protocol version %d does not support parameters.",
584 db->proto_version);
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;
591 tmp = c_psql_query_get (db->queries[j]->name, server_version);
593 if (tmp == db->queries[j])
594 continue;
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);
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));
605 --db->queries_num;
606 --j;
607 continue;
608 }
610 db->queries[j] = tmp;
611 }
612 }
614 plugin_register_read ("postgresql", c_psql_read);
615 plugin_register_shutdown ("postgresql", c_psql_shutdown);
616 return 0;
617 } /* c_psql_init */
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 }
627 sfree (*var);
628 *var = sstrdup (ci->values[0].value.string);
629 return 0;
630 } /* config_set_s */
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 }
640 *var = (int)ci->values[0].value.number;
641 return 0;
642 } /* config_set_i */
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;
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 }
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 }
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 }
674 query->params[query->params_num - 1] = param;
675 return 0;
676 } /* config_set_param */
678 static int config_set_column (c_psql_query_t *query, const oconfig_item_t *ci)
679 {
680 c_psql_col_t *col;
682 int i;
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 }
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 }
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 }
704 col = query->cols + query->cols_num - 1;
706 col->ds_type = -1;
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 */
714 static int set_query (c_psql_database_t *db, const char *name)
715 {
716 c_psql_query_t *query;
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 }
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 }
732 if (query->params_num > db->max_params_num)
733 db->max_params_num = query->params_num;
735 db->queries[db->queries_num - 1] = query;
736 return 0;
737 } /* set_query */
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 */
749 static int c_psql_config_query (oconfig_item_t *ci)
750 {
751 c_psql_query_t *query;
753 int i;
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 }
761 query = c_psql_query_new (ci->values[0].value.string);
763 for (i = 0; i < ci->children_num; ++i) {
764 oconfig_item_t *c = ci->children + i;
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 }
780 for (i = 0; i < queries_num - 1; ++i) {
781 c_psql_query_t *q = queries + i;
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 }
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 }
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 */
812 static int c_psql_config_database (oconfig_item_t *ci)
813 {
814 c_psql_database_t *db;
816 int i;
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 }
824 db = c_psql_database_new (ci->values[0].value.string);
826 for (i = 0; i < ci->children_num; ++i) {
827 oconfig_item_t *c = ci->children + i;
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 }
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 }
856 static int c_psql_config (oconfig_item_t *ci)
857 {
858 static int have_def_config = 0;
860 int i;
862 if (0 == have_def_config) {
863 oconfig_item_t *c;
865 have_def_config = 1;
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);
873 if (NULL == queries)
874 log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
875 "any queries - please check your installation.");
876 }
878 for (i = 0; i < ci->children_num; ++i) {
879 oconfig_item_t *c = ci->children + i;
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 */
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 */
897 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */