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