summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/doc/20-Hooks.md
blob: 5d38843d6330a4d90d1367fc122a12c5bc08fe0b (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
# Monitoring Module Hooks <a id="monitoring-module-hooks"></a>

## Detail View Extension Hook <a id="monitoring-module-hooks-detailviewextension"></a>

This hook can be used to easily extend the detail view of monitored objects (hosts and services).

### How it works <a id="monitoring-module-hooks-detailviewextension-how-it-works"></a>

#### Directory structure <a id="monitoring-module-hooks-detailviewextension-directory-structure"></a>

* `icingaweb2/modules/example`
    * `library/Example/ProvidedHook/Monitoring/DetailviewExtension/Simple.php`
    * `run.php`

#### Files <a id="monitoring-module-hooks-detailviewextension-files"></a>

##### run.php <a id="monitoring-module-hooks-detailviewextension-files-run-php"></a>

```php
<?php
/** @var \Icinga\Application\Modules\Module $this */

$this->provideHook(
    'monitoring/DetailviewExtension',
    'Icinga\Module\Example\ProvidedHook\Monitoring\DetailviewExtension\Simple'
);
```

##### Simple.php <a id="monitoring-module-hooks-detailviewextension-files-simple-php"></a>

```php
<?php
namespace Icinga\Module\Example\ProvidedHook\Monitoring\DetailviewExtension;

use Icinga\Module\Monitoring\Hook\DetailviewExtensionHook;
use Icinga\Module\Monitoring\Object\MonitoredObject;

class Simple extends DetailviewExtensionHook
{
    public function getHtmlForObject(MonitoredObject $object)
    {
        $stats = array();
        foreach (str_split($object->name) as $c) {
            if (isset($stats[$c])) {
                ++$stats[$c];
            } else {
                $stats[$c] = 1;
            }
        }

        ksort($stats);

        $view = $this->getView();

        $thead = '';
        $tbody = '';
        foreach ($stats as $c => $amount) {
            $thead .= '<th>' . $view->escape($c) . '</th>';
            $tbody .= '<td>' . $amount . '</td>';
        }

        return '<h2>'
            . $view->escape(sprintf($view->translate('A %s named "%s"'), $object->getType(), $object->name))
            . '</h2>'
            . '<h3>Character stats</h3>'
            . '<table>'
            . '<thead>' . $thead . '</thead>'
            . '<tbody>' . $tbody . '</tbody>'
            . '</table>';
    }
}
```

### How it looks <a id="monitoring-module-hooks-detailviewextension-how-it-looks"></a>

![Screenshot](img/hooks-detailviewextension-01.png)

## Plugin Output Hook <a id="monitoring-module-hooks-pluginoutput"></a>

The Plugin Output Hook allows you to rewrite the plugin output based on check commands. You have to implement the
following methods:

* `getCommands()`
* and `render()`

With `getCommands()` you specify for which commands the provided hook is responsible for. You may return a single
command as string or a list of commands as array. If you want your hook to be responsible for every command, you have to
specify the `*`.

In `render()` you rewrite the plugin output based on check commands. The parameter `$command` specifies the check
command of the host or service and `$output` specifies the plugin output. The parameter `$detail` tells you
whether the output is requested from the detail area of the host or service.

Do not use complex logic for rewriting plugin output in list views because of the performance impact!

You have to return the rewritten plugin output as string. It is also possible to return a HTML string here.
Please refer to `\Icinga\Module\Monitoring\Web\Helper\PluginOutputPurifier` for a list of allowed tags.

Please also have a look at the following examples.

**Example hook which is responsible for disk checks:**

```php
<?php

namespace Icinga\Module\Example\ProvidedHook\Monitoring;

use Icinga\Module\Monitoring\Hook\PluginOutputHook;

class PluginOutput extends PluginOutputHook
{
    public function getCommands()
    {
        return ['disk'];
    }

    public function render($command, $output, $detail)
    {
        if (! $detail) {
            // Don't rewrite plugin output in list views
            return $output;
        }
        return implode('<br>', explode(';', $output));
    }
}
```

**Example hook which is responsible for disk and procs checks:**

```php
<?php

namespace Icinga\Module\Example\ProvidedHook\Monitoring;

use Icinga\Module\Monitoring\Hook\PluginOutputHook;

class PluginOutput extends PluginOutputHook
{
    public function getCommands()
    {
        return ['disk', 'procs'];
    }

    public function render($command, $output, $detail)
    {
        switch ($command) {
            case 'disk':
                if ($detail) {
                    // Only rewrite plugin output in the detail area
                    $output = implode('<br>', explode(';', $output));
                }
                break;
            case 'procs':
                $output = preg_replace('/(\d)+/', '<b>$1</b>', $output);
                break;
        }

        return $output;
    }
}
```