diff options
author | Mihails Prihodko <mihails.prihodko@zabbix.com> | 2022-04-12 17:01:33 +0300 |
---|---|---|
committer | Mihails Prihodko <mihails.prihodko@zabbix.com> | 2022-04-12 17:04:45 +0300 |
commit | c506017b9acc63f1dc185edc7cd53c4ed20c9aed (patch) | |
tree | 121d4d61dd062f1ac424a8e83f70fc350412edb5 | |
parent | bc5f34486d6e5a8bde5ff6246f9639c7855f1853 (diff) |
........S. [ZBX-20743] refactored according to conversation with MVekslers
-rw-r--r-- | include/zbxdb.h | 4 | ||||
-rw-r--r-- | src/libs/zbxdb/db.c | 93 | ||||
-rw-r--r-- | src/libs/zbxdbhigh/db.c | 39 | ||||
-rw-r--r-- | src/zabbix_server/server.c | 13 |
4 files changed, 68 insertions, 81 deletions
diff --git a/include/zbxdb.h b/include/zbxdb.h index aa283a962dc..b1d77f613a9 100644 --- a/include/zbxdb.h +++ b/include/zbxdb.h @@ -100,7 +100,6 @@ zbx_err_codes_t zbx_db_last_errcode(void); #ifdef HAVE_POSTGRESQL int zbx_tsdb_get_version(void); -char *zbx_tsdb_get_version_friendly(int ver); char *zbx_tsdb_get_license(void); #define ZBX_DB_TSDB_V1 (20000 > zbx_tsdb_get_version()) #endif @@ -183,10 +182,9 @@ int zbx_db_strlen_n(const char *text_loc, size_t maxlen); #define ZBX_DBVERSION_UNDEFINED 0 -#define ZBX_DB_EXT_STATUS_FLAGS_TSDB_CONFIGURED 0x00000001 +#define ZBX_DB_EXT_STATUS_FLAGS_TSDB_EXPECTED 0x00000001 #define ZBX_DB_EXT_STATUS_FLAGS_TSDB_COMPRESSION_AVAILABLE 0x00000002 -#define ZBX_TIMESCALEDB "TimescaleDB" #define ZBX_POSTGRESQL_MIN_VERSION_WITH_TIMESCALEDB 100002 #define ZBX_POSTGRESQL_MIN_VERSION_WITH_TIMESCALEDB_FRIENDLY "10.2" #define ZBX_TIMESCALEDB_MIN_VERSION 10500 diff --git a/src/libs/zbxdb/db.c b/src/libs/zbxdb/db.c index 3bc4ececf7a..515bd6440dd 100644 --- a/src/libs/zbxdb/db.c +++ b/src/libs/zbxdb/db.c @@ -2500,7 +2500,7 @@ int zbx_db_version_check(const char *database, zbx_uint32_t current_version, zbx * * * Purpose: prepare json for front-end with the DB current, minimum and * * maximum versions and a flag that indicates if the version * - * satisfies the requirements, and other infomration as well as * + * satisfies the requirements, and other information as well as * * information about DB extension in a similar way * * * * Parameters: json - [IN/OUT] json data * @@ -2528,13 +2528,16 @@ void zbx_db_version_json_create(struct zbx_json *json, struct zbx_db_version_inf zbx_json_addint64(json, "flag", info->flag); zbx_json_close(json); - if (info->ext_status & ZBX_DB_EXT_STATUS_FLAGS_TSDB_CONFIGURED) + if (0 != (ZBX_DB_EXT_STATUS_FLAGS_TSDB_EXPECTED & info->ext_status)) { zbx_json_addobject(json, NULL); zbx_json_addstring(json, "database", info->extension, ZBX_JSON_TYPE_STRING); if (DB_VERSION_FAILED_TO_RETRIEVE != info->ext_flag) - zbx_json_addstring(json, "current_version", info->ext_friendly_current_version, ZBX_JSON_TYPE_STRING); + { + zbx_json_addstring(json, "current_version", + info->ext_friendly_current_version, ZBX_JSON_TYPE_STRING); + } zbx_json_addstring(json, "min_version", info->ext_friendly_min_version, ZBX_JSON_TYPE_STRING); zbx_json_addstring(json, "max_version", info->ext_friendly_max_version, ZBX_JSON_TYPE_STRING); @@ -2547,8 +2550,14 @@ void zbx_db_version_json_create(struct zbx_json *json, struct zbx_db_version_inf zbx_json_addint64(json, "flag", info->ext_flag); - zbx_json_addint64(json, "compression_availability", - info->ext_status & ZBX_DB_EXT_STATUS_FLAGS_TSDB_COMPRESSION_AVAILABLE ? ON : OFF); + if (0 != (ZBX_DB_EXT_STATUS_FLAGS_TSDB_COMPRESSION_AVAILABLE & info->ext_status)) + { + zbx_json_addint64(json, "compression_availability", ON); + } + else + { + zbx_json_addint64(json, "compression_availability", OFF); + } zbx_json_close(json); } @@ -2828,36 +2837,34 @@ out: **************************************************************************************************************/ void zbx_tsdb_info_extract(struct zbx_db_version_info_t *version_info) { - int tsdb_ver; + int tsdb_ver; - version_info->extension = ZBX_TIMESCALEDB; + if (0 == (ZBX_DB_EXT_STATUS_FLAGS_TSDB_EXPECTED & version_info->ext_status)) + return; - if (0 == (tsdb_ver = zbx_tsdb_get_version())) - { - version_info->ext_flag = DB_VERSION_FAILED_TO_RETRIEVE; - zabbix_log(LOG_LEVEL_CRIT, "Cannot determine TimescaleDB version"); - } - else - { - zabbix_log(LOG_LEVEL_INFORMATION, "TimescaleDB version: [%d]", tsdb_ver); + tsdb_ver = zbx_tsdb_get_version(); - version_info->ext_current_version = (zbx_uint32_t)tsdb_ver; - version_info->ext_min_version = ZBX_TIMESCALEDB_MIN_VERSION; - version_info->ext_max_version = ZBX_TIMESCALEDB_MAX_VERSION; - version_info->ext_min_supported_version = ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION; + version_info->extension = "TimescaleDB"; - version_info->ext_friendly_current_version = zbx_tsdb_get_version_friendly(tsdb_ver); - version_info->ext_friendly_min_version = ZBX_TIMESCALEDB_MIN_VERSION_FRIENDLY; - version_info->ext_friendly_max_version = ZBX_TIMESCALEDB_MAX_VERSION_FRIENDLY; - version_info->ext_friendly_min_supported_version = ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION_FRIENDLY; + version_info->ext_current_version = (zbx_uint32_t)tsdb_ver; + version_info->ext_min_version = ZBX_TIMESCALEDB_MIN_VERSION; + version_info->ext_max_version = ZBX_TIMESCALEDB_MAX_VERSION; + version_info->ext_min_supported_version = ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION; - version_info->ext_flag = zbx_db_version_check(version_info->extension, version_info->ext_current_version, - version_info->ext_min_version, version_info->ext_max_version, version_info->ext_min_supported_version); + version_info->ext_friendly_current_version = zbx_dsprintf(NULL, "%d.%d.%d", + RIGHT2(tsdb_ver/10000), RIGHT2(tsdb_ver/100), RIGHT2(tsdb_ver)); - version_info->ext_lic = zbx_tsdb_get_license(); + version_info->ext_friendly_min_version = ZBX_TIMESCALEDB_MIN_VERSION_FRIENDLY; + version_info->ext_friendly_max_version = ZBX_TIMESCALEDB_MAX_VERSION_FRIENDLY; + version_info->ext_friendly_min_supported_version = ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION_FRIENDLY; - zabbix_log(LOG_LEVEL_INFORMATION, "TimescaleDB license: [%s]", version_info->ext_lic); - } + version_info->ext_flag = zbx_db_version_check(version_info->extension, version_info->ext_current_version, + version_info->ext_min_version, version_info->ext_max_version, + version_info->ext_min_supported_version); + + version_info->ext_lic = zbx_tsdb_get_license(); + + zabbix_log(LOG_LEVEL_DEBUG, "TimescaleDB version: [%d], license: [%s]", tsdb_ver, version_info->ext_lic); } #endif @@ -2926,29 +2933,6 @@ out: /****************************************************************************** * * - * Purpose: converts TimescaleDB (TSDB) version to user friendly format * - * * - * Example: TSDB 10501 version will be returned as "1.5.1" * - * * - * Return value: TSDB version in user friendly format as string * - * * - * Comments: returns a pointer to allocated memory * - * * - ******************************************************************************/ -char *zbx_tsdb_get_version_friendly(int ver) -{ - int major, minor, patch; - - patch = ver % 100; - ver = (ver - patch) / 100; - minor = ver % 100; - major = (ver - minor) / 100; - - return zbx_dsprintf(NULL, "%d.%d.%d", major, minor, patch); -} - -/****************************************************************************** - * * * Purpose: retrievs TimescaleDB (TSDB) license information * * * * Return value: license information from datase as string * @@ -2966,9 +2950,10 @@ char *zbx_tsdb_get_license(void) result = zbx_db_select("show timescaledb.license"); - if ((DB_RESULT)ZBX_DB_DOWN != result && NULL != result) - if (NULL != (row = zbx_db_fetch(result))) - tsdb_lic = zbx_strdup(NULL, row[0]); + if ((DB_RESULT)ZBX_DB_DOWN != result && NULL != result && NULL != (row = zbx_db_fetch(result))) + { + tsdb_lic = zbx_strdup(NULL, row[0]); + } DBfree_result(result); diff --git a/src/libs/zbxdbhigh/db.c b/src/libs/zbxdbhigh/db.c index d97bde19d66..4c557010064 100644 --- a/src/libs/zbxdbhigh/db.c +++ b/src/libs/zbxdbhigh/db.c @@ -810,7 +810,6 @@ void DBextract_version_info(struct zbx_db_version_info_t *version_info) ******************************************************************************/ void DBextract_dbextension_info(struct zbx_db_version_info_t *version_info) { - version_info->extension = NULL; #ifdef HAVE_POSTGRESQL DB_RESULT result; DB_ROW row; @@ -827,15 +826,16 @@ void DBextract_dbextension_info(struct zbx_db_version_info_t *version_info) if (NULL == (row = DBfetch(result))) goto clean; - if (0 != zbx_strcmp_null(row[0], ZBX_CONFIG_DB_EXTENSION_TIMESCALE)) - goto clean; + if (0 == zbx_strcmp_null(row[0], ZBX_CONFIG_DB_EXTENSION_TIMESCALE)) + version_info->ext_status |= ZBX_DB_EXT_STATUS_FLAGS_TSDB_EXPECTED; - version_info->ext_status |= ZBX_DB_EXT_STATUS_FLAGS_TSDB_CONFIGURED; zbx_tsdb_info_extract(version_info); clean: DBfree_result(result); out: DBclose(); +#else + ZBX_UNUSED(version_info); #endif } @@ -870,41 +870,48 @@ int DBcheck_tsdb_capabilities(struct zbx_db_version_info_t *db_version_info) { int ret = SUCCEED; #ifdef HAVE_POSTGRESQL - /* in case of major upgrade, db_extension may be missing */ - if (!(db_version_info->ext_status & ZBX_DB_EXT_STATUS_FLAGS_TSDB_CONFIGURED)) + if (0 == (ZBX_DB_EXT_STATUS_FLAGS_TSDB_EXPECTED & db_version_info->ext_status)) + { goto out; - - ret = FAIL; - db_version_info->ext_status &= ~ZBX_DB_EXT_STATUS_FLAGS_TSDB_COMPRESSION_AVAILABLE; + } /* Timescale compression feature is available in PostgreSQL 10.2 and TimescaleDB 1.5.0 and newer */ /* in TimescaleDB Community Edition, and it is not available in TimescaleDB Apache 2 Edition. */ /* timescaledb.license parameter is available in TimescaleDB API starting from TimescaleDB 2.0. */ if (ZBX_POSTGRESQL_MIN_VERSION_WITH_TIMESCALEDB > db_version_info->current_version) { - zabbix_log(LOG_LEVEL_CRIT, "PostgreSQL version %lu is not supported with TimescaleDB, minimum required is %d", - (unsigned long)db_version_info->current_version, ZBX_POSTGRESQL_MIN_VERSION_WITH_TIMESCALEDB); + zabbix_log(LOG_LEVEL_ERR, "PostgreSQL version %lu is not supported with TimescaleDB, minimum required" + " is %d", (unsigned long)db_version_info->current_version, + ZBX_POSTGRESQL_MIN_VERSION_WITH_TIMESCALEDB); + ret = FAIL; goto out; } if (DB_VERSION_FAILED_TO_RETRIEVE == db_version_info->ext_flag) + { + zabbix_log(LOG_LEVEL_ERR, "Cannot determine TimescaleDB version"); + ret = FAIL; goto out; + } if (ZBX_TIMESCALEDB_MIN_VERSION > db_version_info->ext_current_version) { - zabbix_log(LOG_LEVEL_CRIT, "TimescaleDB version %d is too old.", db_version_info->ext_current_version); - zabbix_log(LOG_LEVEL_CRIT, "Version must be at least %d. Recommended version should be at least %s %s.", + zabbix_log(LOG_LEVEL_ERR, "TimescaleDB version %d is too old.", db_version_info->ext_current_version); + zabbix_log(LOG_LEVEL_ERR, "Version must be at least %d. Recommended version should be at least %s %s.", ZBX_TIMESCALEDB_MIN_VERSION, ZBX_TIMESCALEDB_LICENSE_TIMESCALE_FRIENDLY, ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION_FRIENDLY); + ret = FAIL; goto out; } - else if (ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION > db_version_info->ext_current_version) + + if (ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION > db_version_info->ext_current_version) { db_version_info->ext_status |= ZBX_DB_EXT_STATUS_FLAGS_TSDB_COMPRESSION_AVAILABLE; zabbix_log(LOG_LEVEL_WARNING, "TimescaleDB version %d is not officially supported.", db_version_info->ext_current_version); zabbix_log(LOG_LEVEL_WARNING, "Recommended version should be at least %s %s.", - ZBX_TIMESCALEDB_LICENSE_TIMESCALE_FRIENDLY, ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION_FRIENDLY); + ZBX_TIMESCALEDB_LICENSE_TIMESCALE_FRIENDLY, + ZBX_TIMESCALEDB_MIN_SUPPORTED_VERSION_FRIENDLY); } else { @@ -922,8 +929,6 @@ int DBcheck_tsdb_capabilities(struct zbx_db_version_info_t *db_version_info) ZBX_TIMESCALEDB_LICENSE_TIMESCALE_FRIENDLY); } } - - ret = SUCCEED; out: #else ZBX_UNUSED(db_version_info); diff --git a/src/zabbix_server/server.c b/src/zabbix_server/server.c index 93297115166..0de185be3ed 100644 --- a/src/zabbix_server/server.c +++ b/src/zabbix_server/server.c @@ -1135,7 +1135,9 @@ static void zbx_check_db(void) } if (SUCCEED == result) + { DBextract_dbextension_info(&db_version_info); + } if (SUCCEED == result && (SUCCEED != DBcheck_tsdb_capabilities(&db_version_info) || SUCCEED != DBcheck_version())) @@ -1146,14 +1148,11 @@ static void zbx_check_db(void) DBconnect(ZBX_DB_CONNECT_NORMAL); #if defined(HAVE_POSTGRESQL) - if (SUCCEED == result) + if (SUCCEED == result && 0 != (ZBX_DB_EXT_STATUS_FLAGS_TSDB_EXPECTED & db_version_info.ext_status) && + 0 == (ZBX_DB_EXT_STATUS_FLAGS_TSDB_COMPRESSION_AVAILABLE & db_version_info.ext_status) && + ZBX_DB_OK > DBexecute("update config set compression_status=0")) { - if (db_version_info.ext_status & ZBX_DB_EXT_STATUS_FLAGS_TSDB_CONFIGURED && - !(db_version_info.ext_status & ZBX_DB_EXT_STATUS_FLAGS_TSDB_COMPRESSION_AVAILABLE)) - { - if (ZBX_DB_OK > DBexecute("update config set compression_status=0")) - zabbix_log(LOG_LEVEL_ERR, "failed to set database compression status"); - } + zabbix_log(LOG_LEVEL_ERR, "failed to set database compression status"); } #endif |