Skip to content

Commit

Permalink
Merge pull request #561 from Icinga/check-redis-availability-first
Browse files Browse the repository at this point in the history
ApplicationState: Check the availability of `IcingaRedis` first
  • Loading branch information
nilmerg authored Jun 30, 2022
2 parents 8864d9d + 1267e18 commit 37324f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
101 changes: 52 additions & 49 deletions library/Icingadb/ProvidedHook/ApplicationState.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ class ApplicationState extends ApplicationStateHook

public function collectMessages()
{
try {
$lastIcingaHeartbeat = IcingaRedis::getLastIcingaHeartbeat();
} catch (Exception $e) {
$downSince = Session::getSession()->getNamespace('icingadb')->get('redis.down-since');

if ($downSince === null) {
$downSince = time();
Session::getSession()->getNamespace('icingadb')->set('redis.down-since', $downSince);
}

$this->addError(
'icingadb/redis-down',
$downSince,
sprintf(t("Can't connect to Icinga Redis: %s"), $e->getMessage())
);

return;
}

$instance = Instance::on($this->getDb())
->with(['endpoint'])
->filter(Filter::equal('responsible', true))
Expand Down Expand Up @@ -50,59 +69,43 @@ public function collectMessages()

$outdatedDbHeartbeat = $instance->heartbeat < time() - 60;

try {
$lastIcingaHeartbeat = IcingaRedis::getLastIcingaHeartbeat();
if ($lastIcingaHeartbeat === null) {
$missingSince = Session::getSession()
->getNamespace('icingadb')->get('redis.heartbeat-missing-since');

if ($missingSince === null) {
$missingSince = time();
Session::getSession()
->getNamespace('icingadb')->set('redis.heartbeat-missing-since', $missingSince);
}

$lastIcingaHeartbeat = $missingSince;
} else {
Session::getSession()->getNamespace('icingadb')->delete('redis.heartbeat-missing-since');
}
if ($lastIcingaHeartbeat === null) {
$missingSince = Session::getSession()
->getNamespace('icingadb')->get('redis.heartbeat-missing-since');

switch (true) {
case $outdatedDbHeartbeat && $instance->heartbeat > $lastIcingaHeartbeat:
$this->addError(
'icingadb/redis-outdated',
$lastIcingaHeartbeat,
t('Icinga Redis is outdated. Make sure Icinga 2 is running and connected to Redis.')
);

break;
case $outdatedDbHeartbeat:
$this->addError(
'icingadb/icingadb-down',
$instance->heartbeat,
t(
'It seems that Icinga DB is not running.'
. ' Make sure Icinga DB is running and writing into the database.'
)
);

break;
if ($missingSince === null) {
$missingSince = time();
Session::getSession()
->getNamespace('icingadb')->set('redis.heartbeat-missing-since', $missingSince);
}

Session::getSession()->getNamespace('icingadb')->delete('redis.down-since');
} catch (Exception $e) {
$downSince = Session::getSession()->getNamespace('icingadb')->get('redis.down-since');

if ($downSince === null) {
$downSince = time();
Session::getSession()->getNamespace('icingadb')->set('redis.down-since', $downSince);
}
$lastIcingaHeartbeat = $missingSince;
} else {
Session::getSession()->getNamespace('icingadb')->delete('redis.heartbeat-missing-since');
}

$this->addError(
'icingadb/redis-down',
$downSince,
sprintf(t("Can't connect to Icinga Redis: %s"), $e->getMessage())
);
switch (true) {
case $outdatedDbHeartbeat && $instance->heartbeat > $lastIcingaHeartbeat:
$this->addError(
'icingadb/redis-outdated',
$lastIcingaHeartbeat,
t('Icinga Redis is outdated. Make sure Icinga 2 is running and connected to Redis.')
);

break;
case $outdatedDbHeartbeat:
$this->addError(
'icingadb/icingadb-down',
$instance->heartbeat,
t(
'It seems that Icinga DB is not running.'
. ' Make sure Icinga DB is running and writing into the database.'
)
);

break;
}

Session::getSession()->getNamespace('icingadb')->delete('redis.down-since');
}
}
2 changes: 1 addition & 1 deletion library/Icingadb/ProvidedHook/RedisHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function checkHealth()
$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->setState(self::STATE_CRITICAL);
$this->setMessage(sprintf(t("Can't connect to Icinga Redis: %s"), $e->getMessage()));
}
}
Expand Down

0 comments on commit 37324f3

Please sign in to comment.