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
|
<?php
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Web\Helper;
use Icinga\Application\Logger;
use Icinga\Web\Hook;
/**
* Renderer for plugin output based on hooks
*/
class PluginOutputHookRenderer
{
/** @var array */
protected $commandMap = [];
/**
* Register PluginOutput hooks
*
* Map PluginOutput hooks to their responsible commands.
*
* @return $this
*/
public function registerHooks()
{
if (! Hook::has('monitoring/PluginOutput')) {
return $this;
}
foreach (Hook::all('monitoring/PluginOutput') as $hook) {
/** @var \Icinga\Module\Monitoring\Hook\PluginOutputHook $hook */
try {
$commands = $hook->getCommands();
} catch (\Exception $e) {
Logger::error(
'Failed to get applicable commands from hook "%s". An error occurred: %s',
get_class($hook),
$e
);
continue;
}
if (! is_array($commands)) {
$commands = [$commands];
}
foreach ($commands as $command) {
if (! isset($this->commandMap[$command])) {
$this->commandMap[$command] = [];
}
$this->commandMap[$command][] = $hook;
}
}
return $this;
}
protected function renderCommand($command, $output, $detail)
{
if (isset($this->commandMap[$command])) {
foreach ($this->commandMap[$command] as $hook) {
/** @var \Icinga\Module\Monitoring\Hook\PluginOutputHook $hook */
try {
$output = $hook->render($command, $output, $detail);
} catch (\Exception $e) {
Logger::error(
'Failed to render plugin output from hook "%s". An error occurred: %s',
get_class($hook),
$e
);
continue;
}
}
}
return $output;
}
/**
* Render the given plugin output based on the specified check command
*
* Traverse all hooks which are responsible for the specified check command and call their `render()` methods.
*
* @param string $command Check command
* @param string $output Plugin output
* @param bool $detail Whether the output is requested from the detail area
*
* @return string
*/
public function render($command, $output, $detail)
{
if (empty($this->commandMap)) {
return $output;
}
$output = $this->renderCommand('*', $output, $detail);
$output = $this->renderCommand($command, $output, $detail);
return $output;
}
}
|