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

namespace Icinga\Module\Toplevelview\Tree;

class TLVStatus
{
    protected $properties = array(
        'critical_unhandled' => null,
        'critical_handled'   => null,
        'warning_unhandled'  => null,
        'warning_handled'    => null,
        'unknown_unhandled'  => null,
        'unknown_handled'    => null,
        'downtime_handled'   => null,
        'downtime_active'    => null,
        'ok'                 => null,
        'missing'            => null,
        'total'              => null,
    );

    protected static $statusPriority = array(
        'critical_unhandled',
        'warning_unhandled',
        'unknown_unhandled', // Note: old TLV ignored UNKNOWN basically
        'critical_handled',
        'warning_handled',
        'unknown_handled',
        'ok',
        'downtime_handled',
        'missing',
    );

    protected $meta = array();

    public function merge(TLVStatus $status)
    {
        $properties = $status->getProperties();
        foreach (array_keys($this->properties) as $key) {
            if ($this->properties[$key] === null) {
                $this->properties[$key] = $properties[$key];
            } else {
                $this->properties[$key] += $properties[$key];
            }
        }
        return $this;
    }

    public function get($key)
    {
        return $this->properties[$key];
    }

    public function set($key, $value)
    {
        $this->properties[$key] = (int) $value;
        return $this;
    }

    public function getProperties()
    {
        return $this->properties;
    }

    public function add($key, $value = 1)
    {
        if ($this->properties[$key] === null) {
            $this->properties[$key] = 0;
        }
        $this->properties[$key] += (int) $value;
        return $this;
    }

    public function zero()
    {
        foreach (array_keys($this->properties) as $key) {
            $this->properties[$key] = 0;
        }
        return $this;
    }

    public function getOverall()
    {
        foreach (static::$statusPriority as $key) {
            if ($this->properties[$key] !== null && $this->properties[$key] > 0) {
                return $this->cssFriendly($key);
            }
        }
        return 'missing';
    }

    protected function cssFriendly($key)
    {
        return str_replace('_', ' ', $key);
    }

    public function getMeta($key)
    {
        if (array_key_exists($key, $this->meta)) {
            return $this->meta[$key];
        } else {
            return null;
        }
    }

    public function getAllMeta()
    {
        return $this->meta;
    }

    public function setMeta($key, $value)
    {
        $this->meta[$key] = $value;
    }
}