From abda65f4bdd0ab563a8395654b516dd1d2ee2321 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 4 Jul 2013 13:52:34 +0200 Subject: [PATCH] Refactor command controller and implement restart action. Refactored existing command controller so that it reflects the correct coding style, rewritten the icinga restart command and added a very simple form for it. refs #4355 --- application/forms/Builder.php | 5 +- library/Icinga/Form/Confirmation.php | 59 ++++++++++ library/Icinga/Form/Elements/Note.php | 15 +++ .../controllers/CommandController.php | 110 +++++++++--------- .../views/scripts/command/restart.phtml | 1 + 5 files changed, 130 insertions(+), 60 deletions(-) create mode 100644 library/Icinga/Form/Confirmation.php create mode 100644 library/Icinga/Form/Elements/Note.php create mode 100644 modules/monitoring/application/views/scripts/command/restart.phtml diff --git a/application/forms/Builder.php b/application/forms/Builder.php index 89d2bccfb..76aa78361 100644 --- a/application/forms/Builder.php +++ b/application/forms/Builder.php @@ -369,12 +369,13 @@ class Builder return true; } - if ($this->form->getElement(self::CSRF_ID) == null) { + if ($this->getForm()->getElement(self::CSRF_ID) == null) { return false; } $sessionId = $sessionId ? $sessionId : session_id(); - $seed = $this->form->getElement(self::CSRF_ID.'_seed')->getValue(); + $seed = $this->getForm()->getElement(self::CSRF_ID.'_seed')->getValue(); + if (! is_numeric($seed)) { return false; } diff --git a/library/Icinga/Form/Confirmation.php b/library/Icinga/Form/Confirmation.php new file mode 100644 index 000000000..106c26793 --- /dev/null +++ b/library/Icinga/Form/Confirmation.php @@ -0,0 +1,59 @@ +message = $message; + $this->style = $style; + $this->init(); + } + + public function init() + { + $this->setMethod("post"); + $this->addElement(new Note($this->message)); + + if ($this->style === self::YES_NO) { + $this->addElement('submit', 'btn_yes', array( + 'label' => 'Yes' + ) + ); + $this->addElement('submit', 'btn_no', array( + 'label' => 'No' + ) + ); + } elseif ($this->style === self::OK_CANCEL) { + $this->addElement('submit', 'btn_ok', array( + 'label' => 'Ok' + ) + ); + $this->addElement('submit', 'btn_cancel', array( + 'label' => 'Cancel' + ) + ); + } else { + throw new ProgrammingError("Button style must be one of: YES_NO, OK_CANCEL"); + } + } + + public function isConfirmed() + { + if ($this->style === self::YES_NO) { + return $this->isSubmitted("btn_yes"); + } elseif ($this->style === self::OK_CANCEL) { + return $this->isSubmitted("btn_ok"); + } + } +} + +?> diff --git a/library/Icinga/Form/Elements/Note.php b/library/Icinga/Form/Elements/Note.php new file mode 100644 index 000000000..c8eedbdfe --- /dev/null +++ b/library/Icinga/Form/Elements/Note.php @@ -0,0 +1,15 @@ +setValue("

$value

"); + } +} + +?> \ No newline at end of file diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index a8142fe17..b3c4db385 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -1,6 +1,11 @@ _request->getPost($name, false); - if ($value === false) - throw new IcingaException\MissingParameterException("Missing parameter $name"); - return $value; - } - public function init() { - if (!$this->_request->isPost()) { - $this->_response->clearBody(); - $this->_response->clearHeaders(); - $this->_response->setHttpResponseCode(405); - $this->_redirect("/"); - } - if (!$this->hasValidToken()) - throw new Exception("Invalid token given", 401); - $this->_helper->viewRenderer->setNoRender(true); - $this->_helper->layout()->disableLayout(); - $targets = $this->config->instances; - $instance = $this->_getParam("instance"); - if ($instance && isset($targets[$instance])) { - $this->target = new \Icinga\Protocol\Commandpipe\CommandPipe($targets[$instance]); - } else { - foreach ($targets as $target) { - $this->target = new \Icinga\Protocol\Commandpipe\CommandPipe($target); - break; + if ($this->_request->isPost()) { + // We do not need to display a view.. + $this->_helper->viewRenderer->setNoRender(true); + // ..nor the overall site layout in case its a POST request. + $this->_helper->layout()->disableLayout(); + + $instance = $this->_request->getPost("instance"); + $target_config = Config::getInstance()->getModuleConfig("instances", "monitoring"); + if ($instance) { + if (isset($target_config[$instance])) { + $this->target = new CommandPipe($target_config[$instance]); + } else { + throw new ConfigurationError("Instance $instance is not configured"); + } + } else { + $target_info = $target_config->current(); // Take the very first section + if ($target_info === false) { + throw new ConfigurationError("Not any instances are configured yet"); + } else { + $this->target = new CommandPipe($target_info); + } } } } private function selectCommandTargets() { - $hostname = $this->_getParam("hosts"); - $servicename = $this->_getParam("services"); + $hostname = $this->_request->getPost("hosts"); + $servicename = $this->_request->getPost("services"); $target = "hostlist"; $filter = array(); if (!$hostname && !$servicename) { - throw new IcingaException\MissingParameterException("Missing host and service definition"); + throw new MissingParameterException("Missing host and service definition"); } if ($hostname) { $filter["hostname"] = explode(";", $hostname); @@ -57,12 +57,29 @@ class Monitoring_CommandController extends ModuleActionController $filter["servicedescription"] = explode(";", $servicename); $target = "servicelist"; } + return Backend::getInstance()->select()->from($target)->applyFilters($filter)->fetchAll(); + } - return $this->backend = Icinga\Backend::getInstance() - ->select() - ->from($target) - ->applyFilters($filter) - ->fetchAll(); + private function getMandatoryParameter($name) + { + $value = $this->_request->getParam($name); + if (!$value) { + throw new MissingParameterException("Missing parameter $name"); + } + return $value; + } + + public function restartAction() + { + $form = new Confirmation("Restart Icinga?", Confirmation::YES_NO); + if ($this->_request->isPost()) { + if ($form->isValid() && $form->isConfirmed()) { + $this->target->restartIcinga(); + } + } else { + $form->setAction($this->view->url()); + $this->view->form = $form; + } } public function sendReschedule() @@ -168,11 +185,6 @@ class Monitoring_CommandController extends ModuleActionController } } - public function sendRestartIcinga() - { - $this->target->restartIcinga(); - } - public function sendDeletedowntime() { if ($this->_request->getPost("downtimes")) { @@ -192,22 +204,4 @@ class Monitoring_CommandController extends ModuleActionController $this->target->removeDowntime($this->selectCommandTargets()); } } - - - public function __call($method, $args) - { - $command = substr($method, 0, -6); - if ('Action' == substr($method, -6)) { - $command[0] = strtoupper($command[0]); - if (method_exists($this, "send$command")) { - return $this->{"send$command"}(); - } - throw new Exception("Invalid command $command", 404); - } - - throw new BadMethodCallException("Call to undefined method $method"); - - } - - } diff --git a/modules/monitoring/application/views/scripts/command/restart.phtml b/modules/monitoring/application/views/scripts/command/restart.phtml new file mode 100644 index 000000000..4bae1498a --- /dev/null +++ b/modules/monitoring/application/views/scripts/command/restart.phtml @@ -0,0 +1 @@ +form ?> \ No newline at end of file