mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-25 14:54:24 +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();
|
$transport = new LocalCommandFile();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ConfigurationError;
|
throw new ConfigurationError();
|
||||||
}
|
}
|
||||||
unset($config->transport);
|
unset($config->transport);
|
||||||
foreach ($config as $key => $value) {
|
foreach ($config as $key => $value) {
|
||||||
$method = 'set' . ucfirst($key);
|
$method = 'set' . ucfirst($key);
|
||||||
if (! method_exists($transport, $method)) {
|
if (! method_exists($transport, $method)) {
|
||||||
throw new ConfigurationError;
|
throw new ConfigurationError();
|
||||||
}
|
}
|
||||||
$transport->$method($value);
|
$transport->$method($value);
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ abstract class CommandTransport
|
|||||||
{
|
{
|
||||||
$config = self::getConfig()->get($name);
|
$config = self::getConfig()->get($name);
|
||||||
if ($config === null) {
|
if ($config === null) {
|
||||||
throw new ConfigurationError;
|
throw new ConfigurationError();
|
||||||
}
|
}
|
||||||
return self::fromConfig($config);
|
return self::fromConfig($config);
|
||||||
}
|
}
|
||||||
|
@ -4,19 +4,7 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Monitoring\Command\Transport;
|
namespace Icinga\Module\Monitoring\Command\Transport;
|
||||||
|
|
||||||
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for Icinga command transports
|
* Interface for Icinga command transports
|
||||||
*/
|
*/
|
||||||
interface CommandTransportInterface
|
interface CommandTransportInterface {}
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Send the command
|
|
||||||
*
|
|
||||||
* @param IcingaCommand $command
|
|
||||||
*
|
|
||||||
* @throws \Icinga\Module\Monitoring\Command\Exception\TransportException
|
|
||||||
*/
|
|
||||||
public function send(IcingaCommand $command);
|
|
||||||
}
|
|
||||||
|
@ -9,6 +9,7 @@ use LogicException;
|
|||||||
use Icinga\Logger\Logger;
|
use Icinga\Logger\Logger;
|
||||||
use Icinga\Module\Monitoring\Command\Exception\TransportException;
|
use Icinga\Module\Monitoring\Command\Exception\TransportException;
|
||||||
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
||||||
|
use Icinga\Module\Monitoring\Command\Renderer\IcingaCommandFileCommandRenderer;
|
||||||
use Icinga\Util\File;
|
use Icinga\Util\File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,6 +31,21 @@ class LocalCommandFile implements CommandTransportInterface
|
|||||||
*/
|
*/
|
||||||
protected $openMode = 'wn';
|
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
|
* Set the path to the local Icinga command file
|
||||||
*
|
*
|
||||||
@ -80,25 +96,27 @@ class LocalCommandFile implements CommandTransportInterface
|
|||||||
* Write the command to the local Icinga command file
|
* Write the command to the local Icinga command file
|
||||||
*
|
*
|
||||||
* @param IcingaCommand $command
|
* @param IcingaCommand $command
|
||||||
|
* @param int|null $now
|
||||||
*
|
*
|
||||||
* @throws LogicException
|
* @throws LogicException
|
||||||
* @throws TransportException
|
* @throws TransportException
|
||||||
*/
|
*/
|
||||||
public function send(IcingaCommand $command)
|
public function send(IcingaCommand $command, $now = null)
|
||||||
{
|
{
|
||||||
if (! isset($this->path)) {
|
if (! isset($this->path)) {
|
||||||
throw new LogicException;
|
throw new LogicException;
|
||||||
}
|
}
|
||||||
|
$commandString = $this->renderer->render($command, $now);
|
||||||
Logger::debug(
|
Logger::debug(
|
||||||
sprintf(
|
sprintf(
|
||||||
mt('monitoring', 'Sending external Icinga command "%s" to the local command file "%s"'),
|
mt('monitoring', 'Sending external Icinga command "%s" to the local command file "%s"'),
|
||||||
$command,
|
$commandString,
|
||||||
$this->path
|
$this->path
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
$file = new File($this->path, $this->openMode);
|
$file = new File($this->path, $this->openMode);
|
||||||
$file->fwrite($command . "\n");
|
$file->fwrite($commandString . "\n");
|
||||||
$file->fflush();
|
$file->fflush();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new TransportException(
|
throw new TransportException(
|
||||||
@ -106,7 +124,7 @@ class LocalCommandFile implements CommandTransportInterface
|
|||||||
'monitoring',
|
'monitoring',
|
||||||
'Can\'t send external Icinga command "%s" to the local command file "%s": %s'
|
'Can\'t send external Icinga command "%s" to the local command file "%s": %s'
|
||||||
),
|
),
|
||||||
$command,
|
$commandString,
|
||||||
$this->path,
|
$this->path,
|
||||||
$e
|
$e
|
||||||
);
|
);
|
||||||
|
@ -8,6 +8,7 @@ use LogicException;
|
|||||||
use Icinga\Logger\Logger;
|
use Icinga\Logger\Logger;
|
||||||
use Icinga\Module\Monitoring\Command\Exception\TransportException;
|
use Icinga\Module\Monitoring\Command\Exception\TransportException;
|
||||||
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
||||||
|
use Icinga\Module\Monitoring\Command\Renderer\IcingaCommandFileCommandRenderer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A remote Icinga command file
|
* A remote Icinga command file
|
||||||
@ -46,6 +47,21 @@ class RemoteCommandFile implements CommandTransportInterface
|
|||||||
*/
|
*/
|
||||||
protected $path;
|
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
|
* Set the remote host
|
||||||
*
|
*
|
||||||
@ -144,11 +160,12 @@ class RemoteCommandFile implements CommandTransportInterface
|
|||||||
* Write the command to the Icinga command file on the remote host
|
* 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 LogicException
|
||||||
* @throws TransportException
|
* @throws TransportException
|
||||||
*/
|
*/
|
||||||
public function send(IcingaCommand $command)
|
public function send(IcingaCommand $command, $now = null)
|
||||||
{
|
{
|
||||||
if (! isset($this->path)) {
|
if (! isset($this->path)) {
|
||||||
throw new LogicException;
|
throw new LogicException;
|
||||||
@ -156,10 +173,11 @@ class RemoteCommandFile implements CommandTransportInterface
|
|||||||
if (! isset($this->host)) {
|
if (! isset($this->host)) {
|
||||||
throw new LogicException;
|
throw new LogicException;
|
||||||
}
|
}
|
||||||
|
$commandString = $this->renderer->render($command, $now);
|
||||||
Logger::debug(
|
Logger::debug(
|
||||||
sprintf(
|
sprintf(
|
||||||
mt('monitoring', 'Sending external Icinga command "%s" to the remote command file "%s:%u%s"'),
|
mt('monitoring', 'Sending external Icinga command "%s" to the remote command file "%s:%u%s"'),
|
||||||
$command,
|
$commandString,
|
||||||
$this->host,
|
$this->host,
|
||||||
$this->port,
|
$this->port,
|
||||||
$this->path
|
$this->path
|
||||||
@ -173,7 +191,7 @@ class RemoteCommandFile implements CommandTransportInterface
|
|||||||
$ssh .= sprintf(
|
$ssh .= sprintf(
|
||||||
' %s "echo %s > %s" 2>&1', // Redirect stderr to stdout
|
' %s "echo %s > %s" 2>&1', // Redirect stderr to stdout
|
||||||
escapeshellarg($this->host),
|
escapeshellarg($this->host),
|
||||||
escapeshellarg($command),
|
escapeshellarg($commandString),
|
||||||
escapeshellarg($this->path)
|
escapeshellarg($this->path)
|
||||||
);
|
);
|
||||||
exec($ssh, $output, $status);
|
exec($ssh, $output, $status);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user