2 * collectd - src/postgresql.c
3 * Copyright (C) 2008, 2009 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;
396 if (C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
397 || (0 == strcmp (db->host, "localhost")))
398 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
400 sstrncpy (vl.host, db->host, sizeof (vl.host));
402 sstrncpy (vl.plugin, "postgresql", sizeof (vl.plugin));
403 sstrncpy (vl.plugin_instance, db->database, sizeof (vl.plugin_instance));
405 sstrncpy (vl.type, res->type, sizeof (vl.type));
407 if (0 < instances_num) {
408 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
409 strjoin (vl.type_instance, sizeof (vl.type_instance),
410 instances, instances_num, "-");
412 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
413 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
414 log_warn ("Truncated type instance: %s.", vl.type_instance);
418 plugin_dispatch_values (&vl);
422 static int c_psql_get_colnum (PGresult *pgres,
423 char **strings, int *numbers, int idx)
427 if (0 <= numbers[idx])
430 colnum = PQfnumber (pgres, strings[idx]);
432 log_err ("No such column: %s.", strings[idx]);
434 numbers[idx] = colnum;
436 } /* c_psql_get_colnum */
438 static void c_psql_dispatch_row (c_psql_database_t *db, c_psql_query_t *query,
439 PGresult *pgres, int row)
443 for (i = 0; i < query->results_num; ++i) {
444 c_psql_result_t *res = query->results + i;
446 char *instances[res->instances_num + 1];
447 value_t values[res->values_num];
449 int offset = 0, status = 0, j;
451 /* get the instance name */
452 if (NULL != res->instance_prefix) {
453 instances[0] = res->instance_prefix;
457 for (j = 0; (0 == status) && (j < res->instances_num); ++j) {
458 int col = c_psql_get_colnum (pgres,
459 res->instances_str, res->instances, j);
466 instances[j + offset] = PQgetvalue (pgres, row, col);
467 if (NULL == instances[j + offset])
468 instances[j + offset] = "";
472 for (j = 0; (0 == status) && (j < res->values_num); ++j) {
473 int col = c_psql_get_colnum (pgres,
474 res->values_str, res->values, j);
484 value_str = PQgetvalue (pgres, row, col);
485 if ((NULL == value_str) || ('\0' == *value_str))
488 if (res->ds_types[j] == DS_TYPE_COUNTER)
489 values[j].counter = (counter_t)strtoll (value_str, &endptr, 0);
490 else if (res->ds_types[j] == DS_TYPE_GAUGE)
491 values[j].gauge = (gauge_t)strtod (value_str, &endptr);
493 log_err ("Invalid type \"%s\" (%i).",
494 res->type, res->ds_types[j]);
497 if (value_str == endptr) {
498 log_err ("Failed to parse string as number: %s.", value_str);
502 else if ((NULL != endptr) && ('\0' != *endptr))
503 log_warn ("Ignoring trailing garbage after number: %s.",
510 submit (db, res, instances, values);
512 } /* c_psql_dispatch_row */
514 static int c_psql_check_connection (c_psql_database_t *db)
517 PQclear (PQexec (db->conn, "SELECT 42;"));
519 if (CONNECTION_OK != PQstatus (db->conn)) {
522 /* trigger c_release() */
523 if (0 == db->conn_complaint.interval)
524 db->conn_complaint.interval = 1;
526 if (CONNECTION_OK != PQstatus (db->conn)) {
527 c_complain (LOG_ERR, &db->conn_complaint,
528 "Failed to connect to database %s: %s",
529 db->database, PQerrorMessage (db->conn));
533 db->proto_version = PQprotocolVersion (db->conn);
534 if (3 > db->proto_version)
535 log_warn ("Protocol version %d does not support parameters.",
539 /* We might have connected to a different PostgreSQL version, so we
540 * need to reinitialize stuff. */
541 if (c_would_release (&db->conn_complaint))
542 c_psql_database_init (db, PQserverVersion (db->conn));
544 c_release (LOG_INFO, &db->conn_complaint,
545 "Successfully reconnected to database %s", PQdb (db->conn));
547 } /* c_psql_check_connection */
549 static PGresult *c_psql_exec_query_params (c_psql_database_t *db,
550 c_psql_query_t *query)
552 char *params[db->max_params_num];
556 assert (db->max_params_num >= query->params_num);
558 for (i = 0; i < query->params_num; ++i) {
559 switch (query->params[i]) {
560 case C_PSQL_PARAM_HOST:
561 params[i] = C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
562 ? "localhost" : db->host;
564 case C_PSQL_PARAM_DB:
565 params[i] = db->database;
567 case C_PSQL_PARAM_USER:
568 params[i] = db->user;
570 case C_PSQL_PARAM_INTERVAL:
571 ssnprintf (interval, sizeof (interval), "%i", interval_g);
572 params[i] = interval;
579 return PQexecParams (db->conn, query->stmt, query->params_num, NULL,
580 (const char *const *)((0 == query->params_num) ? NULL : params),
581 NULL, NULL, /* return text data */ 0);
582 } /* c_psql_exec_query_params */
584 static PGresult *c_psql_exec_query_noparams (c_psql_database_t *db,
585 c_psql_query_t *query)
587 return PQexec (db->conn, query->stmt);
588 } /* c_psql_exec_query_noparams */
590 static int c_psql_exec_query (c_psql_database_t *db, int idx)
592 c_psql_query_t *query;
598 if (idx >= db->queries_num)
601 if (0 != db->hidden_queries[idx])
604 query = db->queries[idx];
606 if (3 <= db->proto_version)
607 res = c_psql_exec_query_params (db, query);
608 else if (0 == query->params_num)
609 res = c_psql_exec_query_noparams (db, query);
611 log_err ("Connection to database \"%s\" does not support parameters "
612 "(protocol version %d) - cannot execute query \"%s\".",
613 db->database, db->proto_version, query->name);
617 if (PGRES_TUPLES_OK != PQresultStatus (res)) {
618 log_err ("Failed to execute SQL query: %s",
619 PQerrorMessage (db->conn));
620 log_info ("SQL query was: %s", query->stmt);
625 rows = PQntuples (res);
631 cols = PQnfields (res);
633 for (i = 0; i < rows; ++i)
634 c_psql_dispatch_row (db, query, res, i);
637 } /* c_psql_exec_query */
639 static int c_psql_read (void)
644 for (i = 0; i < databases_num; ++i) {
645 c_psql_database_t *db = databases + i;
649 assert (NULL != db->database);
651 if (0 != c_psql_check_connection (db))
654 for (j = 0; j < db->queries_num; ++j)
655 c_psql_exec_query (db, j);
665 static int c_psql_shutdown (void)
669 if ((NULL == databases) || (0 == databases_num))
672 plugin_unregister_read ("postgresql");
673 plugin_unregister_shutdown ("postgresql");
675 for (i = 0; i < databases_num; ++i) {
676 c_psql_database_t *db = databases + i;
677 c_psql_database_delete (db);
683 for (i = 0; i < queries_num; ++i) {
684 c_psql_query_t *query = queries + i;
685 c_psql_query_delete (query);
691 } /* c_psql_shutdown */
693 static int c_psql_init (void)
697 if ((NULL == databases) || (0 == databases_num))
700 for (i = 0; i < queries_num; ++i)
701 if (0 != c_psql_query_init (queries + i)) {
706 for (i = 0; i < databases_num; ++i) {
707 c_psql_database_t *db = databases + i;
710 char *buf = conninfo;
711 int buf_len = sizeof (conninfo);
717 /* this will happen during reinitialization */
718 if (NULL != db->conn) {
719 c_psql_check_connection (db);
723 status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
729 C_PSQL_PAR_APPEND (buf, buf_len, "host", db->host);
730 C_PSQL_PAR_APPEND (buf, buf_len, "port", db->port);
731 C_PSQL_PAR_APPEND (buf, buf_len, "user", db->user);
732 C_PSQL_PAR_APPEND (buf, buf_len, "password", db->password);
733 C_PSQL_PAR_APPEND (buf, buf_len, "sslmode", db->sslmode);
734 C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
735 C_PSQL_PAR_APPEND (buf, buf_len, "service", db->service);
737 db->conn = PQconnectdb (conninfo);
738 if (0 != c_psql_check_connection (db))
741 db->proto_version = PQprotocolVersion (db->conn);
743 server_host = PQhost (db->conn);
744 server_version = PQserverVersion (db->conn);
745 log_info ("Sucessfully connected to database %s (user %s) "
746 "at server %s%s%s (server version: %d.%d.%d, "
747 "protocol version: %d, pid: %d)",
748 PQdb (db->conn), PQuser (db->conn),
749 C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
750 C_PSQL_SERVER_VERSION3 (server_version),
751 db->proto_version, PQbackendPID (db->conn));
753 if (3 > db->proto_version)
754 log_warn ("Protocol version %d does not support parameters.",
757 c_psql_database_init (db, server_version);
760 plugin_register_read ("postgresql", c_psql_read);
761 plugin_register_shutdown ("postgresql", c_psql_shutdown);
765 static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
767 if ((0 != ci->children_num) || (1 != ci->values_num)
768 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
769 log_err ("%s expects a single string argument.", name);
774 *var = sstrdup (ci->values[0].value.string);
778 static int config_set_i (char *name, int *var, const oconfig_item_t *ci)
780 if ((0 != ci->children_num) || (1 != ci->values_num)
781 || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
782 log_err ("%s expects a single number argument.", name);
786 *var = (int)ci->values[0].value.number;
790 static int config_append_array_s (char *name, char ***var, int *len,
791 const oconfig_item_t *ci)
795 if ((0 != ci->children_num) || (1 > ci->values_num)) {
796 log_err ("%s expects at least one argument.", name);
800 for (i = 0; i < ci->values_num; ++i) {
801 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
802 log_err ("%s expects string arguments.", name);
807 *len += ci->values_num;
808 if (NULL == (*var = (char **)realloc (*var, *len * sizeof (**var)))) {
809 log_err ("Out of memory.");
813 for (i = *len - ci->values_num; i < *len; ++i)
814 (*var)[i] = sstrdup (ci->values[i].value.string);
816 } /* config_append_array_s */
818 static int config_set_param (c_psql_query_t *query, const oconfig_item_t *ci)
820 c_psql_param_t param;
823 if ((0 != ci->children_num) || (1 != ci->values_num)
824 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
825 log_err ("Param expects a single string argument.");
829 param_str = ci->values[0].value.string;
830 if (0 == strcasecmp (param_str, "hostname"))
831 param = C_PSQL_PARAM_HOST;
832 else if (0 == strcasecmp (param_str, "database"))
833 param = C_PSQL_PARAM_DB;
834 else if (0 == strcasecmp (param_str, "username"))
835 param = C_PSQL_PARAM_USER;
836 else if (0 == strcasecmp (param_str, "interval"))
837 param = C_PSQL_PARAM_INTERVAL;
839 log_err ("Invalid parameter \"%s\".", param_str);
844 if (NULL == (query->params = (c_psql_param_t *)realloc (query->params,
845 query->params_num * sizeof (*query->params)))) {
846 log_err ("Out of memory.");
850 query->params[query->params_num - 1] = param;
852 } /* config_set_param */
854 static int config_set_result (c_psql_query_t *query, const oconfig_item_t *ci)
856 c_psql_result_t *res;
860 if (0 != ci->values_num) {
861 log_err ("<Result> does not expect any arguments.");
865 res = c_psql_result_new (query);
867 for (i = 0; i < ci->children_num; ++i) {
868 oconfig_item_t *c = ci->children + i;
870 if (0 == strcasecmp (c->key, "Type"))
871 config_set_s ("Type", &res->type, c);
872 else if (0 == strcasecmp (c->key, "InstancePrefix"))
873 config_set_s ("InstancePrefix", &res->instance_prefix, c);
874 else if (0 == strcasecmp (c->key, "InstancesFrom"))
875 config_append_array_s ("InstancesFrom",
876 &res->instances_str, &res->instances_num, c);
877 else if (0 == strcasecmp (c->key, "ValuesFrom"))
878 config_append_array_s ("ValuesFrom",
879 &res->values_str, &res->values_num, c);
881 log_warn ("Ignoring unknown config key \"%s\".", c->key);
884 if (NULL == res->type) {
885 log_warn ("Query \"%s\": Missing Type option in <Result> block.",
890 if (NULL == res->values_str) {
891 log_warn ("Query \"%s\": Missing ValuesFrom option in <Result> block.",
897 c_psql_result_delete (res);
898 --query->results_num;
902 /* preallocate memory to cache the column numbers and data types */
903 res->values = (int *)smalloc (res->values_num * sizeof (*res->values));
904 for (i = 0; i < res->values_num; ++i)
907 res->instances = (int *)smalloc (res->instances_num
908 * sizeof (*res->instances));
909 for (i = 0; i < res->instances_num; ++i)
910 res->instances[i] = -1;
912 res->ds_types = (int *)smalloc (res->values_num
913 * sizeof (*res->ds_types));
914 for (i = 0; i < res->values_num; ++i)
915 res->ds_types[i] = -1;
917 } /* config_set_result */
919 static int config_set_column (c_psql_query_t *query, int col_num,
920 const oconfig_item_t *ci)
922 c_psql_result_t *res;
926 if ((0 != ci->children_num)
927 || (1 > ci->values_num) || (2 < ci->values_num)) {
928 log_err ("Column expects either one or two arguments.");
932 for (i = 0; i < ci->values_num; ++i) {
933 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
934 log_err ("Column expects either one or two string arguments.");
939 res = c_psql_result_new (query);
941 res->type = sstrdup (ci->values[0].value.string);
943 if (2 == ci->values_num)
944 res->instance_prefix = sstrdup (ci->values[1].value.string);
946 res->values = (int *)smalloc (sizeof (*res->values));
947 res->values[0] = col_num;
948 res->ds_types = (int *)smalloc (sizeof (*res->ds_types));
951 } /* config_set_column */
953 static int set_query (c_psql_database_t *db, const char *name)
955 c_psql_query_t *query;
957 query = c_psql_query_get (name, -1);
959 log_err ("Query \"%s\" not found - please check your configuration.",
965 if (NULL == (db->queries = (c_psql_query_t **)realloc (db->queries,
966 db->queries_num * sizeof (*db->queries)))) {
967 log_err ("Out of memory.");
971 if (query->params_num > db->max_params_num)
972 db->max_params_num = query->params_num;
974 db->queries[db->queries_num - 1] = query;
978 static int config_set_query (c_psql_database_t *db, const oconfig_item_t *ci)
980 if ((0 != ci->children_num) || (1 != ci->values_num)
981 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
982 log_err ("Query expects a single string argument.");
985 return set_query (db, ci->values[0].value.string);
986 } /* config_set_query */
988 static int c_psql_config_query (oconfig_item_t *ci)
990 c_psql_query_t *query;
992 int status = 0, col_num = 0, i;
994 if ((1 != ci->values_num)
995 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
996 log_err ("<Query> expects a single string argument.");
1000 query = c_psql_query_new (ci->values[0].value.string);
1002 for (i = 0; i < ci->children_num; ++i) {
1003 oconfig_item_t *c = ci->children + i;
1005 if (0 == strcasecmp (c->key, "Statement"))
1006 config_set_s ("Statement", &query->stmt, c);
1007 /* backwards compat for versions < 4.6 */
1008 else if (0 == strcasecmp (c->key, "Query")) {
1009 log_warn ("<Query>: 'Query' is deprecated - use 'Statement' instead.");
1010 config_set_s ("Query", &query->stmt, c);
1012 else if (0 == strcasecmp (c->key, "Param"))
1013 config_set_param (query, c);
1014 else if (0 == strcasecmp (c->key, "Result"))
1015 config_set_result (query, c);
1016 /* backwards compat for versions < 4.6 */
1017 else if (0 == strcasecmp (c->key, "Column")) {
1018 log_warn ("<Query>: 'Column' is deprecated - "
1019 "use a <Result> block instead.");
1020 config_set_column (query, col_num, c);
1023 else if (0 == strcasecmp (c->key, "MinPGVersion"))
1024 config_set_i ("MinPGVersion", &query->min_pg_version, c);
1025 else if (0 == strcasecmp (c->key, "MaxPGVersion"))
1026 config_set_i ("MaxPGVersion", &query->max_pg_version, c);
1028 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1031 for (i = 0; i < queries_num - 1; ++i) {
1032 c_psql_query_t *q = queries + i;
1034 if ((0 == strcasecmp (q->name, query->name))
1035 && (q->min_pg_version <= query->max_pg_version)
1036 && (query->min_pg_version <= q->max_pg_version)) {
1037 log_err ("Ignoring redefinition (with overlapping version ranges) "
1038 "of query \"%s\".", query->name);
1044 if (query->min_pg_version > query->max_pg_version) {
1045 log_err ("Query \"%s\": MinPGVersion > MaxPGVersion.",
1050 if (NULL == query->stmt) {
1051 log_err ("Query \"%s\" does not include an SQL query statement - "
1052 "please check your configuration.", query->name);
1057 c_psql_query_delete (query);
1062 } /* c_psql_config_query */
1064 static int c_psql_config_database (oconfig_item_t *ci)
1066 c_psql_database_t *db;
1070 if ((1 != ci->values_num)
1071 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1072 log_err ("<Database> expects a single string argument.");
1076 db = c_psql_database_new (ci->values[0].value.string);
1078 for (i = 0; i < ci->children_num; ++i) {
1079 oconfig_item_t *c = ci->children + i;
1081 if (0 == strcasecmp (c->key, "Host"))
1082 config_set_s ("Host", &db->host, c);
1083 else if (0 == strcasecmp (c->key, "Port"))
1084 config_set_s ("Port", &db->port, c);
1085 else if (0 == strcasecmp (c->key, "User"))
1086 config_set_s ("User", &db->user, c);
1087 else if (0 == strcasecmp (c->key, "Password"))
1088 config_set_s ("Password", &db->password, c);
1089 else if (0 == strcasecmp (c->key, "SSLMode"))
1090 config_set_s ("SSLMode", &db->sslmode, c);
1091 else if (0 == strcasecmp (c->key, "KRBSrvName"))
1092 config_set_s ("KRBSrvName", &db->krbsrvname, c);
1093 else if (0 == strcasecmp (c->key, "Service"))
1094 config_set_s ("Service", &db->service, c);
1095 else if (0 == strcasecmp (c->key, "Query"))
1096 config_set_query (db, c);
1098 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1101 if (NULL == db->queries) {
1102 for (i = 0; i < def_queries_num; ++i)
1103 set_query (db, def_queries[i]);
1106 db->hidden_queries = (int *)calloc (db->queries_num,
1107 sizeof (*db->hidden_queries));
1108 if (NULL == db->hidden_queries) {
1109 log_err ("Out of memory.");
1113 } /* c_psql_config_database */
1115 static int c_psql_config (oconfig_item_t *ci)
1117 static int have_def_config = 0;
1121 if (0 == have_def_config) {
1124 have_def_config = 1;
1126 c = oconfig_parse_file (C_PSQL_DEFAULT_CONF);
1128 log_err ("Failed to read default config ("C_PSQL_DEFAULT_CONF").");
1132 if (NULL == queries)
1133 log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
1134 "any queries - please check your installation.");
1137 for (i = 0; i < ci->children_num; ++i) {
1138 oconfig_item_t *c = ci->children + i;
1140 if (0 == strcasecmp (c->key, "Query"))
1141 c_psql_config_query (c);
1142 else if (0 == strcasecmp (c->key, "Database"))
1143 c_psql_config_database (c);
1145 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1148 } /* c_psql_config */
1150 void module_register (void)
1152 plugin_register_complex_config ("postgresql", c_psql_config);
1153 plugin_register_init ("postgresql", c_psql_init);
1154 } /* module_register */
1156 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */