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
This commit is contained in:
Johannes Meyer 2013-07-04 13:52:34 +02:00 committed by Marius Hein
parent 8d3ff94696
commit abda65f4bd
5 changed files with 130 additions and 60 deletions

View File

@ -369,12 +369,13 @@ class Builder
return true; return true;
} }
if ($this->form->getElement(self::CSRF_ID) == null) { if ($this->getForm()->getElement(self::CSRF_ID) == null) {
return false; return false;
} }
$sessionId = $sessionId ? $sessionId : session_id(); $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)) { if (! is_numeric($seed)) {
return false; return false;
} }

View File

@ -0,0 +1,59 @@
<?php
namespace Icinga\Form;
use Icinga\Form\Builder;
use Icinga\Form\Elements\Note;
use Icinga\Exception\ProgrammingError;
class Confirmation extends Builder
{
const YES_NO = 0;
const OK_CANCEL = 1;
public function __construct($message, $style)
{
parent::__construct();
$this->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");
}
}
}
?>

View File

@ -0,0 +1,15 @@
<?php
namespace Icinga\Form\Elements;
class Note extends \Zend_Form_Element
{
public $helper = "formNote";
public function __construct($value)
{
parent::__construct("notification");
$this->setValue("<p>$value</p>");
}
}
?>

View File

@ -1,6 +1,11 @@
<?php <?php
use Icinga\Backend;
use Icinga\Form\Confirmation;
use Icinga\Application\Config;
use Icinga\Web\ModuleActionController; use Icinga\Web\ModuleActionController;
use \Icinga\Exception as IcingaException; use Icinga\Protocol\Commandpipe\CommandPipe;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\MissingParameterException;
class Monitoring_CommandController extends ModuleActionController class Monitoring_CommandController extends ModuleActionController
{ {
@ -9,46 +14,41 @@ class Monitoring_CommandController extends ModuleActionController
*/ */
public $target; public $target;
private function getMandatoryParameter($name)
{
$value = $this->_request->getPost($name, false);
if ($value === false)
throw new IcingaException\MissingParameterException("Missing parameter $name");
return $value;
}
public function init() public function init()
{ {
if (!$this->_request->isPost()) { if ($this->_request->isPost()) {
$this->_response->clearBody(); // We do not need to display a view..
$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->viewRenderer->setNoRender(true);
// ..nor the overall site layout in case its a POST request.
$this->_helper->layout()->disableLayout(); $this->_helper->layout()->disableLayout();
$targets = $this->config->instances;
$instance = $this->_getParam("instance"); $instance = $this->_request->getPost("instance");
if ($instance && isset($targets[$instance])) { $target_config = Config::getInstance()->getModuleConfig("instances", "monitoring");
$this->target = new \Icinga\Protocol\Commandpipe\CommandPipe($targets[$instance]); if ($instance) {
if (isset($target_config[$instance])) {
$this->target = new CommandPipe($target_config[$instance]);
} else { } else {
foreach ($targets as $target) { throw new ConfigurationError("Instance $instance is not configured");
$this->target = new \Icinga\Protocol\Commandpipe\CommandPipe($target); }
break; } 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() private function selectCommandTargets()
{ {
$hostname = $this->_getParam("hosts"); $hostname = $this->_request->getPost("hosts");
$servicename = $this->_getParam("services"); $servicename = $this->_request->getPost("services");
$target = "hostlist"; $target = "hostlist";
$filter = array(); $filter = array();
if (!$hostname && !$servicename) { if (!$hostname && !$servicename) {
throw new IcingaException\MissingParameterException("Missing host and service definition"); throw new MissingParameterException("Missing host and service definition");
} }
if ($hostname) { if ($hostname) {
$filter["hostname"] = explode(";", $hostname); $filter["hostname"] = explode(";", $hostname);
@ -57,12 +57,29 @@ class Monitoring_CommandController extends ModuleActionController
$filter["servicedescription"] = explode(";", $servicename); $filter["servicedescription"] = explode(";", $servicename);
$target = "servicelist"; $target = "servicelist";
} }
return Backend::getInstance()->select()->from($target)->applyFilters($filter)->fetchAll();
}
return $this->backend = Icinga\Backend::getInstance() private function getMandatoryParameter($name)
->select() {
->from($target) $value = $this->_request->getParam($name);
->applyFilters($filter) if (!$value) {
->fetchAll(); 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() public function sendReschedule()
@ -168,11 +185,6 @@ class Monitoring_CommandController extends ModuleActionController
} }
} }
public function sendRestartIcinga()
{
$this->target->restartIcinga();
}
public function sendDeletedowntime() public function sendDeletedowntime()
{ {
if ($this->_request->getPost("downtimes")) { if ($this->_request->getPost("downtimes")) {
@ -192,22 +204,4 @@ class Monitoring_CommandController extends ModuleActionController
$this->target->removeDowntime($this->selectCommandTargets()); $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");
}
} }

View File

@ -0,0 +1 @@
<?= $this->form ?>