2013-07-31 14:17:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Protocol\Commandpipe\Transport;
|
|
|
|
use Icinga\Application\Logger;
|
|
|
|
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
|
|
|
* CommandPipe Transport class that writes to a file accessible by the filesystem
|
|
|
|
*
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
class LocalPipe implements Transport
|
|
|
|
{
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
|
|
|
* The path of the icinga commandpipe
|
|
|
|
*
|
|
|
|
* @var String
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
private $path;
|
2013-08-01 17:48:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The mode to use for fopen()
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
private $openMode = "w";
|
|
|
|
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
|
|
|
* @see Transport::setEndpoint()
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
public function setEndpoint(\Zend_Config $config)
|
|
|
|
{
|
|
|
|
$this->path = isset($config->path) ? $config->path : '/usr/local/icinga/var/rw/icinga.cmd';
|
|
|
|
}
|
|
|
|
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
|
|
|
* @see Transport::send()
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
public function send($message)
|
|
|
|
{
|
|
|
|
Logger::debug('Attempting to send external icinga command %s to local command file ', $message, $this->path);
|
|
|
|
$file = @fopen($this->path, $this->openMode);
|
|
|
|
if (!$file) {
|
|
|
|
throw new \RuntimeException('Could not open icinga pipe at $file : ' . print_r(error_get_last(), true));
|
|
|
|
}
|
|
|
|
fwrite($file, '[' . time() . '] ' . $message . PHP_EOL);
|
|
|
|
Logger::debug('Writing [' . time() . '] ' . $message . PHP_EOL);
|
|
|
|
fclose($file);
|
|
|
|
}
|
|
|
|
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
|
|
|
* Overwrite the open mode (useful for testing)
|
|
|
|
*
|
|
|
|
* @param string $mode A open mode supported by fopen()
|
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
public function setOpenMode($mode)
|
|
|
|
{
|
|
|
|
$this->openMode = $mode;
|
|
|
|
}
|
|
|
|
}
|