diff --git a/modules/monitoring/library/Monitoring/Command/Transport/LocalCommandFile.php b/modules/monitoring/library/Monitoring/Command/Transport/LocalCommandFile.php new file mode 100644 index 000000000..8278b79b0 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Transport/LocalCommandFile.php @@ -0,0 +1,115 @@ +path = (string) $path; + return $this; + } + + /** + * Get the path to the local Icinga command file + * + * @return string + */ + public function getPath() + { + return $this->path; + } + + /** + * Set the mode used to open the icinga command file + * + * @param string $openMode + * + * @return self + */ + public function setOpenMode($openMode) + { + $this->openMode = (string) $openMode; + return $this; + } + + /** + * Get the mode used to open the icinga command file + * + * @return string + */ + public function getOpenMode() + { + return $this->openMode; + } + + /** + * Write the command to the local Icinga command file + * + * @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 local command file "%s"'), + $command, + $this->path + ) + ); + try { + $file = new File($this->path, $this->openMode); + $file->fwrite($command . "\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, + $this->path, + $e + ); + } + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Transport/LocalPipe.php b/modules/monitoring/library/Monitoring/Command/Transport/LocalPipe.php deleted file mode 100644 index 32f745565..000000000 --- a/modules/monitoring/library/Monitoring/Command/Transport/LocalPipe.php +++ /dev/null @@ -1,70 +0,0 @@ -path = isset($config->path) ? $config->path : '/usr/local/icinga/var/rw/icinga.cmd'; - } - - /** - * @see Transport::send() - */ - public function send($message) - { - Logger::debug('Attempting to send external icinga command %s to local command file ', $message, $this->path); - - try { - $file = new File($this->path, $this->openMode); - $file->fwrite('[' . time() . '] ' . $message . PHP_EOL); - $file->fflush(); - } catch (Exception $e) { - throw new ConfigurationError( - 'Could not open icinga command pipe at "%s" (%s)', - $this->path, - $e->getMessage() - ); - } - - Logger::debug('Command sent: [' . time() . '] ' . $message . PHP_EOL); - } - - /** - * Overwrite the open mode (useful for testing) - * - * @param string $mode The mode to use to access the pipe - */ - public function setOpenMode($mode) - { - $this->openMode = $mode; - } -}