Code

postgresql plugin: Moved pg_stat_database query to postgresql_default.conf.
authorSebastian Harl <sh@tokkee.org>
Fri, 18 Jul 2008 19:39:29 +0000 (21:39 +0200)
committerFlorian Forster <octo@huhu.verplant.org>
Thu, 24 Jul 2008 12:18:04 +0000 (14:18 +0200)
Signed-off-by: Sebastian Harl <sh@tokkee.org>
Signed-off-by: Florian Forster <octo@huhu.verplant.org>
src/postgresql.c
src/postgresql_default.conf

index f2d7665ff785a09f92ddfb978b9aa3dd4b7bd84c..5bf061457f9d50b0c7bdd1b9a17540be81686136 100644 (file)
@@ -123,6 +123,7 @@ typedef struct {
 } c_psql_database_t;
 
 static char *def_queries[] = {
+       "database",
        "user_tables",
        "io_user_tables"
 };
@@ -400,51 +401,6 @@ static int c_psql_exec_query (c_psql_database_t *db, int idx)
        return 0;
 } /* c_psql_exec_query */
 
-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;";
-
-       PGresult *res;
-
-       int n;
-
-       res = PQexecParams (db->conn, query, /* number of parameters */ 1,
-                       NULL, (const char *const *)&db->database, NULL, NULL,
-                       /* return text data */ 0);
-
-       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);
-       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;
-       }
-
-       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 */
-
 static int c_psql_read (void)
 {
        int success = 0;
@@ -460,8 +416,6 @@ static int c_psql_read (void)
                if (0 != c_psql_check_connection (db))
                        continue;
 
-               c_psql_stat_database (db);
-
                for (j = 0; j < db->queries_num; ++j)
                        c_psql_exec_query (db, j);
 
index 1391147cbb4470a2b074d0e5e4a752bcef206ed4..d304de537b2a647a0ea7375cf5e60a40b6ee8d77 100644 (file)
@@ -1,5 +1,17 @@
 # Pre-defined queries of collectd's postgresql plugin.
 
+<Query database>
+       Query "SELECT numbackends, xact_commit, xact_rollback \
+               FROM pg_stat_database \
+               WHERE datname = $1;"
+
+       Param database
+
+       Column pg_numbackends
+       Column pg_xact commit
+       Column pg_xact rollback
+</Query>
+
 <Query user_tables>
        Query "SELECT sum(seq_scan), sum(seq_tup_read), \
                        sum(idx_scan), sum(idx_tup_fetch), \