Implement schedule host/service check action

Refactored existing schedule action and fixed selectCommandTargets.

refs #4355
This commit is contained in:
Johannes Meyer 2013-07-05 16:07:50 +02:00 committed by Marius Hein
parent aa241865b4
commit 15ab7f188a
3 changed files with 139 additions and 14 deletions

View File

@ -0,0 +1,102 @@
<?php
namespace Icinga\Form;
use Icinga\Form\Builder;
use Icinga\Form\Elements\Date;
use Icinga\Form\Elements\Time;
class SendCommand extends Builder
{
public function __construct($message)
{
parent::__construct();
$this->message = $message;
$this->init();
}
public function init()
{
$this->setMethod("post");
$note = new \Zend_Form_Element_Note("note");
$note->setValue($this->message);
$this->addElement($note);
$this->addElement("hidden", "servicelist");
$this->addElement("hidden", "hostlist");
}
public function setHosts($hosts)
{
$this->setDefault("hostlist", $hosts);
}
public function getHosts()
{
return $this->getValue("hostlist");
}
public function setServices($services)
{
$this->setDefault("servicelist", $services);
}
public function getServices()
{
return $this->getValue("servicelist");
}
public function addCheckbox($id, $label, $checked)
{
$this->addElement("checkbox", $id, array(
'checked' => $checked,
'label' => $label
)
);
}
public function isChecked($id)
{
return $this->getElement($id)->isChecked();
}
public function addSubmitButton($label)
{
$this->addElement("submit", "btn_submit", array(
'label' => $label
)
);
}
public function addDatePicker($id, $label, $value = "")
{
$date = new Date($id);
$date->setValue($value);
$this->addElement($date, $id, array(
'label' => $label
)
);
}
public function getDate($id)
{
return $this->getValue($id);
}
public function addTimePicker($id, $label, $value = "")
{
$time = new Time($id);
$time->setValue($value);
$this->addElement($time, $id, array(
'label' => $label
)
);
}
public function getTime($id)
{
return $this->getValue($id);
}
}
?>

View File

@ -1,5 +1,6 @@
<?php
use Icinga\Backend;
use Icinga\Form\SendCommand;
use Icinga\Form\Confirmation;
use Icinga\Application\Config;
use Icinga\Web\ModuleActionController;
@ -41,29 +42,27 @@ class Monitoring_CommandController extends ModuleActionController
}
}
private function selectCommandTargets()
private function selectCommandTargets($hostname, $servicename = null)
{
$hostname = $this->_request->getPost("hosts");
$servicename = $this->_request->getPost("services");
$target = "hostlist";
$filter = array();
if (!$hostname && !$servicename) {
throw new MissingParameterException("Missing host and service definition");
}
if ($hostname) {
$filter["hostname"] = explode(";", $hostname);
$filter["host_name"] = $hostname;
}
if ($servicename) {
$filter["servicedescription"] = explode(";", $servicename);
$filter["service_description"] = $servicename;
$target = "servicelist";
}
return Backend::getInstance()->select()->from($target)->applyFilters($filter)->fetchAll();
}
private function getMandatoryParameter($name)
private function getParameter($name, $mandatory = true)
{
$value = $this->_request->getParam($name);
if (!$value) {
if ($mandatory && !$value) {
throw new MissingParameterException("Missing parameter $name");
}
return $value;
@ -82,15 +81,38 @@ class Monitoring_CommandController extends ModuleActionController
}
}
public function sendReschedule()
public function schedulecheckAction()
{
$forced = (trim($this->_getParam("forced"), false) === "true");
$time = $this->_request->getPost("time", false);
$childs = $this->_request->getPost("withChilds", false);
if ($forced) {
$this->target->scheduleForcedCheck($this->selectCommandTargets(), $time, $childs);
$form = new SendCommand("Schedule Host/Service check");
$form->addDatePicker("checkDate", "Check date", date("m-d-Y"));
$form->addTimePicker("checkTime", "Check time", date("h:i A"));
$form->addCheckbox("forceCheck", "Force check", false);
if ($this->_request->isPost()) {
if ($form->isValid()) {
$withChilds = false;
$services = $form->getServices();
$time = sprintf("%s %s", $form->getDate("checkDate"), $form->getTime("checkTime"));
if (!$services || $services === "all") {
$withChilds = $services === "all";
$targets = $this->selectCommandTargets($form->getHosts());
} else {
$targets = $this->selectCommandTargets($form->getHosts(), $services);
}
if ($form->isChecked("forceCheck")) {
$this->target->scheduleForcedCheck($targets, $time, $withChilds);
} else {
$this->target->scheduleCheck($targets, $time, $withChilds);
}
}
} else {
$this->target->scheduleCheck($this->selectCommandTargets(), $time, $childs);
$form->setServices($this->getParameter("services", false));
$form->setHosts($this->getParameter("hosts"));
$form->setAction($this->view->url());
$form->addSubmitButton("Commit");
$this->view->form = $form;
}
}

View File

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