summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/State/IcingaDbState.php
blob: f33d4a4ede8ec08b2070a032410c0da3a2172b49 (plain)
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
<?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\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;

        Benchmark::measure(sprintf(
            'Retrieving states for business process %s using Icinga DB backend',
            $config->getName()
        ));

        $hosts = $config->listInvolvedHostNames();
        if (empty($hosts)) {
            return $this;
        }

        $queryHost = Host::on($this->backend)->with('state');
        IcingaDbObject::applyIcingaDbRestrictions($queryHost);

        $queryHost->filter(Filter::equal('host.name', $hosts));

        $hostObject = $queryHost->getModel()->getTableName();

        Benchmark::measure('Retrieved states for ' . $queryHost->count() . ' hosts in ' . $config->getName());

        $queryService = Service::on($this->backend)->with([
            'state',
            'host',
            'host.state'
        ]);

        $queryService->filter(Filter::equal('host.name', $hosts));

        IcingaDbObject::applyIcingaDbRestrictions($queryService);

        Benchmark::measure('Retrieved states for ' . $queryService->count() . ' services in ' . $config->getName());

        $configs = $config->listInvolvedConfigs();

        $serviceObject = $queryService->getModel()->getTableName();

        foreach ($configs as $cfg) {
            foreach ($queryService as $row) {
                $this->handleDbRow($row, $cfg, $serviceObject);
            }
            foreach ($queryHost as $row) {
                $this->handleDbRow($row, $cfg, $hostObject);
            }
        }

        Benchmark::measure('Got states for business process ' . $config->getName());

        return $this;
    }

    protected function handleDbRow($row, BpConfig $config, $objectName)
    {
        if ($objectName === 'service') {
            $key = $row->host->name . ';' . $row->name;
        } else {
            $key = $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->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);
            }
        }

        if ($row->state->last_state_change !== null) {
            $node->setLastStateChange($row->state->last_state_change/1000);
        }
        if ($row->state->in_downtime) {
            $node->setDowntime(true);
        }
        if ($row->state->is_acknowledged) {
            $node->setAck(true);
        }

        $node->setAlias($row->display_name);

        if ($node instanceof ServiceNode) {
            $node->setHostAlias($row->host->display_name);
        }
    }
}