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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
<?php
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Icingadb\ProvidedHook;
use Exception;
use Icinga\Application\Hook\ApplicationStateHook;
use Icinga\Module\Icingadb\Common\Database;
use Icinga\Module\Icingadb\Common\IcingaRedis;
use Icinga\Module\Icingadb\Model\Instance;
use Icinga\Web\Session;
use ipl\Stdlib\Filter;
class ApplicationState extends ApplicationStateHook
{
use Database;
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))
->orderBy('heartbeat', 'desc')
->first();
if ($instance === null) {
$noInstanceSince = Session::getSession()
->getNamespace('icingadb')->get('icingadb.no-instance-since');
if ($noInstanceSince === null) {
$noInstanceSince = time();
Session::getSession()
->getNamespace('icingadb')->set('icingadb.no-instance-since', $noInstanceSince);
}
$this->addError(
'icingadb/no-instance',
$noInstanceSince,
t(
'It seems that Icinga DB is not running.'
. ' Make sure Icinga DB is running and writing into the database.'
)
);
return;
} else {
Session::getSession()->getNamespace('icingadb')->delete('db.no-instance-since');
}
$outdatedDbHeartbeat = $instance->heartbeat < time() - 60;
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');
}
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');
}
}
|