From a6033491f42c99df72e338a0729f6403bc3393be Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 30 Jun 2025 10:04:19 +0200 Subject: [PATCH] Do not break sidebar if processes cannot be loaded fixes #466 --- .../Renderer/ProcessProblemsBadge.php | 29 ++++++++++----- .../Renderer/ProcessesProblemsBadge.php | 37 +++++++++++-------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/library/Businessprocess/Web/Navigation/Renderer/ProcessProblemsBadge.php b/library/Businessprocess/Web/Navigation/Renderer/ProcessProblemsBadge.php index 23a61f7..3794422 100644 --- a/library/Businessprocess/Web/Navigation/Renderer/ProcessProblemsBadge.php +++ b/library/Businessprocess/Web/Navigation/Renderer/ProcessProblemsBadge.php @@ -3,8 +3,10 @@ namespace Icinga\Module\Businessprocess\Web\Navigation\Renderer; use Icinga\Module\Businessprocess\Node; +use Icinga\Application\Logger; use Icinga\Module\Businessprocess\Storage\LegacyStorage; use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer; +use Throwable; class ProcessProblemsBadge extends BadgeNavigationItemRenderer { @@ -20,22 +22,29 @@ class ProcessProblemsBadge extends BadgeNavigationItemRenderer public function getCount() { - $count = 0; if ($this->count === null) { - $storage = LegacyStorage::getInstance(); - $bp = $storage->loadProcess($this->getBpConfigName()); - foreach ($bp->getRootNodes() as $rootNode) { - if (! $rootNode->isEmpty() && - ! in_array($rootNode->getState(), [Node::ICINGA_OK, Node::ICINGA_PENDING], true)) { - $count++; + $count = 0; + + try { + $storage = LegacyStorage::getInstance(); + + // State is not applied here, because it's already done in ProcessesProblemsBadge. + // It runs earlier as it is part of the parent menu entry. Do we rely on an implementation detail here? + // Probably, but it is how it is for a very long time. So it is unlikely to change. (Finger crossed.) + $bp = $storage->loadProcess($this->getBpConfigName()); + foreach ($bp->getRootNodes() as $rootNode) { + if (! $rootNode->isEmpty() && + ! in_array($rootNode->getState(), [Node::ICINGA_OK, Node::ICINGA_PENDING], true)) { + $count++; + } } + } catch (Throwable $e) { + Logger::error('Failed to load business process "%s": %s', $this->getBpConfigName(), $e); } $this->count = $count; - $this->setState(self::STATE_CRITICAL); - } - if ($count) { + $this->setState(self::STATE_CRITICAL); $this->setTitle(sprintf( tp('One unhandled root node critical', '%d unhandled root nodes critical', $count), $count diff --git a/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php b/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php index ad3215c..a1fc075 100644 --- a/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php +++ b/library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Web\Navigation\Renderer; +use Icinga\Application\Logger; use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\Node; use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; @@ -9,6 +10,7 @@ use Icinga\Module\Businessprocess\State\IcingaDbState; use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\LegacyStorage; use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer; +use Throwable; class ProcessesProblemsBadge extends BadgeNavigationItemRenderer { @@ -22,26 +24,31 @@ class ProcessesProblemsBadge extends BadgeNavigationItemRenderer public function getCount() { if ($this->count === null) { - $storage = LegacyStorage::getInstance(); $count = 0; - foreach ($storage->listProcessNames() as $processName) { - $bp = $storage->loadProcess($processName); - if (Module::exists('icingadb') && - (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($bp); - } else { - MonitoringState::apply($bp); - } + try { + $storage = LegacyStorage::getInstance(); - foreach ($bp->getRootNodes() as $rootNode) { - if (! $rootNode->isEmpty() && - ! in_array($rootNode->getState(), [Node::ICINGA_OK, Node::ICINGA_PENDING], true)) { - $count++; - break; + foreach ($storage->listProcessNames() as $processName) { + $bp = $storage->loadProcess($processName); + if (Module::exists('icingadb') && + (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) + ) { + IcingaDbState::apply($bp); + } else { + MonitoringState::apply($bp); + } + + foreach ($bp->getRootNodes() as $rootNode) { + if (! $rootNode->isEmpty() && + ! in_array($rootNode->getState(), [Node::ICINGA_OK, Node::ICINGA_PENDING], true)) { + $count++; + break; + } } } + } catch (Throwable $e) { + Logger::error('Failed to load business processes: %s', $e); } $this->count = $count;