Implement a simple configurable command

resolves #4769
This commit is contained in:
Marius Hein 2013-10-08 14:19:14 +02:00 committed by Eric Lippmann
parent d7f6dcb3ce
commit 0626bb19cb
5 changed files with 482 additions and 87 deletions

View File

@ -27,24 +27,25 @@
*/ */
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
use \Icinga\Application\Icinga; use Icinga\Application\Icinga;
use \Icinga\Application\Config; use Icinga\Application\Config;
use \Icinga\Application\Logger; use Icinga\Application\Logger;
use \Icinga\Web\Form; use Icinga\Module\Monitoring\Form\Command\SingleArgumentCommandForm;
use \Icinga\Web\Controller\ActionController; use Icinga\Web\Form;
use \Icinga\Protocol\Commandpipe\CommandPipe; use Icinga\Web\Controller\ActionController;
use \Icinga\Exception\ConfigurationError; use Icinga\Protocol\Commandpipe\CommandPipe;
use \Icinga\Exception\MissingParameterException; use Icinga\Exception\ConfigurationError;
use \Icinga\Module\Monitoring\Backend; use Icinga\Exception\MissingParameterException;
use \Icinga\Module\Monitoring\Form\Command\AcknowledgeForm; use Icinga\Module\Monitoring\Backend;
use \Icinga\Module\Monitoring\Form\Command\CommentForm; use Icinga\Module\Monitoring\Form\Command\AcknowledgeForm;
use \Icinga\Module\Monitoring\Form\Command\CommandForm; use Icinga\Module\Monitoring\Form\Command\CommentForm;
use \Icinga\Module\Monitoring\Form\Command\CommandWithIdentifierForm; use Icinga\Module\Monitoring\Form\Command\CommandForm;
use \Icinga\Module\Monitoring\Form\Command\CustomNotificationForm; use Icinga\Module\Monitoring\Form\Command\CommandWithIdentifierForm;
use \Icinga\Module\Monitoring\Form\Command\DelayNotificationForm; use Icinga\Module\Monitoring\Form\Command\CustomNotificationForm;
use \Icinga\Module\Monitoring\Form\Command\RescheduleNextCheckForm; use Icinga\Module\Monitoring\Form\Command\DelayNotificationForm;
use \Icinga\Module\Monitoring\Form\Command\ScheduleDowntimeForm; use Icinga\Module\Monitoring\Form\Command\RescheduleNextCheckForm;
use \Icinga\Module\Monitoring\Form\Command\SubmitPassiveCheckResultForm; use Icinga\Module\Monitoring\Form\Command\ScheduleDowntimeForm;
use Icinga\Module\Monitoring\Form\Command\SubmitPassiveCheckResultForm;
/** /**
* Class Monitoring_CommandController * Class Monitoring_CommandController
@ -169,8 +170,26 @@ class Monitoring_CommandController extends ActionController
$fields[] = "service_description"; $fields[] = "service_description";
$fields[] = "service_state"; $fields[] = "service_state";
} }
$query = Backend::getInstance($this->_getParam('backend'))->select()->from("status", $fields);
return $query->applyFilters($filter)->fetchAll(); // Implemented manuall search because api is not ready.
// @TODO Implement this using the database api #4663 (mh)
$query = Backend::createBackend($this->_getParam('backend'))->select()->from("status", $fields);
$data = $query->fetchAll();
$out = array();
foreach ($data as $o) {
$test = (array)$o;
if ($test['host_name'] === $hostname) {
if (!$servicename) {
$out[] = (object) $o;
} elseif ($servicename && strtolower($test['service_description']) === strtolower($servicename)) {
$out[] = (object) $o;
}
}
}
return $out;
} catch (\Exception $e) { } catch (\Exception $e) {
Logger::error( Logger::error(
"CommandController: SQL Query '%s' failed (message %s) ", "CommandController: SQL Query '%s' failed (message %s) ",
@ -180,6 +199,29 @@ class Monitoring_CommandController extends ActionController
} }
} }
/**
* Convert other params into valid command structure
*
* @param array $supported Array of supported parameter names
* @param array $params Parameters from request
*
* @return array Return
*/
private function selectOtherTargets(array $supported, array $params)
{
$others = array_diff_key($supported, array('host' => true, 'service' => true));
$otherParams = array_intersect_key($params, $others);
$out = array();
foreach ($otherParams as $name => $value) {
$data = new stdClass();
$data->{$name} = $value;
$out[] = $data;
}
return $out;
}
/** /**
* Displays a list of all commands * Displays a list of all commands
* *
@ -208,25 +250,24 @@ class Monitoring_CommandController extends ActionController
*/ */
private function setSupportedParameters(array $supported) private function setSupportedParameters(array $supported)
{ {
$given = array_intersect_key(array_flip($supported), $this->getRequest()->getParams()); $objects = array();
$supported = array_flip($supported);
$given = array_intersect_key($supported, $this->getRequest()->getParams());
if (empty($given)) { if (empty($given)) {
throw new \Exception('Missing parameter, supported: '.implode(', ', $supported)); throw new \Exception('Missing parameter, supported: '.implode(', ', $supported));
} }
if (isset($given["host"])) {
$this->view->objects = $this->selectCommandTargets(!in_array("service", $supported)); if (isset($given['host'])) {
if (empty($this->view->objects)) { $objects = $this->selectCommandTargets(!in_array("service", $supported));
if (empty($objects)) {
throw new \Exception("No objects found for your command"); throw new \Exception("No objects found for your command");
} }
} elseif (in_array("downtimeid", $supported)) {
$this->view->objects = array();
$downtimes = $this->getParam("downtimeid");
if (!is_array($downtimes)) {
$downtimes = array($downtimes);
}
foreach ($downtimes as $downtimeId) {
$this->view->objects[] = (object) array("downtime_id" => $downtimeId);
}
} }
$this->view->objects = $objects;
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -239,14 +280,16 @@ class Monitoring_CommandController extends ActionController
public function disableactivechecksAction() public function disableactivechecksAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setCommand('DISABLE_HOST_CHECK', 'DISABLE_SVC_CHECK');
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable Active Checks')); $form->setSubmitLabel(t('Disable Active Checks'));
$form->addNote(t('Disable active checks for this object.')); $form->addNote(t('Disable active checks for this object.'));
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->disableActiveChecks($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -256,14 +299,16 @@ class Monitoring_CommandController extends ActionController
public function enableactivechecksAction() public function enableactivechecksAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setCommand('ENABLE_HOST_CHECK', 'ENABLE_SVC_CHECK');
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable active checks')); $form->setSubmitLabel(t('Enable Active Checks'));
$form->addNote(t('Enable active checks for this object.')); $form->addNote(t('Enable active checks for this object.'));
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->enableActiveChecks($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -312,14 +357,15 @@ class Monitoring_CommandController extends ActionController
public function stopobsessingAction() public function stopobsessingAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Stop obsessing')); $form->setSubmitLabel(t('Stop obsessing'));
$form->addNote(t('Stop obsessing over this object.')); $form->addNote(t('Stop obsessing over this object.'));
$form->setCommand('STOP_OBSESSING_OVER_HOST', 'STOP_OBSESSING_OVER_SVC');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->stopObsessing($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -329,14 +375,15 @@ class Monitoring_CommandController extends ActionController
public function startobsessingAction() public function startobsessingAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Start obsessing')); $form->setSubmitLabel(t('Start obsessing'));
$form->addNote(t('Start obsessing over this object.')); $form->addNote(t('Start obsessing over this object.'));
$form->setCommand('START_OBSESSING_OVER_HOST', 'START_OBSESSING_OVER_SVC');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->startObsessing($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -346,14 +393,15 @@ class Monitoring_CommandController extends ActionController
public function stopacceptingpassivechecksAction() public function stopacceptingpassivechecksAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Stop Accepting Passive Checks')); $form->setSubmitLabel(t('Stop Accepting Passive Checks'));
$form->addNote(t('Passive checks for this object will be omitted.')); $form->addNote(t('Passive checks for this object will be omitted.'));
$form->setCommand('STOP_ACCEPTING_PASSIVE_HOST_CHECKS', 'STOP_ACCEPTING_PASSIVE_SVC_CHECKS');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->disablePassiveChecks($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -363,14 +411,15 @@ class Monitoring_CommandController extends ActionController
public function startacceptingpassivechecksAction() public function startacceptingpassivechecksAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Start Accepting Passive Checks')); $form->setSubmitLabel(t('Start Accepting Passive Checks'));
$form->addNote(t('Passive checks for this object will be accepted.')); $form->addNote(t('Passive checks for this object will be accepted.'));
$form->setCommand('START_ACCEPTING_PASSIVE_HOST_CHECKS', 'START_ACCEPTING_PASSIVE_SVC_CHECKS');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->enableActiveChecks($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -380,14 +429,15 @@ class Monitoring_CommandController extends ActionController
public function disablenotificationsAction() public function disablenotificationsAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable Notifications')); $form->setSubmitLabel(t('Disable Notifications'));
$form->addNote(t('Notifications for this object will be disabled.')); $form->addNote(t('Notifications for this object will be disabled.'));
$form->setCommand('DISABLE_HOST_NOTIFICATIONS', 'DISABLE_SVC_NOTIFICATIONS');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->disableNotifications($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -396,14 +446,16 @@ class Monitoring_CommandController extends ActionController
*/ */
public function enablenotificationsAction() public function enablenotificationsAction()
{ {
$form = new CommandForm(); $this->setSupportedParameters(array('host', 'service'));
$form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable Notifications')); $form->setSubmitLabel(t('Enable Notifications'));
$form->addNote(t('Notifications for this object will be enabled.')); $form->addNote(t('Notifications for this object will be enabled.'));
$form->setCommand('ENABLE_HOST_NOTIFICATIONS', 'ENABLE_SVC_NOTIFICATIONS');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->enableNotifications($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -462,14 +514,15 @@ class Monitoring_CommandController extends ActionController
public function removedowntimeswithchildrenAction() public function removedowntimeswithchildrenAction()
{ {
$this->setSupportedParameters(array('host')); $this->setSupportedParameters(array('host'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Remove Downtime(s)')); $form->setSubmitLabel(t('Remove Downtime(s)'));
$form->addNote(t('Remove downtime(s) from this host and its services.')); $form->addNote(t('Remove downtime(s) from this host and its services.'));
$form->setCommand('DEL_DOWNTIME_BY_HOST_NAME', 'DEL_DOWNTIME_BY_HOST_NAME');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->removeDowntime($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -479,15 +532,18 @@ class Monitoring_CommandController extends ActionController
public function disablenotificationswithchildrenAction() public function disablenotificationswithchildrenAction()
{ {
$this->setSupportedParameters(array('host')); $this->setSupportedParameters(array('host'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable Notifications')); $form->setSubmitLabel(t('Disable Notifications'));
$form->addNote(t('Notifications for this host and its services will be disabled.')); $form->addNote(t('Notifications for this host and its services will be disabled.'));
$form->setCommand('DISABLE_ALL_NOTIFICATIONS_BEYOND_HOST');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->disableNotifications($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
$this->target->disableNotificationsForServices($this->view->objects); $form->setCommand('DISABLE_HOST_NOTIFICATIONS', 'DISABLE_SVC_NOTIFICATIONS');
$this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -497,15 +553,17 @@ class Monitoring_CommandController extends ActionController
public function enablenotificationswithchildrenAction() public function enablenotificationswithchildrenAction()
{ {
$this->setSupportedParameters(array('host')); $this->setSupportedParameters(array('host'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable Notifications')); $form->setSubmitLabel(t('Enable Notifications'));
$form->addNote(t('Notifications for this host and its services will be enabled.')); $form->addNote(t('Notifications for this host and its services will be enabled.'));
$form->setCommand('ENABLE_ALL_NOTIFICATIONS_BEYOND_HOST');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->enableNotifications($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
$this->target->enableNotificationsForServices($this->view->objects); $form->setCommand('ENABLE_HOST_NOTIFICATIONS', 'ENABLE_SVC_NOTIFICATIONS');
$this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -533,15 +591,16 @@ class Monitoring_CommandController extends ActionController
public function disableactivecheckswithchildrenAction() public function disableactivecheckswithchildrenAction()
{ {
$this->setSupportedParameters(array('host')); $this->setSupportedParameters(array('host'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable Active Checks')); $form->setSubmitLabel(t('Disable Active Checks'));
$form->addNote(t('Disable active checks for this host and its services.')); $form->addNote(t('Disable active checks for this host and its services.'));
$form->setCommand('DISABLE_HOST_CHECK');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->disableActiveChecks($this->view->objects); // @TODO(mh): Missing child command
$this->target->disableActiveChecksWithChildren($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -551,15 +610,16 @@ class Monitoring_CommandController extends ActionController
public function enableactivecheckswithchildrenAction() public function enableactivecheckswithchildrenAction()
{ {
$this->setSupportedParameters(array('host')); $this->setSupportedParameters(array('host'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable Active Checks')); $form->setSubmitLabel(t('Enable Active Checks'));
$form->addNote(t('Enable active checks for this host and its services.')); $form->addNote(t('Enable active checks for this host and its services.'));
$form->setCommand('ENABLE_HOST_CHECK');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->enableActiveChecks($this->view->objects); // @TODO(mh): Missing child command
$this->target->enableActiveChecksWithChildren($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -569,14 +629,15 @@ class Monitoring_CommandController extends ActionController
public function disableeventhandlerAction() public function disableeventhandlerAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable Event Handler')); $form->setSubmitLabel(t('Disable Event Handler'));
$form->addNote(t('Disable event handler for this object.')); $form->addNote(t('Disable event handler for this object.'));
$form->setCommand('DISABLE_HOST_EVENT_HANDLER', 'DISABLE_SVC_EVENT_HANDLER');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->disableEventHandler($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -586,14 +647,15 @@ class Monitoring_CommandController extends ActionController
public function enableeventhandlerAction() public function enableeventhandlerAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable Event Handler')); $form->setSubmitLabel(t('Enable Event Handler'));
$form->addNote(t('Enable event handler for this object.')); $form->addNote(t('Enable event handler for this object.'));
$form->setCommand('ENABLE_HOST_EVENT_HANDLER', 'ENABLE_SVC_EVENT_HANDLER');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->enableEventHandler($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -603,14 +665,15 @@ class Monitoring_CommandController extends ActionController
public function disableflapdetectionAction() public function disableflapdetectionAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable Flapping Detection')); $form->setSubmitLabel(t('Disable Flapping Detection'));
$form->addNote(t('Disable flapping detection for this object.')); $form->addNote(t('Disable flapping detection for this object.'));
$form->setCommand('DISABLE_HOST_FLAP_DETECTION', 'DISABLE_SVC_FLAP_DETECTION');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->disableFlappingDetection($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -620,14 +683,15 @@ class Monitoring_CommandController extends ActionController
public function enableflapdetectionAction() public function enableflapdetectionAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable Flapping Detection')); $form->setSubmitLabel(t('Enable Flapping Detection'));
$form->addNote(t('Enable flapping detection for this object.')); $form->addNote(t('Enable flapping detection for this object.'));
$form->setCommand('ENABLE_HOST_FLAP_DETECTION', 'ENABLE_SVC_FLAP_DETECTION');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->enableFlappingDetection($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -638,7 +702,7 @@ class Monitoring_CommandController extends ActionController
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommentForm(); $form = new CommentForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->_request);
$this->setForm($form); $this->setForm($form);
@ -647,20 +711,41 @@ class Monitoring_CommandController extends ActionController
} }
} }
/**
* Remove a single comment
*/
public function removecommentAction()
{
$this->setSupportedParameters(array('commentid', 'host', 'service'));
$form = new SingleArgumentCommandForm();
$form->setRequest($this->_request);
$form->setCommand('DEL_HOST_COMMENT', 'DEL_SERVICE_COMMENT');
$form->setParameterName('commentid');
$form->setSubmitLabel(t('Remove comment'));
$form->setObjectIgnoreFlag(true);
$this->setForm($form);
if ($form->IsSubmittedAndValid() === true) {
$this->target->sendCommand($form->createCommand(), $this->view->objects);
}
}
/** /**
* Handle command resetattributes * Handle command resetattributes
*/ */
public function resetattributesAction() public function resetattributesAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Reset Attributes')); $form->setSubmitLabel(t('Reset Attributes'));
$form->addNote(t('Reset modified attributes to its default.')); $form->addNote(t('Reset modified attributes to its default.'));
$form->setCommand('CHANGE_HOST_MODATTR', 'CHANGE_SVC_MODATTR');
$form->setParameterValue(0);
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->resetAttributes($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -687,14 +772,15 @@ class Monitoring_CommandController extends ActionController
public function removeacknowledgementAction() public function removeacknowledgementAction()
{ {
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new CommandForm(); $form = new SingleArgumentCommandForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Remove Problem Acknowledgement')); $form->setSubmitLabel(t('Remove Problem Acknowledgement'));
$form->addNote(t('Remove problem acknowledgement for this object.')); $form->addNote(t('Remove problem acknowledgement for this object.'));
$form->setCommand('REMOVE_HOST_ACKNOWLEDGEMENT', 'REMOVE_SVC_ACKNOWLEDGEMENT');
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->removeAcknowledge($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }

View File

@ -28,14 +28,15 @@
namespace Icinga\Module\Monitoring\Form\Command; namespace Icinga\Module\Monitoring\Form\Command;
use \Zend_Config; use Zend_Config;
use \Icinga\Web\Form; use Zend_Form_Element_Hidden;
use \Zend_Form_Element_Hidden; use Icinga\Module\Monitoring\Command\AcknowledgeCommand;
use Icinga\Web\Form;
/** /**
* Simple confirmation command * Simple confirmation command
*/ */
class CommandForm extends Form abstract class CommandForm extends Form
{ {
/** /**
* Create an instance name containing hidden field * Create an instance name containing hidden field
@ -126,4 +127,11 @@ class CommandForm extends Form
) )
); );
} }
/**
* Create command object for CommandPipe protocol
*
* @return AcknowledgeCommand
*/
abstract public function createCommand();
} }

View File

@ -0,0 +1,149 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Form\Command;
use \Zend_Form_Element_Hidden;
use Icinga\Module\Monitoring\Command\AcknowledgeCommand;
use Icinga\Module\Monitoring\Command\SingleArgumentCommand;
/**
* Sending commands to core which only have one value
*/
class SingleArgumentCommandForm extends CommandForm
{
/**
* Name of host command
*
* @var string
*/
private $hostCommand;
/**
* Name of service command
*
* @var string
*/
private $serviceCommand;
/**
* Name of the parameter used as value
*
* @var string
*/
private $parameterName;
/**
* Value of used parameter
*
* @var mixed
*/
private $parameterValue;
/**
* Flag to ignore object name
*
* @var bool
*/
private $ignoreObject = false;
/**
* Set command names
*
* @param string $hostCommand Name of host command
* @param string $serviceCommand Name of service command
*/
public function setCommand($hostCommand, $serviceCommand = null)
{
$this->hostCommand = $hostCommand;
if ($serviceCommand !== null) {
$this->serviceCommand = $serviceCommand;
}
}
/**
* Use an explicit value to send with command
*
* @param mixed $parameterValue
*/
public function setParameterValue($parameterValue)
{
$this->parameterValue = $parameterValue;
}
/**
* Use a form field to take the value from
*
* @param string $parameterName
*/
public function setParameterName($parameterName)
{
$this->parameterName = $parameterName;
}
public function setObjectIgnoreFlag($flag = true)
{
$this->ignoreObject = (bool) $flag;
}
/**
*
*/
protected function create()
{
if ($this->parameterName) {
$field = new Zend_Form_Element_Hidden($this->parameterName);
$value = $this->getRequest()->getParam($field->getName());
$field->setValue($value);
$this->addElement($field);
}
parent::create();
}
/**
* Create command object for CommandPipe protocol
*
* @return SingleArgumentCommand
*/
public function createCommand()
{
$command = new SingleArgumentCommand();
$command->setCommand($this->hostCommand, $this->serviceCommand);
if ($this->parameterValue !== null) {
$command->setValue($this->parameterValue);
} else {
$command->setValue($this->getValue($this->parameterName));
}
$command->setObjectIgnoreFlag($this->ignoreObject);
return $command;
}
}

View File

@ -23,12 +23,7 @@ class Zend_View_Helper_CommandForm extends Zend_View_Helper_Abstract
*/ */
public function simpleForm($commandName, $submitLabel, array $arguments = array()) public function simpleForm($commandName, $submitLabel, array $arguments = array())
{ {
$form = new CommandForm(); return '{{{COMMAND_FORM}}}';
$form->setRequest(Icinga::app()->getFrontController()->getRequest());
$form->setView($this->view);
$form->setAction($this->view->href('monitoring/command/' . $commandName));
$form->setSubmitLabel($submitLabel);
return $form->render();
} }
} }

View File

@ -0,0 +1,157 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\Command;
/**
* Configurable simple command
*/
class SingleArgumentCommand extends Command
{
/**
* Value used for this command
*
* @var mixed
*/
private $value;
/**
* Name of host command
*
* @var string
*/
private $hostCommand;
/**
* Name of service command
*
* @var string
*/
private $serviceCommand;
/**
* Ignore host in command string
*
* @var bool
*/
private $ignoreObject = false;
/**
* Setter for this value
*
* @param mixed $value
*/
public function setValue($value)
{
$this->value = $value;
}
/**
* Setter for command names
*
* @param string $hostCommand
* @param string $serviceCommand
*/
public function setCommand($hostCommand, $serviceCommand)
{
$this->hostCommand = $hostCommand;
$this->serviceCommand = $serviceCommand;
}
/**
* Ignore object values upon command creation
*
* @param bool $flag
*/
public function setObjectIgnoreFlag($flag = true)
{
$this->ignoreObject = (bool) $flag;
}
/**
* Return this command's arguments in the order expected by the actual command definition
*
* @return array
*/
public function getArguments()
{
if ($this->value !== null) {
return array($this->value);
} else {
return array();
}
}
/**
* Build the argument string based on objects and arguments
*
* @param array $objectNames
*
* @return string String to append to command
*/
private function getArgumentString(array $objectNames)
{
$data = array();
if ($this->ignoreObject === true) {
$data = $this->getArguments();
} else {
$data = array_merge($objectNames, $this->getArguments());
}
return implode(';', $data);
}
/**
* Return the command as a string with the given host being inserted
*
* @param string $hostname The name of the host to insert
*
* @return string The string representation of the command
*/
public function getHostCommand($hostname)
{
return strtoupper($this->hostCommand). ';' . $this->getArgumentString(array($hostname));
}
/**
* Return the command as a string with the given host and service being inserted
*
* @param string $hostname The name of the host to insert
* @param string $servicename The name of the service to insert
*
* @return string The string representation of the command
*/
public function getServiceCommand($hostname, $servicename)
{
return strtoupper($this->serviceCommand)
. ';'
. $this->getArgumentString(array($hostname, $servicename));
}
}