summaryrefslogtreecommitdiffstats
path: root/library/Toplevelview/Tree/TLVHostNode.php
blob: f03ae475aff8a6a5527d6ef5deda1a449339f509 (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
<?php
/* Copyright (C) 2017 Icinga Development Team <info@icinga.com> */

namespace Icinga\Module\Toplevelview\Tree;

use Icinga\Application\Benchmark;
use Icinga\Exception\NotFoundError;

class TLVHostNode extends TLVIcingaNode
{
    protected $type = 'host';

    protected $key = 'host';

    protected static $titleKey = 'host';

    public static function fetch(TLVTree $root)
    {
        Benchmark::measure('Begin fetching hosts');

        if (! array_key_exists('host', $root->registeredObjects) or empty($root->registeredObjects['host'])) {
            throw new NotFoundError('No hosts registered to fetch!');
        }

        $names = array_keys($root->registeredObjects['host']);

        $hosts = $root->getBackend()->select()
            ->from('hoststatus', array(
                'host_name',
                'host_hard_state',
                'host_handled',
                'host_in_downtime',
                'host_notifications_enabled',
            ))
            ->where('host_name', $names);

        foreach ($hosts as $host) {
            $root->registeredObjects['host'][$host->host_name] = $host;
        }

        Benchmark::measure('Finished fetching hosts');
    }

    public function getStatus()
    {
        if ($this->status === null) {
            $this->status = $status = new TLVStatus();
            $key = $this->getKey();

            if (($data = $this->root->getFetched($this->type, $key)) !== null) {
                $status->zero();
                $status->add('total');

                $state = $data->host_hard_state;

                if ($data->host_in_downtime > 0 || $data->host_notifications_enabled === '0') {
                    $status->add('downtime_active');
                    $state = '10';
                    $handled = '';
                } elseif ($data->host_handled === '1' || $this->getRoot()->get('host_never_unhandled') === true) {
                    $handled = '_handled';
                } else {
                    $handled = '_unhandled';
                }

                if ($state === '0') {
                    $status->add('ok');
                } elseif ($state === '1' || $state === '2') {
                    $status->add('critical' . $handled);
                } elseif ($state === '10') {
                    $status->add('downtime_handled');
                } else {
                    $status->add('unknown_handled');
                }
            } else {
                $status->add('missing', 1);
            }
        }
        return $this->status;
    }
}