2013-07-31 14:17:40 +02:00
|
|
|
<?php
|
2013-08-06 09:23:00 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-31 14:17:40 +02:00
|
|
|
|
|
|
|
namespace Icinga\Protocol\Commandpipe\Transport;
|
|
|
|
|
2014-03-20 15:04:52 +01:00
|
|
|
use RuntimeException;
|
|
|
|
use Zend_Config;
|
|
|
|
use Icinga\Logger\Logger;
|
2013-07-31 14:17:40 +02:00
|
|
|
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
2013-08-21 11:02:53 +02:00
|
|
|
* Command pipe transport class that uses ssh for connecting to a remote filesystem with the icinga.cmd pipe
|
2013-08-01 17:48:36 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* The remote host must have KeyAuth enabled for this user
|
2013-08-01 17:48:36 +02:00
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
class SecureShell implements Transport
|
|
|
|
{
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
|
|
|
* The remote host to connect to
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
private $host = 'localhost';
|
2013-08-01 17:48:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The location of the icinga pipe on the remote host
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
private $path = "/usr/local/icinga/var/rw/icinga.cmd";
|
2013-08-01 17:48:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The SSH port of the remote host
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
private $port = 22;
|
2013-08-01 17:48:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The user to authenticate with on the remote host
|
|
|
|
*
|
|
|
|
* @var String
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
private $user = null;
|
|
|
|
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
2013-08-21 11:02:53 +02:00
|
|
|
* Overwrite the target file of this Transport class using the given config from instances.ini
|
2013-08-01 17:48:36 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param Zend_Config $config
|
|
|
|
*
|
|
|
|
* @see Transport::setEndpoint()
|
2013-08-01 17:48:36 +02:00
|
|
|
*/
|
2013-08-21 11:02:53 +02:00
|
|
|
public function setEndpoint(Zend_Config $config)
|
2013-07-31 14:17:40 +02:00
|
|
|
{
|
2013-08-21 11:02:53 +02:00
|
|
|
$this->host = isset($config->host) ? $config->host : 'localhost';
|
2013-07-31 14:17:40 +02:00
|
|
|
$this->port = isset($config->port) ? $config->port : 22;
|
|
|
|
$this->user = isset($config->user) ? $config->user : null;
|
2013-08-21 11:02:53 +02:00
|
|
|
$this->path = isset($config->path) ? $config->path : '/usr/local/icinga/var/rw/icinga.cmd';
|
2013-07-31 14:17:40 +02:00
|
|
|
}
|
|
|
|
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
2013-08-21 11:02:53 +02:00
|
|
|
* Write the given external command to the command pipe
|
|
|
|
*
|
|
|
|
* @param string $command
|
2013-08-01 17:48:36 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @throws RuntimeException When the command could not be sent to the remote Icinga host
|
|
|
|
* @see Transport::send()
|
2013-08-01 17:48:36 +02:00
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
public function send($command)
|
|
|
|
{
|
|
|
|
$retCode = 0;
|
|
|
|
$output = array();
|
|
|
|
Logger::debug(
|
|
|
|
'Icinga instance is on different host, attempting to send command %s via ssh to %s:%s/%s',
|
|
|
|
$command,
|
|
|
|
$this->host,
|
|
|
|
$this->port,
|
|
|
|
$this->path
|
|
|
|
);
|
|
|
|
$hostConnector = $this->user ? $this->user . "@" . $this->host : $this->host;
|
2014-03-20 15:04:52 +01:00
|
|
|
$command = escapeshellarg('['. time() .'] ' . $command);
|
|
|
|
$sshCommand = sprintf(
|
|
|
|
'ssh -o BatchMode=yes -o KbdInteractiveAuthentication=no %s -p %d'
|
|
|
|
. ' "echo %s > %s" 2>&1',
|
|
|
|
$hostConnector,
|
|
|
|
$this->port,
|
|
|
|
$command,
|
|
|
|
$this->path
|
2013-07-31 14:17:40 +02:00
|
|
|
);
|
2014-03-20 15:04:52 +01:00
|
|
|
|
|
|
|
exec($sshCommand, $output, $retCode);
|
|
|
|
Logger::debug("Command '%s' exited with %d: %s", $sshCommand, $retCode, $output);
|
2013-07-31 14:17:40 +02:00
|
|
|
|
|
|
|
if ($retCode != 0) {
|
2013-08-21 11:02:53 +02:00
|
|
|
$msg = 'Could not send command to remote Icinga host: '
|
2013-08-06 09:23:00 +02:00
|
|
|
. implode(PHP_EOL, $output)
|
|
|
|
. " (returncode $retCode)";
|
2013-07-31 14:17:40 +02:00
|
|
|
Logger::error($msg);
|
2013-08-21 11:02:53 +02:00
|
|
|
throw new RuntimeException($msg);
|
2013-07-31 14:17:40 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-06 09:23:00 +02:00
|
|
|
}
|