diff --git a/application/controllers/GraphsController.php b/application/controllers/GraphsController.php deleted file mode 100644 index c9828e6..0000000 --- a/application/controllers/GraphsController.php +++ /dev/null @@ -1,124 +0,0 @@ -add(HtmlElement::create('p', ['class' => 'line-chart-error preformatted'], $message)); - $this->addContent($err); - } - - /** - * Initialize the Controller. - */ - public function init(): void - { - // Assert the user has access to this controller. - $this->assertPermission('perfdatagraphs/view'); - parent::init(); - } - - public function indexAction(): void - { - $this->getTabs() - ->add('graph', [ - 'label' => 'Graph', - 'url' => 'perfdatagraphs/graph' - ]) - ->activate('graph'); - - // Load the module's configuration. - $config = ModuleConfig::getConfigWithDefaults(); - $defaultDuration = $config['default_timerange']; - $duration = $this->params->get('perfdatagraphs.duration', $defaultDuration); - $headline = $this->params->get('perfdatagraphs.headline', $this->translate('Performance Data Graph')); - - // Retrieve the URL parameters. - $hostName = $this->params->getRequired('host'); - $serviceName = $this->params->getRequired('service'); - $checkcommandName = $this->params->getRequired('checkcommand'); - $isHostCheck = $this->params->getRequired('ishostcheck'); - - // Optional list of labels, when passed only the given perfdata metrics will be shown - $labels = $this->params->getValues('label'); - - // Transform the URL param into a boolean just because it is easier to work with - $isHostCheck = $isHostCheck === 'true' ? true : false; - - $header = Html::tag('h2', $headline); - $this->addContent($header); - - if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) { - Logger::debug('Used IcingaDB as database backend'); - $cvh = new IcinaDBCVH(); - } else { - Logger::debug('Used IDO as database backend'); - $cvh = new IdoCVH(); - } - - // Get the object so that we can get its custom variables. - $object = $cvh->getObjectFromString($hostName, $serviceName, $isHostCheck); - - if (empty($object)) { - $this->addError($this->translate('Failed to find object from given host-service strings')); - return; - } - - $customvars = $cvh->getPerfdataGraphsConfigForObject($object); - - // If the object wants the data from a custom backend - if ($customvars[$cvh::CUSTOM_VAR_CONFIG_BACKEND] ?? false) { - $hook = ModuleConfig::getHookByName($customvars[$cvh::CUSTOM_VAR_CONFIG_BACKEND]); - } else { - $hook = ModuleConfig::getHook(); - } - // If there is no hook configured we return here. - if (empty($hook)) { - $this->addError($this->translate('No hook configured')); - return; - } - - $source = new PerfdataSource($config, $hook); - $request = new PerfdataRequest($hostName, $serviceName, $checkcommandName, $duration, $isHostCheck, [], []); - - $customVarsMetrics = $cvh->getPerfdataGraphsMetricsForObject($object); - - $response = $source->fetch($request, $customVarsMetrics); - - $limit = -1; - $chart = $this->createChart(request: $request, response: $response, filter: $labels, limit: $limit); - - if (empty($chart)) { - $this->addError($this->translate('Chart could not be renderd')); - return; - } - - $this->addContent(HtmlString::create($chart)); - } -} diff --git a/doc/10-Dashboard.md b/doc/10-Dashboard.md index 030d23e..d586bbb 100644 --- a/doc/10-Dashboard.md +++ b/doc/10-Dashboard.md @@ -2,7 +2,15 @@ The module offers a dedicated page for graphs that can be used on an Icinga Web Dashboard. -This page is available at `perfdatagraphs/graphs`. +With the IcingaDB module at: + +* `icingadb/host/graphs?name=yourHostName` +* `icingadb/service/graphs?name=yourServiceName&host.name=yourHostName` + +With the Monitoring module at: + +* `monitoring/host/tabhook?host=yourHostName&hook=graphs` +* `monitoring/service/tabhook?host=yourHostName&service=yourServiceName&hook=graphs` HTTP parameters are used to managed what is rendered: @@ -10,22 +18,14 @@ HTTP parameters are used to managed what is rendered: |---------|--------| | `host` | Name of the Icinga host | | `service` | Name of the Icinga service | -| `checkcommand` | Name of the Icinga check command | -| `ishostcheck` | is this a Host or Service Check that is requested | -| `perfdatagraphs.duration` | duration for which to fetch the data for in PHP's [DateInterval](https://www.php.net/manual/en/class.dateinterval.php) format (e.g. PT12H, P1D, P1Y) | | `label` | (optional) Name of a specific performance data label to render. Can be used multiple times | | `perfdatagraphs.duration` | duration for which to fetch the data for in PHP's [DateInterval](https://www.php.net/manual/en/class.dateinterval.php) format (e.g. PT12H, P1D, P1Y) | -| `perfdatagraphs.headline` | Headline for the page. | Example: ``` -http://icingaweb2.internal/perfdatagraphs/graphs - ?host=example - &service=http - &checkcommand=http - &ishostcheck=false +http://icingaweb2.internal/icingadb/host/graphs + ?name=example &perfdatagraphs.duration=P1D - &perfdatagraphs.headline=Example Host HTTP Service &label=time&label=size ``` diff --git a/library/Perfdatagraphs/ProvidedHook/Icingadb/HostDetailExtension.php b/library/Perfdatagraphs/ProvidedHook/Icingadb/HostDetailExtension.php index c3c8ba4..3c9e581 100644 --- a/library/Perfdatagraphs/ProvidedHook/Icingadb/HostDetailExtension.php +++ b/library/Perfdatagraphs/ProvidedHook/Icingadb/HostDetailExtension.php @@ -15,7 +15,6 @@ use ipl\Html\HtmlElement; use ipl\Html\ValidHtml; use ipl\Web\Url; -use ipl\Web\Widget\Link; /** * HostDetailExtension adds the Chart HTML for Host objects. @@ -99,21 +98,9 @@ public function getHtmlForObject(Host $host): ValidHtml $headline = $this->translate('Performance Data Graph'); $header = Html::tag('h2', $headline); - $link = new Link( - $this->translate('Show all performance data graphs'), - Url::fromPath('perfdatagraphs/graphs')->addParams([ - 'host' => $hostName, - 'service' => $serviceName, - 'checkcommand' => $checkCommandName, - 'ishostcheck' => 'true', - 'perfdatagraphs.headline' => $headline - ]), - ); - $d = Html::tag('div'); $d->add($header); $d->add($chart); - $d->add($link); return $d; } diff --git a/library/Perfdatagraphs/ProvidedHook/Icingadb/ServiceDetailExtension.php b/library/Perfdatagraphs/ProvidedHook/Icingadb/ServiceDetailExtension.php index 59528dd..a15b184 100644 --- a/library/Perfdatagraphs/ProvidedHook/Icingadb/ServiceDetailExtension.php +++ b/library/Perfdatagraphs/ProvidedHook/Icingadb/ServiceDetailExtension.php @@ -15,7 +15,6 @@ use ipl\Html\HtmlElement; use ipl\Html\ValidHtml; use ipl\Web\Url; -use ipl\Web\Widget\Link; /** * ServiceDetailExtension adds the Chart HTML for Service objects. @@ -99,21 +98,9 @@ public function getHtmlForObject(Service $service): ValidHtml $headline = $this->translate('Performance Data Graph'); $header = Html::tag('h2', $headline); - $link = new Link( - $this->translate('Show all performance data graphs'), - Url::fromPath('perfdatagraphs/graphs')->addParams([ - 'host' => $hostName, - 'service' => $serviceName, - 'checkcommand' => $checkCommandName, - 'ishostcheck' => 'false', - 'perfdatagraphs.headline' => $headline - ]), - ); - $d = Html::tag('div'); $d->add($header); $d->add($chart); - $d->add($link); return $d; } diff --git a/library/Perfdatagraphs/ProvidedHook/Icingadb/Tab.php b/library/Perfdatagraphs/ProvidedHook/Icingadb/Tab.php new file mode 100644 index 0000000..2e60d27 --- /dev/null +++ b/library/Perfdatagraphs/ProvidedHook/Icingadb/Tab.php @@ -0,0 +1,104 @@ +add(HtmlElement::create('p', ['class' => 'line-chart-error preformatted'], $message)); + return $err; + } + + public function getContent(Model $object): array + { + $isHostCheck = false; + if ($object instanceof Host) { + $serviceName = $object->checkcommand_name; + $isHostCheck = true; + $checkcommandName = $object->checkcommand_name; + $hostName = $object->name; + } elseif ($object instanceof Service) { + $serviceName = $object->name; + $checkcommandName = $object->checkcommand_name; + $hostName = $object->host->name; + } + + $request = Icinga::app()->getRequest(); + + $config = ModuleConfig::getConfigWithDefaults(); + $defaultDuration = $config['default_timerange']; + + // Retrieve the URL parameters. + $duration = $request->getParam('perfdatagraphs_duration', $defaultDuration); + + // Optional list of labels, when passed only the given perfdata metrics will be shown + $labels = $request->getParam('labels', []); + + $cvh = new IcingaObjectHelper(); + + $customvars = $cvh->getPerfdataGraphsConfigForObject($object); + + // If the object wants the data from a custom backend + if ($customvars[$cvh::CUSTOM_VAR_CONFIG_BACKEND] ?? false) { + $hook = ModuleConfig::getHookByName($customvars[$cvh::CUSTOM_VAR_CONFIG_BACKEND]); + } else { + $hook = ModuleConfig::getHook(); + } + + // If there is no hook configured we return here. + $content = []; + if (empty($hook)) { + $content[] = $this->addError($this->translate('No hook configured')); + return $content; + } + + $source = new PerfdataSource($config, $hook); + $request = new PerfdataRequest($hostName, $serviceName, $checkcommandName, $duration, $isHostCheck, [], []); + + $customVarsMetrics = $cvh->getPerfdataGraphsMetricsForObject($object); + + $response = $source->fetch($request, $customVarsMetrics); + + $limit = -1; + $chart = $this->createChart(request: $request, response: $response, filter: $labels, limit: $limit); + $content[] = HtmlString::create($chart); + + if (empty($chart)) { + $content[] = $this->addError($this->translate('Chart could not be rendered')); + return $content; + } + + return $content; + } +} diff --git a/library/Perfdatagraphs/ProvidedHook/Monitoring/DetailviewExtension.php b/library/Perfdatagraphs/ProvidedHook/Monitoring/DetailviewExtension.php index 755b964..4ce10a4 100644 --- a/library/Perfdatagraphs/ProvidedHook/Monitoring/DetailviewExtension.php +++ b/library/Perfdatagraphs/ProvidedHook/Monitoring/DetailviewExtension.php @@ -16,7 +16,6 @@ use ipl\Html\Html; use ipl\Html\HtmlElement; use ipl\Web\Url; -use ipl\Web\Widget\Link; class DetailviewExtension extends DetailviewExtensionHook { @@ -106,21 +105,9 @@ public function getHtmlForObject(MonitoredObject $object) $headline = $this->translate('Performance Data Graph'); $header = Html::tag('h2', $headline); - $link = new Link( - $this->translate('Show all performance data graphs'), - Url::fromPath('perfdatagraphs/graphs')->addParams([ - 'host' => $hostName, - 'service' => $serviceName, - 'checkcommand' => $checkCommandName, - 'ishostcheck' => $isHostCheck, - 'perfdatagraphs.headline' => $headline - ]), - ); - $d = Html::tag('div'); $d->add($header); $d->add($chart); - $d->add($link); return $d; } diff --git a/library/Perfdatagraphs/ProvidedHook/Monitoring/ObjectDetailsTab.php b/library/Perfdatagraphs/ProvidedHook/Monitoring/ObjectDetailsTab.php new file mode 100644 index 0000000..5455cc4 --- /dev/null +++ b/library/Perfdatagraphs/ProvidedHook/Monitoring/ObjectDetailsTab.php @@ -0,0 +1,100 @@ +add(HtmlElement::create('p', ['class' => 'line-chart-error preformatted'], $message)); + return $err; + } + + public function getContent(MonitoredObject $object, Request $request) + { + $isHostCheck = false; + + if ($object instanceof Host) { + $serviceName = $object->host_check_command; + $hostName = $object->getName(); + $checkCommandName = $object->host_check_command; + $isHostCheck = true; + } elseif ($object instanceof Service) { + $serviceName = $object->getName(); + $hostName = $object->getHost()->getName(); + $checkCommandName = $object->check_command; + } else { + return Html::tag('div'); + } + + $config = ModuleConfig::getConfigWithDefaults(); + $defaultDuration = $config['default_timerange']; + // Retrieve the URL parameters. + $duration = $request->getParam('perfdatagraphs_duration', $defaultDuration); + + // Optional list of labels, when passed only the given perfdata metrics will be shown + $labels = $request->getParam('labels', []); + + $cvh = new IdoCVH(); + + $customvars = $cvh->getPerfdataGraphsConfigForObject($object); + + // If the object wants the data from a custom backend + if ($customvars[$cvh::CUSTOM_VAR_CONFIG_BACKEND] ?? false) { + $hook = ModuleConfig::getHookByName($customvars[$cvh::CUSTOM_VAR_CONFIG_BACKEND]); + } else { + $hook = ModuleConfig::getHook(); + } + + // If there is no hook configured we return here. + if (empty($hook)) { + return $this->addError($this->translate('No hook configured')); + } + + $source = new PerfdataSource($config, $hook); + $newRequest = new PerfdataRequest($hostName, $serviceName, $checkCommandName, $duration, $isHostCheck, [], []); + + $customVarsMetrics = $cvh->getPerfdataGraphsMetricsForObject($object); + + $response = $source->fetch($newRequest, $customVarsMetrics); + + $limit = -1; + $chart = $this->createChart(request: $newRequest, response: $response, filter: $labels, limit: $limit); + + if (empty($chart)) { + return $this->addError($this->translate('Chart could not be rendered')); + } + + return Html::tag('div', ['class' => 'icinga-module module-perfdatagraphs'], HtmlString::create($chart)); + } +} diff --git a/run.php b/run.php index 3455c0c..bb94cf4 100644 --- a/run.php +++ b/run.php @@ -5,4 +5,6 @@ $this->provideHook('icingadb/IcingadbSupport'); $this->provideHook('icingadb/ServiceDetailExtension'); $this->provideHook('icingadb/HostDetailExtension'); +$this->provideHook('icingadb/Tab'); +$this->provideHook('monitoring/ObjectDetailsTab'); $this->provideHook('monitoring/DetailviewExtension');