2 * collectd - src/postgresql.c
3 * Copyright (C) 2008 Sebastian Harl
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.
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.
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
19 * Sebastian Harl <sh at tokkee.org>
23 * This module collects PostgreSQL database statistics.
29 #include "configfile.h"
32 #include "utils_complain.h"
34 #include <pg_config_manual.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"
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
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); \
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
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." : ":", \
80 C_PSQL_PARAM_HOST = 1,
83 C_PSQL_PARAM_INTERVAL,
88 char *instance_prefix;
92 char **values_str; /* may be NULL, even if values_num != 0 in
93 case the "Column" option has been used */
103 c_psql_param_t *params;
106 c_psql_result_t *results;
115 c_complain_t conn_complaint;
121 /* user configuration */
122 c_psql_query_t **queries;
139 static char *def_queries[] = {
148 static int def_queries_num = STATIC_ARRAY_SIZE (def_queries);
150 static c_psql_query_t *queries = NULL;
151 static int queries_num = 0;
153 static c_psql_database_t *databases = NULL;
154 static int databases_num = 0;
156 static c_psql_result_t *c_psql_result_new (c_psql_query_t *query)
158 c_psql_result_t *res;
160 ++query->results_num;
161 if (NULL == (query->results = (c_psql_result_t *)realloc (query->results,
162 query->results_num * sizeof (*query->results)))) {
163 log_err ("Out of memory.");
166 res = query->results + query->results_num - 1;
170 res->instance_prefix = NULL;
171 res->instances_str = NULL;
172 res->instances = NULL;
173 res->instances_num = 0;
175 res->values_str = NULL;
177 res->ds_types = NULL;
180 } /* c_psql_result_new */
182 static void c_psql_result_delete (c_psql_result_t *res)
188 sfree (res->instance_prefix);
190 for (i = 0; i < res->instances_num; ++i)
191 sfree (res->instances_str[i]);
192 sfree (res->instances_str);
193 sfree (res->instances);
194 res->instances_num = 0;
196 for (i = 0; (NULL != res->values_str) && (i < res->values_num); ++i)
197 sfree (res->values_str[i]);
198 sfree (res->values_str);
200 sfree (res->ds_types);
202 } /* c_psql_result_delete */
204 static c_psql_query_t *c_psql_query_new (const char *name)
206 c_psql_query_t *query;
209 if (NULL == (queries = (c_psql_query_t *)realloc (queries,
210 queries_num * sizeof (*queries)))) {
211 log_err ("Out of memory.");
214 query = queries + queries_num - 1;
216 query->name = sstrdup (name);
219 query->params = NULL;
220 query->params_num = 0;
222 query->results = NULL;
223 query->results_num = 0;
225 query->min_pg_version = 0;
226 query->max_pg_version = INT_MAX;
228 } /* c_psql_query_new */
230 static int c_psql_query_init (c_psql_query_t *query)
234 /* Get the data set definitions for each query definition. */
235 for (i = 0; i < query->results_num; ++i) {
236 c_psql_result_t *res = query->results + i;
237 const data_set_t *ds;
241 ds = plugin_get_ds (res->type);
243 log_err ("Result: Unknown type \"%s\".", res->type);
247 if (res->values_num != ds->ds_num) {
248 log_err ("Result: Invalid type \"%s\" - "
249 "expected %i data source%s, got %i.",
250 res->type, res->values_num,
251 (1 == res->values_num) ? "" : "s",
256 for (j = 0; j < res->values_num; ++j)
257 res->ds_types[j] = ds->ds[j].type;
260 } /* c_psql_query_init */
262 static void c_psql_query_delete (c_psql_query_t *query)
269 sfree (query->params);
270 query->params_num = 0;
272 for (i = 0; i < query->results_num; ++i)
273 c_psql_result_delete (query->results + i);
274 sfree (query->results);
275 query->results_num = 0;
277 } /* c_psql_query_delete */
279 static c_psql_query_t *c_psql_query_get (const char *name, int server_version)
283 for (i = 0; i < queries_num; ++i)
284 if (0 == strcasecmp (name, queries[i].name)
285 && ((-1 == server_version)
286 || ((queries[i].min_pg_version <= server_version)
287 && (server_version <= queries[i].max_pg_version))))
290 } /* c_psql_query_get */
292 static c_psql_database_t *c_psql_database_new (const char *name)
294 c_psql_database_t *db;
297 if (NULL == (databases = (c_psql_database_t *)realloc (databases,
298 databases_num * sizeof (*databases)))) {
299 log_err ("Out of memory.");
303 db = databases + (databases_num - 1);
307 C_COMPLAIN_INIT (&db->conn_complaint);
309 db->proto_version = 0;
311 db->max_params_num = 0;
314 db->hidden_queries = NULL;
317 db->database = sstrdup (name);
325 db->krbsrvname = NULL;
329 } /* c_psql_database_new */
331 static void c_psql_database_init (c_psql_database_t *db, int server_version)
335 /* Get the right version of each query definition. */
336 for (i = 0; i < db->queries_num; ++i) {
339 tmp = c_psql_query_get (db->queries[i]->name, server_version);
341 if (tmp == db->queries[i])
345 log_err ("Query \"%s\" not found for server version %i - "
346 "please check your configuration.",
347 db->queries[i]->name, server_version);
348 /* By hiding the query (rather than removing it from the list) we
349 * don't lose it in case a reconnect to an available version
350 * happens at a later time. */
351 db->hidden_queries[i] = 1;
355 db->hidden_queries[i] = 0;
356 db->queries[i] = tmp;
358 } /* c_psql_database_init */
360 static void c_psql_database_delete (c_psql_database_t *db)
366 sfree (db->hidden_queries);
369 sfree (db->database);
373 sfree (db->password);
377 sfree (db->krbsrvname);
381 } /* c_psql_database_delete */
383 static void submit (const c_psql_database_t *db, const c_psql_result_t *res,
384 char **instances, value_t *values)
386 value_list_t vl = VALUE_LIST_INIT;
388 int instances_num = res->instances_num;
390 if (NULL != res->instance_prefix)
394 vl.values_len = res->values_num;
395 vl.time = time (NULL);
397 if (C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
398 || (0 == strcmp (db->host, "localhost")))
399 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
401 sstrncpy (vl.host, db->host, sizeof (vl.host));
403 sstrncpy (vl.plugin, "postgresql", sizeof (vl.plugin));
404 sstrncpy (vl.plugin_instance, db->database, sizeof (vl.plugin_instance));
406 sstrncpy (vl.type, res->type, sizeof (vl.type));
408 if (0 < instances_num) {
409 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
410 strjoin (vl.type_instance, sizeof (vl.type_instance),
411 instances, instances_num, "-");
413 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
414 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
415 log_warn ("Truncated type instance: %s.", vl.type_instance);
419 plugin_dispatch_values (&vl);
423 static int c_psql_get_colnum (PGresult *pgres,
424 char **strings, int *numbers, int idx)
428 if (0 <= numbers[idx])
431 colnum = PQfnumber (pgres, strings[idx]);
433 log_err ("No such column: %s.", strings[idx]);
435 numbers[idx] = colnum;
437 } /* c_psql_get_colnum */
439 static void c_psql_dispatch_row (c_psql_database_t *db, c_psql_query_t *query,
440 PGresult *pgres, int row)
444 for (i = 0; i < query->results_num; ++i) {
445 c_psql_result_t *res = query->results + i;
447 char *instances[res->instances_num + 1];
448 value_t values[res->values_num];
450 int offset = 0, status = 0, j;
452 /* get the instance name */
453 if (NULL != res->instance_prefix) {
454 instances[0] = res->instance_prefix;
458 for (j = 0; (0 == status) && (j < res->instances_num); ++j) {
459 int col = c_psql_get_colnum (pgres,
460 res->instances_str, res->instances, j);
467 instances[j + offset] = PQgetvalue (pgres, row, col);
468 if (NULL == instances[j + offset])
469 instances[j + offset] = "";
473 for (j = 0; (0 == status) && (j < res->values_num); ++j) {
474 int col = c_psql_get_colnum (pgres,
475 res->values_str, res->values, j);
485 value_str = PQgetvalue (pgres, row, col);
486 if ((NULL == value_str) || ('\0' == *value_str))
489 if (res->ds_types[j] == DS_TYPE_COUNTER)
490 values[j].counter = (counter_t)strtoll (value_str, &endptr, 0);
491 else if (res->ds_types[j] == DS_TYPE_GAUGE)
492 values[j].gauge = (gauge_t)strtod (value_str, &endptr);
494 log_err ("Invalid type \"%s\" (%i).",
495 res->type, res->ds_types[j]);
498 if (value_str == endptr) {
499 log_err ("Failed to parse string as number: %s.", value_str);
503 else if ((NULL != endptr) && ('\0' != *endptr))
504 log_warn ("Ignoring trailing garbage after number: %s.",
511 submit (db, res, instances, values);
513 } /* c_psql_dispatch_row */
515 static int c_psql_check_connection (c_psql_database_t *db)
518 PQclear (PQexec (db->conn, "SELECT 42;"));
520 if (CONNECTION_OK != PQstatus (db->conn)) {
523 /* trigger c_release() */
524 if (0 == db->conn_complaint.interval)
525 db->conn_complaint.interval = 1;
527 if (CONNECTION_OK != PQstatus (db->conn)) {
528 c_complain (LOG_ERR, &db->conn_complaint,
529 "Failed to connect to database %s: %s",
530 db->database, PQerrorMessage (db->conn));
534 db->proto_version = PQprotocolVersion (db->conn);
535 if (3 > db->proto_version)
536 log_warn ("Protocol version %d does not support parameters.",
540 /* We might have connected to a different PostgreSQL version, so we
541 * need to reinitialize stuff. */
542 if (c_would_release (&db->conn_complaint))
543 c_psql_database_init (db, PQserverVersion (db->conn));
545 c_release (LOG_INFO, &db->conn_complaint,
546 "Successfully reconnected to database %s", PQdb (db->conn));
548 } /* c_psql_check_connection */
550 static PGresult *c_psql_exec_query_params (c_psql_database_t *db,
551 c_psql_query_t *query)
553 char *params[db->max_params_num];
557 assert (db->max_params_num >= query->params_num);
559 for (i = 0; i < query->params_num; ++i) {
560 switch (query->params[i]) {
561 case C_PSQL_PARAM_HOST:
562 params[i] = C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
563 ? "localhost" : db->host;
565 case C_PSQL_PARAM_DB:
566 params[i] = db->database;
568 case C_PSQL_PARAM_USER:
569 params[i] = db->user;
571 case C_PSQL_PARAM_INTERVAL:
572 ssnprintf (interval, sizeof (interval), "%i", interval_g);
573 params[i] = interval;
580 return PQexecParams (db->conn, query->stmt, query->params_num, NULL,
581 (const char *const *)((0 == query->params_num) ? NULL : params),
582 NULL, NULL, /* return text data */ 0);
583 } /* c_psql_exec_query_params */
585 static PGresult *c_psql_exec_query_noparams (c_psql_database_t *db,
586 c_psql_query_t *query)
588 return PQexec (db->conn, query->stmt);
589 } /* c_psql_exec_query_noparams */
591 static int c_psql_exec_query (c_psql_database_t *db, int idx)
593 c_psql_query_t *query;
599 if (idx >= db->queries_num)
602 if (0 != db->hidden_queries[idx])
605 query = db->queries[idx];
607 if (3 <= db->proto_version)
608 res = c_psql_exec_query_params (db, query);
609 else if (0 == query->params_num)
610 res = c_psql_exec_query_noparams (db, query);
612 log_err ("Connection to database \"%s\" does not support parameters "
613 "(protocol version %d) - cannot execute query \"%s\".",
614 db->database, db->proto_version, query->name);
618 if (PGRES_TUPLES_OK != PQresultStatus (res)) {
619 log_err ("Failed to execute SQL query: %s",
620 PQerrorMessage (db->conn));
621 log_info ("SQL query was: %s", query->stmt);
626 rows = PQntuples (res);
632 cols = PQnfields (res);
634 for (i = 0; i < rows; ++i)
635 c_psql_dispatch_row (db, query, res, i);
638 } /* c_psql_exec_query */
640 static int c_psql_read (void)
645 for (i = 0; i < databases_num; ++i) {
646 c_psql_database_t *db = databases + i;
650 assert (NULL != db->database);
652 if (0 != c_psql_check_connection (db))
655 for (j = 0; j < db->queries_num; ++j)
656 c_psql_exec_query (db, j);
666 static int c_psql_shutdown (void)
670 if ((NULL == databases) || (0 == databases_num))
673 plugin_unregister_read ("postgresql");
674 plugin_unregister_shutdown ("postgresql");
676 for (i = 0; i < databases_num; ++i) {
677 c_psql_database_t *db = databases + i;
678 c_psql_database_delete (db);
684 for (i = 0; i < queries_num; ++i) {
685 c_psql_query_t *query = queries + i;
686 c_psql_query_delete (query);
692 } /* c_psql_shutdown */
694 static int c_psql_init (void)
698 if ((NULL == databases) || (0 == databases_num))
701 for (i = 0; i < queries_num; ++i)
702 if (0 != c_psql_query_init (queries + i)) {
707 for (i = 0; i < databases_num; ++i) {
708 c_psql_database_t *db = databases + i;
711 char *buf = conninfo;
712 int buf_len = sizeof (conninfo);
718 /* this will happen during reinitialization */
719 if (NULL != db->conn) {
720 c_psql_check_connection (db);
724 status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
730 C_PSQL_PAR_APPEND (buf, buf_len, "host", db->host);
731 C_PSQL_PAR_APPEND (buf, buf_len, "port", db->port);
732 C_PSQL_PAR_APPEND (buf, buf_len, "user", db->user);
733 C_PSQL_PAR_APPEND (buf, buf_len, "password", db->password);
734 C_PSQL_PAR_APPEND (buf, buf_len, "sslmode", db->sslmode);
735 C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
736 C_PSQL_PAR_APPEND (buf, buf_len, "service", db->service);
738 db->conn = PQconnectdb (conninfo);
739 if (0 != c_psql_check_connection (db))
742 db->proto_version = PQprotocolVersion (db->conn);
744 server_host = PQhost (db->conn);
745 server_version = PQserverVersion (db->conn);
746 log_info ("Sucessfully connected to database %s (user %s) "
747 "at server %s%s%s (server version: %d.%d.%d, "
748 "protocol version: %d, pid: %d)",
749 PQdb (db->conn), PQuser (db->conn),
750 C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
751 C_PSQL_SERVER_VERSION3 (server_version),
752 db->proto_version, PQbackendPID (db->conn));
754 if (3 > db->proto_version)
755 log_warn ("Protocol version %d does not support parameters.",
758 c_psql_database_init (db, server_version);
761 plugin_register_read ("postgresql", c_psql_read);
762 plugin_register_shutdown ("postgresql", c_psql_shutdown);
766 static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
768 if ((0 != ci->children_num) || (1 != ci->values_num)
769 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
770 log_err ("%s expects a single string argument.", name);
775 *var = sstrdup (ci->values[0].value.string);
779 static int config_set_i (char *name, int *var, const oconfig_item_t *ci)
781 if ((0 != ci->children_num) || (1 != ci->values_num)
782 || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
783 log_err ("%s expects a single number argument.", name);
787 *var = (int)ci->values[0].value.number;
791 static int config_append_array_s (char *name, char ***var, int *len,
792 const oconfig_item_t *ci)
796 if ((0 != ci->children_num) || (1 > ci->values_num)) {
797 log_err ("%s expects at least one argument.", name);
801 for (i = 0; i < ci->values_num; ++i) {
802 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
803 log_err ("%s expects string arguments.", name);
808 *len += ci->values_num;
809 if (NULL == (*var = (char **)realloc (*var, *len * sizeof (**var)))) {
810 log_err ("Out of memory.");
814 for (i = *len - ci->values_num; i < *len; ++i)
815 (*var)[i] = sstrdup (ci->values[i].value.string);
817 } /* config_append_array_s */
819 static int config_set_param (c_psql_query_t *query, const oconfig_item_t *ci)
821 c_psql_param_t param;
824 if ((0 != ci->children_num) || (1 != ci->values_num)
825 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
826 log_err ("Param expects a single string argument.");
830 param_str = ci->values[0].value.string;
831 if (0 == strcasecmp (param_str, "hostname"))
832 param = C_PSQL_PARAM_HOST;
833 else if (0 == strcasecmp (param_str, "database"))
834 param = C_PSQL_PARAM_DB;
835 else if (0 == strcasecmp (param_str, "username"))
836 param = C_PSQL_PARAM_USER;
837 else if (0 == strcasecmp (param_str, "interval"))
838 param = C_PSQL_PARAM_INTERVAL;
840 log_err ("Invalid parameter \"%s\".", param_str);
845 if (NULL == (query->params = (c_psql_param_t *)realloc (query->params,
846 query->params_num * sizeof (*query->params)))) {
847 log_err ("Out of memory.");
851 query->params[query->params_num - 1] = param;
853 } /* config_set_param */
855 static int config_set_result (c_psql_query_t *query, const oconfig_item_t *ci)
857 c_psql_result_t *res;
861 if (0 != ci->values_num) {
862 log_err ("<Result> does not expect any arguments.");
866 res = c_psql_result_new (query);
868 for (i = 0; i < ci->children_num; ++i) {
869 oconfig_item_t *c = ci->children + i;
871 if (0 == strcasecmp (c->key, "Type"))
872 config_set_s ("Type", &res->type, c);
873 else if (0 == strcasecmp (c->key, "InstancePrefix"))
874 config_set_s ("InstancePrefix", &res->instance_prefix, c);
875 else if (0 == strcasecmp (c->key, "InstancesFrom"))
876 config_append_array_s ("InstancesFrom",
877 &res->instances_str, &res->instances_num, c);
878 else if (0 == strcasecmp (c->key, "ValuesFrom"))
879 config_append_array_s ("ValuesFrom",
880 &res->values_str, &res->values_num, c);
882 log_warn ("Ignoring unknown config key \"%s\".", c->key);
885 if (NULL == res->type) {
886 log_warn ("Query \"%s\": Missing Type option in <Result> block.",
891 if (NULL == res->values_str) {
892 log_warn ("Query \"%s\": Missing ValuesFrom option in <Result> block.",
898 c_psql_result_delete (res);
899 --query->results_num;
903 /* preallocate memory to cache the column numbers and data types */
904 res->values = (int *)smalloc (res->values_num * sizeof (*res->values));
905 for (i = 0; i < res->values_num; ++i)
908 res->instances = (int *)smalloc (res->instances_num
909 * sizeof (*res->instances));
910 for (i = 0; i < res->instances_num; ++i)
911 res->instances[i] = -1;
913 res->ds_types = (int *)smalloc (res->values_num
914 * sizeof (*res->ds_types));
915 for (i = 0; i < res->values_num; ++i)
916 res->ds_types[i] = -1;
918 } /* config_set_result */
920 static int config_set_column (c_psql_query_t *query, int col_num,
921 const oconfig_item_t *ci)
923 c_psql_result_t *res;
927 if ((0 != ci->children_num)
928 || (1 > ci->values_num) || (2 < ci->values_num)) {
929 log_err ("Column expects either one or two arguments.");
933 for (i = 0; i < ci->values_num; ++i) {
934 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
935 log_err ("Column expects either one or two string arguments.");
940 res = c_psql_result_new (query);
942 res->type = sstrdup (ci->values[0].value.string);
944 if (2 == ci->values_num)
945 res->instance_prefix = sstrdup (ci->values[1].value.string);
947 res->values = (int *)smalloc (sizeof (*res->values));
948 res->values[0] = col_num;
949 res->ds_types = (int *)smalloc (sizeof (*res->ds_types));
952 } /* config_set_column */
954 static int set_query (c_psql_database_t *db, const char *name)
956 c_psql_query_t *query;
958 query = c_psql_query_get (name, -1);
960 log_err ("Query \"%s\" not found - please check your configuration.",
966 if (NULL == (db->queries = (c_psql_query_t **)realloc (db->queries,
967 db->queries_num * sizeof (*db->queries)))) {
968 log_err ("Out of memory.");
972 if (query->params_num > db->max_params_num)
973 db->max_params_num = query->params_num;
975 db->queries[db->queries_num - 1] = query;
979 static int config_set_query (c_psql_database_t *db, const oconfig_item_t *ci)
981 if ((0 != ci->children_num) || (1 != ci->values_num)
982 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
983 log_err ("Query expects a single string argument.");
986 return set_query (db, ci->values[0].value.string);
987 } /* config_set_query */
989 static int c_psql_config_query (oconfig_item_t *ci)
991 c_psql_query_t *query;
993 int status = 0, col_num = 0, i;
995 if ((1 != ci->values_num)
996 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
997 log_err ("<Query> expects a single string argument.");
1001 query = c_psql_query_new (ci->values[0].value.string);
1003 for (i = 0; i < ci->children_num; ++i) {
1004 oconfig_item_t *c = ci->children + i;
1006 if (0 == strcasecmp (c->key, "Statement"))
1007 config_set_s ("Statement", &query->stmt, c);
1008 /* backwards compat for versions < 4.6 */
1009 else if (0 == strcasecmp (c->key, "Query")) {
1010 log_warn ("<Query>: 'Query' is deprecated - use 'Statement' instead.");
1011 config_set_s ("Query", &query->stmt, c);
1013 else if (0 == strcasecmp (c->key, "Param"))
1014 config_set_param (query, c);
1015 else if (0 == strcasecmp (c->key, "Result"))
1016 config_set_result (query, c);
1017 /* backwards compat for versions < 4.6 */
1018 else if (0 == strcasecmp (c->key, "Column")) {
1019 log_warn ("<Query>: 'Column' is deprecated - "
1020 "use a <Result> block instead.");
1021 config_set_column (query, col_num, c);
1024 else if (0 == strcasecmp (c->key, "MinPGVersion"))
1025 config_set_i ("MinPGVersion", &query->min_pg_version, c);
1026 else if (0 == strcasecmp (c->key, "MaxPGVersion"))
1027 config_set_i ("MaxPGVersion", &query->max_pg_version, c);
1029 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1032 for (i = 0; i < queries_num - 1; ++i) {
1033 c_psql_query_t *q = queries + i;
1035 if ((0 == strcasecmp (q->name, query->name))
1036 && (q->min_pg_version <= query->max_pg_version)
1037 && (query->min_pg_version <= q->max_pg_version)) {
1038 log_err ("Ignoring redefinition (with overlapping version ranges) "
1039 "of query \"%s\".", query->name);
1045 if (query->min_pg_version > query->max_pg_version) {
1046 log_err ("Query \"%s\": MinPGVersion > MaxPGVersion.",
1051 if (NULL == query->stmt) {
1052 log_err ("Query \"%s\" does not include an SQL query statement - "
1053 "please check your configuration.", query->name);
1058 c_psql_query_delete (query);
1063 } /* c_psql_config_query */
1065 static int c_psql_config_database (oconfig_item_t *ci)
1067 c_psql_database_t *db;
1071 if ((1 != ci->values_num)
1072 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1073 log_err ("<Database> expects a single string argument.");
1077 db = c_psql_database_new (ci->values[0].value.string);
1079 for (i = 0; i < ci->children_num; ++i) {
1080 oconfig_item_t *c = ci->children + i;
1082 if (0 == strcasecmp (c->key, "Host"))
1083 config_set_s ("Host", &db->host, c);
1084 else if (0 == strcasecmp (c->key, "Port"))
1085 config_set_s ("Port", &db->port, c);
1086 else if (0 == strcasecmp (c->key, "User"))
1087 config_set_s ("User", &db->user, c);
1088 else if (0 == strcasecmp (c->key, "Password"))
1089 config_set_s ("Password", &db->password, c);
1090 else if (0 == strcasecmp (c->key, "SSLMode"))
1091 config_set_s ("SSLMode", &db->sslmode, c);
1092 else if (0 == strcasecmp (c->key, "KRBSrvName"))
1093 config_set_s ("KRBSrvName", &db->krbsrvname, c);
1094 else if (0 == strcasecmp (c->key, "Service"))
1095 config_set_s ("Service", &db->service, c);
1096 else if (0 == strcasecmp (c->key, "Query"))
1097 config_set_query (db, c);
1099 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1102 if (NULL == db->queries) {
1103 for (i = 0; i < def_queries_num; ++i)
1104 set_query (db, def_queries[i]);
1107 db->hidden_queries = (int *)calloc (db->queries_num,
1108 sizeof (*db->hidden_queries));
1109 if (NULL == db->hidden_queries) {
1110 log_err ("Out of memory.");
1114 } /* c_psql_config_database */
1116 static int c_psql_config (oconfig_item_t *ci)
1118 static int have_def_config = 0;
1122 if (0 == have_def_config) {
1125 have_def_config = 1;
1127 c = oconfig_parse_file (C_PSQL_DEFAULT_CONF);
1129 log_err ("Failed to read default config ("C_PSQL_DEFAULT_CONF").");
1133 if (NULL == queries)
1134 log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
1135 "any queries - please check your installation.");
1138 for (i = 0; i < ci->children_num; ++i) {
1139 oconfig_item_t *c = ci->children + i;
1141 if (0 == strcasecmp (c->key, "Query"))
1142 c_psql_config_query (c);
1143 else if (0 == strcasecmp (c->key, "Database"))
1144 c_psql_config_database (c);
1146 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1149 } /* c_psql_config */
1151 void module_register (void)
1153 plugin_register_complex_config ("postgresql", c_psql_config);
1154 plugin_register_init ("postgresql", c_psql_init);
1155 } /* module_register */
1157 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */