Remove CommandType interface

Moved BaseCommand from the monitoring module to the application's library.

refs #4580
This commit is contained in:
Eric Lippmann 2013-09-16 14:39:14 +02:00
parent 4964d84e78
commit bf012fbd7e
13 changed files with 86 additions and 174 deletions

View File

@ -26,18 +26,14 @@
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Command;
namespace Icinga\Protocol\Commandpipe;
use Icinga\Exception\NotImplementedError;
use Icinga\Protocol\Commandpipe\CommandType;
use Icinga\Exception\ProgrammingError;
/**
* Base class for any concrete command implementation
*
* Provides some example methods and often used routines. When implementing
* a new command one is encouraged to override one of those examples.
*/
class BaseCommand implements CommandType
abstract class BaseCommand
{
/**
* Whether hosts are ignored in case of a host- or servicegroup
@ -116,14 +112,11 @@ class BaseCommand implements CommandType
}
/**
* Return this command's parameters properly arranged in an array
* Return this command's arguments in the order expected by the actual command definition
*
* @return array
*/
public function getParameters()
{
throw new NotImplementedError();
}
abstract public function getParameters();
/**
* Return the command as a string with the given host being inserted
@ -132,10 +125,7 @@ class BaseCommand implements CommandType
*
* @return string The string representation of the command
*/
public function getHostCommand($hostname)
{
throw new NotImplementedError();
}
abstract public function getHostCommand($hostname);
/**
* Return the command as a string with the given host and service being inserted
@ -145,10 +135,7 @@ class BaseCommand implements CommandType
*
* @return string The string representation of the command
*/
public function getServiceCommand($hostname, $servicename)
{
throw new NotImplementedError();
}
abstract public function getServiceCommand($hostname, $servicename);
/**
* Return the command as a string with the given hostgroup being inserted
@ -159,7 +146,7 @@ class BaseCommand implements CommandType
*/
public function getHostgroupCommand($hostgroup)
{
throw new NotImplementedError();
throw new ProgrammingError(get_class($this) . ' does not provide a hostgroup command');
}
/**
@ -171,6 +158,6 @@ class BaseCommand implements CommandType
*/
public function getServicegroupCommand($servicegroup)
{
throw new NotImplementedError();
throw new ProgrammingError(get_class($this) . ' does not provide a servicegroup command');
}
}

View File

@ -139,10 +139,10 @@ class CommandPipe
/**
* Send a command to the icinga pipe
*
* @param \Icinga\Protocol\Commandpipe\CommandType $command
* @param array $objects
* @param Command $command
* @param array $objects
*/
public function sendCommand(CommandType $command, array $objects)
public function sendCommand(BaseCommand $command, array $objects)
{
foreach ($objects as $object) {
$objectType = $this->getObjectType($object);

View File

@ -1,36 +0,0 @@
<?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\Protocol\Commandpipe;
/**
* Interface definition for command objects processed by the CommandPipe
*/
interface CommandType
{
}

View File

@ -29,7 +29,7 @@
namespace Icinga\Protocol\Commandpipe;
/**
* Container for comment information that can be send to icinga's external command pipe
* Container for comment information that can be send to Icinga's external command pipe
*/
class Comment
{

View File

@ -28,6 +28,7 @@
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
use Icinga\Protocol\Commandpipe\Comment;
/**
@ -88,7 +89,7 @@ class AcknowledgeCommand extends BaseCommand
*/
public function setExpire($expireTime)
{
$this->expireTime = intval($expireTime);
$this->expireTime = (int) $expireTime;
return $this;
}
@ -134,9 +135,8 @@ class AcknowledgeCommand extends BaseCommand
/**
* Return this command's parameters properly arranged in an array
*
* @return array
*
* @see BaseCommand::getParameters()
* @return array
* @see BaseCommand::getParameters()
*/
public function getParameters()
{
@ -161,8 +161,7 @@ class AcknowledgeCommand extends BaseCommand
* @param string $hostname The name of the host to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getHostCommand()
* @see BaseCommand::getHostCommand()
*/
public function getHostCommand($hostname)
{
@ -178,8 +177,7 @@ class AcknowledgeCommand extends BaseCommand
* @param string $servicename The name of the service to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand()
* @see BaseCommand::getServiceCommand()
*/
public function getServiceCommand($hostname, $servicename)
{

View File

@ -28,6 +28,7 @@
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
use Icinga\Protocol\Commandpipe\Comment;
/**
@ -67,18 +68,22 @@ class AddCommentCommand extends BaseCommand
return $this;
}
public function getParameters()
{
return $this->comment->getParameters();
}
/**
* 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()
* @see BaseCommand::getHostCommand()
*/
public function getHostCommand($hostname)
{
return sprintf('ADD_HOST_COMMENT;%s;', $hostname) . implode(';', $this->comment->getParameters());
return sprintf('ADD_HOST_COMMENT;%s;', $hostname) . implode(';', $this->getParameters());
}
/**
@ -88,12 +93,11 @@ class AddCommentCommand extends BaseCommand
* @param string $servicename The name of the service to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand()
* @see BaseCommand::getServiceCommand()
*/
public function getServiceCommand($hostname, $servicename)
{
return sprintf('ADD_SVC_COMMENT;%s;%s;', $hostname, $servicename)
. implode(';', $this->comment->getParameters());
. implode(';', $this->getParameters());
}
}

View File

@ -28,6 +28,7 @@
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
use Icinga\Protocol\Commandpipe\Comment;
/**
@ -112,9 +113,8 @@ class CustomNotificationCommand extends BaseCommand
/**
* Return this command's parameters properly arranged in an array
*
* @return array
*
* @see BaseCommand::getParameters()
* @return array
* @see BaseCommand::getParameters()
*/
public function getParameters()
{
@ -134,8 +134,7 @@ class CustomNotificationCommand extends BaseCommand
* @param string $hostname The name of the host to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getHostCommand()
* @see BaseCommand::getHostCommand()
*/
public function getHostCommand($hostname)
{
@ -149,8 +148,7 @@ class CustomNotificationCommand extends BaseCommand
* @param string $servicename The name of the service to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand()
* @see BaseCommand::getServiceCommand()
*/
public function getServiceCommand($hostname, $servicename)
{
@ -162,4 +160,4 @@ class CustomNotificationCommand extends BaseCommand
)
);
}
}
}

View File

@ -28,6 +28,8 @@
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
/**
* Command to delay a notification
*/
@ -59,16 +61,15 @@ class DelayNotificationCommand extends BaseCommand
*/
public function setDelay($seconds)
{
$this->delay = intval($seconds);
$this->delay = (int) $seconds;
return $this;
}
/**
* Return this command's parameters properly arranged in an array
*
* @return array
*
* @see BaseCommand::getParameters()
* @return array
* @see BaseCommand::getParameters()
*/
public function getParameters()
{
@ -81,8 +82,7 @@ class DelayNotificationCommand extends BaseCommand
* @param string $hostname The name of the host to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getHostCommand()
* @see BaseCommand::getHostCommand()
*/
public function getHostCommand($hostname)
{
@ -96,8 +96,7 @@ class DelayNotificationCommand extends BaseCommand
* @param string $servicename The name of the service to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand()
* @see BaseCommand::getServiceCommand()
*/
public function getServiceCommand($hostname, $servicename)
{
@ -109,4 +108,4 @@ class DelayNotificationCommand extends BaseCommand
)
);
}
}
}

View File

@ -28,6 +28,8 @@
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
/**
* Command to schedule checks
*/
@ -68,7 +70,7 @@ class ScheduleCheckCommand extends BaseCommand
*/
public function setCheckTime($checkTime)
{
$this->checkTime = intval($checkTime);
$this->checkTime = (int) $checkTime;
return $this;
}
@ -88,9 +90,8 @@ class ScheduleCheckCommand extends BaseCommand
/**
* Return this command's parameters properly arranged in an array
*
* @return array
*
* @see BaseCommand::getParameters()
* @return array
* @see BaseCommand::getParameters()
*/
public function getParameters()
{
@ -101,11 +102,9 @@ class ScheduleCheckCommand extends BaseCommand
* 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()
* @see BaseCommand::getHostCommand()
*/
public function getHostCommand($hostname)
{
@ -123,12 +122,11 @@ class ScheduleCheckCommand extends BaseCommand
* @param string $servicename The name of the service to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand()
* @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

@ -29,6 +29,7 @@
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Protocol\Commandpipe\BaseCommand;
/**
* Command for scheduling a new downtime
@ -93,7 +94,7 @@ class ScheduleDowntimeCommand extends BaseCommand
*/
public function setStart($startTime)
{
$this->startTime = intval($startTime);
$this->startTime = (int) $startTime;
return $this;
}
@ -106,7 +107,7 @@ class ScheduleDowntimeCommand extends BaseCommand
*/
public function setEnd($endTime)
{
$this->endTime = intval($endTime);
$this->endTime = (int) $endTime;
return $this;
}
@ -145,7 +146,7 @@ class ScheduleDowntimeCommand extends BaseCommand
*/
public function setDuration($duration)
{
$this->duration = intval($duration);
$this->duration = (int) $duration;
return $this;
}
@ -158,7 +159,7 @@ class ScheduleDowntimeCommand extends BaseCommand
*/
public function setTriggerId($triggerId)
{
$this->triggerId = intval($triggerId);
$this->triggerId = (int) $triggerId;
return $this;
}
@ -198,9 +199,8 @@ class ScheduleDowntimeCommand extends BaseCommand
/**
* Return this command's parameters properly arranged in an array
*
* @return array
*
* @see BaseCommand::getParameters()
* @return array
* @see BaseCommand::getParameters()
*/
public function getParameters()
{
@ -278,4 +278,4 @@ class ScheduleDowntimeCommand extends BaseCommand
return sprintf('SCHEDULE_SERVICEGROUP_%s_DOWNTIME;', $this->withoutServices ? 'HOST' : 'SVC')
. implode(';', array_merge(array($servicegroup), $this->getParameters()));
}
}
}

View File

@ -28,6 +28,8 @@
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\BaseCommand;
/**
* Command to submit passive check results
*/
@ -77,13 +79,13 @@ class SubmitPassiveCheckresultCommand extends BaseCommand
*/
public function setState($state)
{
$this->state = intval($state);
$this->state = (int) $state;
return $this;
}
/**
* Set the plugin-output to include in the result
*
*
* @param string $output
*
* @return self
@ -109,9 +111,8 @@ class SubmitPassiveCheckresultCommand extends BaseCommand
/**
* Return this command's parameters properly arranged in an array
*
* @return array
*
* @see BaseCommand::getParameters()
* @return array
* @see BaseCommand::getParameters()
*/
public function getParameters()
{
@ -127,8 +128,7 @@ class SubmitPassiveCheckresultCommand extends BaseCommand
* @param string $hostname The name of the host to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getHostCommand()
* @see BaseCommand::getHostCommand()
*/
public function getHostCommand($hostname)
{
@ -142,8 +142,7 @@ class SubmitPassiveCheckresultCommand extends BaseCommand
* @param string $servicename The name of the service to insert
*
* @return string The string representation of the command
*
* @see BaseCommand::getServiceCommand()
* @see BaseCommand::getServiceCommand()
*/
public function getServiceCommand($hostname, $servicename)
{

View File

@ -1,33 +0,0 @@
<?php
namespace Tests\Icinga\Protocol\Commandpipe;
use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Module\Monitoring\Command\AcknowledgeCommand;
require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php");
require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php");
require_once("../../library/Icinga/Protocol/Commandpipe/CommandPipe.php");
require_once('../../modules/monitoring/library/Monitoring/Command/BaseCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php');
class AcknowledgementTest extends \PHPUnit_Framework_TestCase
{
public function testAcknowledgeHostMessage()
{
$ack = new AcknowledgeCommand(new Comment("author", "commentdata"));
$this->assertEquals("ACKNOWLEDGE_HOST_PROBLEM;foo;0;0;0;author;commentdata", $ack->getHostCommand('foo'));
$ack->setExpire(1000);
$this->assertEquals("ACKNOWLEDGE_HOST_PROBLEM_EXPIRE;bar;0;0;0;1000;author;commentdata", $ack->getHostCommand('bar'));
}
public function testAcknowledgeServiceMessage()
{
$ack = new AcknowledgeCommand(new Comment("author", "commentdata"));
$this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM;foo;bar;0;0;0;author;commentdata", $ack->getServiceCommand('foo', 'bar'));
$ack->setExpire(1000);
$this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM_EXPIRE;bar;foo;0;0;0;1000;author;commentdata", $ack->getServiceCommand('bar', 'foo'));
}
}

View File

@ -28,7 +28,7 @@
namespace Tests\Icinga\Protocol\Commandpipe;
require_once("./library/Icinga/LibraryLoader.php");
require_once('./library/Icinga/LibraryLoader.php');
use Test\Icinga\LibraryLoader;
@ -36,25 +36,23 @@ class CommandPipeLoader extends LibraryLoader {
public static function requireLibrary()
{
require_once("Zend/Config.php");
require_once("Zend/Log.php");
require_once("../../library/Icinga/Application/Logger.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php");
require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php");
require_once("../../library/Icinga/Protocol/Commandpipe/CommandPipe.php");
require_once("../../library/Icinga/Protocol/Commandpipe/PropertyModifier.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/Transport.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/SecureShell.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php");
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');
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');
require_once('../../modules/monitoring/library/Monitoring/Command/SubmitPassiveCheckresultCommand.php');
require_once('Zend/Config.php');
require_once('Zend/Log.php');
require_once(realpath('../../library/Icinga/Application/Logger.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/BaseCommand.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Comment.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/CommandPipe.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/PropertyModifier.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Transport/Transport.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Transport/SecureShell.php'));
require_once(realpath('../../library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php'));
require_once(realpath('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php'));
require_once(realpath('../../modules/monitoring/library/Monitoring/Command/AddCommentCommand.php'));
require_once(realpath('../../modules/monitoring/library/Monitoring/Command/ScheduleDowntimeCommand.php'));
require_once(realpath('../../modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php'));
require_once(realpath('../../modules/monitoring/library/Monitoring/Command/DelayNotificationCommand.php'));
require_once(realpath('../../modules/monitoring/library/Monitoring/Command/ScheduleCheckCommand.php'));
require_once(realpath('../../modules/monitoring/library/Monitoring/Command/SubmitPassiveCheckresultCommand.php'));
}
}