From fef8370d5fa7ca9224eb29b0b055f71de16b212b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 5 Sep 2013 16:02:09 +0200 Subject: [PATCH] Adjust reschedulecheck command handling refs #4580 --- .../Protocol/Commandpipe/CommandPipe.php | 44 ------ .../controllers/CommandController.php | 11 +- .../forms/Command/RescheduleNextCheckForm.php | 19 ++- .../Monitoring/Command/BaseCommand.php | 76 ++++++++++ .../Command/CustomNotificationCommand.php | 4 +- .../Command/ScheduleCheckCommand.php | 134 ++++++++++++++++++ .../Commandpipe/CommandPipeLoader.php | 1 + .../Protocol/Commandpipe/CommandPipeTest.php | 68 +++++---- 8 files changed, 271 insertions(+), 86 deletions(-) create mode 100644 modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index 3f2925b99..25221eb98 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -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 * diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 48c43fa38..4d2471346 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -280,7 +280,7 @@ class Monitoring_CommandController extends ActionController $this->setForm($form); 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->setRequest($this->getRequest()); $form->setConfiguration(Config::app()); - $form->setWithChildren(true); $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - if ($form->isForced()) { - $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); - } + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } diff --git a/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php b/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php index 0aa18e903..a5deb3b7b 100644 --- a/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php +++ b/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php @@ -28,9 +28,10 @@ namespace Icinga\Module\Monitoring\Form\Command; -use \Zend_Form_Element_Checkbox; -use \Icinga\Web\Form\Element\DateTimePicker; -use \Icinga\Util\DateTimeFactory; +use Zend_Form_Element_Checkbox; +use Icinga\Util\DateTimeFactory; +use Icinga\Web\Form\Element\DateTimePicker; +use Icinga\Module\Monitoring\Command\ScheduleCheckCommand; /** * 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()); } } diff --git a/modules/monitoring/library/Monitoring/Command/BaseCommand.php b/modules/monitoring/library/Monitoring/Command/BaseCommand.php index 06f8979cc..e0412850b 100644 --- a/modules/monitoring/library/Monitoring/Command/BaseCommand.php +++ b/modules/monitoring/library/Monitoring/Command/BaseCommand.php @@ -39,6 +39,82 @@ use Icinga\Protocol\Commandpipe\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 * diff --git a/modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php b/modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php index 51a82ceca..097350363 100644 --- a/modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php +++ b/modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php @@ -92,7 +92,7 @@ class CustomNotificationCommand extends BaseCommand */ public function setForced($state) { - $this->forced = $state; + $this->forced = (bool) $state; return $this; } @@ -105,7 +105,7 @@ class CustomNotificationCommand extends BaseCommand */ public function setBroadcast($state) { - $this->broadcast = $state; + $this->broadcast = (bool) $state; return $this; } diff --git a/modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php b/modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php new file mode 100644 index 000000000..f743dd1a3 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php @@ -0,0 +1,134 @@ + + * @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 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())); + } +} \ 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 00a22c1fd..544856a72 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php @@ -54,5 +54,6 @@ class CommandPipeLoader extends LibraryLoader { 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'); + require_once('../../modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php'); } } diff --git a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php index 6db393979..b0df98676 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php @@ -41,6 +41,7 @@ use Icinga\Module\Monitoring\Command\AddCommentCommand; use Icinga\Module\Monitoring\Command\ScheduleDowntimeCommand; use Icinga\Module\Monitoring\Command\CustomNotificationCommand; use Icinga\Module\Monitoring\Command\DelayNotificationCommand; +use Icinga\Module\Monitoring\Command\ScheduleCheckCommand; if (!defined("EXTCMD_TEST_BIN")) { define("EXTCMD_TEST_BIN", "./bin/extcmd_test"); @@ -296,32 +297,51 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase { $pipe = $this->getLocalTestPipe(); try { - $pipe->getTransport()->setOpenMode("a"); // append so we have multiple results - $t = time(); - // normal reschedule - $pipe->scheduleCheck(array( - (object) array("host_name"=>"test"), - (object) array("host_name"=>"test","service_description"=>"svc1") - ),$t); - // forced - $pipe->scheduleForcedCheck(array( - (object) array("host_name"=>"test"), - (object) array("host_name"=>"test","service_description"=>"svc1") - ),$t); - // forced, recursive - $pipe->scheduleForcedCheck(array( - (object) array("host_name"=>"test"), - ),$t,true); + $pipe->getTransport()->setOpenMode('a'); // append so we have multiple results + $command = new ScheduleCheckCommand(5000); + $pipe->sendCommand( + $command, + array( + (object) array( + 'host_name' => 'test' + ), + (object) array( + 'host_name' => 'test', + 'service_description' => 'svc1' + ) + ) + ); + $command->setForced(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())); - $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]); + $result = explode("\n", file_get_contents($this->getPipeName())); + $this->assertCount(6, $result, 'Asserting a correct number of commands being written to the commandpipe'); + $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) { $this->cleanup(); throw $e;