summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/application/controllers/TimelineController.php
blob: deeeb365ad1103db8726064736c529568553cb48 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Controllers;

use DateInterval;
use DateTime;
use Icinga\Module\Monitoring\Controller;
use Icinga\Module\Monitoring\Timeline\TimeLine;
use Icinga\Module\Monitoring\Timeline\TimeRange;
use Icinga\Module\Monitoring\Web\Widget\SelectBox;
use Icinga\Util\Format;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tabextension\DashboardAction;
use Icinga\Web\Widget\Tabextension\MenuAction;

class TimelineController extends Controller
{
    public function indexAction()
    {
        $this->getTabs()->add(
            'timeline',
            array(
                'title' => $this->translate('Show the number of historical event records grouped by time and type'),
                'label' => $this->translate('Timeline'),
                'url'   => Url::fromRequest()
            )
        )->extend(new DashboardAction())->extend(new MenuAction())->activate('timeline');
        $this->view->title = $this->translate('Timeline');

        // TODO: filter for hard_states (precedence adjustments necessary!)
        $this->setupIntervalBox();
        list($displayRange, $forecastRange) = $this->buildTimeRanges();

        $detailUrl = Url::fromPath('monitoring/list/eventhistory');

        $timeline = new TimeLine(
            $this->applyRestriction(
                'monitoring/filter/objects',
                $this->backend->select()->from(
                    'eventhistory',
                    array(
                        'name' => 'type',
                        'time' => 'timestamp'
                    )
                )
            ),
            array(
                'notification_ack'  => array(
                    'class'         => 'timeline-notification',
                    'detailUrl'     => $detailUrl,
                    'label'         => mt('monitoring', 'Notifications'),
                    'groupBy'       => 'notification_*'
                ),
                'notification_flapping'  => array(
                    'class'         => 'timeline-notification',
                    'detailUrl'     => $detailUrl,
                    'label'         => mt('monitoring', 'Notifications'),
                    'groupBy'       => 'notification_*'
                ),
                'notification_flapping_end'  => array(
                    'class'         => 'timeline-notification',
                    'detailUrl'     => $detailUrl,
                    'label'         => mt('monitoring', 'Notifications'),
                    'groupBy'       => 'notification_*'
                ),
                'notification_dt_start'  => array(
                    'class'         => 'timeline-notification',
                    'detailUrl'     => $detailUrl,
                    'label'         => mt('monitoring', 'Notifications'),
                    'groupBy'       => 'notification_*'
                ),
                'notification_dt_end'  => array(
                    'class'         => 'timeline-notification',
                    'detailUrl'     => $detailUrl,
                    'label'         => mt('monitoring', 'Notifications'),
                    'groupBy'       => 'notification_*'
                ),
                'notification_custom'  => array(
                    'class'         => 'timeline-notification',
                    'detailUrl'     => $detailUrl,
                    'label'         => mt('monitoring', 'Notifications'),
                    'groupBy'       => 'notification_*'
                ),
                'notification_state'  => array(
                    'class'         => 'timeline-notification',
                    'detailUrl'     => $detailUrl,
                    'label'         => mt('monitoring', 'Notifications'),
                    'groupBy'       => 'notification_*'
                ),
                'hard_state'    => array(
                    'class'     => 'timeline-hard-state',
                    'detailUrl' => $detailUrl,
                    'label'     => mt('monitoring', 'Hard state changes')
                ),
                'comment'       => array(
                    'class'     => 'timeline-comment',
                    'detailUrl' => $detailUrl,
                    'label'     => mt('monitoring', 'Comments')
                ),
                'ack'           => array(
                    'class'     => 'timeline-ack',
                    'detailUrl' => $detailUrl,
                    'label'     => mt('monitoring', 'Acknowledgements')
                ),
                'dt_start'      => array(
                    'class'     => 'timeline-downtime-start',
                    'detailUrl' => $detailUrl,
                    'label'     => mt('monitoring', 'Started downtimes')
                ),
                'dt_end'        => array(
                    'class'     => 'timeline-downtime-end',
                    'detailUrl' => $detailUrl,
                    'label'     => mt('monitoring', 'Ended downtimes')
                )
            )
        );
        $timeline->setMaximumCircleWidth('6em');
        $timeline->setMinimumCircleWidth('0.3em');
        $timeline->setDisplayRange($displayRange);
        $timeline->setForecastRange($forecastRange);
        $beingExtended = $this->getRequest()->getParam('extend') == 1;
        $timeline->setSession($this->Window()->getSessionNamespace('timeline', !$beingExtended));

        $this->view->timeline = $timeline;
        $this->view->nextRange = $forecastRange;
        $this->view->beingExtended = $beingExtended;
        $this->view->intervalFormat = $this->getIntervalFormat();
        $oldBase = $timeline->getCalculationBase(false);
        $this->view->switchedContext = $oldBase !== null && $oldBase !== $timeline->getCalculationBase(true);
    }

    /**
     * Create a select box the user can choose the timeline interval from
     */
    private function setupIntervalBox()
    {
        $box = new SelectBox(
            'intervalBox',
            array(
                '4h' => mt('monitoring', '4 Hours'),
                '1d' => mt('monitoring', 'One day'),
                '1w' => mt('monitoring', 'One week'),
                '1m' => mt('monitoring', 'One month'),
                '1y' => mt('monitoring', 'One year')
            ),
            mt('monitoring', 'TimeLine interval'),
            'interval'
        );
        $box->applyRequest($this->getRequest());
        $this->view->intervalBox = $box;
    }

    /**
     * Return the chosen interval
     *
     * @return  DateInterval    The chosen interval
     */
    private function getTimelineInterval()
    {
        switch ($this->view->intervalBox->getInterval()) {
            case '1d':
                return new DateInterval('P1D');
            case '1w':
                return new DateInterval('P1W');
            case '1m':
                return new DateInterval('P1M');
            case '1y':
                return new DateInterval('P1Y');
            default:
                return new DateInterval('PT4H');
        }
    }

    /**
     * Get an appropriate datetime format string for the chosen interval
     *
     * @return  string
     */
    private function getIntervalFormat()
    {
        switch ($this->view->intervalBox->getInterval()) {
            case '1d':
                return $this->getDateFormat();
            case '1w':
                return '\W\e\ek W\<b\r\>\of Y';
            case '1m':
                return 'F Y';
            case '1y':
                return 'Y';
            default:
                return $this->getDateFormat() . '\<b\r\>' . $this->getTimeFormat();
        }
    }

    /**
     * Return a preload interval based on the chosen timeline interval and the given date and time
     *
     * @param   DateTime    $dateTime   The date and time to use
     *
     * @return  DateInterval            The interval to pre-load
     */
    private function getPreloadInterval(DateTime $dateTime)
    {
        switch ($this->view->intervalBox->getInterval()) {
            case '1d':
                return DateInterval::createFromDateString('1 week -1 second');
            case '1w':
                return DateInterval::createFromDateString('8 weeks -1 second');
            case '1m':
                $dateCopy = clone $dateTime;
                for ($i = 0; $i < 6; $i++) {
                    $dateCopy->sub(new DateInterval('PT' . Format::secondsByMonth($dateCopy) . 'S'));
                }
                return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime);
            case '1y':
                $dateCopy = clone $dateTime;
                for ($i = 0; $i < 4; $i++) {
                    $dateCopy->sub(new DateInterval('PT' . Format::secondsByYear($dateCopy) . 'S'));
                }
                return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime);
            default:
                return DateInterval::createFromDateString('1 day -1 second');
        }
    }

    /**
     * Extrapolate the given datetime based on the chosen timeline interval
     *
     * @param   DateTime    $dateTime   The datetime to extrapolate
     */
    private function extrapolateDateTime(DateTime &$dateTime)
    {
        switch ($this->view->intervalBox->getInterval()) {
            case '1d':
                $dateTime->setTimestamp(strtotime('tomorrow', $dateTime->getTimestamp()) - 1);
                break;
            case '1w':
                $dateTime->setTimestamp(strtotime('next monday', $dateTime->getTimestamp()) - 1);
                break;
            case '1m':
                $dateTime->setTimestamp(
                    strtotime(
                        'last day of this month',
                        strtotime(
                            'tomorrow',
                            $dateTime->getTimestamp()
                        ) - 1
                    )
                );
                break;
            case '1y':
                $dateTime->setTimestamp(strtotime('1 january next year', $dateTime->getTimestamp()) - 1);
                break;
            default:
                $hour = $dateTime->format('G');
                $end = $hour < 4 ? 4 : ($hour < 8 ? 8 : ($hour < 12 ? 12 : ($hour < 16 ? 16 : ($hour < 20 ? 20 : 24))));
                $dateTime = DateTime::createFromFormat(
                    'd/m/y G:i:s',
                    $dateTime->format('d/m/y') . ($end - 1) . ':59:59'
                );
        }
    }

    /**
     * Return a display- and forecast time range
     *
     * Assembles a time range each for display and forecast purposes based on the start- and
     * end time if given in the current request otherwise based on the current time and a
     * end time that is calculated based on the chosen timeline interval.
     *
     * @return  array   The resulting time ranges
     */
    private function buildTimeRanges()
    {
        $startTime = new DateTime();
        $startParam = $this->_request->getParam('start');
        $startTimestamp = is_numeric($startParam) ? intval($startParam) : strtotime($startParam ?? '');
        if ($startTimestamp !== false) {
            $startTime->setTimestamp($startTimestamp);
        } else {
            $this->extrapolateDateTime($startTime);
        }

        $endTime = clone $startTime;
        $endParam = $this->_request->getParam('end');
        $endTimestamp = is_numeric($endParam) ? intval($endParam) : strtotime($endParam ?? '');
        if ($endTimestamp !== false) {
            $endTime->setTimestamp($endTimestamp);
        } else {
            $endTime->sub($this->getPreloadInterval($startTime));
        }

        $forecastStart = clone $endTime;
        $forecastStart->sub(new DateInterval('PT1S'));
        $forecastEnd = clone $forecastStart;
        $forecastEnd->sub($this->getPreloadInterval($forecastStart));

        $timelineInterval = $this->getTimelineInterval();
        return array(
            new TimeRange($startTime, $endTime, $timelineInterval),
            new TimeRange($forecastStart, $forecastEnd, $timelineInterval)
        );
    }

    /**
     * Get the user's preferred time format or the application's default
     *
     * @return  string
     */
    private function getTimeFormat()
    {
        return 'H:i';
    }

    /**
     * Get the user's preferred date format or the application's default
     *
     * @return  string
     */
    private function getDateFormat()
    {
        return 'Y-m-d';
    }
}