X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fpostgresql.c;h=ae887a17ec86da4c728d77444c2d9e04028c9ab1;hb=338db8de5f2cc289641f5f947f0fa1ef4070c3f2;hp=d0c0ae1324a457e5aeb706196536878fbea10a11;hpb=662c44a84ae3daecd4ffdea940fffce35a41b52a;p=collectd.git diff --git a/src/postgresql.c b/src/postgresql.c index d0c0ae13..ae887a17 100644 --- a/src/postgresql.c +++ b/src/postgresql.c @@ -58,7 +58,7 @@ * is ignored. */ #define C_PSQL_PAR_APPEND(buf, buf_len, parameter, value) \ if ((0 < (buf_len)) && (NULL != (value)) && ('\0' != *(value))) { \ - int s = ssnprintf(buf, buf_len, " %s = '%s'", parameter, value); \ + int s = snprintf(buf, buf_len, " %s = '%s'", parameter, value); \ if (0 < s) { \ buf += s; \ buf_len -= s; \ @@ -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); @@ -322,7 +323,7 @@ static int c_psql_connect(c_psql_database_t *db) { if ((!db) || (!db->database)) return -1; - status = ssnprintf(buf, buf_len, "dbname = '%s'", db->database); + status = snprintf(buf, buf_len, "dbname = '%s'", db->database); if (0 < status) { buf += status; buf_len -= status; @@ -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) @@ -424,9 +426,8 @@ static PGresult *c_psql_exec_query_params(c_psql_database_t *db, udb_query_t *q, params[i] = db->user; break; case C_PSQL_PARAM_INTERVAL: - ssnprintf(interval, sizeof(interval), "%.3f", - (db->interval > 0) ? CDTIME_T_TO_DOUBLE(db->interval) - : plugin_get_interval()); + snprintf(interval, sizeof(interval), "%.3f", + CDTIME_T_TO_DOUBLE(plugin_get_interval())); params[i] = interval; break; case C_PSQL_PARAM_INSTANCE: @@ -438,7 +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, 0); + NULL, (const char *const *)params, NULL, NULL, 0); } /* c_psql_exec_query_params */ /* db->db_lock must be locked when calling this function */ @@ -539,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); @@ -629,7 +632,7 @@ static char *values_name_to_sqlarray(const data_set_t *ds, char *string, str_len = string_len; for (size_t i = 0; i < ds->ds_num; ++i) { - int status = ssnprintf(str_ptr, str_len, ",'%s'", ds->ds[i].name); + int status = snprintf(str_ptr, str_len, ",'%s'", ds->ds[i].name); if (status < 1) return NULL; @@ -656,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; @@ -667,10 +670,10 @@ static char *values_type_to_sqlarray(const data_set_t *ds, char *string, int status; if (store_rates) - status = ssnprintf(str_ptr, str_len, ",'gauge'"); + status = snprintf(str_ptr, str_len, ",'gauge'"); else - status = ssnprintf(str_ptr, str_len, ",'%s'", - DS_TYPE_TO_STRING(ds->ds[i].type)); + status = snprintf(str_ptr, str_len, ",'%s'", + DS_TYPE_TO_STRING(ds->ds[i].type)); if (status < 1) { str_len = 0; @@ -699,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; @@ -722,7 +725,7 @@ static char *values_to_sqlarray(const data_set_t *ds, const value_list_t *vl, if (ds->ds[i].type == DS_TYPE_GAUGE) status = - ssnprintf(str_ptr, str_len, "," GAUGE_FORMAT, vl->values[i].gauge); + snprintf(str_ptr, str_len, "," GAUGE_FORMAT, vl->values[i].gauge); else if (store_rates) { if (rates == NULL) rates = uc_get_rate(ds, vl); @@ -732,13 +735,14 @@ static char *values_to_sqlarray(const data_set_t *ds, const value_list_t *vl, return NULL; } - status = ssnprintf(str_ptr, str_len, ",%lf", rates[i]); + status = snprintf(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 = snprintf(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); + status = snprintf(str_ptr, str_len, ",%" PRIi64, vl->values[i].derive); else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) - status = ssnprintf(str_ptr, str_len, ",%" PRIu64, vl->values[i].absolute); + status = snprintf(str_ptr, str_len, ",%" PRIu64, vl->values[i].absolute); if (status < 1) { str_len = 0; @@ -927,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"); @@ -936,11 +940,11 @@ static int c_psql_shutdown(void) { if (db->writers_num > 0) { char cb_name[DATA_MAX_NAME_LEN]; - ssnprintf(cb_name, sizeof(cb_name), "postgresql-%s", db->database); + snprintf(cb_name, sizeof(cb_name), "postgresql-%s", db->database); if (!had_flush) { plugin_unregister_flush("postgresql"); - had_flush = 1; + had_flush = true; } plugin_unregister_flush(cb_name); @@ -1088,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; @@ -1114,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."); @@ -1139,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")) @@ -1152,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) @@ -1194,14 +1201,14 @@ static int c_psql_config_database(oconfig_item_t *ci) { } } - ssnprintf(cb_name, sizeof(cb_name), "postgresql-%s", db->instance); + snprintf(cb_name, sizeof(cb_name), "postgresql-%s", db->instance); user_data_t ud = {.data = db, .free_func = c_psql_database_delete}; 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; @@ -1210,7 +1217,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 */ @@ -1226,7 +1233,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;