1<?php
2use Icinga\Module\Monitoring\Object\Host;
3use Icinga\Module\Monitoring\Object\Service;
4use Icinga\Web\Url;
5use Icinga\Web\UrlParams;
6
7function contactsLink($match, $view) {
8    $links = array();
9    foreach (preg_split('/,\s/', $match[1]) as $contact) {
10        $links[] = $view->qlink(
11            $contact,
12            'monitoring/show/contact',
13            array('contact_name' => $contact),
14            array('title' => sprintf($view->translate('Show detailed information about %s'), $contact))
15        );
16    }
17    return '[' . implode(', ', $links) . ']';
18}
19
20$self = $this;
21
22$url = $this->url();
23$limit = (int) $url->getParam('limit', 25);
24if (! $url->hasParam('page') || ($page = (int) $url->getParam('page')) < 1) {
25    $page = 1;
26}
27
28/** @var \Icinga\Module\Monitoring\DataView\EventHistory $history */
29$history->limit($limit * $page);
30?>
31<div class="content">
32<?php
33$dateFormat = $this->translate('%A, %B %e, %Y', 'date.verbose');
34$lastDate = null;
35$flappingMsg = $this->translate('Flapping with a %.2f%% state change rate');
36$rowAction = Url::fromPath('monitoring/event/show');
37?>
38    <?php foreach ($history->peekAhead() as $event): ?>
39<?php if ($lastDate === null): ?>
40    <table class="table-row-selectable state-table" data-base-target="_next">
41        <tbody>
42<?php endif;
43        $icon = '';
44        $iconTitle = null;
45        $isService = isset($event->service_description);
46        $msg = $event->output;
47        $stateName = 'no-state';
48
49        $rowAction->setParams(new UrlParams())->addParams(array(
50            'type' => $event->type,
51            'id' => $event->id
52        ));
53        switch ($event->type) {
54            case substr($event->type, 0, 13) === 'notification_':
55                $rowAction->setParam('type', 'notify');
56                $icon = 'bell';
57                switch (substr($event->type, 13)) {
58                    case 'state':
59                        $iconTitle = $this->translate('State notification', 'tooltip');
60                        $label = $this->translate('NOTIFICATION');
61                        $stateName = $isService ? Service::getStateText($event->state) : Host::getStateText($event->state);
62                        break;
63                    case 'ack':
64                        $iconTitle = $this->translate('Ack Notification', 'tooltip');
65                        $label = $this->translate('ACK NOTIFICATION');
66                        break;
67                    case 'dt_start':
68                        $iconTitle = $this->translate('Downtime start notification', 'tooltip');
69                        $label = $this->translate('DOWNTIME START NOTIFICATION');
70                        break;
71                    case 'dt_end':
72                        $iconTitle = $this->translate('Downtime end notification', 'tooltip');
73                        $label = $this->translate('DOWNTIME END NOTIFICATION');
74                        break;
75                    case 'flapping':
76                        $iconTitle = $this->translate('Flapping notification', 'tooltip');
77                        $label = $this->translate('FLAPPING NOTIFICATION');
78                        break;
79                    case 'flapping_end':
80                        $iconTitle = $this->translate('Flapping end notification', 'tooltip');
81                        $label = $this->translate('FLAPPING END NOTIFICATION');
82                        break;
83                    case 'custom':
84                        $iconTitle = $this->translate('Custom notification', 'tooltip');
85                        $label = $this->translate('CUSTOM NOTIFICATION');
86                        break;
87                }
88                $msg = $msg ? preg_replace_callback(
89                    '/^\[([^\]]+)\]/',
90                    function($match) use ($self) { return contactsLink($match, $self); },
91                    $msg
92                ) : $this->translate('This notification was not sent out to any contact.');
93                break;
94            case 'comment':
95                $icon = 'comment-empty';
96                $iconTitle = $this->translate('Comment', 'tooltip');
97                $label = $this->translate('COMMENT');
98                break;
99            case 'comment_deleted':
100                $icon = 'cancel';
101                $iconTitle = $this->translate('Comment removed', 'tooltip');
102                $label = $this->translate('COMMENT DELETED');
103                break;
104            case 'ack':
105                $icon = 'ok';
106                $iconTitle = $this->translate('Acknowledged', 'tooltip');
107                $label = $this->translate('ACKNOWLEDGED');
108                break;
109            case 'ack_deleted':
110                $icon = 'ok';
111                $iconTitle = $this->translate('Acknowledgement removed', 'tooltip');
112                $label = $this->translate('ACKNOWLEDGEMENT REMOVED');
113                break;
114            case 'dt_comment':
115                $icon = 'plug';
116                $iconTitle = $this->translate('Downtime scheduled', 'tooltip');
117                $label = $this->translate('SCHEDULED DOWNTIME');
118                break;
119            case 'dt_comment_deleted':
120                $icon = 'plug';
121                $iconTitle = $this->translate('Downtime removed', 'tooltip');
122                $label = $this->translate('DOWNTIME DELETED');
123                break;
124            case 'flapping':
125                $icon = 'flapping';
126                $iconTitle = $this->translate('Flapping started', 'tooltip');
127                $label = $this->translate('FLAPPING');
128                $msg = sprintf($flappingMsg, $msg);
129                break;
130            case 'flapping_deleted':
131                $icon = 'flapping';
132                $iconTitle = $this->translate('Flapping stopped', 'tooltip');
133                $label = $this->translate('FLAPPING STOPPED');
134                $msg = sprintf($flappingMsg, $msg);
135                break;
136            case 'hard_state':
137                if ((int) $event->state === 0) {
138                    $icon = 'thumbs-up';
139                } else {
140                    $icon = 'warning-empty';
141                }
142                $iconTitle = $this->translate('Hard state', 'tooltip');
143                $label = $isService ? Service::getStateText($event->state, true) : Host::getStateText($event->state, true);
144                $stateName = $isService ? Service::getStateText($event->state) : Host::getStateText($event->state);
145                break;
146            case 'soft_state':
147                $icon = 'spinner';
148                $iconTitle = $this->translate('Soft state', 'tooltip');
149                $label = $isService ? Service::getStateText($event->state, true) : Host::getStateText($event->state, true);
150                $stateName = $isService ? Service::getStateText($event->state) : Host::getStateText($event->state);
151                break;
152            case 'dt_start':
153                $icon = 'plug';
154                $iconTitle = $this->translate('Downtime started', 'tooltip');
155                $label = $this->translate('DOWNTIME START');
156                break;
157            case 'dt_end':
158                $icon = 'plug';
159                $iconTitle = $this->translate('Downtime ended', 'tooltip');
160                $label = $this->translate('DOWNTIME END');
161                break;
162        } ?>
163        <?php
164        $currentDate = strftime($dateFormat, (int) $event->timestamp);
165        if ($currentDate !== $lastDate):
166            $lastDate = $currentDate;
167        ?>
168            <tr>
169                <th colspan="2"><?= $currentDate ?></th>
170            </tr>
171        <?php endif ?>
172        <tr href="<?= $rowAction ?>">
173            <td class="state-col state-<?= $stateName ?>">
174            <?php if ($history->getIteratorPosition() % $limit === 0): ?>
175                <a id="page-<?= $history->getIteratorPosition() / $limit + 1 ?>"></a>
176            <?php endif ?>
177                <div class="state-label"><?= $this->escape($label) ?></div>
178                <div class="state-meta"><?= $this->formatTime($event->timestamp) ?></div>
179            </td>
180            <td>
181                <div class="history-message-container">
182                <?php if ($icon): ?>
183                    <div class="history-message-icon">
184                        <?= $this->icon($icon, $iconTitle) ?>
185                    </div>
186                <?php endif ?>
187                    <div class="history-message-output">
188                    <?php if ($this->isOverview): ?>
189                        <?php if ($isService) {
190                            echo '<span class="service-on">';
191                            echo sprintf(
192                                $this->translate('%s on %s', 'service on host'),
193                                $this->qlink(
194                                    $event->service_display_name,
195                                    'monitoring/service/show',
196                                    [
197                                        'host' => $event->host_name,
198                                        'service' => $event->service_description
199                                    ],
200                                    [
201                                        'title' => sprintf(
202                                            $this->translate('Show detailed information for service %s on host %s'),
203                                            $event->service_display_name,
204                                            $event->host_display_name
205                                        )
206                                    ]
207                                ),
208                                $this->qlink(
209                                    $event->host_display_name,
210                                    'monitoring/host/show',
211                                    ['host' => $event->host_name],
212                                    [
213                                        'title' => sprintf(
214                                            $this->translate('Show detailed information for host %s'),
215                                            $event->host_display_name
216                                        )
217                                    ]
218                                )
219                            );
220                            echo '</span>';
221                        } else {
222                            echo $this->qlink(
223                                $event->host_display_name,
224                                'monitoring/host/show',
225                                ['host' => $event->host_name],
226                                [
227                                    'title' => sprintf(
228                                        $this->translate('Show detailed information for host %s'),
229                                        $event->host_display_name
230                                    )
231                                ]
232                            );
233                        } ?>
234                    <?php endif ?>
235                        <?= $this->nl2br($this->createTicketLinks($this->markdown($msg, ['class' => 'overview-plugin-output']))) ?>
236                    </div>
237                </div>
238            </td>
239        </tr>
240        <?php endforeach ?>
241<?php if ($lastDate !== null): ?>
242        </tbody>
243    </table>
244<?php endif ?>
245<?php if ($history->hasMore()): ?>
246    <div class="action-links">
247    <?php if ($this->compact) {
248        echo $this->qlink(
249            $this->translate('Show More'),
250            $url->without(array('showCompact', 'limit')),
251            null,
252            array(
253                'class'             => 'action-link',
254                'data-base-target'  => '_next'
255            )
256        );
257    } else {
258        echo $this->qlink(
259            $this->translate('Load More'),
260            $url->setAnchor('page-' . ($page + 1)),
261            array('page' => $page + 1,),
262            array('class' => 'action-link')
263        );
264    } ?>
265    </div>
266<?php endif ?>
267</div>
268