Introduce hook RedisHealth

This commit is contained in:
Johannes Meyer 2021-04-20 14:58:25 +02:00
parent 7a7ecd22e9
commit 54a1b20aea
2 changed files with 52 additions and 0 deletions

View file

@ -0,0 +1,51 @@
<?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;
use IcingaRedis;
public function getName()
{
return 'Icinga Redis';
}
public function checkHealth()
{
try {
$redis = $this->getIcingaRedis();
$lastIcingaHeartbeat = $this->getLastIcingaHeartbeat($redis);
if ($lastIcingaHeartbeat === false) {
$this->setState(self::STATE_UNKNOWN);
$this->setMessage(t("Can't connect to Icinga Redis: phpredis>=4.3.0 required"));
return;
} elseif ($lastIcingaHeartbeat === null) {
$lastIcingaHeartbeat = time();
}
$instance = Instance::on($this->getDb())->columns('heartbeat')->first();
$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_UNKNOWN);
$this->setMessage(sprintf(t("Can't connect to Icinga Redis: %s"), $e->getMessage()));
}
}
}

View file

@ -7,3 +7,4 @@
$this->provideHook('ApplicationState');
$this->provideHook('X509/Sni');
$this->provideHook('health', 'IcingaHealth');
$this->provideHook('health', 'RedisHealth');