/** * Write the command to the Icinga command file on the remote host * * @param IcingaCommand $command * @param int|null $now * * @throws ConfigurationError * @throws CommandTransportException */ public function send(IcingaCommand $command, $now = null) { if (!isset($this->path)) { throw new ConfigurationError('Can\'t send external Icinga Command. Path to the remote command file is missing'); } if (!isset($this->host)) { throw new ConfigurationError('Can\'t send external Icinga Command. Remote host is missing'); } $commandString = $this->renderer->render($command, $now); Logger::debug('Sending external Icinga command "%s" to the remote command file "%s:%u%s"', $commandString, $this->host, $this->port, $this->path); return $this->sendCommandString($commandString); }
/** * Write the command to the local Icinga command file * * @param IcingaCommand $command * @param int|null $now * * @throws ConfigurationError * @throws CommandTransportException */ public function send(IcingaCommand $command, $now = null) { if (!isset($this->path)) { throw new ConfigurationError('Can\'t send external Icinga Command. Path to the local command file is missing'); } $commandString = $this->renderer->render($command, $now); Logger::debug('Sending external Icinga command "%s" to the local command file "%s"', $commandString, $this->path); try { $file = new File($this->path, $this->openMode); $file->fwrite($commandString . "\n"); } catch (Exception $e) { $message = $e->getMessage(); if ($e instanceof RuntimeException && ($pos = strrpos($message, ':')) !== false) { // Assume RuntimeException thrown by SplFileObject in the format: __METHOD__ . "({$filename}): Message" $message = substr($message, $pos + 1); } throw new CommandTransportException('Can\'t send external Icinga command to the local command file "%s": %s', $this->path, $message); } }
/** * Write the command to the Icinga command file on the remote host * * @param IcingaCommand $command * @param int|null $now * * @throws ConfigurationError * @throws TransportException */ public function send(IcingaCommand $command, $now = null) { if (!isset($this->path)) { throw new ConfigurationError('Can\'t send external Icinga Command. Path to the remote command file is missing'); } if (!isset($this->host)) { throw new ConfigurationError('Can\'t send external Icinga Command. Remote host is missing'); } $commandString = $this->renderer->render($command, $now); Logger::debug('Sending external Icinga command "%s" to the remote command file "%s:%u%s"', $commandString, $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)); } if (isset($this->privateKey)) { $ssh .= sprintf(' -o StrictHostKeyChecking=no -i %s', escapeshellarg($this->privateKey)); } $ssh .= sprintf(' %s "echo %s > %s" 2>&1', escapeshellarg($this->host), escapeshellarg($commandString), escapeshellarg($this->path)); exec($ssh, $output, $status); if ($status !== 0) { throw new TransportException('Can\'t send external Icinga command: %s', implode(' ', $output)); } }