1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Help class for handling PNP related stuff such as
4 * checking if we should display a graph link or not.
9 * Check that there is actually a graph to display
10 * and that we can show a link to it.
12 public static function has_graph($host=false, $service=false)
14 throw new Exception('deprecated');
15 /* TODO: integrate in normal query instead calling it for every host / service */
17 $ls = Livestatus
::instance();
19 if (empty($service)) {
20 $objects = $ls->getHosts(array('filter' => array('name' => $host), 'columns' => array('pnpgraph_present')));
22 $service = urldecode($service);
23 $objects = $ls->getServices(array('filter' => array('host_name' => $host, 'description' => $service), 'columns' => array('pnpgraph_present')));
25 if (isset($objects) and isset($objects[0]) and $objects[0]['pnpgraph_present'] === 1) {
28 } catch (LivestatusException
$ex) {}
33 * Given a host and optionally a service, return all rrd sources
35 * @param $host A host name
36 * @param $service A service description
37 * @return An array of DS sources
39 public static function get_sources($host=false, $service=false)
44 if (!self
::is_enabled()) {
47 $host = self
::clean($host);
48 $rrdbase = self
::pnp_config('rrdbase');
49 if (empty($rrdbase)) {
50 # config missing or some other error
54 $rrdbase = trim($rrdbase);
56 # Better safe than sorry...
57 if (substr($rrdbase, -1, 1) != '/') {
61 if (empty($service)) {
64 # replace some strings in service name
66 $service = urldecode($service);
67 $service = self
::clean($service);
70 $path = $rrdbase . $host . '/' . $service . '.xml';
72 if (!posix_access($path, POSIX_R_OK
))
75 $contents = file_get_contents($path);
76 $xmldata = @simplexml_load_string
($contents);
77 if ($xmldata === false) {
78 // one can always try...
79 $xmldata = @simplexml_load_string
(utf8_encode($contents));
81 if ($xmldata === false ||
!isset($xmldata->DATASOURCE
)) {
85 if ($xmldata->DATASOURCE
) {
87 foreach ($xmldata->DATASOURCE
as $ds) {
96 * Check if PNP is installed (enabled) on this machine
98 public static function is_enabled()
100 $pnp_path = config
::get('config.pnp4nagios_path');
101 return $pnp_path === false ?
false : true;
105 * Fetch PNP config options and stash in current session.
106 * Returns the value of $key or entire config if no params
108 public static function pnp_config($key=false)
110 $conf = Session
::instance()->get('pnp_config', false);
113 # PNP config file consists of PHP code which makes it possible
114 # for us to just include it to get options available in $conf array
115 $pnp_config_file = Kohana
::config('config.pnp4nagios_config_path');
116 if (file_exists($pnp_config_file))
117 include_once($pnp_config_file);
119 # Since the PNP is not very likely to change very often,
120 # we may store the config in session to save us from
121 # fetching it more than once per session.
122 Session
::instance()->set('pnp_config', $conf);
124 return empty($key) ?
$conf : $conf[$key];
128 * Cleanses a string for use as a pnp object reference
129 * @param $string The string to cleanse
130 * @return The mangled string
132 public static function clean($string)
134 $string = trim($string);
135 return preg_replace('/[ :\/\\\]/', "_", $string);
139 * Creates a pnp url for a host or service
141 * @param $host The host
142 * @param $service The service
143 * @return A url usable from Ninja to get the desired pnp page
145 public static function url($host, $service=false)
147 $base = config
::get('config.pnp4nagios_path');
150 return 'PNP_seems_to_be_improperly_configured';
152 $host = urlencode(pnp
::clean($host));
153 if ($service !== false) {
154 $service = urlencode(pnp
::clean($service));
158 return $base . "/graph?host=$host&srv=$service";