/usr/share/icingaweb2/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php is in icingaweb2-module-monitoring 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Command\Transport;
use Icinga\Application\Config;
use Icinga\Application\Logger;
use Icinga\Data\ConfigObject;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Monitoring\Command\IcingaCommand;
use Icinga\Module\Monitoring\Command\Object\ObjectCommand;
use Icinga\Module\Monitoring\Exception\CommandTransportException;
/**
* Command transport
*
* This class is subject to change as we do not have environments yet (#4471).
*/
class CommandTransport implements CommandTransportInterface
{
/**
* Transport configuration
*
* @var Config
*/
protected static $config;
/**
* Get transport configuration
*
* @return Config
*
* @throws ConfigurationError
*/
public static function getConfig()
{
if (static::$config === null) {
$config = Config::module('monitoring', 'commandtransports');
if ($config->isEmpty()) {
throw new ConfigurationError(
mt('monitoring', 'No command transports have been configured in "%s".'),
$config->getConfigFile()
);
}
static::$config = $config;
}
return static::$config;
}
/**
* Create a transport from config
*
* @param ConfigObject $config
*
* @return LocalCommandFile|RemoteCommandFile
*
* @throws ConfigurationError
*/
public static function createTransport(ConfigObject $config)
{
$config = clone $config;
switch (strtolower($config->transport)) {
case RemoteCommandFile::TRANSPORT:
$transport = new RemoteCommandFile();
break;
case LocalCommandFile::TRANSPORT:
case '': // Casting null to string is the empty string
$transport = new LocalCommandFile();
break;
default:
throw new ConfigurationError(
mt(
'monitoring',
'Cannot create command transport "%s". Invalid transport'
. ' defined in "%s". Use one of "%s" or "%s".'
),
$config->transport,
static::getConfig()->getConfigFile(),
LocalCommandFile::TRANSPORT,
RemoteCommandFile::TRANSPORT
);
}
unset($config->transport);
foreach ($config as $key => $value) {
$method = 'set' . ucfirst($key);
if (! method_exists($transport, $method)) {
// Ignore settings from config that don't have a setter on the transport instead of throwing an
// exception here because the transport should throw an exception if it's not fully set up
// when being about to send a command
continue;
}
$transport->$method($value);
}
return $transport;
}
/**
* Send the given command over an appropriate Icinga command transport
*
* This will try one configured transport after another until the command has been successfully sent.
*
* @param IcingaCommand $command The command to send
* @param int|null $now Timestamp of the command or null for now
*
* @throws CommandTransportException If sending the Icinga command failed
*/
public function send(IcingaCommand $command, $now = null)
{
$tries = 0;
foreach (static::getConfig() as $transportConfig) {
$transport = static::createTransport($transportConfig);
if ($this->transferPossible($command, $transport)) {
try {
$transport->send($command, $now);
} catch (CommandTransportException $e) {
Logger::error($e);
$tries += 1;
continue; // Try the next transport
}
return; // The command was successfully sent
}
}
if ($tries > 0) {
throw new CommandTransportException(
mt(
'monitoring',
'Failed to send external Icinga command. None of the configured transports'
. ' was able to transfer the command. Please see the log for more details.'
)
);
}
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);
}
}
|