summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/library/Monitoring/Cli/CliUtils.php
blob: 3d7d3ee32d7303f7f40bd11dac60d2887fbc1594 (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
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Cli;

use Icinga\Cli\Screen;

class CliUtils
{
    protected $hostColors = array(
        0 => array('black', 'lightgreen'),
        1 => array('black', 'lightred'),
        2 => array('black', 'brown'),
        99 => array('black', 'lightgray'),
    );
    protected $serviceColors = array(
        0 => array('black', 'lightgreen'),
        1 => array('black', 'yellow'),
        2 => array('black', 'lightred'),
        3 => array('black', 'lightpurple'),
        99 => array('black', 'lightgray'),
    );
    protected $hostStates = array(
        0 => 'UP',
        1 => 'DOWN',
        2 => 'UNREACHABLE',
        99 => 'PENDING',
    );

    protected $serviceStates = array(
        0 => 'OK',
        1 => 'WARNING',
        2 => 'CRITICAL',
        3 => 'UNKNOWN',
        99 => 'PENDING',
    );

    protected $screen;
    protected $hostState;
    protected $serviceState;

    public function __construct(Screen $screen)
    {
        $this->screen = $screen;
    }

    public function setHostState($state)
    {
        $this->hostState = $state;
    }

    public function setServiceState($state)
    {
        $this->serviceState = $state;
    }

    public function shortHostState($state = null)
    {
        if ($state === null) {
            $state = $this->hostState;
        }
        return sprintf('%-4s', substr($this->hostStates[$state], 0, 4));
    }

    public function shortServiceState($state = null)
    {
        if ($state === null) {
            $state = $this->serviceState;
        }
        return sprintf('%-4s', substr($this->serviceStates[$state], 0, 4));
    }

    public function hostStateBackground($text, $state = null)
    {
        if ($state === null) {
            $state = $this->hostState;
        }
        return $this->screen->colorize(
            $text,
            $this->hostColors[$state][0],
            $this->hostColors[$state][1]
        );
    }

    public function serviceStateBackground($text, $state = null)
    {
        if ($state === null) {
            $state = $this->serviceState;
        }
        return $this->screen->colorize(
            $text,
            $this->serviceColors[$state][0],
            $this->serviceColors[$state][1]
        );
    }

    public function objectStateFlags($type, &$row)
    {
        $extra = array();
        if ($row->{$type . '_in_downtime'}) {
            if ($this->screen->hasUtf8()) {
                $extra[] = 'DOWNTIME ⌚';
            } else {
                $extra[] = 'DOWNTIME';
            }
        }
        if ($row->{$type . '_acknowledged'}) {
            if ($this->screen->hasUtf8()) {
                $extra[] = 'ACK ✓';
            } else {
                $extra[] = 'ACK';
            }
        }

        if (empty($extra)) {
            $extra = '';
        } else {
            $extra = sprintf(' [ %s ]', implode(', ', $extra));
        }
        return $extra;
    }
}