diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index 500b0a9a0..3f2925b99 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -667,24 +667,6 @@ class CommandPipe } } - /** - * Delay notifications for all provided hosts and services for $time seconds - * - * @param array $objects An array of hosts and services - * @param int $time The number of seconds to delay notifications for - */ - public function delayNotification($objects, $time) - { - foreach ($objects as $object) { - $type = $this->getObjectType($object); - if ($type === self::TYPE_SERVICE) { - $this->send('DELAY_SVC_NOTIFICATION;'.$object->host_name.';'.$object->service_description.';'.$time); - } else { - $this->send('DELAY_HOST_NOTIFICATION;'.$object->host_name.';'.$time); - } - } - } - /** * Return the transport handler that handles actual sending of commands * diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 56e8312fc..48c43fa38 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -717,7 +717,7 @@ class Monitoring_CommandController extends ActionController $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->delayNotification($this->view->objects, $form->getDelayTime()); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } diff --git a/modules/monitoring/application/forms/Command/DelayNotificationForm.php b/modules/monitoring/application/forms/Command/DelayNotificationForm.php index 34bda250e..464610ba6 100644 --- a/modules/monitoring/application/forms/Command/DelayNotificationForm.php +++ b/modules/monitoring/application/forms/Command/DelayNotificationForm.php @@ -28,6 +28,8 @@ namespace Icinga\Module\Monitoring\Form\Command; +use Icinga\Module\Monitoring\Command\DelayNotificationCommand; + /** * Form for the delay notification command */ @@ -76,12 +78,12 @@ class DelayNotificationForm extends CommandForm } /** - * Return the currently set delay time in seconds + * Create the command object to delay notifications * - * @return integer + * @return DelayNotificationCommand */ - public function getDelayTime() + public function createCommand() { - return $this->getValue('minutes') * 60; + return new DelayNotificationCommand($this->getValue('minutes') * 60); } } diff --git a/modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php b/modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php new file mode 100644 index 000000000..6702bf339 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php @@ -0,0 +1,112 @@ + + * @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; + +/** + * Command to delay a notification + */ +class DelayNotificationCommand extends BaseCommand +{ + /** + * The delay in seconds + * + * @var int + */ + private $delay; + + /** + * Initialise a new delay notification command object + * + * @param int $delay How long, in seconds, notifications should be delayed + */ + public function __construct($delay) + { + $this->delay = $delay; + } + + /** + * Set how long notifications should be delayed + * + * @param int $seconds In seconds + * + * @return self + */ + public function setDelay($seconds) + { + $this->delay = intval($seconds); + return $this; + } + + /** + * Return this command's parameters properly arranged in an array + * + * @return array + * + * @see BaseCommand::getParameters() + */ + public function getParameters() + { + return array($this->delay); + } + + /** + * 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 + * + * @see BaseCommand::getHostCommand() + */ + public function getHostCommand($hostname) + { + return 'DELAY_HOST_NOTIFICATION;' . implode(';', array_merge(array($hostname), $this->getParameters())); + } + + /** + * 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 + * + * @see BaseCommand::getServiceCommand() + */ + public function getServiceCommand($hostname, $servicename) + { + return 'DELAY_SVC_NOTIFICATION;' . implode( + ';', + array_merge( + array($hostname, $servicename), + $this->getParameters() + ) + ); + } +} \ No newline at end of file diff --git a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php index ffdfa56ee..00a22c1fd 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php @@ -1,11 +1,35 @@ + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ // {{{ICINGA_LICENSE_HEADER}}} namespace Tests\Icinga\Protocol\Commandpipe; require_once("./library/Icinga/LibraryLoader.php"); + use Test\Icinga\LibraryLoader; class CommandPipeLoader extends LibraryLoader { @@ -29,6 +53,6 @@ class CommandPipeLoader extends LibraryLoader { require_once('../../modules/monitoring/library/Monitoring/Command/AddCommentCommand.php'); require_once('../../modules/monitoring/library/Monitoring/Command/ScheduleDowntimeCommand.php'); require_once('../../modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php'); + require_once('../../modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php'); } } -// @codingStandardsIgnoreEnd diff --git a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php index 1531eb787..6db393979 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php @@ -1,6 +1,29 @@ + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ // {{{ICINGA_LICENSE_HEADER}}} namespace Tests\Icinga\Protocol\Commandpipe; @@ -17,9 +40,11 @@ use Icinga\Module\Monitoring\Command\AcknowledgeCommand; use Icinga\Module\Monitoring\Command\AddCommentCommand; use Icinga\Module\Monitoring\Command\ScheduleDowntimeCommand; use Icinga\Module\Monitoring\Command\CustomNotificationCommand; +use Icinga\Module\Monitoring\Command\DelayNotificationCommand; -if(!defined("EXTCMD_TEST_BIN")) +if (!defined("EXTCMD_TEST_BIN")) { define("EXTCMD_TEST_BIN", "./bin/extcmd_test"); +} /** * Several tests for the command pipe component @@ -495,6 +520,43 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase $this->cleanup(); } + /** + * Test whether commands to delay notifications are being sent to the commandpipe + * + * @throws Exception + */ + public function testDelayNotification() + { + $pipe = $this->getLocalTestPipe(); + try { + $delay = new DelayNotificationCommand(300); + $pipe->sendCommand( + $delay, + array( + (object) array( + 'host_name' => 'Host', + 'service_description' => 'Service' + ) + ) + ); + $this->assertCommandSucceeded('DELAY_SVC_NOTIFICATION;Host;Service;300'); + + $pipe->sendCommand( + $delay, + array( + (object) array( + 'host_name' => 'Host' + ) + ) + ); + $this->assertCommandSucceeded('DELAY_HOST_NOTIFICATION;Host;300'); + } catch (Exception $e) { + $this->cleanup(); + throw $e; + } + $this->cleanup(); + } + /** * Test sending of commands via SSH (currently disabled) * @@ -525,4 +587,3 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase $this->cleanup(); } } -// @codingStandardsIgnoreStop