summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Model/State.php
blob: 4529c9b1010b5771a2540e859b08d2c14c415a62 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Model;

use DateTime;
use Icinga\Module\Icingadb\Common\Icons;
use Icinga\Module\Icingadb\Model\Behavior\BoolCast;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behavior\MillisecondTimestamp;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Web\Widget\Icon;

/**
 * Base class for the {@link HostState} and {@link ServiceState} models providing common columns.
 *
 * @property string $id
 * @property string $environment_id The environment id
 * @property string $state_type The state type (hard or soft)
 * @property int $soft_state The current soft state code (0 = OK, 1 = WARNING, 2 = CRITICAL, 3 = UNKNOWN)
 * @property int $hard_state The current hard state code (0 = OK, 1 = WARNING, 2 = CRITICAL, 3 = UNKNOWN)
 * @property int $previous_soft_state The previous soft state code (0 = OK, 1 = WARNING, 2 = CRITICAL, 3 = UNKNOWN)
 * @property int $previous_hard_state The previous hard state code (0 = OK, 1 = WARNING, 2 = CRITICAL, 3 = UNKNOWN)
 * @property int $check_attempt The check attempt count
 * @property int $severity The calculated severity
 * @property ?string $output The check output
 * @property ?string $long_output The long check output
 * @property ?string $performance_data The performance data
 * @property ?string $normalized_performance_data The normalized performance data (converted ms to s, GiB to byte etc.)
 * @property ?string $check_commandline The executed check command
 * @property bool $is_problem Whether in non-OK state
 * @property bool $is_handled Whether the state is handled
 * @property bool $is_reachable Whether the node is reachable
 * @property bool $is_flapping Whether the state is flapping
 * @property bool $is_overdue Whether the check is overdue
 * @property bool|string $is_acknowledged Whether the state is acknowledged (bool), can also be `sticky` (string)
 * @property ?string $acknowledgement_comment_id The id of acknowledgement comment
 * @property ?string $last_comment_id The id of last comment
 * @property bool $in_downtime Whether the node is in downtime
 * @property ?int $execution_time The check execution time
 * @property ?int $latency The check latency
 * @property ?int $check_timeout The check timeout
 * @property ?string $check_source The name of the node that executes the check
 * @property ?string $scheduling_source The name of the node that schedules the check
 * @property ?DateTime $last_update The time when the node was last updated
 * @property DateTime $last_state_change The time when the node last got a status change
 * @property DateTime $next_check The time when the node will execute the next check
 * @property DateTime $next_update The time when the next check of the node is expected to end
 */
abstract class State extends Model
{
    /**
     * Get the state as the textual representation
     *
     * @return string
     */
    abstract public function getStateText(): string;

    /**
     * Get the state as the translated textual representation
     *
     * @return string
     */
    abstract public function getStateTextTranslated(): string;

    public function getColumns()
    {
        return [
            'environment_id',
            'state_type',
            'soft_state',
            'hard_state',
            'previous_soft_state',
            'previous_hard_state',
            'check_attempt',
            'severity',
            'output',
            'long_output',
            'performance_data',
            'normalized_performance_data',
            'check_commandline',
            'is_problem',
            'is_handled',
            'is_reachable',
            'is_flapping',
            'is_overdue',
            'is_acknowledged',
            'acknowledgement_comment_id',
            'last_comment_id',
            'in_downtime',
            'execution_time',
            'latency',
            'check_timeout',
            'check_source',
            'scheduling_source',
            'last_update',
            'last_state_change',
            'next_check',
            'next_update'
        ];
    }

    public function createBehaviors(Behaviors $behaviors)
    {
        $behaviors->add(new BoolCast([
            'is_problem',
            'is_handled',
            'is_reachable',
            'is_flapping',
            'is_overdue',
            'is_acknowledged',
            'in_downtime'
        ]));

        $behaviors->add(new MillisecondTimestamp([
            'last_update',
            'last_state_change',
            'next_check',
            'next_update'
        ]));

        $behaviors->add(new Binary([
            $this->getKeyName(),
            'environment_id',
            'acknowledgement_comment_id',
            'last_comment_id'
        ]));
    }

    /**
     * Get the state icon
     *
     * @return Icon|null
     */
    public function getIcon(): ?Icon
    {
        $icon = null;
        switch (true) {
            case $this->is_acknowledged:
                $icon = new Icon(Icons::IS_ACKNOWLEDGED);

                break;
            case $this->in_downtime:
                $icon = new Icon(
                    Icons::IN_DOWNTIME,
                    ['title' => sprintf(
                        '%s (%s)',
                        strtoupper($this->getStateTextTranslated()),
                        $this->is_handled ? t('handled by Downtime') : t('in Downtime')
                    )]
                );

                break;
            case $this->is_flapping:
                $icon = new Icon(Icons::IS_FLAPPING);

                break;
            case ! $this->is_reachable:
                $icon = new Icon(Icons::HOST_DOWN, [
                    'title' => sprintf(
                        '%s (%s)',
                        strtoupper($this->getStateTextTranslated()),
                        t('is unreachable')
                    )
                ]);

                break;
            case $this->is_handled:
                $icon = new Icon(Icons::HOST_DOWN);

                break;
        }

        return $icon;
    }
}