X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fpostgresql.c;h=4e608be10e1e06d1283eec918e47cbf5aede79d5;hb=d931e9e904f61e28013c01ee6903985d3c9f0ba8;hp=2af898863edc1ea4b5cde8675fe5ce1c8d6e9f90;hpb=55ffaf9ad7c8b180ac0192adb1a6ff6adc72c5a3;p=collectd.git diff --git a/src/postgresql.c b/src/postgresql.c index 2af89886..4e608be1 100644 --- a/src/postgresql.c +++ b/src/postgresql.c @@ -32,13 +32,13 @@ #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 #include @@ -102,7 +102,7 @@ typedef struct { typedef struct { char *name; char *statement; - _Bool store_rates; + bool store_rates; } c_psql_writer_t; typedef struct { @@ -125,8 +125,6 @@ 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; @@ -139,6 +137,7 @@ typedef struct { char *password; char *instance; + char *plugin_name; char *sslmode; @@ -154,14 +153,14 @@ static const char *const def_queries[] = { "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"); @@ -236,8 +235,6 @@ static c_psql_database_t *c_psql_database_new(const char *name) { pthread_mutex_init(&db->db_lock, /* attrs = */ NULL); - db->interval = 0; - db->commit_interval = 0; db->next_commit = 0; db->expire_delay = 0; @@ -250,6 +247,8 @@ static c_psql_database_t *c_psql_database_new(const char *name) { db->instance = sstrdup(name); + db->plugin_name = NULL; + db->sslmode = NULL; db->krbsrvname = NULL; @@ -300,6 +299,8 @@ static void c_psql_database_delete(void *data) { sfree(db->instance); + sfree(db->plugin_name); + sfree(db->sslmode); sfree(db->krbsrvname); @@ -335,6 +336,7 @@ static int c_psql_connect(c_psql_database_t *db) { 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); @@ -342,10 +344,10 @@ static int c_psql_connect(c_psql_database_t *db) { } /* 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) @@ -407,7 +409,7 @@ static PGresult *c_psql_exec_query_params(c_psql_database_t *db, udb_query_t *q, 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); @@ -425,8 +427,7 @@ static PGresult *c_psql_exec_query_params(c_psql_database_t *db, udb_query_t *q, 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: @@ -438,8 +439,7 @@ static PGresult *c_psql_exec_query_params(c_psql_database_t *db, udb_query_t *q, } 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 */ @@ -511,14 +511,14 @@ static int c_psql_exec_query(c_psql_database_t *db, udb_query_t *q, } 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); } @@ -540,9 +540,11 @@ static int c_psql_exec_query(c_psql_database_t *db, udb_query_t *q, 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); @@ -657,7 +659,7 @@ static char *values_name_to_sqlarray(const data_set_t *ds, char *string, } /* 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; @@ -700,7 +702,7 @@ static char *values_type_to_sqlarray(const data_set_t *ds, char *string, 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; @@ -735,7 +737,8 @@ static char *values_to_sqlarray(const data_set_t *ds, const value_list_t *vl, 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) @@ -928,7 +931,7 @@ static int c_psql_flush(cdtime_t timeout, } /* c_psql_flush */ static int c_psql_shutdown(void) { - _Bool had_flush = 0; + bool had_flush = false; plugin_unregister_read_group("postgresql"); @@ -941,7 +944,7 @@ static int c_psql_shutdown(void) { if (!had_flush) { plugin_unregister_flush("postgresql"); - had_flush = 1; + had_flush = true; } plugin_unregister_flush(cb_name); @@ -973,9 +976,9 @@ static int config_query_param_add(udb_query_t *q, oconfig_item_t *ci) { 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; } @@ -986,7 +989,7 @@ static int config_query_param_add(udb_query_t *q, oconfig_item_t *ci) { } tmp = realloc(data->params, (data->params_num + 1) * sizeof(*data->params)); - if (NULL == tmp) { + if (tmp == NULL) { log_err("Out of memory."); return -1; } @@ -1009,7 +1012,7 @@ static int config_query_param_add(udb_query_t *q, oconfig_item_t *ci) { } data->params_num++; - return (0); + return 0; } /* config_query_param_add */ static int config_query_callback(udb_query_t *q, oconfig_item_t *ci) { @@ -1018,7 +1021,7 @@ 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, @@ -1089,7 +1092,7 @@ static int c_psql_config_writer(oconfig_item_t *ci) { 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; @@ -1115,8 +1118,9 @@ static int c_psql_config_writer(oconfig_item_t *ci) { 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(" expects a single string argument."); @@ -1140,6 +1144,8 @@ static int c_psql_config_database(oconfig_item_t *ci) { 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")) @@ -1153,7 +1159,7 @@ static int c_psql_config_database(oconfig_item_t *ci) { 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) @@ -1170,9 +1176,7 @@ static int c_psql_config_database(oconfig_item_t *ci) { } 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); @@ -1201,8 +1205,8 @@ static int c_psql_config_database(oconfig_item_t *ci) { 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; @@ -1211,7 +1215,7 @@ static int c_psql_config_database(oconfig_item_t *ci) { 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 */ @@ -1227,7 +1231,7 @@ static int c_psql_config_database(oconfig_item_t *ci) { } /* 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; @@ -1265,5 +1269,3 @@ void module_register(void) { 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 : */