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,
87 char *instance_prefix;
91 char **values_str; /* may be NULL, even if values_num != 0 in
92 case the "Column" option has been used */
102 c_psql_param_t *params;
105 c_psql_result_t *results;
114 c_complain_t conn_complaint;
120 /* user configuration */
121 c_psql_query_t **queries;
138 static char *def_queries[] = {
147 static int def_queries_num = STATIC_ARRAY_SIZE (def_queries);
149 static c_psql_query_t *queries = NULL;
150 static int queries_num = 0;
152 static c_psql_database_t *databases = NULL;
153 static int databases_num = 0;
155 static c_psql_result_t *c_psql_result_new (c_psql_query_t *query)
157 c_psql_result_t *res;
159 ++query->results_num;
160 if (NULL == (query->results = (c_psql_result_t *)realloc (query->results,
161 query->results_num * sizeof (*query->results)))) {
162 log_err ("Out of memory.");
165 res = query->results + query->results_num - 1;
169 res->instance_prefix = NULL;
170 res->instances_str = NULL;
171 res->instances = NULL;
172 res->instances_num = 0;
174 res->values_str = NULL;
176 res->ds_types = NULL;
179 } /* c_psql_result_new */
181 static void c_psql_result_delete (c_psql_result_t *res)
187 sfree (res->instance_prefix);
189 for (i = 0; i < res->instances_num; ++i)
190 sfree (res->instances_str[i]);
191 sfree (res->instances_str);
192 sfree (res->instances);
193 res->instances_num = 0;
195 for (i = 0; (NULL != res->values_str) && (i < res->values_num); ++i)
196 sfree (res->values_str[i]);
197 sfree (res->values_str);
199 sfree (res->ds_types);
201 } /* c_psql_result_delete */
203 static c_psql_query_t *c_psql_query_new (const char *name)
205 c_psql_query_t *query;
208 if (NULL == (queries = (c_psql_query_t *)realloc (queries,
209 queries_num * sizeof (*queries)))) {
210 log_err ("Out of memory.");
213 query = queries + queries_num - 1;
215 query->name = sstrdup (name);
218 query->params = NULL;
219 query->params_num = 0;
221 query->results = NULL;
222 query->results_num = 0;
224 query->min_pg_version = 0;
225 query->max_pg_version = INT_MAX;
227 } /* c_psql_query_new */
229 static int c_psql_query_init (c_psql_query_t *query)
233 /* Get the data set definitions for each query definition. */
234 for (i = 0; i < query->results_num; ++i) {
235 c_psql_result_t *res = query->results + i;
236 const data_set_t *ds;
240 ds = plugin_get_ds (res->type);
242 log_err ("Result: Unknown type \"%s\".", res->type);
246 if (res->values_num != ds->ds_num) {
247 log_err ("Result: Invalid type \"%s\" - "
248 "expected %i data source%s, got %i.",
249 res->type, res->values_num,
250 (1 == res->values_num) ? "" : "s",
255 for (j = 0; j < res->values_num; ++j)
256 res->ds_types[j] = ds->ds[j].type;
259 } /* c_psql_query_init */
261 static void c_psql_query_delete (c_psql_query_t *query)
268 sfree (query->params);
269 query->params_num = 0;
271 for (i = 0; i < query->results_num; ++i)
272 c_psql_result_delete (query->results + i);
273 sfree (query->results);
274 query->results_num = 0;
276 } /* c_psql_query_delete */
278 static c_psql_query_t *c_psql_query_get (const char *name, int server_version)
282 for (i = 0; i < queries_num; ++i)
283 if (0 == strcasecmp (name, queries[i].name)
284 && ((-1 == server_version)
285 || ((queries[i].min_pg_version <= server_version)
286 && (server_version <= queries[i].max_pg_version))))
289 } /* c_psql_query_get */
291 static c_psql_database_t *c_psql_database_new (const char *name)
293 c_psql_database_t *db;
296 if (NULL == (databases = (c_psql_database_t *)realloc (databases,
297 databases_num * sizeof (*databases)))) {
298 log_err ("Out of memory.");
302 db = databases + (databases_num - 1);
306 C_COMPLAIN_INIT (&db->conn_complaint);
308 db->proto_version = 0;
310 db->max_params_num = 0;
313 db->hidden_queries = NULL;
316 db->database = sstrdup (name);
324 db->krbsrvname = NULL;
328 } /* c_psql_database_new */
330 static void c_psql_database_init (c_psql_database_t *db, int server_version)
334 /* Get the right version of each query definition. */
335 for (i = 0; i < db->queries_num; ++i) {
338 tmp = c_psql_query_get (db->queries[i]->name, server_version);
340 if (tmp == db->queries[i])
344 log_err ("Query \"%s\" not found for server version %i - "
345 "please check your configuration.",
346 db->queries[i]->name, server_version);
347 /* By hiding the query (rather than removing it from the list) we
348 * don't lose it in case a reconnect to an available version
349 * happens at a later time. */
350 db->hidden_queries[i] = 1;
354 db->hidden_queries[i] = 0;
355 db->queries[i] = tmp;
357 } /* c_psql_database_init */
359 static void c_psql_database_delete (c_psql_database_t *db)
365 sfree (db->hidden_queries);
368 sfree (db->database);
372 sfree (db->password);
376 sfree (db->krbsrvname);
380 } /* c_psql_database_delete */
382 static void submit (const c_psql_database_t *db, const c_psql_result_t *res,
383 char **instances, value_t *values)
385 value_list_t vl = VALUE_LIST_INIT;
387 int instances_num = res->instances_num;
389 if (NULL != res->instance_prefix)
393 vl.values_len = res->values_num;
394 vl.time = time (NULL);
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];
555 assert (db->max_params_num >= query->params_num);
557 for (i = 0; i < query->params_num; ++i) {
558 switch (query->params[i]) {
559 case C_PSQL_PARAM_HOST:
560 params[i] = C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
561 ? "localhost" : db->host;
563 case C_PSQL_PARAM_DB:
564 params[i] = db->database;
566 case C_PSQL_PARAM_USER:
567 params[i] = db->user;
574 return PQexecParams (db->conn, query->stmt, query->params_num, NULL,
575 (const char *const *)((0 == query->params_num) ? NULL : params),
576 NULL, NULL, /* return text data */ 0);
577 } /* c_psql_exec_query_params */
579 static PGresult *c_psql_exec_query_noparams (c_psql_database_t *db,
580 c_psql_query_t *query)
582 return PQexec (db->conn, query->stmt);
583 } /* c_psql_exec_query_noparams */
585 static int c_psql_exec_query (c_psql_database_t *db, int idx)
587 c_psql_query_t *query;
593 if (idx >= db->queries_num)
596 if (0 != db->hidden_queries[idx])
599 query = db->queries[idx];
601 if (3 <= db->proto_version)
602 res = c_psql_exec_query_params (db, query);
603 else if (0 == query->params_num)
604 res = c_psql_exec_query_noparams (db, query);
606 log_err ("Connection to database \"%s\" does not support parameters "
607 "(protocol version %d) - cannot execute query \"%s\".",
608 db->database, db->proto_version, query->name);
612 if (PGRES_TUPLES_OK != PQresultStatus (res)) {
613 log_err ("Failed to execute SQL query: %s",
614 PQerrorMessage (db->conn));
615 log_info ("SQL query was: %s", query->stmt);
620 rows = PQntuples (res);
626 cols = PQnfields (res);
628 for (i = 0; i < rows; ++i)
629 c_psql_dispatch_row (db, query, res, i);
632 } /* c_psql_exec_query */
634 static int c_psql_read (void)
639 for (i = 0; i < databases_num; ++i) {
640 c_psql_database_t *db = databases + i;
644 assert (NULL != db->database);
646 if (0 != c_psql_check_connection (db))
649 for (j = 0; j < db->queries_num; ++j)
650 c_psql_exec_query (db, j);
660 static int c_psql_shutdown (void)
664 if ((NULL == databases) || (0 == databases_num))
667 plugin_unregister_read ("postgresql");
668 plugin_unregister_shutdown ("postgresql");
670 for (i = 0; i < databases_num; ++i) {
671 c_psql_database_t *db = databases + i;
672 c_psql_database_delete (db);
678 for (i = 0; i < queries_num; ++i) {
679 c_psql_query_t *query = queries + i;
680 c_psql_query_delete (query);
686 } /* c_psql_shutdown */
688 static int c_psql_init (void)
692 if ((NULL == databases) || (0 == databases_num))
695 for (i = 0; i < queries_num; ++i)
696 if (0 != c_psql_query_init (queries + i)) {
701 for (i = 0; i < databases_num; ++i) {
702 c_psql_database_t *db = databases + i;
705 char *buf = conninfo;
706 int buf_len = sizeof (conninfo);
712 /* this will happen during reinitialization */
713 if (NULL != db->conn) {
714 c_psql_check_connection (db);
718 status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
724 C_PSQL_PAR_APPEND (buf, buf_len, "host", db->host);
725 C_PSQL_PAR_APPEND (buf, buf_len, "port", db->port);
726 C_PSQL_PAR_APPEND (buf, buf_len, "user", db->user);
727 C_PSQL_PAR_APPEND (buf, buf_len, "password", db->password);
728 C_PSQL_PAR_APPEND (buf, buf_len, "sslmode", db->sslmode);
729 C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
730 C_PSQL_PAR_APPEND (buf, buf_len, "service", db->service);
732 db->conn = PQconnectdb (conninfo);
733 if (0 != c_psql_check_connection (db))
736 db->proto_version = PQprotocolVersion (db->conn);
738 server_host = PQhost (db->conn);
739 server_version = PQserverVersion (db->conn);
740 log_info ("Sucessfully connected to database %s (user %s) "
741 "at server %s%s%s (server version: %d.%d.%d, "
742 "protocol version: %d, pid: %d)",
743 PQdb (db->conn), PQuser (db->conn),
744 C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
745 C_PSQL_SERVER_VERSION3 (server_version),
746 db->proto_version, PQbackendPID (db->conn));
748 if (3 > db->proto_version)
749 log_warn ("Protocol version %d does not support parameters.",
752 c_psql_database_init (db, server_version);
755 plugin_register_read ("postgresql", c_psql_read);
756 plugin_register_shutdown ("postgresql", c_psql_shutdown);
760 static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
762 if ((0 != ci->children_num) || (1 != ci->values_num)
763 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
764 log_err ("%s expects a single string argument.", name);
769 *var = sstrdup (ci->values[0].value.string);
773 static int config_set_i (char *name, int *var, const oconfig_item_t *ci)
775 if ((0 != ci->children_num) || (1 != ci->values_num)
776 || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
777 log_err ("%s expects a single number argument.", name);
781 *var = (int)ci->values[0].value.number;
785 static int config_append_array_s (char *name, char ***var, int *len,
786 const oconfig_item_t *ci)
790 if ((0 != ci->children_num) || (1 > ci->values_num)) {
791 log_err ("%s expects at least one argument.", name);
795 for (i = 0; i < ci->values_num; ++i) {
796 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
797 log_err ("%s expects string arguments.", name);
802 *len += ci->values_num;
803 if (NULL == (*var = (char **)realloc (*var, *len * sizeof (**var)))) {
804 log_err ("Out of memory.");
808 for (i = *len - ci->values_num; i < *len; ++i)
809 (*var)[i] = sstrdup (ci->values[i].value.string);
811 } /* config_append_array_s */
813 static int config_set_param (c_psql_query_t *query, const oconfig_item_t *ci)
815 c_psql_param_t param;
818 if ((0 != ci->children_num) || (1 != ci->values_num)
819 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
820 log_err ("Param expects a single string argument.");
824 param_str = ci->values[0].value.string;
825 if (0 == strcasecmp (param_str, "hostname"))
826 param = C_PSQL_PARAM_HOST;
827 else if (0 == strcasecmp (param_str, "database"))
828 param = C_PSQL_PARAM_DB;
829 else if (0 == strcasecmp (param_str, "username"))
830 param = C_PSQL_PARAM_USER;
832 log_err ("Invalid parameter \"%s\".", param_str);
837 if (NULL == (query->params = (c_psql_param_t *)realloc (query->params,
838 query->params_num * sizeof (*query->params)))) {
839 log_err ("Out of memory.");
843 query->params[query->params_num - 1] = param;
845 } /* config_set_param */
847 static int config_set_result (c_psql_query_t *query, const oconfig_item_t *ci)
849 c_psql_result_t *res;
853 if (0 != ci->values_num) {
854 log_err ("<Result> does not expect any arguments.");
858 res = c_psql_result_new (query);
860 for (i = 0; i < ci->children_num; ++i) {
861 oconfig_item_t *c = ci->children + i;
863 if (0 == strcasecmp (c->key, "Type"))
864 config_set_s ("Type", &res->type, c);
865 else if (0 == strcasecmp (c->key, "InstancePrefix"))
866 config_set_s ("InstancePrefix", &res->instance_prefix, c);
867 else if (0 == strcasecmp (c->key, "InstancesFrom"))
868 config_append_array_s ("InstancesFrom",
869 &res->instances_str, &res->instances_num, c);
870 else if (0 == strcasecmp (c->key, "ValuesFrom"))
871 config_append_array_s ("ValuesFrom",
872 &res->values_str, &res->values_num, c);
874 log_warn ("Ignoring unknown config key \"%s\".", c->key);
877 if (NULL == res->type) {
878 log_warn ("Query \"%s\": Missing Type option in <Result> block.",
883 if (NULL == res->values_str) {
884 log_warn ("Query \"%s\": Missing ValuesFrom option in <Result> block.",
890 c_psql_result_delete (res);
891 --query->results_num;
895 /* preallocate memory to cache the column numbers and data types */
896 res->values = (int *)smalloc (res->values_num * sizeof (*res->values));
897 for (i = 0; i < res->values_num; ++i)
900 res->instances = (int *)smalloc (res->instances_num
901 * sizeof (*res->instances));
902 for (i = 0; i < res->instances_num; ++i)
903 res->instances[i] = -1;
905 res->ds_types = (int *)smalloc (res->values_num
906 * sizeof (*res->ds_types));
907 for (i = 0; i < res->values_num; ++i)
908 res->ds_types[i] = -1;
910 } /* config_set_result */
912 static int config_set_column (c_psql_query_t *query, int col_num,
913 const oconfig_item_t *ci)
915 c_psql_result_t *res;
919 if ((0 != ci->children_num)
920 || (1 > ci->values_num) || (2 < ci->values_num)) {
921 log_err ("Column expects either one or two arguments.");
925 for (i = 0; i < ci->values_num; ++i) {
926 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
927 log_err ("Column expects either one or two string arguments.");
932 res = c_psql_result_new (query);
934 res->type = sstrdup (ci->values[0].value.string);
936 if (2 == ci->values_num)
937 res->instance_prefix = sstrdup (ci->values[1].value.string);
939 res->values = (int *)smalloc (sizeof (*res->values));
940 res->values[0] = col_num;
941 res->ds_types = (int *)smalloc (sizeof (*res->ds_types));
944 } /* config_set_column */
946 static int set_query (c_psql_database_t *db, const char *name)
948 c_psql_query_t *query;
950 query = c_psql_query_get (name, -1);
952 log_err ("Query \"%s\" not found - please check your configuration.",
958 if (NULL == (db->queries = (c_psql_query_t **)realloc (db->queries,
959 db->queries_num * sizeof (*db->queries)))) {
960 log_err ("Out of memory.");
964 if (query->params_num > db->max_params_num)
965 db->max_params_num = query->params_num;
967 db->queries[db->queries_num - 1] = query;
971 static int config_set_query (c_psql_database_t *db, const oconfig_item_t *ci)
973 if ((0 != ci->children_num) || (1 != ci->values_num)
974 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
975 log_err ("Query expects a single string argument.");
978 return set_query (db, ci->values[0].value.string);
979 } /* config_set_query */
981 static int c_psql_config_query (oconfig_item_t *ci)
983 c_psql_query_t *query;
985 int status = 0, col_num = 0, i;
987 if ((1 != ci->values_num)
988 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
989 log_err ("<Query> expects a single string argument.");
993 query = c_psql_query_new (ci->values[0].value.string);
995 for (i = 0; i < ci->children_num; ++i) {
996 oconfig_item_t *c = ci->children + i;
998 if (0 == strcasecmp (c->key, "Statement"))
999 config_set_s ("Statement", &query->stmt, c);
1000 /* backwards compat for versions < 4.6 */
1001 else if (0 == strcasecmp (c->key, "Query")) {
1002 log_warn ("<Query>: 'Query' is deprecated - use 'Statement' instead.");
1003 config_set_s ("Query", &query->stmt, c);
1005 else if (0 == strcasecmp (c->key, "Param"))
1006 config_set_param (query, c);
1007 else if (0 == strcasecmp (c->key, "Result"))
1008 config_set_result (query, c);
1009 /* backwards compat for versions < 4.6 */
1010 else if (0 == strcasecmp (c->key, "Column")) {
1011 log_warn ("<Query>: 'Column' is deprecated - "
1012 "use a <Result> block instead.");
1013 config_set_column (query, col_num, c);
1016 else if (0 == strcasecmp (c->key, "MinPGVersion"))
1017 config_set_i ("MinPGVersion", &query->min_pg_version, c);
1018 else if (0 == strcasecmp (c->key, "MaxPGVersion"))
1019 config_set_i ("MaxPGVersion", &query->max_pg_version, c);
1021 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1024 for (i = 0; i < queries_num - 1; ++i) {
1025 c_psql_query_t *q = queries + i;
1027 if ((0 == strcasecmp (q->name, query->name))
1028 && (q->min_pg_version <= query->max_pg_version)
1029 && (query->min_pg_version <= q->max_pg_version)) {
1030 log_err ("Ignoring redefinition (with overlapping version ranges) "
1031 "of query \"%s\".", query->name);
1037 if (query->min_pg_version > query->max_pg_version) {
1038 log_err ("Query \"%s\": MinPGVersion > MaxPGVersion.",
1043 if (NULL == query->stmt) {
1044 log_err ("Query \"%s\" does not include an SQL query statement - "
1045 "please check your configuration.", query->name);
1050 c_psql_query_delete (query);
1055 } /* c_psql_config_query */
1057 static int c_psql_config_database (oconfig_item_t *ci)
1059 c_psql_database_t *db;
1063 if ((1 != ci->values_num)
1064 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1065 log_err ("<Database> expects a single string argument.");
1069 db = c_psql_database_new (ci->values[0].value.string);
1071 for (i = 0; i < ci->children_num; ++i) {
1072 oconfig_item_t *c = ci->children + i;
1074 if (0 == strcasecmp (c->key, "Host"))
1075 config_set_s ("Host", &db->host, c);
1076 else if (0 == strcasecmp (c->key, "Port"))
1077 config_set_s ("Port", &db->port, c);
1078 else if (0 == strcasecmp (c->key, "User"))
1079 config_set_s ("User", &db->user, c);
1080 else if (0 == strcasecmp (c->key, "Password"))
1081 config_set_s ("Password", &db->password, c);
1082 else if (0 == strcasecmp (c->key, "SSLMode"))
1083 config_set_s ("SSLMode", &db->sslmode, c);
1084 else if (0 == strcasecmp (c->key, "KRBSrvName"))
1085 config_set_s ("KRBSrvName", &db->krbsrvname, c);
1086 else if (0 == strcasecmp (c->key, "Service"))
1087 config_set_s ("Service", &db->service, c);
1088 else if (0 == strcasecmp (c->key, "Query"))
1089 config_set_query (db, c);
1091 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1094 if (NULL == db->queries) {
1095 for (i = 0; i < def_queries_num; ++i)
1096 set_query (db, def_queries[i]);
1099 db->hidden_queries = (int *)calloc (db->queries_num,
1100 sizeof (*db->hidden_queries));
1101 if (NULL == db->hidden_queries) {
1102 log_err ("Out of memory.");
1106 } /* c_psql_config_database */
1108 static int c_psql_config (oconfig_item_t *ci)
1110 static int have_def_config = 0;
1114 if (0 == have_def_config) {
1117 have_def_config = 1;
1119 c = oconfig_parse_file (C_PSQL_DEFAULT_CONF);
1121 log_err ("Failed to read default config ("C_PSQL_DEFAULT_CONF").");
1125 if (NULL == queries)
1126 log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
1127 "any queries - please check your installation.");
1130 for (i = 0; i < ci->children_num; ++i) {
1131 oconfig_item_t *c = ci->children + i;
1133 if (0 == strcasecmp (c->key, "Query"))
1134 c_psql_config_query (c);
1135 else if (0 == strcasecmp (c->key, "Database"))
1136 c_psql_config_database (c);
1138 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1141 } /* c_psql_config */
1143 void module_register (void)
1145 plugin_register_complex_config ("postgresql", c_psql_config);
1146 plugin_register_init ("postgresql", c_psql_init);
1147 } /* module_register */
1149 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */