From: Sebastian Harl Date: Thu, 18 Oct 2012 11:06:37 +0000 (+0200) Subject: postgresql plugin: Added "Instance" option to config block. X-Git-Tag: collectd-5.2.0~33 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=997d279d179a29a2bc2941270e89d4fa5ff4762c;p=collectd.git postgresql plugin: Added "Instance" option to config block. This option may be used to specify the plugin instance that should be used instead of the database name. This allows to query multiple databases of the same name on the same host (e.g. when running multiple database server versions in parallel). This fixes GH #161 suggested by Bryan Varner. --- diff --git a/src/postgresql.c b/src/postgresql.c index 0a5e66c2..5f29becb 100644 --- a/src/postgresql.c +++ b/src/postgresql.c @@ -125,6 +125,8 @@ typedef struct { char *user; char *password; + char *instance; + char *sslmode; char *krbsrvname; @@ -177,6 +179,8 @@ static c_psql_database_t *c_psql_database_new (const char *name) db->user = NULL; db->password = NULL; + db->instance = sstrdup (name); + db->sslmode = NULL; db->krbsrvname = NULL; @@ -208,6 +212,8 @@ static void c_psql_database_delete (void *data) sfree (db->user); sfree (db->password); + sfree (db->instance); + sfree (db->sslmode); sfree (db->krbsrvname); @@ -271,8 +277,9 @@ static int c_psql_check_connection (c_psql_database_t *db) if (CONNECTION_OK != PQstatus (db->conn)) { c_complain (LOG_ERR, &db->conn_complaint, - "Failed to connect to database %s: %s", - db->database, PQerrorMessage (db->conn)); + "Failed to connect to database %s (%s): %s", + db->database, db->instance, + PQerrorMessage (db->conn)); return -1; } @@ -377,9 +384,10 @@ static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q, 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, + log_err ("Connection to database \"%s\" (%s) does not support " + "parameters (protocol version %d) - " + "cannot execute query \"%s\".", + db->database, db->instance, db->proto_version, udb_query_get_name (q)); return -1; } @@ -436,7 +444,7 @@ static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q, host = db->host; status = udb_query_prepare_result (q, prep_area, host, "postgresql", - db->database, column_names, (size_t) column_num, db->interval); + db->instance, column_names, (size_t) column_num, db->interval); if (0 != status) { log_err ("udb_query_prepare_result failed with status %i.", status); @@ -487,6 +495,7 @@ static int c_psql_read (user_data_t *ud) db = ud->data; assert (NULL != db->database); + assert (NULL != db->instance); if (0 != c_psql_check_connection (db)) return -1; @@ -612,6 +621,8 @@ static int c_psql_config_database (oconfig_item_t *ci) cf_util_get_string (c, &db->user); else if (0 == strcasecmp (c->key, "Password")) cf_util_get_string (c, &db->password); + else if (0 == strcasecmp (c->key, "Instance")) + cf_util_get_string (c, &db->instance); else if (0 == strcasecmp (c->key, "SSLMode")) cf_util_get_string (c, &db->sslmode); else if (0 == strcasecmp (c->key, "KRBSrvName")) @@ -665,7 +676,7 @@ static int c_psql_config_database (oconfig_item_t *ci) ud.data = db; ud.free_func = c_psql_database_delete; - ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->database); + ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->instance); CDTIME_T_TO_TIMESPEC (db->interval, &cb_interval);