mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-25 23:04:51 +02:00
monitoring/commands: Replace LocalPipe' with
LocalCommandFile'
`LocalCommandFile' is configured via property setters instead of the too general `setEndpoint' function. refs #6593
This commit is contained in:
parent
2ac4a8503c
commit
ec46b368df
@ -0,0 +1,115 @@
|
|||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
namespace Icinga\Module\Monitoring\Command\Transport;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use LogicException;
|
||||||
|
use Icinga\Logger\Logger;
|
||||||
|
use Icinga\Module\Monitoring\Command\Exception\TransportException;
|
||||||
|
use Icinga\Module\Monitoring\Command\IcingaCommand;
|
||||||
|
use Icinga\Util\File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A local Icinga command file
|
||||||
|
*/
|
||||||
|
class LocalCommandFile implements CommandTransportInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Path to the icinga command file
|
||||||
|
*
|
||||||
|
* @var String
|
||||||
|
*/
|
||||||
|
protected $path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mode used to open the icinga command file
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $openMode = 'wn';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the path to the local Icinga command file
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setPath($path)
|
||||||
|
{
|
||||||
|
$this->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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,70 +0,0 @@
|
|||||||
<?php
|
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
// {{{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 to access the pipe
|
|
||||||
*
|
|
||||||
* @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 = 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;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user