Skip to content

Commit

Permalink
monitoring/commands: Let transports use the Icinga command file comma…
Browse files Browse the repository at this point in the history
…nd renderer

refs #6593
  • Loading branch information
lippserd committed Sep 11, 2014
1 parent 1df8076 commit b6ac31d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Expand Up @@ -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 {}
Expand Up @@ -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;

/**
Expand All @@ -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
*
Expand Down Expand Up @@ -79,34 +95,36 @@ 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(
mt(
'monitoring',
'Can\'t send external Icinga command "%s" to the local command file "%s": %s'
),
$command,
$commandString,
$this->path,
$e
);
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -143,23 +159,25 @@ 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;
}
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
Expand All @@ -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);
Expand Down

0 comments on commit b6ac31d

Please sign in to comment.