CommandTransport: Respect instance association

refs #9651
This commit is contained in:
Johannes Meyer 2015-08-31 12:19:42 +02:00
parent 1d6d4f0b10
commit b7cdfcfae0

View File

@ -8,6 +8,7 @@ use Icinga\Application\Logger;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Module\Monitoring\Command\IcingaCommand; use Icinga\Module\Monitoring\Command\IcingaCommand;
use Icinga\Module\Monitoring\Command\Object\ObjectCommand;
use Icinga\Module\Monitoring\Exception\CommandTransportException; use Icinga\Module\Monitoring\Exception\CommandTransportException;
/** /**
@ -110,19 +111,24 @@ class CommandTransport implements CommandTransportInterface
*/ */
public function send(IcingaCommand $command, $now = null) public function send(IcingaCommand $command, $now = null)
{ {
$tries = 0;
foreach (static::getConfig() as $transportConfig) { foreach (static::getConfig() as $transportConfig) {
$transport = static::createTransport($transportConfig); $transport = static::createTransport($transportConfig);
if ($this->transferPossible($command, $transport)) {
try { try {
$transport->send($command, $now); $transport->send($command, $now);
} catch (CommandTransportException $e) { } catch (CommandTransportException $e) {
Logger::error($e); Logger::error($e);
$tries += 1;
continue; // Try the next transport continue; // Try the next transport
} }
return; // The command was successfully sent return; // The command was successfully sent
} }
}
if ($tries > 0) {
throw new CommandTransportException( throw new CommandTransportException(
mt( mt(
'monitoring', 'monitoring',
@ -131,4 +137,35 @@ class CommandTransport implements CommandTransportInterface
) )
); );
} }
throw new CommandTransportException(
mt(
'monitoring',
'Failed to send external Icinga command. No transport has been configured'
. ' for this instance. Please contact your Icinga Web administrator.'
)
);
}
/**
* Return whether it is possible to send the given command using the given transport
*
* @param IcingaCommand $command
* @param CommandTransportInterface $transport
*
* @return bool
*/
protected function transferPossible($command, $transport)
{
if (! method_exists($transport, 'getInstance') || !$command instanceof ObjectCommand) {
return true;
}
$transportInstance = $transport->getInstance();
if (! $transportInstance || $transportInstance === 'none') {
return true;
}
return strtolower($transportInstance) === strtolower($command->getObject()->instance_name);
}
} }