From 54a1b20aea8464709794e693a5eebdd33f60366c Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 20 Apr 2021 14:58:25 +0200 Subject: [PATCH] Introduce hook `RedisHealth` --- library/Icingadb/ProvidedHook/RedisHealth.php | 51 +++++++++++++++++++ run.php | 1 + 2 files changed, 52 insertions(+) create mode 100644 library/Icingadb/ProvidedHook/RedisHealth.php diff --git a/library/Icingadb/ProvidedHook/RedisHealth.php b/library/Icingadb/ProvidedHook/RedisHealth.php new file mode 100644 index 00000000..6e2dc1c1 --- /dev/null +++ b/library/Icingadb/ProvidedHook/RedisHealth.php @@ -0,0 +1,51 @@ +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())); + } + } +} diff --git a/run.php b/run.php index 79dc5c11..d5af9d46 100644 --- a/run.php +++ b/run.php @@ -7,3 +7,4 @@ $this->provideHook('ApplicationState'); $this->provideHook('X509/Sni'); $this->provideHook('health', 'IcingaHealth'); +$this->provideHook('health', 'RedisHealth');