summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Application/Hook/HealthHook.php
blob: f6420b5ac1618779a5a4230b7cad935d9eaa4ace (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/* Icinga Web 2 | (c) 2021 Icinga GmbH | GPLv2+ */

namespace Icinga\Application\Hook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Exception\IcingaException;
use ipl\Web\Url;
use LogicException;

abstract class HealthHook
{
    /** @var int */
    const STATE_OK = 0;

    /** @var int */
    const STATE_WARNING = 1;

    /** @var int */
    const STATE_CRITICAL = 2;

    /** @var int */
    const STATE_UNKNOWN = 3;

    /** @var int The overall state */
    protected $state;

    /** @var string Message describing the overall state */
    protected $message;

    /** @var array Available metrics */
    protected $metrics;

    /** @var Url Url to a graphical representation of the available metrics */
    protected $url;

    /**
     * Get overall state
     *
     * @return int
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * Set overall state
     *
     * @param int $state
     *
     * @return $this
     */
    public function setState($state)
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get the message describing the overall state
     *
     * @return string
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * Set the message describing the overall state
     *
     * @param string $message
     *
     * @return $this
     */
    public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

    /**
     * Get available metrics
     *
     * @return array
     */
    public function getMetrics()
    {
        return $this->metrics;
    }

    /**
     * Set available metrics
     *
     * @param array $metrics
     *
     * @return $this
     */
    public function setMetrics(array $metrics)
    {
        $this->metrics = $metrics;

        return $this;
    }

    /**
     * Get the url to a graphical representation of the available metrics
     *
     * @return Url
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Set the url to a graphical representation of the available metrics
     *
     * @param Url $url
     *
     * @return $this
     */
    public function setUrl(Url $url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * Collect available health data from hooks
     *
     * @return ArrayDatasource
     */
    final public static function collectHealthData()
    {
        $checks = [];
        foreach (Hook::all('health') as $hook) {
            /** @var self $hook */

            try {
                $hook->checkHealth();
                $url = $hook->getUrl();
                $state = $hook->getState();
                $message = $hook->getMessage();
                $metrics = $hook->getMetrics();
            } catch (Exception $e) {
                Logger::error('Failed to check health: %s', $e);

                $state = self::STATE_UNKNOWN;
                $message = IcingaException::describe($e);
                $metrics = null;
                $url = null;
            }

            $checks[] = (object) [
                'module'    => $hook->getModuleName(),
                'name'      => $hook->getName(),
                'url'       => $url ? $url->getAbsoluteUrl() : null,
                'state'     => $state,
                'message'   => $message,
                'metrics'   => (object) $metrics
            ];
        }

        return (new ArrayDatasource($checks))
            ->setKeyColumn('name');
    }

    /**
     * Get the name of the hook
     *
     * Only used in API responses to differentiate it from other hooks of the same module.
     *
     * @return string
     */
    public function getName()
    {
        $classPath = get_class($this);
        $parts = explode('\\', $classPath);
        $className = array_pop($parts);

        if (substr($className, -4) === 'Hook') {
            $className = substr($className, 1, -4);
        }

        return strtolower($className[0]) . substr($className, 1);
    }

    /**
     * Get the name of the module providing this hook
     *
     * @return string
     *
     * @throws LogicException
     */
    public function getModuleName()
    {
        $classPath = get_class($this);
        if (substr($classPath, 0, 14) !== 'Icinga\\Module\\') {
            throw new LogicException('Not a module hook');
        }

        $withoutPrefix = substr($classPath, 14);
        return strtolower(substr($withoutPrefix, 0, strpos($withoutPrefix, '\\')));
    }

    /**
     * Check health
     *
     * Implement this method and set the overall state, message, url and metrics.
     *
     * @return void
     */
    abstract public function checkHealth();
}