summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 12d48e6)
raw | patch | inline | side by side (parent: 12d48e6)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 18 Jul 2008 19:38:03 +0000 (21:38 +0200) | ||
committer | Florian Forster <octo@huhu.verplant.org> | |
Thu, 24 Jul 2008 12:18:04 +0000 (14:18 +0200) |
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@tokkee.org>
Signed-off-by: Florian Forster <octo@huhu.verplant.org>
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@tokkee.org>
Signed-off-by: Florian Forster <octo@huhu.verplant.org>
src/Makefile.am | patch | blob | history | |
src/collectd.h | patch | blob | history | |
src/postgresql.c | patch | blob | history | |
src/postgresql_default.conf | [new file with mode: 0644] | patch | blob |
diff --git a/src/Makefile.am b/src/Makefile.am
index c4e7d69f78b725020f55e768275fd212ef5c8897..dc36a2a8c0cec9ad06842b05c53678ab54fb954e 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
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
$(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 a262bf1d1fc99621e07b9aea7619351bc736d809..196530fbc3dfdedec57048ff6da2e602b0554d1f 100644 (file)
--- a/src/collectd.h
+++ b/src/collectd.h
#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 6a270d84b6b22590db7fd308e6c3dae764b8f033..5c5fbb38e0d26a21972e9152bc8f75588a5c1e08 100644 (file)
--- a/src/postgresql.c
+++ b/src/postgresql.c
#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
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;
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;
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);
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
--- /dev/null
@@ -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>
+