diff --git a/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php b/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php new file mode 100644 index 000000000..fe1ae92c2 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php @@ -0,0 +1,188 @@ +host = (string) $host; + return $this; + } + + /** + * Get the remote host + * + * @return string + */ + public function getHost() + { + return $this->host; + } + + /** + * Set the port to connect to on the remote host + * + * @param int $port + * + * @return self + */ + public function setPort($port) + { + $this->port = (int) $port; + return $this; + } + + /** + * Get the port to connect on the remote host + * + * @return int + */ + public function getPort() + { + return $this->port; + } + + /** + * Set the user to log in as on the remote host + * + * @param string $user + * + * @return self + */ + public function setUser($user) + { + $this->user = (string) $user; + return $this; + } + + /** + * Get the user to log in as on the remote host + * + * Defaults to current PHP process' user + * + * @return string|null + */ + public function getUser() + { + return $this->user; + } + + /** + * Set the path to the Icinga command file on the remote host + * + * @param string $path + * + * @return self + */ + public function setPath($path) + { + $this->path = (string) $path; + return $this; + } + + /** + * Get the path to the Icinga command file on the remote host + * + * @return string + */ + public function getPath() + { + return $this->path; + } + + /** + * Write the command to the Icinga command file on the remote host + * + * @param IcingaCommand $command + * + * @throws LogicException + * @throws TransportException + */ + public function send(IcingaCommand $command) + { + if (! isset($this->path)) { + throw new LogicException; + } + Logger::debug( + sprintf( + mt('monitoring', 'Sending external Icinga command "%s" to the remote command file "%s:%u%s"'), + $command, + $this->host, + $this->port, + $this->path + ) + ); + $ssh = sprintf('ssh -o BatchMode=yes -p %u', $this->port); // -o BatchMode=yes for disabling interactive + // authentication methods + if (isset($this->user)) { + $ssh .= sprintf(' -l %s', escapeshellarg($this->user)); + } + $ssh .= sprintf( + ' %s "echo %s > %s" 2>&1', // Redirect stderr to stdout + escapeshellarg($this->host), + escapeshellarg($command), + escapeshellarg($this->path) + ); + exec($ssh, $output, $status); + if ($status !== 0) { + throw new TransportException( + mt( + 'monitoring', + 'Can\'t send external Icinga command "%s": %s' + ), + $ssh, + implode(' ', $output) + ); + } + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Transport/SecureShell.php b/modules/monitoring/library/Monitoring/Command/Transport/SecureShell.php deleted file mode 100644 index f1505c2ef..000000000 --- a/modules/monitoring/library/Monitoring/Command/Transport/SecureShell.php +++ /dev/null @@ -1,102 +0,0 @@ -host = isset($config->host) ? $config->host : 'localhost'; - $this->port = isset($config->port) ? $config->port : 22; - $this->user = isset($config->user) ? $config->user : null; - $this->path = isset($config->path) ? $config->path : '/usr/local/icinga/var/rw/icinga.cmd'; - } - - /** - * Write the given external command to the command pipe - * - * @param string $command - * - * @throws RuntimeException When the command could not be sent to the remote Icinga host - * @see Transport::send() - */ - 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; - $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 - ); - - exec($sshCommand, $output, $retCode); - Logger::debug("Command '%s' exited with %d: %s", $sshCommand, $retCode, $output); - - if ($retCode != 0) { - $msg = 'Could not send command to remote Icinga host: ' - . implode(PHP_EOL, $output) - . " (returncode $retCode)"; - Logger::error($msg); - throw new RuntimeException($msg); - } - } -}