2013-07-31 14:17:40 +02:00
|
|
|
<?php
|
2013-08-06 09:23:00 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-31 14:17:40 +02:00
|
|
|
|
|
|
|
namespace Icinga\Protocol\Commandpipe\Transport;
|
2013-08-06 09:23:00 +02:00
|
|
|
|
2014-06-23 13:19:06 +02:00
|
|
|
use Exception;
|
|
|
|
use Icinga\Util\File;
|
2014-02-26 11:19:52 +01:00
|
|
|
use Icinga\Logger\Logger;
|
2014-05-02 11:19:35 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2013-07-31 14:17:40 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2014-06-26 15:56:57 +02:00
|
|
|
* The mode to use to access the pipe
|
2013-08-01 17:48:36 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-06-23 13:19:06 +02:00
|
|
|
private $openMode = "wn";
|
2013-07-31 14:17:40 +02:00
|
|
|
|
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);
|
2014-06-23 13:19:06 +02:00
|
|
|
|
|
|
|
try {
|
2014-06-26 15:56:57 +02:00
|
|
|
$file = new File($this->path, $this->openMode);
|
|
|
|
$file->fwrite('[' . time() . '] ' . $message . PHP_EOL);
|
|
|
|
$file->fflush();
|
2014-06-23 13:19:06 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new ConfigurationError(
|
2014-08-22 11:46:11 +02:00
|
|
|
'Could not open icinga command pipe at "%s" (%s)',
|
|
|
|
$this->path,
|
|
|
|
$e->getMessage()
|
2014-06-23 13:19:06 +02:00
|
|
|
);
|
2013-07-31 14:17:40 +02:00
|
|
|
}
|
2014-06-23 13:19:06 +02:00
|
|
|
|
|
|
|
Logger::debug('Command sent: [' . time() . '] ' . $message . PHP_EOL);
|
2013-07-31 14:17:40 +02:00
|
|
|
}
|
|
|
|
|
2013-08-01 17:48:36 +02:00
|
|
|
/**
|
|
|
|
* Overwrite the open mode (useful for testing)
|
|
|
|
*
|
2014-06-26 15:56:57 +02:00
|
|
|
* @param string $mode The mode to use to access the pipe
|
2013-08-01 17:48:36 +02:00
|
|
|
*/
|
2013-07-31 14:17:40 +02:00
|
|
|
public function setOpenMode($mode)
|
|
|
|
{
|
|
|
|
$this->openMode = $mode;
|
|
|
|
}
|
2013-08-06 09:23:00 +02:00
|
|
|
}
|