From 73e8d60060ffa22703faac0c447e3a3ee4774969 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Sat, 19 Oct 2013 13:24:36 +0200 Subject: [PATCH] Implement global commands resolves #4142 --- doc/command.md | 9 + .../Icinga/Protocol/Commandpipe/Command.php | 29 ++ .../Protocol/Commandpipe/CommandPipe.php | 16 +- library/Icinga/Web/Form.php | 2 +- .../controllers/CommandController.php | 326 ++++++++++++++++-- .../application/forms/Command/CommandForm.php | 43 +++ .../DisableNotificationWithExpireForm.php | 80 +++++ .../Command/SingleArgumentCommandForm.php | 46 ++- .../DisableNotificationWithExpireCommand.php | 116 +++++++ .../Command/SingleArgumentCommand.php | 54 ++- 10 files changed, 674 insertions(+), 47 deletions(-) create mode 100644 modules/monitoring/application/forms/Command/DisableNotificationWithExpireForm.php create mode 100644 modules/monitoring/library/Monitoring/Command/DisableNotificationWithExpireCommand.php diff --git a/doc/command.md b/doc/command.md index 9c26da9bd..f442432a4 100644 --- a/doc/command.md +++ b/doc/command.md @@ -45,6 +45,15 @@ http://localhost:8080/icinga2-web/monitoring/command/scheduledowntime http://localhost:8080/icinga2-web/monitoring/command/addcomment ``` +# Provide a global command for host or service checks + +http://localhost:8080/icinga2-web/monitoring/command/disableactivechecks?global=host +http://localhost:8080/icinga2-web/monitoring/command/disableactivechecks?global=service + +# Provide a object command globally + +http://localhost:8080/icinga2-web/monitoring/command/disablenotifications?global=1 + ## List of commands *Please note that the list is not complete yet, more commands will follow* diff --git a/library/Icinga/Protocol/Commandpipe/Command.php b/library/Icinga/Protocol/Commandpipe/Command.php index bcba3aa2c..56bdd540f 100644 --- a/library/Icinga/Protocol/Commandpipe/Command.php +++ b/library/Icinga/Protocol/Commandpipe/Command.php @@ -63,6 +63,13 @@ abstract class Command */ protected $onlyServices = false; + /** + * Whether it is a global command or not + * + * @var bool + */ + protected $globalCommand = false; + /** * Set whether this command should only affect the services of a host- or servicegroup * @@ -111,6 +118,15 @@ abstract class Command return $this; } + /** + * Getter for flag whether a command is global + * @return bool + */ + public function provideGlobalCommand() + { + return (bool) $this->globalCommand; + } + /** * Return this command's arguments in the order expected by the actual command definition * @@ -160,4 +176,17 @@ abstract class Command { throw new ProgrammingError(get_class($this) . ' does not provide a servicegroup command'); } + + /** + * Return the command as a string for the whole instance + * + * @param string $instance + * + * @return string + * @throws ProgrammingError + */ + public function getGlobalCommand($instance = null) + { + throw new ProgrammingError(getclass($this) . ' does not provide a global command'); + } } diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index 20e389134..f42f0fc14 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -144,12 +144,16 @@ class CommandPipe */ public function sendCommand(Command $command, array $objects) { - foreach ($objects as $object) { - $objectType = $this->getObjectType($object); - if ($objectType === self::TYPE_SERVICE) { - $this->transport->send($command->getServiceCommand($object->host_name, $object->service_description)); - } else { - $this->transport->send($command->getHostCommand($object->host_name)); + if ($command->provideGlobalCommand() === true) { + $this->transport->send($command->getGlobalCommand()); + } else { + foreach ($objects as $object) { + $objectType = $this->getObjectType($object); + if ($objectType === self::TYPE_SERVICE) { + $this->transport->send($command->getServiceCommand($object->host_name, $object->service_description)); + } else { + $this->transport->send($command->getHostCommand($object->host_name)); + } } } } diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 927c255d8..e50207571 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -359,7 +359,7 @@ class Form extends Zend_Form array( 'name' => 'btn_submit', 'label' => $this->submitLabel, - 'class' => 'btn btn-primary pull-right' + 'class' => 'button btn btn-cta' ) ); $this->addElement($submitButton); diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index fe09db712..00ba83a6c 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -30,6 +30,7 @@ use Icinga\Application\Icinga; use Icinga\Application\Config; use Icinga\Application\Logger; +use Icinga\Module\Monitoring\Form\Command\DisableNotificationWithExpireForm; use Icinga\Module\Monitoring\Form\Command\SingleArgumentCommandForm; use Icinga\Web\Form; use Icinga\Web\Controller\ActionController; @@ -73,9 +74,9 @@ class Monitoring_CommandController extends ActionController /** * Setter for form * - * @param Form $form + * @param CommandForm $form */ - public function setForm($form) + public function setForm(CommandForm $form) { $this->form = $form; } @@ -97,6 +98,7 @@ class Monitoring_CommandController extends ActionController */ public function postDispatch() { + if ($this->issetForm()) { if ($this->form->isSubmittedAndValid()) { $this->_helper->viewRenderer->setNoRender(true); @@ -125,7 +127,6 @@ class Monitoring_CommandController extends ActionController public function init() { if ($this->_request->isPost()) { - $instance = $this->_request->getPost("instance"); $targetConfig = Config::module('monitoring', 'instances'); if ($instance) { @@ -148,6 +149,8 @@ class Monitoring_CommandController extends ActionController if ($this->getRequest()->getActionName() !== 'list') { $this->_helper->viewRenderer->setRender(self::DEFAULT_VIEW_SCRIPT); } + + $this->view->objects = array(); } /** @@ -289,13 +292,29 @@ class Monitoring_CommandController extends ActionController */ public function disableactivechecksAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); + $form = new SingleArgumentCommandForm(); - $form->setCommand('DISABLE_HOST_CHECK', 'DISABLE_SVC_CHECK'); + + $form->setCommand( + 'DISABLE_HOST_CHECK', + 'DISABLE_SVC_CHECK' + ); + + $form->setGlobalCommands( + 'STOP_EXECUTING_HOST_CHECKS', + 'STOP_EXECUTING_SVC_CHECKS' + ); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Active Checks')); - $form->addNote(t('Disable active checks for this object.')); + + if ($form->provideGlobalCommand()) { + $form->addNote(t('Disable active checks on a program-wide basis.')); + } else { + $form->addNote(t('Disable active checks for this object.')); + } + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -308,13 +327,18 @@ class Monitoring_CommandController extends ActionController */ public function enableactivechecksAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setCommand('ENABLE_HOST_CHECK', 'ENABLE_SVC_CHECK'); + $form->setGlobalCommands('START_EXECUTING_HOST_CHECKS', 'START_EXECUTING_SVC_CHECKS'); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable Active Checks')); - $form->addNote(t('Enable active checks for this object.')); + if ($form->provideGlobalCommand()) { + $form->addNote(t('Enable active checks on a program-wide basis.')); + } else { + $form->addNote(t('Enable active checks for this object.')); + } $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -366,12 +390,27 @@ class Monitoring_CommandController extends ActionController */ public function stopobsessingAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Stop obsessing')); - $form->addNote(t('Stop obsessing over this object.')); - $form->setCommand('STOP_OBSESSING_OVER_HOST', 'STOP_OBSESSING_OVER_SVC'); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Disable obsessing on a program-wide basis.')); + } else { + $form->addNote(t('Stop obsessing over this object.')); + } + + $form->setCommand( + 'STOP_OBSESSING_OVER_HOST', + 'STOP_OBSESSING_OVER_SVC' + ); + + $form->setGlobalCommands( + 'STOP_OBSESSING_OVER_HOST_CHECKS', + 'STOP_OBSESSING_OVER_SVC_CHECKS' + ); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -384,12 +423,27 @@ class Monitoring_CommandController extends ActionController */ public function startobsessingAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Start obsessing')); - $form->addNote(t('Start obsessing over this object.')); - $form->setCommand('START_OBSESSING_OVER_HOST', 'START_OBSESSING_OVER_SVC'); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Enable obsessing on a program-wide basis.')); + } else { + $form->addNote(t('Start obsessing over this object.')); + } + + $form->setCommand( + 'START_OBSESSING_OVER_HOST', + 'START_OBSESSING_OVER_SVC' + ); + + $form->setGlobalCommands( + 'START_OBSESSING_OVER_HOST_CHECKS', + 'START_OBSESSING_OVER_SVC_CHECKS' + ); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -402,12 +456,27 @@ class Monitoring_CommandController extends ActionController */ public function stopacceptingpassivechecksAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Stop Accepting Passive Checks')); - $form->addNote(t('Passive checks for this object will be omitted.')); - $form->setCommand('STOP_ACCEPTING_PASSIVE_HOST_CHECKS', 'STOP_ACCEPTING_PASSIVE_SVC_CHECKS'); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Disable passive checks on a program-wide basis.')); + } else { + $form->addNote(t('Passive checks for this object will be omitted.')); + } + + $form->setCommand( + 'STOP_ACCEPTING_PASSIVE_HOST_CHECKS', + 'STOP_ACCEPTING_PASSIVE_SVC_CHECKS' + ); + + $form->setGlobalCommands( + 'STOP_ACCEPTING_PASSIVE_HOST_CHECKS', + 'STOP_ACCEPTING_PASSIVE_SVC_CHECKS' + ); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -420,12 +489,43 @@ class Monitoring_CommandController extends ActionController */ public function startacceptingpassivechecksAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Start Accepting Passive Checks')); - $form->addNote(t('Passive checks for this object will be accepted.')); - $form->setCommand('START_ACCEPTING_PASSIVE_HOST_CHECKS', 'START_ACCEPTING_PASSIVE_SVC_CHECKS'); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Enable passive checks on a program-wide basis.')); + } else { + $form->addNote(t('Passive checks for this object will be accepted.')); + } + + $form->setCommand( + 'START_ACCEPTING_PASSIVE_HOST_CHECKS', + 'START_ACCEPTING_PASSIVE_SVC_CHECKS' + ); + + $form->setGlobalCommands( + 'START_ACCEPTING_PASSIVE_HOST_CHECKS', + 'START_ACCEPTING_PASSIVE_SVC_CHECKS' + ); + $this->setForm($form); + + if ($form->IsSubmittedAndValid() === true) { + $this->target->sendCommand($form->createCommand(), $this->view->objects); + } + } + + /** + * Disable notifications with expiration + * + * This is a global command only + */ + public function disablenotificationswithexpireAction() + { + $this->setParam('global', 1); + $form = new DisableNotificationWithExpireForm(); + $form->setRequest($this->_request); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -438,17 +538,27 @@ class Monitoring_CommandController extends ActionController */ public function disablenotificationsAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); + $form->setSubmitLabel(t('Disable Notifications')); - $form->addNote(t('Notifications for this object will be disabled.')); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Disable notifications on a program-wide basis.')); + } else { + $form->addNote(t('Notifications for this object will be disabled.')); + } + $form->setCommand('DISABLE_HOST_NOTIFICATIONS', 'DISABLE_SVC_NOTIFICATIONS'); + $form->setGlobalCommands('DISABLE_NOTIFICATIONS'); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { $this->target->sendCommand($form->createCommand(), $this->view->objects); } + } /** @@ -456,12 +566,21 @@ class Monitoring_CommandController extends ActionController */ public function enablenotificationsAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); + $form->setSubmitLabel(t('Enable Notifications')); - $form->addNote(t('Notifications for this object will be enabled.')); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Enable notifications on a program-wide basis.')); + } else { + $form->addNote(t('Notifications for this object will be enabled.')); + } + $form->setCommand('ENABLE_HOST_NOTIFICATIONS', 'ENABLE_SVC_NOTIFICATIONS'); + $form->setGlobalCommands('ENABLE_NOTIFICATIONS'); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -638,12 +757,24 @@ class Monitoring_CommandController extends ActionController */ public function disableeventhandlerAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Event Handler')); - $form->addNote(t('Disable event handler for this object.')); - $form->setCommand('DISABLE_HOST_EVENT_HANDLER', 'DISABLE_SVC_EVENT_HANDLER'); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Disable event handler for the whole system.')); + } else { + $form->addNote(t('Disable event handler for this object.')); + } + + $form->setCommand( + 'DISABLE_HOST_EVENT_HANDLER', + 'DISABLE_SVC_EVENT_HANDLER' + ); + + $form->setGlobalCommands('DISABLE_EVENT_HANDLERS'); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -656,12 +787,25 @@ class Monitoring_CommandController extends ActionController */ public function enableeventhandlerAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); + $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable Event Handler')); - $form->addNote(t('Enable event handler for this object.')); - $form->setCommand('ENABLE_HOST_EVENT_HANDLER', 'ENABLE_SVC_EVENT_HANDLER'); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Enable event handlers on the whole system.')); + } else { + $form->addNote(t('Enable event handler for this object.')); + } + + $form->setCommand( + 'ENABLE_HOST_EVENT_HANDLER', + 'ENABLE_SVC_EVENT_HANDLER' + ); + + $form->setGlobalCommands('ENABLE_EVENT_HANDLERS'); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -674,12 +818,26 @@ class Monitoring_CommandController extends ActionController */ public function disableflapdetectionAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable Flapping Detection')); - $form->addNote(t('Disable flapping detection for this object.')); - $form->setCommand('DISABLE_HOST_FLAP_DETECTION', 'DISABLE_SVC_FLAP_DETECTION'); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Disable flapping detection on a program-wide basis.')); + } else { + $form->addNote(t('Disable flapping detection for this object.')); + } + + $form->setCommand( + 'DISABLE_HOST_FLAP_DETECTION', + 'DISABLE_SVC_FLAP_DETECTION' + ); + + $form->setGlobalCommands( + 'DISABLE_FLAP_DETECTION' + ); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -692,12 +850,26 @@ class Monitoring_CommandController extends ActionController */ public function enableflapdetectionAction() { - $this->setSupportedParameters(array('host', 'service')); + $this->setSupportedParameters(array('host', 'service', 'global')); $form = new SingleArgumentCommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable Flapping Detection')); - $form->addNote(t('Enable flapping detection for this object.')); - $form->setCommand('ENABLE_HOST_FLAP_DETECTION', 'ENABLE_SVC_FLAP_DETECTION'); + + if ($form->provideGlobalCommand() === true) { + $form->addNote(t('Enable flapping detection on a program-wide basis.')); + } else { + $form->addNote(t('Enable flapping detection for this object.')); + } + + $form->setCommand( + 'ENABLE_HOST_FLAP_DETECTION', + 'ENABLE_SVC_FLAP_DETECTION' + ); + + $form->setGlobalCommands( + 'ENABLE_FLAP_DETECTION' + ); + $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { @@ -830,6 +1002,86 @@ class Monitoring_CommandController extends ActionController $this->target->sendCommand($form->createCommand(), $this->view->objects); } } -} + /** + * Shutdown the icinga process + */ + public function shutdownprocessAction() + { + $this->setParam('global', '1'); + $form = new SingleArgumentCommandForm(); + $form->setRequest($this->_request); + + $form->setSubmitLabel(t('Shutdown monitoring process')); + $form->addNote(t('Stop monitoring instance. You have to start it again from command line.')); + $form->setGlobalCommands('SHUTDOWN_PROCESS'); + $this->setForm($form); + + if ($form->IsSubmittedAndValid() === true) { + $this->target->sendCommand($form->createCommand(), $this->view->objects); + } + } + + /** + * Restart the icinga process + */ + public function restartprocessAction() + { + $this->setParam('global', '1'); + $form = new SingleArgumentCommandForm(); + $form->setRequest($this->_request); + + $form->setSubmitLabel(t('Restart monitoring process')); + $form->addNote(t('Restart the monitoring process.')); + $form->setGlobalCommands('RESTART_PROCESS'); + + $this->setForm($form); + + if ($form->IsSubmittedAndValid() === true) { + $this->target->sendCommand($form->createCommand(), $this->view->objects); + } + } + + /** + * Disable processing of performance data + */ + public function disableperformancedataAction() + { + $this->setParam('global', 1); + $form = new SingleArgumentCommandForm(); + $form->setRequest($this->_request); + + $form->setSubmitLabel(t('Disable Performance Data')); + $form->addNote(t('Disable processing of performance data on a program-wide basis.')); + + $form->setGlobalCommands('DISABLE_PERFORMANCE_DATA'); + + $this->setForm($form); + + if ($form->IsSubmittedAndValid() === true) { + $this->target->sendCommand($form->createCommand(), $this->view->objects); + } + } + + /** + * Enable processing of performance data + */ + public function enableperformancedataAction() + { + $this->setParam('global', 1); + $form = new SingleArgumentCommandForm(); + $form->setRequest($this->_request); + + $form->setSubmitLabel(t('Enable Performance Data')); + $form->addNote(t('Enable processing of performance data on a program-wide basis.')); + + $form->setGlobalCommands('ENABLE_PERFORMANCE_DATA'); + + $this->setForm($form); + + if ($form->IsSubmittedAndValid() === true) { + $this->target->sendCommand($form->createCommand(), $this->view->objects); + } + } +} // @codingStandardsIgnoreStop diff --git a/modules/monitoring/application/forms/Command/CommandForm.php b/modules/monitoring/application/forms/Command/CommandForm.php index 2f42fc2fd..b0e10f24d 100644 --- a/modules/monitoring/application/forms/Command/CommandForm.php +++ b/modules/monitoring/application/forms/Command/CommandForm.php @@ -29,6 +29,7 @@ namespace Icinga\Module\Monitoring\Form\Command; use Zend_Config; +use Zend_Controller_Request_Abstract; use Zend_Form_Element_Hidden; use Icinga\Module\Monitoring\Command\AcknowledgeCommand; use Icinga\Web\Form; @@ -38,6 +39,33 @@ use Icinga\Web\Form; */ abstract class CommandForm extends Form { + /** + * If the form is for a global command + * + * @var bool + */ + protected $globalCommand = false; + + /** + * Set command program wide + * + * @param bool $flag + */ + public function setProvideGlobalCommand($flag = true) + { + $this->globalCommand = (boolean) $flag; + } + + /** + * Getter for globalCommand + * + * @return bool + */ + public function provideGlobalCommand() + { + return (boolean) $this->globalCommand; + } + /** * Create an instance name containing hidden field * @@ -128,6 +156,21 @@ abstract class CommandForm extends Form ); } + /** + * Sets the form to global if we have data in the request + * + * @param Zend_Controller_Request_Abstract $request + */ + public function setRequest(Zend_Controller_Request_Abstract $request) + { + parent::setRequest($request); + + if ($request->getParam('global')) { + $this->setProvideGlobalCommand(true); + } + } + + /** * Create command object for CommandPipe protocol * diff --git a/modules/monitoring/application/forms/Command/DisableNotificationWithExpireForm.php b/modules/monitoring/application/forms/Command/DisableNotificationWithExpireForm.php new file mode 100644 index 000000000..a858c5fd9 --- /dev/null +++ b/modules/monitoring/application/forms/Command/DisableNotificationWithExpireForm.php @@ -0,0 +1,80 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Icinga\Module\Monitoring\Form\Command; + +use Icinga\Module\Monitoring\Command\DisableNotificationWithExpireCommand; +use Icinga\Util\DateTimeFactory; +use Icinga\Web\Form\Element\DateTimePicker; + +/** + * Provide expiration when notifications should be disabled + */ +class DisableNotificationWithExpireForm extends CommandForm +{ + /** + * Build form content + */ + protected function create() + { + $this->addNote('Disable notifications for a specific time on a program-wide basis'); + + $now = DateTimeFactory::create(); + $this->addElement( + new DateTimePicker( + array( + 'name' => 'expiretime', + 'label' => t('Expire Time'), + 'value' => $now->getTimestamp() + 3600, + 'patterns' => $this->getValidDateTimeFormats(), + 'helptext' => t( + 'Enter the expire date/time for this acknowledgement here. Icinga will ' + . ' delete the acknowledgement after this date expired.' + ) + ) + ) + ); + + $this->setSubmitLabel('Disable notifications'); + + parent::create(); + } + + + /** + * Create command object for CommandPipe protocol + * + * @return AcknowledgeCommand + */ + public function createCommand() + { + $command = new DisableNotificationWithExpireCommand(); + $command->setExpirationTimestamp($this->getValue('expiretime')); + return $command; + } +} diff --git a/modules/monitoring/application/forms/Command/SingleArgumentCommandForm.php b/modules/monitoring/application/forms/Command/SingleArgumentCommandForm.php index 6f3aa6afa..089dcfefe 100644 --- a/modules/monitoring/application/forms/Command/SingleArgumentCommandForm.php +++ b/modules/monitoring/application/forms/Command/SingleArgumentCommandForm.php @@ -28,6 +28,7 @@ namespace Icinga\Module\Monitoring\Form\Command; +use Zend_Controller_Request_Abstract; use \Zend_Form_Element_Hidden; use Icinga\Module\Monitoring\Command\AcknowledgeCommand; use Icinga\Module\Monitoring\Command\SingleArgumentCommand; @@ -51,6 +52,13 @@ class SingleArgumentCommandForm extends CommandForm */ private $serviceCommand; + /** + * Name of global command + * + * @var array + */ + private $globalCommands = array(); + /** * Name of the parameter used as value * @@ -81,11 +89,27 @@ class SingleArgumentCommandForm extends CommandForm public function setCommand($hostCommand, $serviceCommand = null) { $this->hostCommand = $hostCommand; + if ($serviceCommand !== null) { $this->serviceCommand = $serviceCommand; } } + /** + * Setter for global commands + * + * @param string $hostOrGenericGlobalCommand Generic command or one for host + * @param string $serviceGlobalCommand If any (leave blank if you need a global global) + */ + public function setGlobalCommands($hostOrGenericGlobalCommand, $serviceGlobalCommand = null) + { + $this->globalCommands[] = $hostOrGenericGlobalCommand; + + if ($serviceGlobalCommand !== null) { + $this->globalCommands[] = $serviceGlobalCommand; + } + } + /** * Use an explicit value to send with command * @@ -106,6 +130,11 @@ class SingleArgumentCommandForm extends CommandForm $this->parameterName = $parameterName; } + /** + * Flag to ignore every objects + * + * @param bool $flag + */ public function setObjectIgnoreFlag($flag = true) { $this->ignoreObject = (bool) $flag; @@ -125,6 +154,15 @@ class SingleArgumentCommandForm extends CommandForm parent::create(); } + public function setRequest(Zend_Controller_Request_Abstract $request) + { + parent::setRequest($request); + + if ($this->globalCommand === true) { + $this->setParameterName('global'); + } + } + /** * Create command object for CommandPipe protocol @@ -134,7 +172,6 @@ class SingleArgumentCommandForm extends CommandForm public function createCommand() { $command = new SingleArgumentCommand(); - $command->setCommand($this->hostCommand, $this->serviceCommand); if ($this->parameterValue !== null) { $command->setValue($this->parameterValue); @@ -142,6 +179,13 @@ class SingleArgumentCommandForm extends CommandForm $command->setValue($this->getValue($this->parameterName)); } + if ($this->provideGlobalCommand() == true) { + $command->setGlobalCommands($this->globalCommands); + $this->ignoreObject = true; + } else { + $command->setCommand($this->hostCommand, $this->serviceCommand); + } + $command->setObjectIgnoreFlag($this->ignoreObject); return $command; diff --git a/modules/monitoring/library/Monitoring/Command/DisableNotificationWithExpireCommand.php b/modules/monitoring/library/Monitoring/Command/DisableNotificationWithExpireCommand.php new file mode 100644 index 000000000..a6b1325e0 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/DisableNotificationWithExpireCommand.php @@ -0,0 +1,116 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Icinga\Module\Monitoring\Command; + +use Icinga\Exception\ProgrammingError; +use Icinga\Protocol\Commandpipe\Command; + +/** + * Disable notifications with expire + */ +class DisableNotificationWithExpireCommand extends Command +{ + /** + * Timestamp when deactivation should expire + * + * @var integer + */ + private $expirationTimestamp; + + /** + * Create a new instance of this command + */ + public function __construct() + { + // There is now object specific implementation, only global + $this->globalCommand = true; + } + + /** + * Setter for expiration timestamp + * + * @param integer $timestamp + */ + public function setExpirationTimestamp($timestamp) + { + $this->expirationTimestamp = $timestamp; + } + + /** + * Return this command's arguments in the order expected by the actual command definition + * + * @return array + */ + public function getArguments() + { + return array($this->expirationTimestamp); + } + + /** + * Return the command as a string with the given host being inserted + * + * @param string $hostname The name of the host to insert + * @throws ProgrammingError + * + * @return string The string representation of the command + */ + public function getHostCommand($hostname) + { + throw new ProgrammingError('This is not supported for single objects'); + } + + /** + * 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 + * @throws ProgrammingError + * @return string The string representation of the command# + */ + public function getServiceCommand($hostname, $servicename) + { + throw new ProgrammingError('This is not supported for single objects'); + } + + /** + * Create a global command + * + * @param string $instance + * + * @return string + */ + public function getGlobalCommand($instance = null) + { + return sprintf( + 'DISABLE_NOTIFICATIONS_EXPIRE_TIME;%d;%s', + time(), + implode(';', $this->getArguments()) + ); + } +} diff --git a/modules/monitoring/library/Monitoring/Command/SingleArgumentCommand.php b/modules/monitoring/library/Monitoring/Command/SingleArgumentCommand.php index b6f6ae7bb..a35d81010 100644 --- a/modules/monitoring/library/Monitoring/Command/SingleArgumentCommand.php +++ b/modules/monitoring/library/Monitoring/Command/SingleArgumentCommand.php @@ -28,6 +28,7 @@ namespace Icinga\Module\Monitoring\Command; +use Icinga\Exception\ProgrammingError; use Icinga\Protocol\Commandpipe\Command; /** @@ -56,6 +57,13 @@ class SingleArgumentCommand extends Command */ private $serviceCommand; + /** + * Name of global command + * + * @var array + */ + private $globalCommands = array(); + /** * Ignore host in command string * @@ -76,8 +84,8 @@ class SingleArgumentCommand extends Command /** * Setter for command names * - * @param string $hostCommand - * @param string $serviceCommand + * @param string $hostCommand + * @param string $serviceCommand */ public function setCommand($hostCommand, $serviceCommand) { @@ -85,6 +93,17 @@ class SingleArgumentCommand extends Command $this->serviceCommand = $serviceCommand; } + /** + * Set a bunch of global commands + * + * @param array $commands One or more commands to control global parameters + */ + public function setGlobalCommands(array $commands) + { + $this->globalCommands = $commands; + $this->globalCommand = true; + } + /** * Ignore object values upon command creation * @@ -154,4 +173,35 @@ class SingleArgumentCommand extends Command . ';' . $this->getArgumentString(array($hostname, $servicename)); } + + /** + * Getter for global command if configured + * + * @param string $instance + * + * @throws ProgrammingError + * @return string + */ + public function getGlobalCommand($instance = null) + { + if (!count($this->globalCommands)) { + // This throws exception for us that globalCommand + // is not implemented properly + parent::getGlobalCommand(); + } + + if ($this->value === 'host') { + return strtoupper($this->globalCommands[0]); + } + + if ($this->value === 'service') { + if (count($this->globalCommands) < 2) { + throw new ProgrammingError('If use global values you need at least 2 global commands'); + } + + return strtoupper($this->globalCommands[1]); + } + + return strtoupper(implode(';', $this->globalCommands)); + } }