summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/ProvidedHook/RedisHealth.php
blob: 1471abaf6066ca7cfe4160bb1789d592902c7fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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->getTimestamp() < time() - 60;
            if (! $outdatedDbHeartbeat || $instance->heartbeat->getTimestamp() <= $lastIcingaHeartbeat) {
                $this->setState(self::STATE_OK);
                $this->setMessage(t('Icinga Redis available and up to date.'));
            } elseif ($instance->heartbeat->getTimestamp() > $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()));
        }
    }
}