From 3e0bd96ec6598d4620fd3c16e7d3356bbbfb8ea1 Mon Sep 17 00:00:00 2001 From: Alvar Penning Date: Fri, 20 Jun 2025 08:48:43 +0200 Subject: [PATCH] IcingaHealth: Fix version comparison There are multiple possible outputs for an Icinga DB version. The package version contain the git tag, with a leading "v". The development version mimics git-describe(1), including a commit hash separated by a dash after the semantic version. The current version comparison uses PHP's builtin version_compare(). This results in leading "v"s to return invalid results. Furthermore, it treats everything behind the version as an "any string"[^0], which is smaller than dev, alpha, beta, and so on. Thus, any git-describe(1) version of Icinga DB 1.4.0 would be considered smaller as 1.4.0. Fixes #1230. [^0]: https://www.php.net/manual/en/function.version-compare.php --- .../Icingadb/ProvidedHook/IcingaHealth.php | 24 ++++++++++++++++++- library/Icingadb/Widget/Health.php | 6 ++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/library/Icingadb/ProvidedHook/IcingaHealth.php b/library/Icingadb/ProvidedHook/IcingaHealth.php index 1b87720b..100e91ed 100644 --- a/library/Icingadb/ProvidedHook/IcingaHealth.php +++ b/library/Icingadb/ProvidedHook/IcingaHealth.php @@ -16,6 +16,24 @@ class IcingaHealth extends HealthHook public const REQUIRED_ICINGADB_VERSION = '1.4.0'; + /** + * normalizeVersion extracts a version string from Icinga DB's reported + * version comparable with REQUIRED_ICINGADB_VERSION via version_compare. + * + * @param string $version Icinga DB version string to normalize. + * + * @return string Normalized Icinga DB version string. + */ + public static function normalizeVersion(string $version): string + { + // Ignore leading "v" when the git tag is used, e.g., "v1.4.0". + $version = ltrim($version, 'v'); + // Ignore everything after "-" used by git describe, e.g., "1.4.0-g...". + $version = explode('-', $version, 2)[0]; + + return $version; + } + /** @var Instance */ protected $instance; @@ -47,7 +65,11 @@ class IcingaHealth extends HealthHook )); } elseif ( ! isset($instance->icingadb_version) - || version_compare($instance->icingadb_version, self::REQUIRED_ICINGADB_VERSION, '<') + || version_compare( + self::normalizeVersion($instance->icingadb_version), + self::REQUIRED_ICINGADB_VERSION, + '<' + ) ) { $this->setState(self::STATE_CRITICAL); $this->setMessage(sprintf( diff --git a/library/Icingadb/Widget/Health.php b/library/Icingadb/Widget/Health.php index b40d1119..9812ef24 100644 --- a/library/Icingadb/Widget/Health.php +++ b/library/Icingadb/Widget/Health.php @@ -26,7 +26,11 @@ class Health extends BaseHtmlElement { if ( ! isset($this->data->icingadb_version) - || version_compare($this->data->icingadb_version, IcingaHealth::REQUIRED_ICINGADB_VERSION, '<') + || version_compare( + IcingaHealth::normalizeVersion($this->data->icingadb_version), + IcingaHealth::REQUIRED_ICINGADB_VERSION, + '<' + ) ) { $this->addHtml(Html::tag('div', ['class' => 'icinga-health down'], [ sprintf(