postgresql plugin: Added support for passing parameters to a query.
[collectd.git] / src / postgresql.c
index 6a270d8..d65a66f 100644 (file)
 #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_t;
+
 typedef struct {
        char *type;
        char *type_instance;
@@ -82,6 +92,9 @@ typedef struct {
        char *name;
        char *query;
 
+       c_psql_param_t *params;
+       int             params_num;
+
        c_psql_col_t *cols;
        int           cols_num;
 } c_psql_query_t;
@@ -90,6 +103,8 @@ typedef struct {
        PGconn      *conn;
        c_complain_t conn_complaint;
 
+       int max_params_num;
+
        /* user configuration */
        c_psql_query_t **queries;
        int              queries_num;
@@ -107,6 +122,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;
 
@@ -128,6 +149,9 @@ static c_psql_query_t *c_psql_query_new (const char *name)
        query->name  = sstrdup (name);
        query->query = NULL;
 
+       query->params     = NULL;
+       query->params_num = 0;
+
        query->cols     = NULL;
        query->cols_num = 0;
        return query;
@@ -140,6 +164,9 @@ static void c_psql_query_delete (c_psql_query_t *query)
        sfree (query->name);
        sfree (query->query);
 
+       sfree (query->params);
+       query->params_num = 0;
+
        for (i = 0; i < query->cols_num; ++i) {
                sfree (query->cols[i].type);
                sfree (query->cols[i].type_instance);
@@ -177,6 +204,8 @@ static c_psql_database_t *c_psql_database_new (const char *name)
        db->conn_complaint.last     = 0;
        db->conn_complaint.interval = 0;
 
+       db->max_params_num = 0;
+
        db->queries     = NULL;
        db->queries_num = 0;
 
@@ -301,6 +330,8 @@ static int c_psql_exec_query (c_psql_database_t *db, int idx)
        c_psql_query_t *query;
        PGresult       *res;
 
+       char *params[db->max_params_num];
+
        int rows, cols;
        int i;
 
@@ -309,7 +340,27 @@ static int c_psql_exec_query (c_psql_database_t *db, int idx)
 
        query = db->queries[idx];
 
-       res = PQexec (db->conn, query->query);
+       assert (db->max_params_num >= query->params_num);
+
+       for (i = 0; i < query->params_num; ++i) {
+               switch (query->params[i]) {
+                       case C_PSQL_PARAM_HOST:
+                               params[i] = (NULL == 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;
+                       default:
+                               assert (0);
+               }
+       }
+
+       res = PQexecParams (db->conn, query->query, query->params_num, NULL,
+                       (const char *const *)((0 == query->params_num) ? NULL : params),
+                       NULL, NULL, /* return text data */ 0);
 
        if (PGRES_TUPLES_OK != PQresultStatus (res)) {
                log_err ("Failed to execute SQL query: %s",
@@ -393,97 +444,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 +460,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);
@@ -634,6 +592,40 @@ static int config_set (char *name, char **var, const oconfig_item_t *ci)
        return 0;
 } /* config_set */
 
+static int config_set_param (c_psql_query_t *query, const oconfig_item_t *ci)
+{
+       c_psql_param_t param;
+       char          *param_str;
+
+       if ((0 != ci->children_num) || (1 != ci->values_num)
+                       || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
+               log_err ("Param expects a single string argument.");
+               return 1;
+       }
+
+       param_str = ci->values[0].value.string;
+       if (0 == strcasecmp (param_str, "hostname"))
+               param = C_PSQL_PARAM_HOST;
+       else if (0 == strcasecmp (param_str, "database"))
+               param = C_PSQL_PARAM_DB;
+       else if (0 == strcasecmp (param_str, "username"))
+               param = C_PSQL_PARAM_USER;
+       else {
+               log_err ("Invalid parameter \"%s\".", param_str);
+               return 1;
+       }
+
+       ++query->params_num;
+       if (NULL == (query->params = (c_psql_param_t *)realloc (query->params,
+                               query->params_num * sizeof (*query->params)))) {
+               log_err ("Out of memory.");
+               exit (5);
+       }
+
+       query->params[query->params_num - 1] = param;
+       return 0;
+} /* config_set_param */
+
 static int config_set_column (c_psql_query_t *query, const oconfig_item_t *ci)
 {
        c_psql_col_t *col;
@@ -694,6 +686,9 @@ static int config_set_query (c_psql_database_t *db, const oconfig_item_t *ci)
                exit (5);
        }
 
+       if (query->params_num > db->max_params_num)
+               db->max_params_num = query->params_num;
+
        db->queries[db->queries_num - 1] = query;
        return 0;
 } /* config_set_query */
@@ -717,6 +712,8 @@ static int c_psql_config_query (oconfig_item_t *ci)
 
                if (0 == strcasecmp (c->key, "Query"))
                        config_set ("Query", &query->query, c);
+               else if (0 == strcasecmp (c->key, "Param"))
+                       config_set_param (query, c);
                else if (0 == strcasecmp (c->key, "Column"))
                        config_set_column (query, c);
                else
@@ -761,13 +758,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;