diff --git a/library/Icinga/Common/HealthBadgeTrait.php b/library/Icinga/Common/HealthBadgeTrait.php new file mode 100644 index 000000000..dc01122dc --- /dev/null +++ b/library/Icinga/Common/HealthBadgeTrait.php @@ -0,0 +1,79 @@ +getHealthCount() > 0) { + $stateBadge = new StateBadge($this->getHealthCount(), $this->state); + $stateBadge->addAttributes(['class' => 'disabled', 'title' => $this->title]); + } + + return $stateBadge; + } + + /** + * Get the number of health problems + * + * @return int + */ + protected function getHealthCount():int + { + $count = 0; + $title = null; + $worstState = null; + foreach (HealthHook::collectHealthData()->select() as $result) { + if ($worstState === null || $result->state > $worstState) { + $worstState = $result->state; + $title = $result->message; + $count = 1; + } elseif ($worstState === $result->state) { + $count++; + } + } + + switch ($worstState) { + case HealthHook::STATE_OK: + $count = 0; + break; + case HealthHook::STATE_WARNING: + $this->state = $this->STATE_WARNING; + break; + case HealthHook::STATE_CRITICAL: + $this->state = $this->STATE_CRITICAL; + break; + case HealthHook::STATE_UNKNOWN: + $this->state = $this->STATE_UNKNOWN; + break; + } + + $this->title = $title; + + return $count; + } +}