Adjust reschedulecheck command handling

refs #4580
This commit is contained in:
Johannes Meyer 2013-09-05 16:02:09 +02:00 committed by Eric Lippmann
parent e9dc895b37
commit fef8370d5f
8 changed files with 271 additions and 86 deletions

View File

@ -194,50 +194,6 @@ class CommandPipe
} }
} }
/**
* Reschedule a forced check for all provided objects
*
* @param array $objects An array of hosts and services to reschedule
* @param int|bool $time The time to submit, if empty time() will be used
* @param bool $withChilds Whether only childs should be rescheduled
*/
public function scheduleForcedCheck($objects, $time = false, $withChilds = false)
{
if (!$time) {
$time = time();
}
$base = "SCHEDULE_FORCED_";
foreach ($objects as $object) {
if (isset($object->service_description)) {
$this->send($base . "SVC_CHECK;$object->host_name;$object->service_description;$time");
} else {
$this->send($base . 'HOST_' . ($withChilds ? 'SVC_CHECKS' : 'CHECK') . ";$object->host_name;$time");
}
}
}
/**
* Reschedule a check for all provided objects
*
* @param array $objects An array of hosts and services to reschedule
* @param int|bool $time The time to submit, if empty time() will be used
* @param bool $withChilds Whether only childs should be rescheduled
*/
public function scheduleCheck($objects, $time = false, $withChilds = false)
{
if (!$time) {
$time = time();
}
$base = "SCHEDULE_";
foreach ($objects as $object) {
if (isset($object->service_description)) {
$this->send($base . "SVC_CHECK;$object->host_name;$object->service_description;$time");
} else {
$this->send($base . 'HOST_' . ($withChilds ? 'SVC_CHECKS' : 'CHECK') . ";$object->host_name;$time");
}
}
}
/** /**
* Removes the submitted comments * Removes the submitted comments
* *

View File

@ -280,7 +280,7 @@ class Monitoring_CommandController extends ActionController
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$this->target->scheduleCheck($this->view->objects); $this->target->sendCommand($form->createCommand(), $this->view->objects);
} }
} }
@ -518,19 +518,12 @@ class Monitoring_CommandController extends ActionController
$form = new RescheduleNextCheckForm(); $form = new RescheduleNextCheckForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setConfiguration(Config::app()); $form->setConfiguration(Config::app());
$form->setWithChildren(true); $form->setWithChildren(true);
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
if ($form->isForced()) { $this->target->sendCommand($form->createCommand(), $this->view->objects);
$this->target->scheduleForcedCheck($this->view->objects, time());
$this->target->scheduleForcedCheck($this->view->objects, time(), true);
} else {
$this->target->scheduleCheck($this->view->objects, time());
$this->target->scheduleCheck($this->view->objects, time(), true);
}
} }
} }

View File

@ -28,9 +28,10 @@
namespace Icinga\Module\Monitoring\Form\Command; namespace Icinga\Module\Monitoring\Form\Command;
use \Zend_Form_Element_Checkbox; use Zend_Form_Element_Checkbox;
use \Icinga\Web\Form\Element\DateTimePicker; use Icinga\Util\DateTimeFactory;
use \Icinga\Util\DateTimeFactory; use Icinga\Web\Form\Element\DateTimePicker;
use Icinga\Module\Monitoring\Command\ScheduleCheckCommand;
/** /**
* Form for scheduling checks * Form for scheduling checks
@ -90,12 +91,16 @@ class RescheduleNextCheckForm extends WithChildrenCommandForm
} }
/** /**
* Return whether this is a forced check (force is checked) * Create the command object to schedule checks
* *
* @return bool * @return ScheduleCheckCommand
*/ */
public function isForced() public function createCommand()
{ {
return $this->getValue('forcecheck') == true; $command = new ScheduleCheckCommand(
$this->getValue('checktime'),
$this->getValue('forcecheck')
);
return $command->excludeHost($this->getWithChildren());
} }
} }

View File

@ -39,6 +39,82 @@ use Icinga\Protocol\Commandpipe\CommandType;
*/ */
class BaseCommand implements CommandType class BaseCommand implements CommandType
{ {
/**
* Whether hosts are ignored in case of a host- or servicegroup
*
* @var bool
*/
protected $withoutHosts = false;
/**
* Whether services are ignored in case of a host- or servicegroup
*
* @var bool
*/
protected $withoutServices = false;
/**
* Whether child hosts are going to be included in case of a host command
*
* @var bool
*/
protected $withChildren = false;
/**
* Whether only services are going to be included in case of a host command
*
* @var bool
*/
protected $onlyServices = false;
/**
* Set whether this command should only affect the services of a host- or servicegroup
*
* @param bool $state
* @return self
*/
public function excludeHosts($state = true)
{
$this->withoutHosts = (bool) $state;
return $this;
}
/**
* Set whether this command should only affect the hosts of a host- or servicegroup
*
* @param bool $state
* @return self
*/
public function excludeServices($state = true)
{
$this->withoutServices = (bool) $state;
return $this;
}
/**
* Set whether this command should also affect all children hosts of a host
*
* @param bool $state
* @return self
*/
public function includeChildren($state = true)
{
$this->withChildren = (bool) $state;
return $this;
}
/**
* Set whether this command only affects those services beyond a host
*
* @param bool $state
* @return self
*/
public function excludeHost($state = true)
{
$this->onlyServices = (bool) $state;
return $this;
}
/** /**
* Return this command's parameters properly arranged in an array * Return this command's parameters properly arranged in an array
* *

View File

@ -92,7 +92,7 @@ class CustomNotificationCommand extends BaseCommand
*/ */
public function setForced($state) public function setForced($state)
{ {
$this->forced = $state; $this->forced = (bool) $state;
return $this; return $this;
} }
@ -105,7 +105,7 @@ class CustomNotificationCommand extends BaseCommand
*/ */
public function setBroadcast($state) public function setBroadcast($state)
{ {
$this->broadcast = $state; $this->broadcast = (bool) $state;
return $this; return $this;
} }

View File

@ -0,0 +1,134 @@
<?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 schedule checks
*/
class ScheduleCheckCommand extends BaseCommand
{
/**
* When this check is scheduled
*
* @var int The time as UNIX timestamp
*/
private $checkTime;
/**
* Whether this check is forced
*
* @var bool
*/
private $forced;
/**
* Initialises a new command object to schedule checks
*
* @param int $checkTime The time as UNIX timestamp
* @param bool $forced Whether this check is forced
*/
public function __construct($checkTime, $forced = false)
{
$this->checkTime = $checkTime;
$this->forced = $forced;
}
/**
* Set when to schedule this check
*
* @param int $checkTime The time as UNIX timestamp
*
* @return self
*/
public function setCheckTime($checkTime)
{
$this->checkTime = intval($checkTime);
return $this;
}
/**
* Set whether this check is forced
*
* @param bool $state
*
* @return self
*/
public function setForced($state)
{
$this->forced = (bool) $state;
return $this;
}
/**
* Return this command's parameters properly arranged in an array
*
* @return array
*
* @see BaseCommand::getParameters()
*/
public function getParameters()
{
return array($this->checkTime);
}
/**
* Return the command as a string for the given host or all of it's services
*
* @param string $hostname The name of the host to insert
* @param type $servicesOnly Whether the given host or each of it's services is checked
*
* @return string The string representation of the command
*
* @see BaseCommand::getHostCommand()
*/
public function getHostCommand($hostname)
{
return sprintf(
'SCHEDULE%s_HOST_%s;',
$this->forced ? '_FORCED' : '',
$this->onlyServices ? 'SVC_CHECKS' : 'CHECK'
) . implode(';', array_merge(array($hostname), $this->getParameters()));
}
/**
* Return the command as a string for the given service
*
* @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 sprintf('SCHEDULE%s_SVC_CHECK;', $this->forced ? '_FORCED' : '')
. implode(';', array_merge(array($hostname, $servicename), $this->getParameters()));
}
}

View File

@ -54,5 +54,6 @@ class CommandPipeLoader extends LibraryLoader {
require_once('../../modules/monitoring/library/Monitoring/Command/ScheduleDowntimeCommand.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/CustomNotificationCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php'); require_once('../../modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php');
} }
} }

View File

@ -41,6 +41,7 @@ use Icinga\Module\Monitoring\Command\AddCommentCommand;
use Icinga\Module\Monitoring\Command\ScheduleDowntimeCommand; use Icinga\Module\Monitoring\Command\ScheduleDowntimeCommand;
use Icinga\Module\Monitoring\Command\CustomNotificationCommand; use Icinga\Module\Monitoring\Command\CustomNotificationCommand;
use Icinga\Module\Monitoring\Command\DelayNotificationCommand; use Icinga\Module\Monitoring\Command\DelayNotificationCommand;
use Icinga\Module\Monitoring\Command\ScheduleCheckCommand;
if (!defined("EXTCMD_TEST_BIN")) { if (!defined("EXTCMD_TEST_BIN")) {
define("EXTCMD_TEST_BIN", "./bin/extcmd_test"); define("EXTCMD_TEST_BIN", "./bin/extcmd_test");
@ -296,32 +297,51 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
{ {
$pipe = $this->getLocalTestPipe(); $pipe = $this->getLocalTestPipe();
try { try {
$pipe->getTransport()->setOpenMode("a"); // append so we have multiple results $pipe->getTransport()->setOpenMode('a'); // append so we have multiple results
$t = time(); $command = new ScheduleCheckCommand(5000);
// normal reschedule $pipe->sendCommand(
$pipe->scheduleCheck(array( $command,
(object) array("host_name"=>"test"), array(
(object) array("host_name"=>"test","service_description"=>"svc1") (object) array(
),$t); 'host_name' => 'test'
// forced ),
$pipe->scheduleForcedCheck(array( (object) array(
(object) array("host_name"=>"test"), 'host_name' => 'test',
(object) array("host_name"=>"test","service_description"=>"svc1") 'service_description' => 'svc1'
),$t); )
// forced, recursive )
$pipe->scheduleForcedCheck(array( );
(object) array("host_name"=>"test"), $command->setForced(true);
),$t,true); $pipe->sendCommand(
$command,
array(
(object) array(
'host_name' => 'test'
),
(object) array(
'host_name' => 'test',
'service_description' => 'svc1'
)
)
);
$command->excludeHost();
$pipe->sendCommand(
$command,
array(
(object) array(
'host_name' => 'test'
)
)
);
$result = explode("\n",file_get_contents($this->getPipeName())); $result = explode("\n", file_get_contents($this->getPipeName()));
$this->assertCount(6,$result, "Asserting a correct number of commands being written to the commandpipe"); $this->assertCount(6, $result, 'Asserting a correct number of commands being written to the commandpipe');
$this->assertCommandSucceeded("SCHEDULE_HOST_CHECK;test;".$t,$result[0]);
$this->assertCommandSucceeded("SCHEDULE_SVC_CHECK;test;svc1;".$t,$result[1]);
$this->assertCommandSucceeded("SCHEDULE_FORCED_HOST_CHECK;test;".$t,$result[2]);
$this->assertCommandSucceeded("SCHEDULE_FORCED_SVC_CHECK;test;svc1;".$t,$result[3]);
$this->assertCommandSucceeded("SCHEDULE_FORCED_HOST_SVC_CHECKS;test;".$t,$result[4]);
$this->assertCommandSucceeded('SCHEDULE_HOST_CHECK;test;5000', $result[0]);
$this->assertCommandSucceeded('SCHEDULE_SVC_CHECK;test;svc1;5000', $result[1]);
$this->assertCommandSucceeded('SCHEDULE_FORCED_HOST_CHECK;test;5000', $result[2]);
$this->assertCommandSucceeded('SCHEDULE_FORCED_SVC_CHECK;test;svc1;5000', $result[3]);
$this->assertCommandSucceeded('SCHEDULE_FORCED_HOST_SVC_CHECKS;test;5000', $result[4]);
} catch(Exception $e) { } catch(Exception $e) {
$this->cleanup(); $this->cleanup();
throw $e; throw $e;