2020-04-27 19:08:24 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Businessprocess\State;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Icinga\Application\Benchmark;
|
|
|
|
|
use Icinga\Module\Businessprocess\BpConfig;
|
2021-11-17 10:22:22 -05:00
|
|
|
use Icinga\Module\Businessprocess\IcingaDbObject;
|
2020-04-27 19:08:24 -04:00
|
|
|
use Icinga\Module\Businessprocess\ServiceNode;
|
2021-11-17 04:10:00 -05:00
|
|
|
use Icinga\Module\Icingadb\Common\Auth;
|
2020-04-27 19:08:24 -04:00
|
|
|
use Icinga\Module\Icingadb\Model\Host;
|
|
|
|
|
use Icinga\Module\Icingadb\Model\Service;
|
2022-02-15 04:20:58 -05:00
|
|
|
use ipl\Sql\Connection as IcingaDbConnection;
|
2020-04-27 19:08:24 -04:00
|
|
|
|
2021-11-17 09:07:33 -05:00
|
|
|
class IcingaDbState
|
2020-04-27 19:08:24 -04:00
|
|
|
{
|
2021-11-17 04:10:00 -05:00
|
|
|
use Auth;
|
|
|
|
|
|
2020-04-27 19:08:24 -04:00
|
|
|
/** @var BpConfig */
|
|
|
|
|
protected $config;
|
|
|
|
|
|
2022-02-15 04:20:58 -05:00
|
|
|
/** @var IcingaDbConnection */
|
2020-04-27 19:08:24 -04:00
|
|
|
protected $backend;
|
|
|
|
|
|
|
|
|
|
public function __construct(BpConfig $config)
|
|
|
|
|
{
|
|
|
|
|
$this->config = $config;
|
2022-02-10 10:03:09 -05:00
|
|
|
$this->backend = IcingaDbObject::fetchDb();
|
2020-04-27 19:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function apply(BpConfig $config)
|
|
|
|
|
{
|
|
|
|
|
$self = new static($config);
|
|
|
|
|
$self->retrieveStatesFromBackend();
|
2020-05-12 17:20:01 -04:00
|
|
|
|
2020-04-27 19:08:24 -04:00
|
|
|
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;
|
|
|
|
|
|
2020-05-04 03:35:10 -04:00
|
|
|
Benchmark::measure(sprintf(
|
2020-05-12 17:20:01 -04:00
|
|
|
'Retrieving states for business process %s using Icinga DB backend',
|
2020-05-04 03:35:10 -04:00
|
|
|
$config->getName()
|
|
|
|
|
));
|
2020-04-27 19:08:24 -04:00
|
|
|
|
|
|
|
|
$hosts = $config->listInvolvedHostNames();
|
|
|
|
|
if (empty($hosts)) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 09:55:49 -04:00
|
|
|
$queryHost = Host::on($this->backend)->with('state');
|
2021-11-17 10:22:22 -05:00
|
|
|
IcingaDbObject::applyIcingaDbRestrictions($queryHost);
|
2020-04-30 09:55:49 -04:00
|
|
|
|
2020-04-27 19:08:24 -04:00
|
|
|
$queryHost->getSelectBase()
|
|
|
|
|
->where(['host.name IN (?)' => $hosts]);
|
|
|
|
|
|
2021-11-17 09:07:33 -05:00
|
|
|
$hostObject = $queryHost->getModel()->getTableName();
|
2020-04-27 19:08:24 -04:00
|
|
|
|
2021-11-17 10:22:22 -05:00
|
|
|
IcingaDbObject::applyIcingaDbRestrictions($queryHost);
|
2020-04-27 19:08:24 -04:00
|
|
|
|
2021-11-17 09:07:33 -05:00
|
|
|
Benchmark::measure('Retrieved states for ' . $queryHost->count() . ' hosts in ' . $config->getName());
|
2020-04-27 19:08:24 -04:00
|
|
|
|
2020-04-30 09:55:49 -04:00
|
|
|
$queryService = Service::on($this->backend)->with([
|
2020-04-27 19:08:24 -04:00
|
|
|
'state',
|
|
|
|
|
'host',
|
|
|
|
|
'host.state'
|
|
|
|
|
]);
|
2021-11-17 09:07:33 -05:00
|
|
|
|
2020-04-27 19:08:24 -04:00
|
|
|
$queryService->getSelectBase()
|
|
|
|
|
->where(['service_host.name IN (?)' => $hosts]);
|
|
|
|
|
|
2021-11-17 10:22:22 -05:00
|
|
|
IcingaDbObject::applyIcingaDbRestrictions($queryService);
|
2020-04-27 19:08:24 -04:00
|
|
|
|
2021-11-17 09:07:33 -05:00
|
|
|
Benchmark::measure('Retrieved states for ' . $queryService->count() . ' services in ' . $config->getName());
|
2020-04-27 19:08:24 -04:00
|
|
|
|
|
|
|
|
$configs = $config->listInvolvedConfigs();
|
2021-11-17 09:07:33 -05:00
|
|
|
|
|
|
|
|
$serviceObject = $queryService->getModel()->getTableName();
|
2020-04-27 19:08:24 -04:00
|
|
|
|
|
|
|
|
foreach ($configs as $cfg) {
|
2021-11-17 09:07:33 -05:00
|
|
|
foreach ($queryService as $row) {
|
|
|
|
|
$this->handleDbRow($row, $cfg, $serviceObject);
|
2020-04-27 19:08:24 -04:00
|
|
|
}
|
2021-11-17 09:07:33 -05:00
|
|
|
foreach ($queryHost as $row) {
|
|
|
|
|
$this->handleDbRow($row, $cfg, $hostObject);
|
2020-04-27 19:08:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Benchmark::measure('Got states for business process ' . $config->getName());
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 09:07:33 -05:00
|
|
|
protected function handleDbRow($row, BpConfig $config, $objectName)
|
2020-04-27 19:08:24 -04:00
|
|
|
{
|
2021-11-17 09:07:33 -05:00
|
|
|
if ($objectName === 'service') {
|
|
|
|
|
$key = $row->host->name . ';' . $row->name;
|
2020-04-27 19:08:24 -04:00
|
|
|
} else {
|
2021-11-17 09:07:33 -05:00
|
|
|
$key = $row->name . ';Hoststatus';
|
2020-04-27 19:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We fetch more states than we need, so skip unknown ones
|
|
|
|
|
if (! $config->hasNode($key)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$node = $config->getNode($key);
|
|
|
|
|
|
2021-11-17 09:07:33 -05:00
|
|
|
if ($this->config->usesHardStates()) {
|
|
|
|
|
if ($row->state->hard_state !== null) {
|
|
|
|
|
$node->setState($row->state->hard_state)->setMissing(false);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ($row->state->soft_state !== null) {
|
|
|
|
|
$node->setState($row->state->soft_state)->setMissing(false);
|
|
|
|
|
}
|
2020-04-27 19:08:24 -04:00
|
|
|
}
|
2021-11-17 09:07:33 -05:00
|
|
|
|
|
|
|
|
if ($row->state->last_state_change !== null) {
|
|
|
|
|
$node->setLastStateChange($row->state->last_state_change/1000);
|
2020-04-27 19:08:24 -04:00
|
|
|
}
|
2021-11-17 09:07:33 -05:00
|
|
|
if ($row->state->in_downtime === 'y') {
|
2020-04-27 19:08:24 -04:00
|
|
|
$node->setDowntime(true);
|
|
|
|
|
}
|
2021-11-17 09:07:33 -05:00
|
|
|
if ($row->state->is_acknowledged !== 'n') {
|
2020-04-27 19:08:24 -04:00
|
|
|
$node->setAck(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$node->setAlias($row->display_name);
|
|
|
|
|
|
|
|
|
|
if ($node instanceof ServiceNode) {
|
2021-11-17 09:07:33 -05:00
|
|
|
$node->setHostAlias($row->host->display_name);
|
2020-04-27 19:08:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|