mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-04-08 17:15:08 +02:00
monitoring/commands: Let transports use the Icinga command file command renderer
refs #6593
This commit is contained in:
parent
1df8076234
commit
b6ac31d10f
@ -58,13 +58,13 @@ abstract class CommandTransport
|
||||
$transport = new LocalCommandFile();
|
||||
break;
|
||||
default:
|
||||
throw new ConfigurationError;
|
||||
throw new ConfigurationError();
|
||||
}
|
||||
unset($config->transport);
|
||||
foreach ($config as $key => $value) {
|
||||
$method = 'set' . ucfirst($key);
|
||||
if (! method_exists($transport, $method)) {
|
||||
throw new ConfigurationError;
|
||||
throw new ConfigurationError();
|
||||
}
|
||||
$transport->$method($value);
|
||||
}
|
||||
@ -83,7 +83,7 @@ abstract class CommandTransport
|
||||
{
|
||||
$config = self::getConfig()->get($name);
|
||||
if ($config === null) {
|
||||
throw new ConfigurationError;
|
||||
throw new ConfigurationError();
|
||||
}
|
||||
return self::fromConfig($config);
|
||||
}
|
||||
|
@ -4,19 +4,7 @@
|
||||
|
||||
namespace Icinga\Module\Monitoring\Command\Transport;
|
||||
|
||||
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
||||
|
||||
/**
|
||||
* Interface for Icinga command transports
|
||||
*/
|
||||
interface CommandTransportInterface
|
||||
{
|
||||
/**
|
||||
* Send the command
|
||||
*
|
||||
* @param IcingaCommand $command
|
||||
*
|
||||
* @throws \Icinga\Module\Monitoring\Command\Exception\TransportException
|
||||
*/
|
||||
public function send(IcingaCommand $command);
|
||||
}
|
||||
interface CommandTransportInterface {}
|
||||
|
@ -9,6 +9,7 @@ use LogicException;
|
||||
use Icinga\Logger\Logger;
|
||||
use Icinga\Module\Monitoring\Command\Exception\TransportException;
|
||||
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
||||
use Icinga\Module\Monitoring\Command\Renderer\IcingaCommandFileCommandRenderer;
|
||||
use Icinga\Util\File;
|
||||
|
||||
/**
|
||||
@ -30,6 +31,21 @@ class LocalCommandFile implements CommandTransportInterface
|
||||
*/
|
||||
protected $openMode = 'wn';
|
||||
|
||||
/**
|
||||
* Command renderer
|
||||
*
|
||||
* @var IcingaCommandFileCommandRenderer
|
||||
*/
|
||||
protected $renderer;
|
||||
|
||||
/**
|
||||
* Create a new local command file command transport
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->renderer = new IcingaCommandFileCommandRenderer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the path to the local Icinga command file
|
||||
*
|
||||
@ -79,26 +95,28 @@ class LocalCommandFile implements CommandTransportInterface
|
||||
/**
|
||||
* Write the command to the local Icinga command file
|
||||
*
|
||||
* @param IcingaCommand $command
|
||||
* @param IcingaCommand $command
|
||||
* @param int|null $now
|
||||
*
|
||||
* @throws LogicException
|
||||
* @throws TransportException
|
||||
*/
|
||||
public function send(IcingaCommand $command)
|
||||
public function send(IcingaCommand $command, $now = null)
|
||||
{
|
||||
if (! isset($this->path)) {
|
||||
throw new LogicException;
|
||||
}
|
||||
$commandString = $this->renderer->render($command, $now);
|
||||
Logger::debug(
|
||||
sprintf(
|
||||
mt('monitoring', 'Sending external Icinga command "%s" to the local command file "%s"'),
|
||||
$command,
|
||||
$commandString,
|
||||
$this->path
|
||||
)
|
||||
);
|
||||
try {
|
||||
$file = new File($this->path, $this->openMode);
|
||||
$file->fwrite($command . "\n");
|
||||
$file->fwrite($commandString . "\n");
|
||||
$file->fflush();
|
||||
} catch (Exception $e) {
|
||||
throw new TransportException(
|
||||
@ -106,7 +124,7 @@ class LocalCommandFile implements CommandTransportInterface
|
||||
'monitoring',
|
||||
'Can\'t send external Icinga command "%s" to the local command file "%s": %s'
|
||||
),
|
||||
$command,
|
||||
$commandString,
|
||||
$this->path,
|
||||
$e
|
||||
);
|
||||
|
@ -8,6 +8,7 @@ use LogicException;
|
||||
use Icinga\Logger\Logger;
|
||||
use Icinga\Module\Monitoring\Command\Exception\TransportException;
|
||||
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
||||
use Icinga\Module\Monitoring\Command\Renderer\IcingaCommandFileCommandRenderer;
|
||||
|
||||
/**
|
||||
* A remote Icinga command file
|
||||
@ -46,6 +47,21 @@ class RemoteCommandFile implements CommandTransportInterface
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* Command renderer
|
||||
*
|
||||
* @var IcingaCommandFileCommandRenderer
|
||||
*/
|
||||
protected $renderer;
|
||||
|
||||
/**
|
||||
* Create a new remote command file command transport
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->renderer = new IcingaCommandFileCommandRenderer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the remote host
|
||||
*
|
||||
@ -143,12 +159,13 @@ class RemoteCommandFile implements CommandTransportInterface
|
||||
/**
|
||||
* Write the command to the Icinga command file on the remote host
|
||||
*
|
||||
* @param IcingaCommand $command
|
||||
* @param IcingaCommand $command
|
||||
* @param int|null $now
|
||||
*
|
||||
* @throws LogicException
|
||||
* @throws TransportException
|
||||
*/
|
||||
public function send(IcingaCommand $command)
|
||||
public function send(IcingaCommand $command, $now = null)
|
||||
{
|
||||
if (! isset($this->path)) {
|
||||
throw new LogicException;
|
||||
@ -156,10 +173,11 @@ class RemoteCommandFile implements CommandTransportInterface
|
||||
if (! isset($this->host)) {
|
||||
throw new LogicException;
|
||||
}
|
||||
$commandString = $this->renderer->render($command, $now);
|
||||
Logger::debug(
|
||||
sprintf(
|
||||
mt('monitoring', 'Sending external Icinga command "%s" to the remote command file "%s:%u%s"'),
|
||||
$command,
|
||||
$commandString,
|
||||
$this->host,
|
||||
$this->port,
|
||||
$this->path
|
||||
@ -173,7 +191,7 @@ class RemoteCommandFile implements CommandTransportInterface
|
||||
$ssh .= sprintf(
|
||||
' %s "echo %s > %s" 2>&1', // Redirect stderr to stdout
|
||||
escapeshellarg($this->host),
|
||||
escapeshellarg($command),
|
||||
escapeshellarg($commandString),
|
||||
escapeshellarg($this->path)
|
||||
);
|
||||
exec($ssh, $output, $status);
|
||||
|
Loading…
x
Reference in New Issue
Block a user