/**
* collectd - src/postgresql.c
- * Copyright (C) 2008 Sebastian Harl
+ * Copyright (C) 2008, 2009 Sebastian Harl
+ * Copyright (C) 2009 Florian Forster
+ * All rights reserved.
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; only version 2 of the License is applicable.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
*
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
*
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
*
- * Author:
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors:
* Sebastian Harl <sh at tokkee.org>
+ * Florian Forster <octo at verplant.org>
**/
/*
#include "configfile.h"
#include "plugin.h"
+#include "utils_db_query.h"
#include "utils_complain.h"
#include <pg_config_manual.h>
#define log_warn(...) WARNING ("postgresql: " __VA_ARGS__)
#define log_info(...) INFO ("postgresql: " __VA_ARGS__)
+#ifndef C_PSQL_DEFAULT_CONF
+# define C_PSQL_DEFAULT_CONF PKGDATADIR "/postgresql_default.conf"
+#endif
+
/* Appends the (parameter, value) pair to the string
* pointed to by 'buf' suitable to be used as argument
* for PQconnectdb(). If value equals NULL, the pair
C_PSQL_IS_UNIX_DOMAIN_SOCKET (host) ? "/.s.PGSQL." : ":", \
port
+typedef enum {
+ C_PSQL_PARAM_HOST = 1,
+ C_PSQL_PARAM_DB,
+ C_PSQL_PARAM_USER,
+ C_PSQL_PARAM_INTERVAL,
+} c_psql_param_t;
+
+/* Parameter configuration. Stored as `user data' in the query objects. */
+typedef struct {
+ c_psql_param_t *params;
+ int params_num;
+} c_psql_user_data_t;
+
typedef struct {
PGconn *conn;
c_complain_t conn_complaint;
+ int proto_version;
+ int server_version;
+
+ int max_params_num;
+
/* user configuration */
+ udb_query_preparation_area_t **q_prep_areas;
+ udb_query_t **queries;
+ size_t queries_num;
+
+ cdtime_t interval;
+
char *host;
char *port;
char *database;
char *service;
} c_psql_database_t;
-static c_psql_database_t *databases = NULL;
-static int databases_num = 0;
-
-static void submit (const c_psql_database_t *db,
- const char *type, const char *type_instance,
- value_t *values, size_t values_len)
+static char *def_queries[] = {
+ "backends",
+ "transactions",
+ "queries",
+ "query_plans",
+ "table_states",
+ "disk_io",
+ "disk_usage"
+};
+static int def_queries_num = STATIC_ARRAY_SIZE (def_queries);
+
+static udb_query_t **queries = NULL;
+static size_t queries_num = 0;
+
+static c_psql_database_t *c_psql_database_new (const char *name)
{
- value_list_t vl = VALUE_LIST_INIT;
+ c_psql_database_t *db;
- vl.values = values;
- vl.values_len = values_len;
- vl.time = time (NULL);
+ db = (c_psql_database_t *)malloc (sizeof (*db));
+ if (NULL == db) {
+ log_err ("Out of memory.");
+ return NULL;
+ }
- if (C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
- || (0 == strcmp (db->host, "localhost")))
- sstrncpy (vl.host, hostname_g, sizeof (vl.host));
- else
- sstrncpy (vl.host, db->host, sizeof (vl.host));
+ db->conn = NULL;
- sstrncpy (vl.plugin, "postgresql", sizeof (vl.plugin));
- sstrncpy (vl.plugin_instance, db->database, sizeof (vl.plugin_instance));
+ C_COMPLAIN_INIT (&db->conn_complaint);
- sstrncpy (vl.type, type, sizeof (vl.type));
+ db->proto_version = 0;
+ db->server_version = 0;
- if (NULL != type_instance)
- sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+ db->max_params_num = 0;
- plugin_dispatch_values (&vl);
- return;
-} /* submit */
+ db->q_prep_areas = NULL;
+ db->queries = NULL;
+ db->queries_num = 0;
+
+ db->interval = 0;
+
+ db->database = sstrdup (name);
+ db->host = NULL;
+ db->port = NULL;
+ db->user = NULL;
+ db->password = NULL;
+
+ db->sslmode = NULL;
+
+ db->krbsrvname = NULL;
-static void submit_counter (const c_psql_database_t *db,
- const char *type, const char *type_instance,
- const char *value)
+ db->service = NULL;
+ return db;
+} /* c_psql_database_new */
+
+static void c_psql_database_delete (void *data)
{
- value_t values[1];
+ size_t i;
+
+ c_psql_database_t *db = data;
- if ((NULL == value) || ('\0' == *value))
- return;
+ PQfinish (db->conn);
+ db->conn = NULL;
- values[0].counter = atoll (value);
- submit (db, type, type_instance, values, 1);
+ if (db->q_prep_areas)
+ for (i = 0; i < db->queries_num; ++i)
+ udb_query_delete_preparation_area (db->q_prep_areas[i]);
+ free (db->q_prep_areas);
+
+ sfree (db->queries);
+ db->queries_num = 0;
+
+ sfree (db->database);
+ sfree (db->host);
+ sfree (db->port);
+ sfree (db->user);
+ sfree (db->password);
+
+ sfree (db->sslmode);
+
+ sfree (db->krbsrvname);
+
+ sfree (db->service);
return;
-} /* submit_counter */
+} /* c_psql_database_delete */
-static void submit_gauge (const c_psql_database_t *db,
- const char *type, const char *type_instance,
- const char *value)
+static int c_psql_connect (c_psql_database_t *db)
{
- value_t values[1];
+ char conninfo[4096];
+ char *buf = conninfo;
+ int buf_len = sizeof (conninfo);
+ int status;
- if ((NULL == value) || ('\0' == *value))
- return;
+ if (! db)
+ return -1;
- values[0].gauge = atof (value);
- submit (db, type, type_instance, values, 1);
- return;
-} /* submit_gauge */
+ status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
+ if (0 < status) {
+ buf += status;
+ buf_len -= status;
+ }
+
+ C_PSQL_PAR_APPEND (buf, buf_len, "host", db->host);
+ C_PSQL_PAR_APPEND (buf, buf_len, "port", db->port);
+ C_PSQL_PAR_APPEND (buf, buf_len, "user", db->user);
+ C_PSQL_PAR_APPEND (buf, buf_len, "password", db->password);
+ C_PSQL_PAR_APPEND (buf, buf_len, "sslmode", db->sslmode);
+ C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
+ C_PSQL_PAR_APPEND (buf, buf_len, "service", db->service);
+
+ db->conn = PQconnectdb (conninfo);
+ db->proto_version = PQprotocolVersion (db->conn);
+ return 0;
+} /* c_psql_connect */
static int c_psql_check_connection (c_psql_database_t *db)
{
+ _Bool init = 0;
+
+ if (! db->conn) {
+ init = 1;
+
+ /* trigger c_release() */
+ if (0 == db->conn_complaint.interval)
+ db->conn_complaint.interval = 1;
+
+ c_psql_connect (db);
+ }
+
/* "ping" */
PQclear (PQexec (db->conn, "SELECT 42;"));
db->database, PQerrorMessage (db->conn));
return -1;
}
- }
- c_release (LOG_INFO, &db->conn_complaint,
- "Successfully reconnected to database %s", PQdb (db->conn));
- return 0;
-} /* c_psql_check_connection */
+ db->proto_version = PQprotocolVersion (db->conn);
+ }
-static int c_psql_stat_database (c_psql_database_t *db)
-{
- const char *const query =
- "SELECT numbackends, xact_commit, xact_rollback "
- "FROM pg_stat_database "
- "WHERE datname = $1;";
+ db->server_version = PQserverVersion (db->conn);
- PGresult *res;
-
- int n;
+ if (c_would_release (&db->conn_complaint)) {
+ char *server_host;
+ int server_version;
- res = PQexecParams (db->conn, query, /* number of parameters */ 1,
- NULL, (const char *const *)&db->database, NULL, NULL,
- /* return text data */ 0);
+ server_host = PQhost (db->conn);
+ server_version = PQserverVersion (db->conn);
- if (PGRES_TUPLES_OK != PQresultStatus (res)) {
- log_err ("Failed to execute SQL query: %s",
- PQerrorMessage (db->conn));
- log_info ("SQL query was: %s", query);
- PQclear (res);
- return -1;
- }
+ c_do_release (LOG_INFO, &db->conn_complaint,
+ "Successfully %sconnected to database %s (user %s) "
+ "at server %s%s%s (server version: %d.%d.%d, "
+ "protocol version: %d, pid: %d)", init ? "" : "re",
+ PQdb (db->conn), PQuser (db->conn),
+ C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
+ C_PSQL_SERVER_VERSION3 (server_version),
+ db->proto_version, PQbackendPID (db->conn));
- n = PQntuples (res);
- if (1 < n) {
- log_warn ("pg_stat_database has more than one entry "
- "for database %s - ignoring additional results.",
- db->database);
- }
- else if (1 > n) {
- log_err ("pg_stat_database has no entry for database %s",
- db->database);
- PQclear (res);
- return -1;
+ if (3 > db->proto_version)
+ log_warn ("Protocol version %d does not support parameters.",
+ db->proto_version);
}
-
- submit_gauge (db, "pg_numbackends", NULL, PQgetvalue (res, 0, 0));
-
- submit_counter (db, "pg_xact", "commit", PQgetvalue (res, 0, 1));
- submit_counter (db, "pg_xact", "rollback", PQgetvalue (res, 0, 2));
-
- PQclear (res);
return 0;
-} /* c_psql_stat_database */
+} /* c_psql_check_connection */
-static int c_psql_stat_user_tables (c_psql_database_t *db)
+static PGresult *c_psql_exec_query_noparams (c_psql_database_t *db,
+ udb_query_t *q)
{
- const char *const query =
- "SELECT sum(seq_scan), sum(seq_tup_read), "
- "sum(idx_scan), sum(idx_tup_fetch), "
- "sum(n_tup_ins), sum(n_tup_upd), sum(n_tup_del), "
- "sum(n_tup_hot_upd), sum(n_live_tup), sum(n_dead_tup) "
- "FROM pg_stat_user_tables;";
-
- PGresult *res;
-
- int n;
+ return PQexec (db->conn, udb_query_get_statement (q));
+} /* c_psql_exec_query_noparams */
- res = PQexec (db->conn, query);
-
- if (PGRES_TUPLES_OK != PQresultStatus (res)) {
- log_err ("Failed to execute SQL query: %s",
- PQerrorMessage (db->conn));
- log_info ("SQL query was: %s", query);
- PQclear (res);
- return -1;
+static PGresult *c_psql_exec_query_params (c_psql_database_t *db,
+ udb_query_t *q, c_psql_user_data_t *data)
+{
+ char *params[db->max_params_num];
+ char interval[64];
+ int i;
+
+ if ((data == NULL) || (data->params_num == 0))
+ return (c_psql_exec_query_noparams (db, q));
+
+ assert (db->max_params_num >= data->params_num);
+
+ for (i = 0; i < data->params_num; ++i) {
+ switch (data->params[i]) {
+ case C_PSQL_PARAM_HOST:
+ params[i] = C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
+ ? "localhost" : db->host;
+ break;
+ case C_PSQL_PARAM_DB:
+ params[i] = db->database;
+ break;
+ case C_PSQL_PARAM_USER:
+ params[i] = db->user;
+ break;
+ case C_PSQL_PARAM_INTERVAL:
+ ssnprintf (interval, sizeof (interval), "%.3f",
+ (db->interval > 0)
+ ? CDTIME_T_TO_DOUBLE (db->interval) : interval_g);
+ params[i] = interval;
+ break;
+ default:
+ assert (0);
+ }
}
- n = PQntuples (res);
- assert (1 >= n);
+ return PQexecParams (db->conn, udb_query_get_statement (q),
+ data->params_num, NULL,
+ (const char *const *) params,
+ NULL, NULL, /* return text data */ 0);
+} /* c_psql_exec_query_params */
- if (1 > n) /* no user tables */
- return 0;
+static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q,
+ udb_query_preparation_area_t *prep_area)
+{
+ PGresult *res;
- submit_counter (db, "pg_scan", "seq", PQgetvalue (res, 0, 0));
- submit_counter (db, "pg_scan", "seq_tup_read", PQgetvalue (res, 0, 1));
- submit_counter (db, "pg_scan", "idx", PQgetvalue (res, 0, 2));
- submit_counter (db, "pg_scan", "idx_tup_fetch", PQgetvalue (res, 0, 3));
+ c_psql_user_data_t *data;
- submit_counter (db, "pg_n_tup_c", "ins", PQgetvalue (res, 0, 4));
- submit_counter (db, "pg_n_tup_c", "upd", PQgetvalue (res, 0, 5));
- submit_counter (db, "pg_n_tup_c", "del", PQgetvalue (res, 0, 6));
- submit_counter (db, "pg_n_tup_c", "hot_upd", PQgetvalue (res, 0, 7));
+ const char *host;
- submit_gauge (db, "pg_n_tup_g", "live", PQgetvalue (res, 0, 8));
- submit_gauge (db, "pg_n_tup_g", "dead", PQgetvalue (res, 0, 9));
+ char **column_names;
+ char **column_values;
+ int column_num;
- PQclear (res);
- return 0;
-} /* c_psql_stat_user_tables */
+ int rows_num;
+ int status;
+ int row, col;
-static int c_psql_statio_user_tables (c_psql_database_t *db)
-{
- const char *const query =
- "SELECT sum(heap_blks_read), sum(heap_blks_hit), "
- "sum(idx_blks_read), sum(idx_blks_hit), "
- "sum(toast_blks_read), sum(toast_blks_hit), "
- "sum(tidx_blks_read), sum(tidx_blks_hit) "
- "FROM pg_statio_user_tables;";
+ /* The user data may hold parameter information, but may be NULL. */
+ data = udb_query_get_user_data (q);
- PGresult *res;
+ /* Versions up to `3' don't know how to handle parameters. */
+ if (3 <= db->proto_version)
+ res = c_psql_exec_query_params (db, q, data);
+ else if ((NULL == data) || (0 == data->params_num))
+ res = c_psql_exec_query_noparams (db, q);
+ else {
+ log_err ("Connection to database \"%s\" does not support parameters "
+ "(protocol version %d) - cannot execute query \"%s\".",
+ db->database, db->proto_version,
+ udb_query_get_name (q));
+ return -1;
+ }
- int n;
+ column_names = NULL;
+ column_values = NULL;
- res = PQexec (db->conn, query);
+#define BAIL_OUT(status) \
+ sfree (column_names); \
+ sfree (column_values); \
+ PQclear (res); \
+ return status
if (PGRES_TUPLES_OK != PQresultStatus (res)) {
log_err ("Failed to execute SQL query: %s",
PQerrorMessage (db->conn));
- log_info ("SQL query was: %s", query);
- PQclear (res);
- return -1;
+ log_info ("SQL query was: %s",
+ udb_query_get_statement (q));
+ BAIL_OUT (-1);
}
- n = PQntuples (res);
- assert (1 >= n);
+ rows_num = PQntuples (res);
+ if (1 > rows_num) {
+ BAIL_OUT (0);
+ }
+
+ column_num = PQnfields (res);
+ column_names = (char **) calloc (column_num, sizeof (char *));
+ if (NULL == column_names) {
+ log_err ("calloc failed.");
+ BAIL_OUT (-1);
+ }
+
+ column_values = (char **) calloc (column_num, sizeof (char *));
+ if (NULL == column_values) {
+ log_err ("calloc failed.");
+ BAIL_OUT (-1);
+ }
+
+ for (col = 0; col < column_num; ++col) {
+ /* Pointers returned by `PQfname' are freed by `PQclear' via
+ * `BAIL_OUT'. */
+ column_names[col] = PQfname (res, col);
+ if (NULL == column_names[col]) {
+ log_err ("Failed to resolve name of column %i.", col);
+ BAIL_OUT (-1);
+ }
+ }
- if (1 > n) /* no user tables */
- return 0;
+ if (C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
+ || (0 == strcmp (db->host, "localhost")))
+ host = hostname_g;
+ else
+ host = db->host;
+
+ status = udb_query_prepare_result (q, prep_area, host, "postgresql",
+ db->database, column_names, (size_t) column_num, db->interval);
+ if (0 != status) {
+ log_err ("udb_query_prepare_result failed with status %i.",
+ status);
+ BAIL_OUT (-1);
+ }
- submit_counter (db, "pg_blks", "heap_read", PQgetvalue (res, 0, 0));
- submit_counter (db, "pg_blks", "heap_hit", PQgetvalue (res, 0, 1));
+ for (row = 0; row < rows_num; ++row) {
+ for (col = 0; col < column_num; ++col) {
+ /* Pointers returned by `PQgetvalue' are freed by `PQclear' via
+ * `BAIL_OUT'. */
+ column_values[col] = PQgetvalue (res, row, col);
+ if (NULL == column_values[col]) {
+ log_err ("Failed to get value at (row = %i, col = %i).",
+ row, col);
+ break;
+ }
+ }
- submit_counter (db, "pg_blks", "idx_read", PQgetvalue (res, 0, 2));
- submit_counter (db, "pg_blks", "idx_hit", PQgetvalue (res, 0, 3));
+ /* check for an error */
+ if (col < column_num)
+ continue;
- submit_counter (db, "pg_blks", "toast_read", PQgetvalue (res, 0, 4));
- submit_counter (db, "pg_blks", "toast_hit", PQgetvalue (res, 0, 5));
+ status = udb_query_handle_result (q, prep_area, column_values);
+ if (status != 0) {
+ log_err ("udb_query_handle_result failed with status %i.",
+ status);
+ }
+ } /* for (row = 0; row < rows_num; ++row) */
- submit_counter (db, "pg_blks", "tidx_read", PQgetvalue (res, 0, 6));
- submit_counter (db, "pg_blks", "tidx_hit", PQgetvalue (res, 0, 7));
+ udb_query_finish_result (q, prep_area);
- PQclear (res);
- return 0;
-} /* c_psql_statio_user_tables */
+ BAIL_OUT (0);
+#undef BAIL_OUT
+} /* c_psql_exec_query */
-static int c_psql_read (void)
+static int c_psql_read (user_data_t *ud)
{
+ c_psql_database_t *db;
+
int success = 0;
int i;
- for (i = 0; i < databases_num; ++i) {
- c_psql_database_t *db = databases + i;
+ if ((ud == NULL) || (ud->data == NULL)) {
+ log_err ("c_psql_read: Invalid user data.");
+ return -1;
+ }
- assert (NULL != db->database);
+ db = ud->data;
- if (0 != c_psql_check_connection (db))
- continue;
+ assert (NULL != db->database);
+
+ if (0 != c_psql_check_connection (db))
+ return -1;
- c_psql_stat_database (db);
- c_psql_stat_user_tables (db);
- c_psql_statio_user_tables (db);
+ for (i = 0; i < db->queries_num; ++i)
+ {
+ udb_query_preparation_area_t *prep_area;
+ udb_query_t *q;
+
+ prep_area = db->q_prep_areas[i];
+ q = db->queries[i];
+
+ if ((0 != db->server_version)
+ && (udb_query_check_version (q, db->server_version) <= 0))
+ continue;
- ++success;
+ if (0 == c_psql_exec_query (db, q, prep_area))
+ success = 1;
}
if (! success)
static int c_psql_shutdown (void)
{
- int i;
-
- if ((NULL == databases) || (0 == databases_num))
- return 0;
-
- plugin_unregister_read ("postgresql");
- plugin_unregister_shutdown ("postgresql");
-
- for (i = 0; i < databases_num; ++i) {
- c_psql_database_t *db = databases + i;
-
- PQfinish (db->conn);
-
- sfree (db->database);
- sfree (db->host);
- sfree (db->port);
- sfree (db->user);
- sfree (db->password);
+ plugin_unregister_read_group ("postgresql");
- sfree (db->sslmode);
+ udb_query_free (queries, queries_num);
+ queries = NULL;
+ queries_num = 0;
- sfree (db->krbsrvname);
-
- sfree (db->service);
- }
-
- sfree (databases);
- databases_num = 0;
return 0;
} /* c_psql_shutdown */
-static int c_psql_init (void)
+static int config_query_param_add (udb_query_t *q, oconfig_item_t *ci)
{
- int i;
+ c_psql_user_data_t *data;
+ const char *param_str;
- if ((NULL == databases) || (0 == databases_num))
- return 0;
+ c_psql_param_t *tmp;
- for (i = 0; i < databases_num; ++i) {
- c_psql_database_t *db = databases + i;
-
- char conninfo[4096];
- char *buf = conninfo;
- int buf_len = sizeof (conninfo);
- int status;
-
- char *server_host;
- int server_version;
-
- status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
- if (0 < status) {
- buf += status;
- buf_len -= status;
+ data = udb_query_get_user_data (q);
+ if (NULL == data) {
+ data = (c_psql_user_data_t *) smalloc (sizeof (*data));
+ if (NULL == data) {
+ log_err ("Out of memory.");
+ return -1;
}
+ memset (data, 0, sizeof (*data));
+ data->params = NULL;
+ }
- C_PSQL_PAR_APPEND (buf, buf_len, "host", db->host);
- C_PSQL_PAR_APPEND (buf, buf_len, "port", db->port);
- C_PSQL_PAR_APPEND (buf, buf_len, "user", db->user);
- C_PSQL_PAR_APPEND (buf, buf_len, "password", db->password);
- C_PSQL_PAR_APPEND (buf, buf_len, "sslmode", db->sslmode);
- C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
- C_PSQL_PAR_APPEND (buf, buf_len, "service", db->service);
-
- db->conn = PQconnectdb (conninfo);
- if (0 != c_psql_check_connection (db))
- continue;
-
- server_host = PQhost (db->conn);
- server_version = PQserverVersion (db->conn);
- log_info ("Sucessfully connected to database %s (user %s) "
- "at server %s%s%s (server version: %d.%d.%d, "
- "protocol version: %d, pid: %d)",
- PQdb (db->conn), PQuser (db->conn),
- C_PSQL_SOCKET3(server_host, PQport (db->conn)),
- C_PSQL_SERVER_VERSION3 (server_version),
- PQprotocolVersion (db->conn), PQbackendPID (db->conn));
+ tmp = (c_psql_param_t *) realloc (data->params,
+ (data->params_num + 1) * sizeof (c_psql_param_t));
+ if (NULL == tmp) {
+ log_err ("Out of memory.");
+ return -1;
+ }
+ data->params = tmp;
+
+ param_str = ci->values[0].value.string;
+ if (0 == strcasecmp (param_str, "hostname"))
+ data->params[data->params_num] = C_PSQL_PARAM_HOST;
+ else if (0 == strcasecmp (param_str, "database"))
+ data->params[data->params_num] = C_PSQL_PARAM_DB;
+ else if (0 == strcasecmp (param_str, "username"))
+ data->params[data->params_num] = C_PSQL_PARAM_USER;
+ else if (0 == strcasecmp (param_str, "interval"))
+ data->params[data->params_num] = C_PSQL_PARAM_INTERVAL;
+ else {
+ log_err ("Invalid parameter \"%s\".", param_str);
+ return 1;
}
- plugin_register_read ("postgresql", c_psql_read);
- plugin_register_shutdown ("postgresql", c_psql_shutdown);
- return 0;
-} /* c_psql_init */
+ data->params_num++;
+ udb_query_set_user_data (q, data);
-static int config_set (char *name, char **var, const oconfig_item_t *ci)
+ return (0);
+} /* config_query_param_add */
+
+static int config_query_callback (udb_query_t *q, oconfig_item_t *ci)
{
- if ((0 != ci->children_num) || (1 != ci->values_num)
- || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
- log_err ("%s expects a single string argument.", name);
- return 1;
- }
+ if (0 == strcasecmp ("Param", ci->key))
+ return config_query_param_add (q, ci);
- sfree (*var);
- *var = sstrdup (ci->values[0].value.string);
- return 0;
-} /* config_set */
+ log_err ("Option not allowed within a Query block: `%s'", ci->key);
+
+ return (-1);
+} /* config_query_callback */
static int c_psql_config_database (oconfig_item_t *ci)
{
c_psql_database_t *db;
+ char cb_name[DATA_MAX_NAME_LEN];
+ struct timespec cb_interval = { 0, 0 };
+ user_data_t ud;
+
int i;
if ((1 != ci->values_num)
return 1;
}
- ++databases_num;
- if (NULL == (databases = (c_psql_database_t *)realloc (databases,
- databases_num * sizeof (*databases)))) {
- log_err ("Out of memory.");
- exit (5);
- }
-
- db = databases + (databases_num - 1);
-
- db->conn = NULL;
-
- db->conn_complaint.last = 0;
- db->conn_complaint.interval = 0;
+ memset (&ud, 0, sizeof (ud));
- db->database = sstrdup (ci->values[0].value.string);
- db->host = NULL;
- db->port = NULL;
- db->user = NULL;
- db->password = NULL;
-
- db->sslmode = NULL;
-
- db->krbsrvname = NULL;
-
- db->service = NULL;
+ db = c_psql_database_new (ci->values[0].value.string);
+ if (db == NULL)
+ return -1;
for (i = 0; i < ci->children_num; ++i) {
oconfig_item_t *c = ci->children + i;
if (0 == strcasecmp (c->key, "Host"))
- config_set ("Host", &db->host, c);
+ cf_util_get_string (c, &db->host);
else if (0 == strcasecmp (c->key, "Port"))
- config_set ("Port", &db->port, c);
+ cf_util_get_service (c, &db->port);
else if (0 == strcasecmp (c->key, "User"))
- config_set ("User", &db->user, c);
+ cf_util_get_string (c, &db->user);
else if (0 == strcasecmp (c->key, "Password"))
- config_set ("Password", &db->password, c);
+ cf_util_get_string (c, &db->password);
else if (0 == strcasecmp (c->key, "SSLMode"))
- config_set ("SSLMode", &db->sslmode, c);
+ cf_util_get_string (c, &db->sslmode);
else if (0 == strcasecmp (c->key, "KRBSrvName"))
- config_set ("KRBSrvName", &db->krbsrvname, c);
+ cf_util_get_string (c, &db->krbsrvname);
else if (0 == strcasecmp (c->key, "Service"))
- config_set ("Service", &db->service, c);
+ cf_util_get_string (c, &db->service);
+ else if (0 == strcasecmp (c->key, "Query"))
+ udb_query_pick_from_list (c, queries, queries_num,
+ &db->queries, &db->queries_num);
+ else if (0 == strcasecmp (c->key, "Interval"))
+ cf_util_get_cdtime (c, &db->interval);
else
log_warn ("Ignoring unknown config key \"%s\".", c->key);
}
+
+ /* If no `Query' options were given, add the default queries.. */
+ if (db->queries_num == 0) {
+ for (i = 0; i < def_queries_num; i++)
+ udb_query_pick_from_list_by_name (def_queries[i],
+ queries, queries_num,
+ &db->queries, &db->queries_num);
+ }
+
+ if (db->queries_num > 0) {
+ db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
+ db->queries_num, sizeof (*db->q_prep_areas));
+
+ if (db->q_prep_areas == NULL) {
+ log_err ("Out of memory.");
+ c_psql_database_delete (db);
+ return -1;
+ }
+ }
+
+ for (i = 0; (size_t)i < db->queries_num; ++i) {
+ c_psql_user_data_t *data;
+ data = udb_query_get_user_data (db->queries[i]);
+ if ((data != NULL) && (data->params_num > db->max_params_num))
+ db->max_params_num = data->params_num;
+
+ db->q_prep_areas[i]
+ = udb_query_allocate_preparation_area (db->queries[i]);
+
+ if (db->q_prep_areas[i] == NULL) {
+ log_err ("Out of memory.");
+ c_psql_database_delete (db);
+ return -1;
+ }
+ }
+
+ ud.data = db;
+ ud.free_func = c_psql_database_delete;
+
+ ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->database);
+
+ CDTIME_T_TO_TIMESPEC (db->interval, &cb_interval);
+
+ plugin_register_complex_read ("postgresql", cb_name, c_psql_read,
+ /* interval = */ (db->interval > 0) ? &cb_interval : NULL,
+ &ud);
return 0;
-}
+} /* c_psql_config_database */
static int c_psql_config (oconfig_item_t *ci)
{
+ static int have_def_config = 0;
+
int i;
+ if (0 == have_def_config) {
+ oconfig_item_t *c;
+
+ have_def_config = 1;
+
+ c = oconfig_parse_file (C_PSQL_DEFAULT_CONF);
+ if (NULL == c)
+ log_err ("Failed to read default config ("C_PSQL_DEFAULT_CONF").");
+ else
+ c_psql_config (c);
+
+ if (NULL == queries)
+ log_err ("Default config ("C_PSQL_DEFAULT_CONF") did not define "
+ "any queries - please check your installation.");
+ }
+
for (i = 0; i < ci->children_num; ++i) {
oconfig_item_t *c = ci->children + i;
- if (0 == strcasecmp (c->key, "Database"))
+ if (0 == strcasecmp (c->key, "Query"))
+ udb_query_create (&queries, &queries_num, c,
+ /* callback = */ config_query_callback);
+ else if (0 == strcasecmp (c->key, "Database"))
c_psql_config_database (c);
else
log_warn ("Ignoring unknown config key \"%s\".", c->key);
void module_register (void)
{
plugin_register_complex_config ("postgresql", c_psql_config);
- plugin_register_init ("postgresql", c_psql_init);
+ plugin_register_shutdown ("postgresql", c_psql_shutdown);
} /* module_register */
/* vim: set sw=4 ts=4 tw=78 noexpandtab : */
-