Adjust delay notification handling

refs #4580
This commit is contained in:
Johannes Meyer 2013-09-05 14:00:38 +02:00 committed by Eric Lippmann
parent 36424b508b
commit e9dc895b37
6 changed files with 209 additions and 28 deletions

View File

@ -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
*

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,112 @@
<?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;
/**
* 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()
)
);
}
}

View File

@ -1,11 +1,35 @@
<?php
// @codingStandardsIgnoreStart
// {{{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 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

View File

@ -1,6 +1,29 @@
<?php
// @codingStandardsIgnoreStart
// {{{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 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