From 8e7acb813000c0bfd9750a602c6396898719ef6a Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Tue, 15 Aug 2017 09:48:17 +0200 Subject: [PATCH 1/5] Move graph access to GraphController Add tabs, so the UI allows the user to close the container --- application/controllers/GraphController.php | 37 +++++++++++++++++++ application/controllers/IndexController.php | 12 +----- .../index/{index.phtml => iframe.phtml} | 0 library/Pnp/ProvidedHook/Grapher.php | 2 +- library/Pnp/Web/Controller.php | 29 +++++++++++++++ 5 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 application/controllers/GraphController.php rename application/views/scripts/index/{index.phtml => iframe.phtml} (100%) create mode 100644 library/Pnp/Web/Controller.php diff --git a/application/controllers/GraphController.php b/application/controllers/GraphController.php new file mode 100644 index 0000000..685b414 --- /dev/null +++ b/application/controllers/GraphController.php @@ -0,0 +1,37 @@ +getRequest()->getUrl(); + $queryString = $url->getQueryString(); + + $this->view->url = sprintf( + '%s/graph?%s', + $this->getBaseUrl(), + $queryString + ); + + $host = $this->getParam('host'); + $service = $this->getParam('srv'); + + $this->view->title = $title = sprintf('%s: %s%s', + $this->translate('Host'), + $host, + ($service ? sprintf(' | %s: %s', $this->translate('Service'), $service) : '') + ); + + $this->getTabs()->add('graph', array( + 'label' => $title, + 'url' => $url, + ))->activate('graph'); + + $this->setViewScript('index/iframe'); + } +} diff --git a/application/controllers/IndexController.php b/application/controllers/IndexController.php index 3a2b334..4b90949 100644 --- a/application/controllers/IndexController.php +++ b/application/controllers/IndexController.php @@ -3,20 +3,12 @@ namespace Icinga\Module\Pnp\Controllers; -use Icinga\Web\Controller; +use Icinga\Module\Pnp\Web\Controller; class IndexController extends Controller { public function indexAction() { - $baseUrl = rtrim($this->Config()->get('pnp4nagios', 'base_url', '/pnp4nagios'), '/'); - - $this->view->url = sprintf( - '%s/graph?host=%s&srv=%s&view=%d', - $baseUrl, - urlencode($this->getParam('host')), - urlencode($this->getParam('srv')), - $this->getParam('view') - ); + $this->redirectNow($this->getRequest()->getUrl()->setPath('pnp/graph')); } } diff --git a/application/views/scripts/index/index.phtml b/application/views/scripts/index/iframe.phtml similarity index 100% rename from application/views/scripts/index/index.phtml rename to application/views/scripts/index/iframe.phtml diff --git a/library/Pnp/ProvidedHook/Grapher.php b/library/Pnp/ProvidedHook/Grapher.php index ba301fd..e334c52 100644 --- a/library/Pnp/ProvidedHook/Grapher.php +++ b/library/Pnp/ProvidedHook/Grapher.php @@ -234,7 +234,7 @@ private function getPreviewImg($host, $service, $view) '%s on %s, %s', $service, $host, $viewName ); - $url = Url::fromPath('pnp', array( + $url = Url::fromPath('pnp/graph', array( 'host' => $this->pnpClean($host), 'srv' => $this->pnpClean($service), 'view' => $view diff --git a/library/Pnp/Web/Controller.php b/library/Pnp/Web/Controller.php new file mode 100644 index 0000000..7ace560 --- /dev/null +++ b/library/Pnp/Web/Controller.php @@ -0,0 +1,29 @@ +getTabs()->add('pnp', array( + 'label' => $this->translate('PNP'), + 'url' => 'pnp', + )); + */ + } + + protected function setViewScript($name) + { + $this->_helper->viewRenderer->setNoController(true); + $this->_helper->viewRenderer->setScriptAction($name); + } + + protected function getBaseUrl() + { + return rtrim($this->Config()->get('pnp4nagios', 'base_url', '/pnp4nagios'), '/'); + } +} From 31611c3f7eb84300cd418ac09e9eab43efb12a58 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Tue, 15 Aug 2017 10:38:25 +0200 Subject: [PATCH 2/5] Add proper IndexController and menu entry There is an option to disable the menu entry. --- application/controllers/IndexController.php | 13 +++++++- .../forms/Config/GeneralConfigForm.php | 31 +++++++++++++++---- configuration.php | 11 +++++++ library/Pnp/Web/Controller.php | 2 -- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/application/controllers/IndexController.php b/application/controllers/IndexController.php index 4b90949..7725268 100644 --- a/application/controllers/IndexController.php +++ b/application/controllers/IndexController.php @@ -9,6 +9,17 @@ class IndexController extends Controller { public function indexAction() { - $this->redirectNow($this->getRequest()->getUrl()->setPath('pnp/graph')); + $this->getTabs()->activate('pnp'); + + $defaultQuery = $this->Config()->get('pnp4nagios', 'default_query', 'host=.pnp-internal&srv=runtime'); + + $this->view->title = 'PNP'; + $this->view->url = sprintf( + '%s/graph?%s', + $this->getBaseUrl(), + $defaultQuery + ); + + $this->setViewScript('index/iframe'); } } diff --git a/application/forms/Config/GeneralConfigForm.php b/application/forms/Config/GeneralConfigForm.php index 10d6a83..d751055 100644 --- a/application/forms/Config/GeneralConfigForm.php +++ b/application/forms/Config/GeneralConfigForm.php @@ -25,18 +25,37 @@ public function createElements(array $formData) 'text', 'pnp4nagios_config_dir', array( - 'value' => '/etc/pnp4nagios', - 'label' => $this->translate('PNP4Nagios configuration'), - 'description' => $this->translate('PNP4Nagios configuration path name (e.g. /etc/pnp4nagios)') + 'value' => '/etc/pnp4nagios', + 'label' => $this->translate('PNP4Nagios configuration'), + 'description' => $this->translate('PNP4Nagios configuration path name (e.g. /etc/pnp4nagios)') ) ); $this->addElement( 'text', 'pnp4nagios_base_url', array( - 'value' => '/pnp4nagios', - 'label' => $this->translate('PNP4Nagios url'), - 'description' => $this->translate('The base URL of your PNP4Nagios installation (e.g. /pnp4nagios)') + 'value' => '/pnp4nagios', + 'label' => $this->translate('PNP4Nagios url'), + 'description' => $this->translate('The base URL of your PNP4Nagios installation (e.g. /pnp4nagios)') + ) + ); + + $this->addElement( + 'checkbox', + 'pnp4nagios_menu_disabled', + array( + 'label' => $this->translate('Disable menu entry'), + 'description' => $this->translate('Hide PNP from main menu') + ) + ); + + $this->addElement( + 'text', + 'pnp4nagios_default_query', + array( + 'value' => 'host=.pnp-internal&srv=runtime', + 'label' => $this->translate('Index query'), + 'description' => $this->translate('Default URL query for the index view: /pnp4nagios/graph?{query}') ) ); } diff --git a/configuration.php b/configuration.php index 6c62055..76713e5 100644 --- a/configuration.php +++ b/configuration.php @@ -6,3 +6,14 @@ 'label' => $this->translate('Config'), 'url' => 'config' )); + + +$menuDisabled = $this->getConfig()->get('pnp4nagios', 'menu_disabled'); +if (! $menuDisabled) { + /** @var \Icinga\Web\Navigation\NavigationItem $section */ + $section = $this->menuSection('pnp'); + $section->setLabel('PNP') + ->setUrl('pnp') + ->setIcon('chart-line') + ->setPriority(50); +} diff --git a/library/Pnp/Web/Controller.php b/library/Pnp/Web/Controller.php index 7ace560..4af5c06 100644 --- a/library/Pnp/Web/Controller.php +++ b/library/Pnp/Web/Controller.php @@ -8,12 +8,10 @@ class Controller extends IcingaController { public function init() { - /* TODO $this->getTabs()->add('pnp', array( 'label' => $this->translate('PNP'), 'url' => 'pnp', )); - */ } protected function setViewScript($name) From 9139b95caee522994489f0d0ba99892215228b9e Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Tue, 15 Aug 2017 10:46:48 +0200 Subject: [PATCH 3/5] ConfigController: Protect for admins only --- application/controllers/ConfigController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index 0ab8b1d..f6a0dcd 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -13,6 +13,8 @@ class ConfigController extends Controller */ public function indexAction() { + $this->assertPermission('config/modules'); + $form = new GeneralConfigForm(); $form->setIniConfig($this->Config()); $form->handleRequest(); From a57697f6dc848814f37d01f01838c7d10826100b Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Tue, 15 Aug 2017 10:54:55 +0200 Subject: [PATCH 4/5] GraphController: Do not show _HOST_ as service --- application/controllers/GraphController.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/application/controllers/GraphController.php b/application/controllers/GraphController.php index 685b414..312b802 100644 --- a/application/controllers/GraphController.php +++ b/application/controllers/GraphController.php @@ -21,10 +21,14 @@ public function indexAction() $host = $this->getParam('host'); $service = $this->getParam('srv'); + $serviceTitle = ''; + if ($service && $service !== '_HOST_') { + $serviceTitle = sprintf(' | %s: %s', $this->translate('Service'), $service); + } $this->view->title = $title = sprintf('%s: %s%s', $this->translate('Host'), $host, - ($service ? sprintf(' | %s: %s', $this->translate('Service'), $service) : '') + $serviceTitle ); $this->getTabs()->add('graph', array( From 6a1b76ac653a59e8a70f9fdddd87ba53a53b9481 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Tue, 15 Aug 2017 10:58:11 +0200 Subject: [PATCH 5/5] Grapher: Change alt text to loading --- library/Pnp/ProvidedHook/Grapher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Pnp/ProvidedHook/Grapher.php b/library/Pnp/ProvidedHook/Grapher.php index e334c52..2dcfbda 100644 --- a/library/Pnp/ProvidedHook/Grapher.php +++ b/library/Pnp/ProvidedHook/Grapher.php @@ -254,7 +254,7 @@ private function getPreviewImg($host, $service, $view) $url, htmlspecialchars($title), $imgUrl, - htmlspecialchars($viewName) + htmlspecialchars(mt('pnp', 'Loading') . '...') ); }