From b6ac31d10f5598771a8c0a32689fe5a1bdf676c7 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 11 Sep 2014 17:39:59 +0200 Subject: [PATCH] monitoring/commands: Let transports use the Icinga command file command renderer refs #6593 --- .../Command/Transport/CommandTransport.php | 6 ++-- .../Transport/CommandTransportInterface.php | 14 +--------- .../Command/Transport/LocalCommandFile.php | 28 +++++++++++++++---- .../Command/Transport/RemoteCommandFile.php | 26 ++++++++++++++--- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php index 9adbbb062f..84845f23f0 100644 --- a/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php +++ b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php @@ -58,13 +58,13 @@ public static function fromConfig(Zend_Config $config) $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 @@ public static function create($name) { $config = self::getConfig()->get($name); if ($config === null) { - throw new ConfigurationError; + throw new ConfigurationError(); } return self::fromConfig($config); } diff --git a/modules/monitoring/library/Monitoring/Command/Transport/CommandTransportInterface.php b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransportInterface.php index bb0da27163..5f0908fa3c 100644 --- a/modules/monitoring/library/Monitoring/Command/Transport/CommandTransportInterface.php +++ b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransportInterface.php @@ -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 {} diff --git a/modules/monitoring/library/Monitoring/Command/Transport/LocalCommandFile.php b/modules/monitoring/library/Monitoring/Command/Transport/LocalCommandFile.php index 8278b79b0e..2fc68d3155 100644 --- a/modules/monitoring/library/Monitoring/Command/Transport/LocalCommandFile.php +++ b/modules/monitoring/library/Monitoring/Command/Transport/LocalCommandFile.php @@ -9,6 +9,7 @@ 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 @@ public function getOpenMode() /** * 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 @@ public function send(IcingaCommand $command) 'monitoring', 'Can\'t send external Icinga command "%s" to the local command file "%s": %s' ), - $command, + $commandString, $this->path, $e ); diff --git a/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php b/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php index 8092a00bd4..c019106cc3 100644 --- a/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php +++ b/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php @@ -8,6 +8,7 @@ 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 @@ public function getPath() /** * 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 @@ public function send(IcingaCommand $command) 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 @@ public function send(IcingaCommand $command) $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);