Adjust schedule downtime handling (WIP)

refs #4580
This commit is contained in:
Johannes Meyer 2013-09-05 10:55:32 +02:00 committed by Eric Lippmann
parent 8022bf57af
commit ba7f455643
6 changed files with 286 additions and 76 deletions

View File

@ -302,26 +302,6 @@ class CommandPipe
return self::TYPE_HOST;
}
/**
* Schedule a downtime for all provided objects
*
* @param array $objects An array of monitoring objects to schedule the downtime for
* @param Downtime $downtime The downtime object to schedule
*/
public function scheduleDowntime($objects, Downtime $downtime)
{
foreach ($objects as $object) {
$type = $this->getObjectType($object);
if ($type == self::TYPE_SERVICE) {
$this->send(
sprintf($downtime->getFormatString($type), $object->host_name, $object->service_description)
);
} else {
$this->send(sprintf($downtime->getFormatString($type), $object->host_name));
}
}
}
/**
* Remove downtimes for objects
*

View File

@ -435,7 +435,7 @@ class Monitoring_CommandController extends ActionController
$this->setForm($form);
if ($form->IsSubmittedAndValid() === true) {
$this->target->scheduleDowntime($this->view->objects, $form->getDowntime());
$this->target->sendCommand($form->createCommand(), $this->view->objects);
}
}
@ -452,7 +452,7 @@ class Monitoring_CommandController extends ActionController
$this->setForm($form);
if ($form->IsSubmittedAndValid() === true) {
$this->target->scheduleDowntime($this->view->objects, $form->getDowntime());
$this->target->sendCommand($form->createCommand(), $this->view->objects);
}
}

View File

@ -28,14 +28,14 @@
namespace Icinga\Module\Monitoring\Form\Command;
use \Zend_Form_Element_Text;
use \Zend_Validate_GreaterThan;
use \Zend_Validate_Digits;
use \Icinga\Web\Form\Element\DateTimePicker;
use \Icinga\Protocol\Commandpipe\Downtime;
use \Icinga\Protocol\Commandpipe\Comment;
use \Icinga\Util\DateTimeFactory;
use \Icinga\Module\Monitoring\Backend;
use Zend_Form_Element_Text;
use Zend_Validate_GreaterThan;
use Zend_Validate_Digits;
use Icinga\Web\Form\Element\DateTimePicker;
use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Util\DateTimeFactory;
use Icinga\Module\Monitoring\Backend;
use Icinga\Module\Monitoring\Command\ScheduleDowntimeCommand;
/**
* Form for scheduling downtimes
@ -328,43 +328,23 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
}
/**
* Create Downtime from request Data
* Create ScheduleDowntimeCommand object
*
* @return Downtime
* @return ScheduleDowntimeCommand
*/
public function getDowntime()
public function createCommand()
{
$comment = new Comment(
$this->getRequest()->getUser()->getUsername(),
$this->getValue('comment')
);
$duration = 0;
if ($this->getValue('type') === self::TYPE_FLEXIBLE) {
$duration = ($this->getValue('hours') * 3600) + ($this->getValue('minutes') * 60);
}
$starttime = $this->getValue('starttime');
$endtime = $this->getValue('endtime');
$downtime = new Downtime(
$starttime,
$endtime,
$comment,
$duration,
// TODO: Add support for propagation, host-/servicegroups and services only (#4588)
return new ScheduleDowntimeCommand(
$this->getValue('starttime'),
$this->getValue('endtime'),
new Comment(
$this->getRequest()->getUser()->getUsername(),
$this->getValue('comment')
),
$this->getValue('type') === self::TYPE_FLEXIBLE,
$this->getValue('hours') * 3600 + $this->getValue('minutes') * 60,
$this->getValue('triggered')
);
if (!$this->getWithChildren()) {
switch ($this->getValue('childobjects')) {
case 1:
$downtime->setType(Downtime::TYPE_WITH_CHILDREN_TRIGGERED);
break;
case 2:
$downtime->setType(Downtime::TYPE_WITH_CHILDREN);
break;
}
} else {
$downtime->setType(Downtime::TYPE_HOST_SVC);
}
return $downtime;
}
}

View File

@ -0,0 +1,243 @@
<?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\Comment;
use Icinga\Exception\NotImplementedError;
/**
* Command for scheduling a new downtime
*/
class ScheduleDowntimeCommand extends BaseCommand
{
/**
* When this downtime should start
*
* @var int The time as UNIX timestamp
*/
private $startTime;
/**
* When this downtime should end
*
* @var int The time as UNIX timestamp
*/
private $endTime;
/**
* The comment associated with this downtime
*
* @var Comment
*/
private $comment;
/**
* Whether this is a fixed or flexible downtime
*
* @var bool
*/
private $fixed;
/**
* The duration to use when this downtime is a flexible one
*
* @var int In seconds
*/
private $duration;
/**
* The ID of the downtime which triggers this one
*
* @var int
*/
private $triggerId;
/**
* Set when to start this downtime
*
* @param int $startTime
*
* @return self
*/
public function setStart($startTime)
{
$this->startTime = intval($startTime);
return $this;
}
/**
* Set when to end this downtime
*
* @param int $endTime
*
* @return self
*/
public function setEnd($endTime)
{
$this->endTime = intval($endTime);
return $this;
}
/**
* Set the comment for this downtime
*
* @param Comment $comment
*
* @return self
*/
public function setComment(Comment $comment)
{
$this->comment = $comment;
return $this;
}
/**
* Set whether this downtime is fixed or flexible
*
* @param bool $state
*
* @return self
*/
public function setFixed($state)
{
$this->fixed = (bool) $state;
return $this;
}
/**
* Set the duration of this downtime
*
* @param int $duration
*
* @return self
*/
public function setDuration($duration)
{
$this->duration = intval($duration);
return $this;
}
/**
* Set the triggering id for this downtime
*
* @param int $triggerId
*
* @return self
*/
public function setTriggerId($triggerId)
{
$this->triggerId = intval($triggerId);
return $this;
}
/**
* Initialise a new command object to schedule a downtime
*
* @param int $startTime When to start this downtime as UNIX timestamp
* @param int $endTime When to end this downtime as UNIX timestamp
* @param Comment $comment The comment to use for this downtime
* @param bool $fixed Whether this downtime is fixed or flexible
* @param int $duration How long in seconds this downtime should apply if flexible
* @param int $triggerId The ID of the triggering downtime
*/
public function __construct($startTime, $endTime, Comment $comment, $fixed = true, $duration = 0, $triggerId = 0)
{
$this->startTime = $startTime;
$this->endTime = $endTime;
$this->comment = $comment;
$this->fixed = $fixed;
$this->duration = $duration;
$this->triggerId = $triggerId;
}
/**
* Return the command as a string for the given host or all of it's services
*
* @param type $hostname The name of the host to insert
* @param type $servicesOnly Whether this downtime is for the given host or all of it's services
*
* @return string The string representation of the command
*/
public function getHostCommand($hostname, $servicesOnly = false)
{
throw new NotImplementedError();
}
/**
* Return the command as a string for the given host and all of it's children hosts
*
* @param string $hostname The name of the host to insert
* @param bool $triggered Whether it's children are triggered
*
* @return string The string representation of the command
*/
public function getPropagatedHostCommand($hostname, $triggered = false)
{
throw new NotImplementedError();
}
/**
* Return the command as a string for the given service
*
* @param type $hostname The name of the host to insert
* @param type $servicename The name of the service to insert
*
* @return string The string representation of the command
*/
public function getServiceCommand($hostname, $servicename)
{
throw new NotImplementedError();
}
/**
* Return the command as a string for all hosts or services of the given hostgroup
*
* @param type $hostgroup The name of the hostgroup to insert
* @param type $hostsOnly Whether only hosts or services are taken into account
*
* @return string The string representation of the command
*/
public function getHostgroupCommand($hostgroup, $hostsOnly = true)
{
throw new NotImplementedError();
}
/**
* Return the command as a string for all hosts or services of the given servicegroup
*
* @param type $servicegroup The name of the servicegroup to insert
* @param type $hostsOnly Whether only hosts or services are taken into account
*
* @return string The string representation of the command
*/
public function getServicegroupCommand($servicegroup, $hostsOnly = true)
{
throw new NotImplementedError();
}
}

View File

@ -15,6 +15,7 @@ class CommandPipeLoader extends LibraryLoader {
require_once("Zend/Config.php");
require_once("Zend/Log.php");
require_once("../../library/Icinga/Application/Logger.php");
require_once("../../library/Icinga/Exception/NotImplementedError.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php");
require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php");
@ -29,6 +30,7 @@ class CommandPipeLoader extends LibraryLoader {
require_once('../../modules/monitoring/library/Monitoring/Command/BaseCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/AddCommentCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/ScheduleDowntimeCommand.php');
}
}
// @codingStandardsIgnoreEnd

View File

@ -11,12 +11,12 @@ CommandPipeLoader::requireLibrary();
use Zend_Config;
use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Protocol\Commandpipe\CustomNotification;
use Icinga\Protocol\Commandpipe\Downtime as Downtime;
use Icinga\Protocol\Commandpipe\Commandpipe as Commandpipe;
use Icinga\Protocol\Commandpipe\PropertyModifier as MONFLAG;
use Icinga\Protocol\Ldap\Exception;
use Icinga\Module\Monitoring\Command\AcknowledgeCommand;
use Icinga\Module\Monitoring\Command\AddCommentCommand;
use Icinga\Module\Monitoring\Command\ScheduleDowntimeCommand;
if(!defined("EXTCMD_TEST_BIN"))
define("EXTCMD_TEST_BIN", "./bin/extcmd_test");
@ -369,28 +369,33 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
/**
* Test whether host and servicedowntimes are correctly scheduled
*
* @throws \Exception|Exception
* @throws Exception
*/
public function testScheduleDowntime()
{
$pipe = $this->getLocalTestPipe();
try {
$downtime = new Downtime(25, 26, new Comment("me", "test"));
$pipe->scheduleDowntime(array(
(object) array(
"host_name" => "Testhost"
$downtime = new ScheduleDowntimeCommand(25, 26, new Comment("me", "test"));
$pipe->sendCommand(
$downtime,
array(
(object) array(
"host_name" => "Testhost"
)
)
),$downtime);
);
$this->assertCommandSucceeded("SCHEDULE_HOST_DOWNTIME;Testhost;25;26;1;0;0;me;test");
$pipe->scheduleDowntime(array(
(object) array(
"host_name" => "Testhost",
"service_description" => "svc"
$pipe->sendCommand(
$downtime,
array(
(object) array(
"host_name" => "Testhost",
"service_description" => "svc"
)
)
),$downtime);
);
$this->assertCommandSucceeded("SCHEDULE_SVC_DOWNTIME;Testhost;svc;25;26;1;0;0;me;test");
} catch (Exception $e) {
$this->cleanup();
throw $e;