* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team * */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Protocol\Commandpipe\Transport; use Exception; use Icinga\Util\File; use Icinga\Logger\Logger; use Icinga\Exception\ConfigurationError; /** * CommandPipe Transport class that writes to a file accessible by the filesystem */ class LocalPipe implements Transport { /** * The path of the icinga commandpipe * * @var String */ private $path; /** * The mode to use for fopen() * * @var string */ private $openMode = "wn"; /** * @see Transport::setEndpoint() */ public function setEndpoint(\Zend_Config $config) { $this->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::open($this->path, $this->openMode)->write('[' . time() . '] ' . $message . PHP_EOL)->close(); } catch (Exception $e) { throw new ConfigurationError( sprintf( '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 A open mode supported by fopen() */ public function setOpenMode($mode) { $this->openMode = $mode; } }