#include "collectd.h"
-#include "common.h"
+#include "utils/common/common.h"
#include "plugin.h"
+#include "utils/db_query/db_query.h"
#include "utils_cache.h"
#include "utils_complain.h"
-#include "utils_db_query.h"
#include <libpq-fe.h>
#include <pg_config_manual.h>
typedef struct {
char *name;
char *statement;
- _Bool store_rates;
+ bool store_rates;
} c_psql_writer_t;
typedef struct {
/* make sure we don't access the database object in parallel */
pthread_mutex_t db_lock;
- cdtime_t interval;
-
/* writer "caching" settings */
cdtime_t commit_interval;
cdtime_t next_commit;
char *password;
char *instance;
+ char *plugin_name;
char *sslmode;
"table_states", "disk_io", "disk_usage"};
static int def_queries_num = STATIC_ARRAY_SIZE(def_queries);
-static c_psql_database_t **databases = NULL;
-static size_t databases_num = 0;
+static c_psql_database_t **databases;
+static size_t databases_num;
-static udb_query_t **queries = NULL;
-static size_t queries_num = 0;
+static udb_query_t **queries;
+static size_t queries_num;
-static c_psql_writer_t *writers = NULL;
-static size_t writers_num = 0;
+static c_psql_writer_t *writers;
+static size_t writers_num;
static int c_psql_begin(c_psql_database_t *db) {
PGresult *r = PQexec(db->conn, "BEGIN");
pthread_mutex_init(&db->db_lock, /* attrs = */ NULL);
- db->interval = 0;
-
db->commit_interval = 0;
db->next_commit = 0;
db->expire_delay = 0;
db->instance = sstrdup(name);
+ db->plugin_name = NULL;
+
db->sslmode = NULL;
db->krbsrvname = NULL;
sfree(db->instance);
+ sfree(db->plugin_name);
+
sfree(db->sslmode);
sfree(db->krbsrvname);
C_PSQL_PAR_APPEND(buf, buf_len, "sslmode", db->sslmode);
C_PSQL_PAR_APPEND(buf, buf_len, "krbsrvname", db->krbsrvname);
C_PSQL_PAR_APPEND(buf, buf_len, "service", db->service);
+ C_PSQL_PAR_APPEND(buf, buf_len, "application_name", "collectd_postgresql");
db->conn = PQconnectdb(conninfo);
db->proto_version = PQprotocolVersion(db->conn);
} /* c_psql_connect */
static int c_psql_check_connection(c_psql_database_t *db) {
- _Bool init = 0;
+ bool init = false;
if (!db->conn) {
- init = 1;
+ init = true;
/* trigger c_release() */
if (0 == db->conn_complaint.interval)
char interval[64];
if ((data == NULL) || (data->params_num == 0))
- return (c_psql_exec_query_noparams(db, q));
+ return c_psql_exec_query_noparams(db, q);
assert(db->max_params_num >= data->params_num);
break;
case C_PSQL_PARAM_INTERVAL:
ssnprintf(interval, sizeof(interval), "%.3f",
- (db->interval > 0) ? CDTIME_T_TO_DOUBLE(db->interval)
- : plugin_get_interval());
+ CDTIME_T_TO_DOUBLE(plugin_get_interval()));
params[i] = interval;
break;
case C_PSQL_PARAM_INSTANCE:
}
return PQexecParams(db->conn, udb_query_get_statement(q), data->params_num,
- NULL, (const char *const *)params, NULL, NULL,
- /* return text data */ 0);
+ NULL, (const char *const *)params, NULL, NULL, 0);
} /* c_psql_exec_query_params */
/* db->db_lock must be locked when calling this function */
}
column_num = PQnfields(res);
- column_names = (char **)calloc(column_num, sizeof(char *));
- if (NULL == column_names) {
+ column_names = calloc(column_num, sizeof(*column_names));
+ if (column_names == NULL) {
log_err("calloc failed.");
BAIL_OUT(-1);
}
- column_values = (char **)calloc(column_num, sizeof(char *));
- if (NULL == column_values) {
+ column_values = calloc(column_num, sizeof(*column_values));
+ if (column_values == NULL) {
log_err("calloc failed.");
BAIL_OUT(-1);
}
else
host = db->host;
- status =
- udb_query_prepare_result(q, prep_area, host, "postgresql", db->instance,
- column_names, (size_t)column_num, db->interval);
+ status = udb_query_prepare_result(
+ q, prep_area, host,
+ (db->plugin_name != NULL) ? db->plugin_name : "postgresql", db->instance,
+ column_names, (size_t)column_num);
+
if (0 != status) {
log_err("udb_query_prepare_result failed with status %i.", status);
BAIL_OUT(-1);
} /* values_name_to_sqlarray */
static char *values_type_to_sqlarray(const data_set_t *ds, char *string,
- size_t string_len, _Bool store_rates) {
+ size_t string_len, bool store_rates) {
char *str_ptr;
size_t str_len;
static char *values_to_sqlarray(const data_set_t *ds, const value_list_t *vl,
char *string, size_t string_len,
- _Bool store_rates) {
+ bool store_rates) {
char *str_ptr;
size_t str_len;
status = ssnprintf(str_ptr, str_len, ",%lf", rates[i]);
} else if (ds->ds[i].type == DS_TYPE_COUNTER)
- status = ssnprintf(str_ptr, str_len, ",%llu", vl->values[i].counter);
+ status = ssnprintf(str_ptr, str_len, ",%" PRIu64,
+ (uint64_t)vl->values[i].counter);
else if (ds->ds[i].type == DS_TYPE_DERIVE)
status = ssnprintf(str_ptr, str_len, ",%" PRIi64, vl->values[i].derive);
else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
assert(db->database != NULL);
assert(db->writers != NULL);
- if (rfc3339nano(time_str, sizeof(time_str), vl->time) != 0) {
+ if (rfc3339nano_local(time_str, sizeof(time_str), vl->time) != 0) {
log_err("c_psql_write: Failed to convert time to RFC 3339 format");
return -1;
}
} /* c_psql_flush */
static int c_psql_shutdown(void) {
- _Bool had_flush = 0;
+ bool had_flush = false;
plugin_unregister_read_group("postgresql");
if (!had_flush) {
plugin_unregister_flush("postgresql");
- had_flush = 1;
+ had_flush = true;
}
plugin_unregister_flush(cb_name);
c_psql_param_t *tmp;
data = udb_query_get_user_data(q);
- if (NULL == data) {
+ if (data == NULL) {
data = calloc(1, sizeof(*data));
- if (NULL == data) {
+ if (data == NULL) {
log_err("Out of memory.");
return -1;
}
}
tmp = realloc(data->params, (data->params_num + 1) * sizeof(*data->params));
- if (NULL == tmp) {
+ if (tmp == NULL) {
log_err("Out of memory.");
return -1;
}
}
data->params_num++;
- return (0);
+ return 0;
} /* config_query_param_add */
static int config_query_callback(udb_query_t *q, oconfig_item_t *ci) {
log_err("Option not allowed within a Query block: `%s'", ci->key);
- return (-1);
+ return -1;
} /* config_query_callback */
static int config_add_writer(oconfig_item_t *ci, c_psql_writer_t *src_writers,
writer->name = sstrdup(ci->values[0].value.string);
writer->statement = NULL;
- writer->store_rates = 1;
+ writer->store_rates = true;
for (int i = 0; i < ci->children_num; ++i) {
oconfig_item_t *c = ci->children + i;
static int c_psql_config_database(oconfig_item_t *ci) {
c_psql_database_t *db;
+ cdtime_t interval = 0;
char cb_name[DATA_MAX_NAME_LEN];
- static _Bool have_flush = 0;
+ static bool have_flush;
if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
log_err("<Database> expects a single string argument.");
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, "Plugin"))
+ cf_util_get_string(c, &db->plugin_name);
else if (0 == strcasecmp(c->key, "SSLMode"))
cf_util_get_string(c, &db->sslmode);
else if (0 == strcasecmp(c->key, "KRBSrvName"))
config_add_writer(c, writers, writers_num, &db->writers,
&db->writers_num);
else if (0 == strcasecmp(c->key, "Interval"))
- cf_util_get_cdtime(c, &db->interval);
+ cf_util_get_cdtime(c, &interval);
else if (strcasecmp("CommitInterval", c->key) == 0)
cf_util_get_cdtime(c, &db->commit_interval);
else if (strcasecmp("ExpireDelay", c->key) == 0)
}
if (db->queries_num > 0) {
- db->q_prep_areas = (udb_query_preparation_area_t **)calloc(
- db->queries_num, sizeof(*db->q_prep_areas));
-
+ db->q_prep_areas = calloc(db->queries_num, sizeof(*db->q_prep_areas));
if (db->q_prep_areas == NULL) {
log_err("Out of memory.");
c_psql_database_delete(db);
if (db->queries_num > 0) {
++db->ref_cnt;
- plugin_register_complex_read("postgresql", cb_name, c_psql_read,
- /* interval = */ db->interval, &ud);
+ plugin_register_complex_read("postgresql", cb_name, c_psql_read, interval,
+ &ud);
}
if (db->writers_num > 0) {
++db->ref_cnt;
if (!have_flush) {
/* flush all */
plugin_register_flush("postgresql", c_psql_flush, /* user data = */ NULL);
- have_flush = 1;
+ have_flush = true;
}
/* flush this connection only */
} /* c_psql_config_database */
static int c_psql_config(oconfig_item_t *ci) {
- static int have_def_config = 0;
+ static int have_def_config;
if (0 == have_def_config) {
oconfig_item_t *c;
plugin_register_complex_config("postgresql", c_psql_config);
plugin_register_shutdown("postgresql", c_psql_shutdown);
} /* module_register */
-
-/* vim: set sw=4 ts=4 tw=78 noexpandtab : */