summaryrefslogtreecommitdiffstats
path: root/library/Pnp/ProvidedHook/Grapher.php
blob: 2dcfbda29127917905ccc2099fa83f433c953f17 (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
<?php

namespace Icinga\Module\Pnp\ProvidedHook;

use Icinga\Application\Config;
use Icinga\Exception\ConfigurationError;
use Icinga\Application\Hook\GrapherHook;
use Icinga\Module\Monitoring\Object\MonitoredObject;
use Icinga\Module\Monitoring\Object\Host;
use Icinga\Module\Monitoring\Object\Service;
use Icinga\Web\Url;

class Grapher extends GrapherHook
{
    protected $hasPreviews = true;

    protected $pnpConfig;

    protected $pnpViews;

    protected $configDir = '/etc/pnp4nagios';

    protected $baseUrl = '/pnp4nagios';

    protected function init()
    {
        $cfg = Config::module('pnp')->getSection('pnp4nagios');
        $this->configDir = rtrim($cfg->get('config_dir', $this->configDir), '/');
        $this->baseUrl   = rtrim($cfg->get('base_url', $this->baseUrl), '/');
        $this->readPnpConfig();
    }

    public function has(MonitoredObject $object)
    {
        if ($object instanceof Host) {
            $service = '_HOST_';
        } elseif ($object instanceof Service) {
            $service = $object->service_description;
        } else {
            return false;
        }

        $host = $object->host_name;
        return is_file($this->getXmlFilename($host, $service));
    }

    public function getPreviewHtml(MonitoredObject $object)
    {
        if (! $object->process_perfdata) {
            return '';
        }

        // Skip preview images when missing, for local installations only
        if (false === strpos($this->baseUrl, '://') && ! $this->has($object)) {
            return '';
        }

        if ($object instanceof Host) {
            $service = '_HOST_';
        } elseif ($object instanceof Service) {
            $service = $object->service_description;
        } else {
            return '';
        }

        $host = $object->host_name;

        $html = '<table style="width: 100%; max-width: 40em; text-align: center;'
              . ' font-size: 0.8em; line-height: 0.8em; table-layout: fixed">'
              . "\n  <tr>\n";
        $viewKeys = array_reverse(array_keys($this->pnpViews));
        foreach ($viewKeys as $view) {
            $html .= '<th>' . htmlspecialchars($this->getViewName($view)) . "</th>\n";
        }
        $html .= "  </tr>\n  <tr>\n";
        foreach ($viewKeys as $view) {
            $html .= '    <td style="border-left: 1px solid #555; padding-right: 3px">'
                   . $this->getPreviewImg($host, $service, $view)
                   . "</td>\n";
        }
        $html .= "</tr></table>\n";
        return $html;
    }

    // Currently unused, but would work fine. This is for tiny preview images
    // in list views
    public function getSmallPreviewImage($host, $service = null)
    {
        if ($service === null) {
            $service = '_HOST_';
        }

        return sprintf(
            '<img src="%s/image?host=%s&srv=%s&view=0&source=0&h=20&w=50" alt="" style="float: right" />',
            $this->baseUrl,
            urlencode($this->pnpClean($host)),
            urlencode($this->pnpClean($service))
        );
    }

    private function listAdditionalConfigFiles()
    {
        $files = array();
        $base = $this->configDir . '/config';

        $file = $base . '_local.php';
        if (file_exists($file) && is_readable($file)) {
            $files[] = $file;
        }

        $confd = $base . '.d';
        if (is_dir($confd) && is_readable($confd)) {
            $dh = opendir($confd);
            while ($file === readdir($dh)) {
                if ($file[0] === '.') continue;
                if (substr($file, -4) !== '.php') continue;

                $filename = $confd . '/' . $file;
                if (is_file($filename) && is_readable($filename)) {
                    $files[] = $filename;
                }
            }

            closedir($dh);
        }

        return $files;
    }

    // This reads the PNP4Nagios config and makes it's $conf available
    private function readPnpConfig()
    {
        $file = $this->configDir . '/config.php';

        if (! is_readable($file)) {
            throw new ConfigurationError(
                sprintf(
                    'Cannot read PNP4Nagios-Web config file "%s"',
                    $file
                )
            );
        }
        if (! include($file)) {
            throw new ConfigurationError(
                sprintf(
                    'Including PNP4Nagios-Web config "%s" failed',
                    $file
                )
            );
        }

        if (! isset($views)) {
            $views = array();
        }

        foreach ($this->listAdditionalConfigFiles() as $file) {
            $oldViews = $views;
            include $file;
            if (empty($views)) {
                $views = $oldViews;
            }
        } 

        if (! isset($conf) || ! is_array($conf)) {
            throw new ConfigurationError(
                sprintf(
                    'There is no $conf in your PNP4Nagios config file "%s"',
                    $file
                )
            );
        }

        if (! isset($views) || ! is_array($views)) {
            throw new ConfigurationError(
                sprintf(
                    'There is no $views array in your PNP4Nagios config file "%s"',
                    $file
                )
            );
        }

        if (! array_key_exists('rrdbase', $conf)) {
            throw new ConfigurationError(
                sprintf(
                    'There is no rrdbase in your PNP4Nagios config file "%s"',
                    $file
                )
            );
        }
        $this->pnpConfig = $conf;
        $this->pnpViews  = $views;
        return $this;
    }

    // pnp_Core::clean
    private function pnpClean($string)
    {
        if ($string === false) {
            return null;
        }
        return preg_replace('~[ :/\\\]~', '_', $string);
    }

    private function getBasePath($host, $service)
    {
        if ($service === null) {
            $service = '_HOST_';
        }
        return rtrim($this->pnpConfig['rrdbase'], '/')
              . '/' . $this->pnpClean($host) . '/'
              . $this->pnpClean($service);
    }

    private function getRrdFilename($host, $service)
    {
        return $this->getBasePath($host, $service) . '.rrd';
    }

    private function getXmlFilename($host, $service)
    {
        return $this->getBasePath($host, $service) . '.xml';
    }

    private function getPreviewImg($host, $service, $view)
    {
        $viewName = $this->getViewName($view);

        $host = $this->pnpClean($host);
        $service = $this->pnpClean($service);

        $title = $service === '_HOST_' ? sprintf(
            '%s, %s', $host, $viewName
        ) : sprintf(
            '%s on %s, %s', $service, $host, $viewName
        );

        $url = Url::fromPath('pnp/graph', array(
            'host' => $this->pnpClean($host),
            'srv' => $this->pnpClean($service),
            'view' => $view
        ));
        $imgUrl = sprintf(
            '%s/image?host=%s&srv=%s&view=%d&source=0&w=120&h=30',
            $this->baseUrl,
            urlencode($this->pnpClean($host)),
            urlencode($this->pnpClean($service)),
            $view
        );

        $html = '<a href="%s" title="%s"><img src="%s" alt="%s" width="100%%" height="30" /></a>';

        return sprintf(
            $html,
            $url,
            htmlspecialchars($title),
            $imgUrl,
            htmlspecialchars(mt('pnp', 'Loading') . '...')
        );
    }

    protected function getViewName($view)
    {
        return mt('pnp4nagios', $this->pnpViews[$view]['title']);
    }

    private function unusedFunctionAllowingToTranslateForeignStrings()
    {
        mt('pnp4nagios', '4 Hours');
        mt('pnp4nagios', '25 Hours');
        mt('pnp4nagios', 'One Week');
        mt('pnp4nagios', 'One Month');
        mt('pnp4nagios', 'One Year');
    }
}