[collectd] [PATCH] postgresql plugin: Define default queries in a configuration file.
Sebastian Harl
sh at tokkee.org
Fri Jul 18 21:38:03 CEST 2008
Instead of writing custom code for each default query, those are now defined
in a configuration file which is parsed using the same mechanism as used for
user-defined queries. This configuration file (postgresql_default.conf) is
installed into $pkgdatadir.
Querying the pg_stat_database view is not currently supported this way. This
would require support for parameters in user-defined queries which is not
(yet) available.
Signed-off-by: Sebastian Harl <sh at tokkee.org>
---
src/Makefile.am | 3 +
src/collectd.h | 4 +
src/postgresql.c | 136 ++++++++++++++-----------------------------
src/postgresql_default.conf | 38 ++++++++++++
4 files changed, 88 insertions(+), 93 deletions(-)
create mode 100644 src/postgresql_default.conf
diff --git a/src/Makefile.am b/src/Makefile.am
index 2be8cb3..72f1f0d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -21,6 +21,7 @@ if BUILD_FEATURE_DAEMON
AM_CPPFLAGS += -DPIDFILE='"${localstatedir}/run/${PACKAGE_NAME}.pid"'
endif
AM_CPPFLAGS += -DPLUGINDIR='"${pkglibdir}"'
+AM_CPPFLAGS += -DPKGDATADIR='"${pkgdatadir}"'
sbin_PROGRAMS = collectd collectdmon
bin_PROGRAMS = collectd-nagios
@@ -785,3 +786,5 @@ install-exec-hook:
$(INSTALL) -m 0640 collectd.conf $(DESTDIR)$(sysconfdir)/collectd.conf; \
fi; \
cp -f $(srcdir)/types.db $(DESTDIR)$(pkglibdir)/;
+ $(INSTALL) -D -m 0644 $(srcdir)/postgresql_default.conf \
+ $(DESTDIR)$(pkgdatadir)/postgresql_default.conf;
diff --git a/src/collectd.h b/src/collectd.h
index a262bf1..196530f 100644
--- a/src/collectd.h
+++ b/src/collectd.h
@@ -239,6 +239,10 @@
#define PLUGINDIR PREFIX "/lib/" PACKAGE_NAME
#endif
+#ifndef PKGDATADIR
+#define PKGDATADIR PREFIX "/share/" PACKAGE_NAME
+#endif
+
#ifndef COLLECTD_GRP_NAME
# define COLLECTD_GRP_NAME "collectd"
#endif
diff --git a/src/postgresql.c b/src/postgresql.c
index 6a270d8..5c5fbb3 100644
--- a/src/postgresql.c
+++ b/src/postgresql.c
@@ -38,6 +38,10 @@
#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
@@ -107,6 +111,12 @@ typedef struct {
char *service;
} c_psql_database_t;
+static char *def_queries[] = {
+ "user_tables",
+ "io_user_tables"
+};
+static int def_queries_num = STATIC_ARRAY_SIZE (def_queries);
+
static c_psql_query_t *queries = NULL;
static int queries_num = 0;
@@ -393,97 +403,6 @@ static int c_psql_stat_database (c_psql_database_t *db)
return 0;
} /* c_psql_stat_database */
-static int c_psql_stat_user_tables (c_psql_database_t *db)
-{
- 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;
-
- 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;
- }
-
- n = PQntuples (res);
- assert (1 >= n);
-
- if (1 > n) /* no user tables */
- return 0;
-
- 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));
-
- 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));
-
- submit_gauge (db, "pg_n_tup_g", "live", PQgetvalue (res, 0, 8));
- submit_gauge (db, "pg_n_tup_g", "dead", PQgetvalue (res, 0, 9));
-
- PQclear (res);
- return 0;
-} /* c_psql_stat_user_tables */
-
-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;";
-
- PGresult *res;
-
- int n;
-
- 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;
- }
-
- n = PQntuples (res);
- assert (1 >= n);
-
- if (1 > n) /* no user tables */
- return 0;
-
- submit_counter (db, "pg_blks", "heap_read", PQgetvalue (res, 0, 0));
- submit_counter (db, "pg_blks", "heap_hit", PQgetvalue (res, 0, 1));
-
- submit_counter (db, "pg_blks", "idx_read", PQgetvalue (res, 0, 2));
- submit_counter (db, "pg_blks", "idx_hit", PQgetvalue (res, 0, 3));
-
- submit_counter (db, "pg_blks", "toast_read", PQgetvalue (res, 0, 4));
- submit_counter (db, "pg_blks", "toast_hit", PQgetvalue (res, 0, 5));
-
- submit_counter (db, "pg_blks", "tidx_read", PQgetvalue (res, 0, 6));
- submit_counter (db, "pg_blks", "tidx_hit", PQgetvalue (res, 0, 7));
-
- PQclear (res);
- return 0;
-} /* c_psql_statio_user_tables */
-
static int c_psql_read (void)
{
int success = 0;
@@ -500,8 +419,6 @@ static int c_psql_read (void)
continue;
c_psql_stat_database (db);
- c_psql_stat_user_tables (db);
- c_psql_statio_user_tables (db);
for (j = 0; j < db->queries_num; ++j)
c_psql_exec_query (db, j);
@@ -761,13 +678,46 @@ static int c_psql_config_database (oconfig_item_t *ci)
else
log_warn ("Ignoring unknown config key \"%s\".", c->key);
}
+
+ if (NULL == db->queries) {
+ db->queries = (c_psql_query_t **)malloc (def_queries_num
+ * sizeof (*db->queries));
+
+ for (i = 0; i < def_queries_num; ++i) {
+ db->queries[i] = c_psql_query_get (def_queries[i]);
+ if (NULL == db->queries[i])
+ log_err ("Query \"%s\" not found - "
+ "please check your installation.",
+ def_queries[i]);
+ else
+ ++db->queries_num;
+ }
+ }
return 0;
}
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;
diff --git a/src/postgresql_default.conf b/src/postgresql_default.conf
new file mode 100644
index 0000000..1391147
--- /dev/null
+++ b/src/postgresql_default.conf
@@ -0,0 +1,38 @@
+# Pre-defined queries of collectd's postgresql plugin.
+
+<Query user_tables>
+ 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"
+
+ Column pg_scan seq
+ Column pg_scan seq_tup_read
+ Column pg_scan idx
+ Column pg_scan idx_tup_fetch
+ Column pg_n_tup_c ins
+ Column pg_n_tup_c upd
+ Column pg_n_tup_c del
+ Column pg_n_tup_c hot_upd
+ Column pg_n_tup_g live
+ Column pg_n_tup_g dead
+</Query>
+
+<Query io_user_tables>
+ 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;"
+
+ Column pg_blks heap_read
+ Column pg_blks heap_hit
+ Column pg_blks idx_read
+ Column pg_blks idx_hit
+ Column pg_blks toast_read
+ Column pg_blks toast_hit
+ Column pg_blks tidx_read
+ Column pg_blks tidx_hit
+</Query>
+
--
1.5.6.1.156.ge903b
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20080718/5b5f332b/attachment.pgp
More information about the collectd
mailing list