From 15ab7f188afe173644e2e060b4f897461947e481 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 5 Jul 2013 16:07:50 +0200 Subject: [PATCH] Implement schedule host/service check action Refactored existing schedule action and fixed selectCommandTargets. refs #4355 --- library/Icinga/Form/SendCommand.php | 102 ++++++++++++++++++ .../controllers/CommandController.php | 50 ++++++--- .../views/scripts/command/schedulecheck.phtml | 1 + 3 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 library/Icinga/Form/SendCommand.php create mode 100644 modules/monitoring/application/views/scripts/command/schedulecheck.phtml diff --git a/library/Icinga/Form/SendCommand.php b/library/Icinga/Form/SendCommand.php new file mode 100644 index 000000000..dc363de19 --- /dev/null +++ b/library/Icinga/Form/SendCommand.php @@ -0,0 +1,102 @@ +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); + } +} + +?> \ No newline at end of file diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index b3c4db385..8e05853ef 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -1,5 +1,6 @@ _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; } } diff --git a/modules/monitoring/application/views/scripts/command/schedulecheck.phtml b/modules/monitoring/application/views/scripts/command/schedulecheck.phtml new file mode 100644 index 000000000..3ded5a7be --- /dev/null +++ b/modules/monitoring/application/views/scripts/command/schedulecheck.phtml @@ -0,0 +1 @@ +form ?>