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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
<?php
namespace Icinga\Module\Businessprocess\State;
use Exception;
use Icinga\Application\Benchmark;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\IcingaDbObject;
use Icinga\Module\Businessprocess\ServiceNode;
use Icinga\Module\Icingadb\Common\IcingaRedis;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use ipl\Sql\Connection as IcingaDbConnection;
use ipl\Stdlib\Filter;
class IcingaDbState
{
/** @var BpConfig */
protected $config;
/** @var IcingaDbConnection */
protected $backend;
public function __construct(BpConfig $config)
{
$this->config = $config;
$this->backend = IcingaDbObject::fetchDb();
}
public static function apply(BpConfig $config)
{
$self = new static($config);
$self->retrieveStatesFromBackend();
return $config;
}
public function retrieveStatesFromBackend()
{
$config = $this->config;
try {
$this->reallyRetrieveStatesFromBackend();
} catch (Exception $e) {
$config->addError(
$config->translate('Could not retrieve process state: %s'),
$e->getMessage()
);
}
}
public function reallyRetrieveStatesFromBackend()
{
$config = $this->config;
$involvedHostNames = $config->listInvolvedHostNames();
if (empty($involvedHostNames)) {
return $this;
}
Benchmark::measure(sprintf(
'Retrieving states for business process %s using Icinga DB backend',
$config->getName()
));
$hosts = Host::on($this->backend)->columns([
'id' => 'host.id',
'name' => 'host.name',
'display_name' => 'host.display_name',
'hard_state' => 'host.state.hard_state',
'soft_state' => 'host.state.soft_state',
'last_state_change' => 'host.state.last_state_change',
'in_downtime' => 'host.state.in_downtime',
'is_acknowledged' => 'host.state.is_acknowledged'
])->filter(Filter::equal('host.name', $involvedHostNames));
$services = Service::on($this->backend)->columns([
'id' => 'service.id',
'name' => 'service.name',
'display_name' => 'service.display_name',
'host_name' => 'host.name',
'host_display_name' => 'host.display_name',
'hard_state' => 'service.state.hard_state',
'soft_state' => 'service.state.soft_state',
'last_state_change' => 'service.state.last_state_change',
'in_downtime' => 'service.state.in_downtime',
'is_acknowledged' => 'service.state.is_acknowledged'
])->filter(Filter::equal('host.name', $involvedHostNames));
// All of this is ipl-sql now, for performance reasons
foreach ($config->listInvolvedConfigs() as $cfg) {
$serviceIds = [];
$serviceResults = [];
foreach ($this->backend->yieldAll($services->assembleSelect()) as $row) {
$row->hex_id = bin2hex(is_resource($row->id) ? stream_get_contents($row->id) : $row->id);
$serviceIds[] = $row->hex_id;
$serviceResults[] = $row;
}
$redisServiceResults = iterator_to_array(IcingaRedis::fetchServiceState($serviceIds, [
'hard_state',
'soft_state',
'last_state_change',
'in_downtime',
'is_acknowledged'
]));
foreach ($serviceResults as $row) {
if (isset($redisServiceResults[$row->hex_id])) {
$row = (object) array_merge(
(array) $row,
$redisServiceResults[$row->hex_id]
);
}
$this->handleDbRow($row, $cfg, 'service');
}
Benchmark::measure('Retrieved states for ' . count($serviceIds) . ' services in ' . $config->getName());
$hostIds = [];
$hostResults = [];
foreach ($this->backend->yieldAll($hosts->assembleSelect()) as $row) {
$row->hex_id = bin2hex(is_resource($row->id) ? stream_get_contents($row->id) : $row->id);
$hostIds[] = $row->hex_id;
$hostResults[] = $row;
}
$redisHostResults = iterator_to_array(IcingaRedis::fetchHostState($hostIds, [
'hard_state',
'soft_state',
'last_state_change',
'in_downtime',
'is_acknowledged'
]));
foreach ($hostResults as $row) {
if (isset($redisHostResults[$row->hex_id])) {
$row = (object) array_merge(
(array) $row,
$redisHostResults[$row->hex_id]
);
}
$this->handleDbRow($row, $cfg, 'host');
}
Benchmark::measure('Retrieved states for ' . count($hostIds) . ' hosts in ' . $config->getName());
}
Benchmark::measure('Got states for business process ' . $config->getName());
return $this;
}
protected function handleDbRow($row, BpConfig $config, $type)
{
if ($type === 'service') {
$key = BpConfig::joinNodeName($row->host_name, $row->name);
} else {
$key = BpConfig::joinNodeName($row->name, 'Hoststatus');
}
// We fetch more states than we need, so skip unknown ones
if (! $config->hasNode($key)) {
return;
}
$node = $config->getNode($key);
if ($this->config->usesHardStates()) {
if ($row->hard_state !== null) {
$node->setState($row->hard_state)->setMissing(false);
}
} else {
if ($row->soft_state !== null) {
$node->setState($row->soft_state)->setMissing(false);
}
}
if ($row->last_state_change !== null) {
$node->setLastStateChange($row->last_state_change / 1000.0);
}
$node->setDowntime($this->toBoolean($row->in_downtime));
$node->setAck($this->toBoolean($row->is_acknowledged));
$node->setAlias($row->display_name);
if ($node instanceof ServiceNode) {
$node->setHostAlias($row->host_display_name);
}
}
private function toBoolean($value): bool
{
switch (true) {
// Icinga Redis >= 6 (is_acknowledged, in_downtime)
case $value === true:
case $value === false:
return $value;
// Icinga Redis < 6 (is_acknowledged)
case $value === 1:
case $value === 2:
return true;
case $value === 0:
return false;
// Icinga DB < 1.4 (is_acknowledged)
case $value === 'sticky':
return true;
// Icinga DB >= * (is_acknowledged, in_downtime)
case $value === 'y':
return true;
case $value === 'n':
return false;
}
return false;
}
}
|