diff options
Diffstat (limited to '')
-rw-r--r-- | library/Icingadb/ProvidedHook/RedisHealth.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/library/Icingadb/ProvidedHook/RedisHealth.php b/library/Icingadb/ProvidedHook/RedisHealth.php new file mode 100644 index 0000000..9e71154 --- /dev/null +++ b/library/Icingadb/ProvidedHook/RedisHealth.php @@ -0,0 +1,55 @@ +<?php + +// Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 + +namespace Icinga\Module\Icingadb\ProvidedHook; + +use Exception; +use Icinga\Application\Hook\HealthHook; +use Icinga\Module\Icingadb\Common\Database; +use Icinga\Module\Icingadb\Common\IcingaRedis; +use Icinga\Module\Icingadb\Model\Instance; + +class RedisHealth extends HealthHook +{ + use Database; + + public function getName(): string + { + return 'Icinga Redis'; + } + + public function checkHealth() + { + try { + $lastIcingaHeartbeat = IcingaRedis::getLastIcingaHeartbeat(); + if ($lastIcingaHeartbeat === null) { + $lastIcingaHeartbeat = time(); + } + + $instance = Instance::on($this->getDb())->columns('heartbeat')->first(); + + if ($instance === null) { + $this->setState(self::STATE_UNKNOWN); + $this->setMessage(t( + 'Can\'t check Icinga Redis: Icinga DB is not running or not writing into the database' + . ' (make sure the icinga feature "icingadb" is enabled)' + )); + + return; + } + + $outdatedDbHeartbeat = $instance->heartbeat < time() - 60; + if (! $outdatedDbHeartbeat || $instance->heartbeat <= $lastIcingaHeartbeat) { + $this->setState(self::STATE_OK); + $this->setMessage(t('Icinga Redis available and up to date.')); + } elseif ($instance->heartbeat > $lastIcingaHeartbeat) { + $this->setState(self::STATE_CRITICAL); + $this->setMessage(t('Icinga Redis outdated. Make sure Icinga 2 is running and connected to Redis.')); + } + } catch (Exception $e) { + $this->setState(self::STATE_CRITICAL); + $this->setMessage(sprintf(t("Can't connect to Icinga Redis: %s"), $e->getMessage())); + } + } +} |