2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-07-26 15:58:16 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
namespace Icinga\Application;
|
|
|
|
|
2013-10-22 14:26:45 +02:00
|
|
|
use Icinga\Application\Platform;
|
|
|
|
use Icinga\Application\ApplicationBootstrap;
|
|
|
|
use Icinga\Application\Modules\Manager as ModuleManager;
|
|
|
|
use Icinga\Cli\Params;
|
|
|
|
use Icinga\Cli\Loader;
|
|
|
|
use Icinga\Cli\Screen;
|
|
|
|
use Icinga\Application\Benchmark;
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
// @codingStandardsIgnoreStart
|
2013-10-22 14:26:45 +02:00
|
|
|
require_once dirname(__FILE__) . '/ApplicationBootstrap.php';
|
2013-07-30 10:47:50 +02:00
|
|
|
require_once dirname(__FILE__). '/../Exception/ProgrammingError.php';
|
2013-07-26 15:58:16 +02:00
|
|
|
// @codingStandardsIgnoreStop
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
class Cli extends ApplicationBootstrap
|
|
|
|
{
|
|
|
|
protected $isCli = true;
|
|
|
|
|
2013-10-22 14:26:45 +02:00
|
|
|
protected $params;
|
|
|
|
|
|
|
|
protected $showBenchmark = false;
|
|
|
|
|
|
|
|
protected $watchTimeout;
|
|
|
|
|
|
|
|
protected $cliLoader;
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
protected function bootstrap()
|
|
|
|
{
|
|
|
|
$this->assertRunningOnCli();
|
2013-07-26 15:58:16 +02:00
|
|
|
return $this->setupConfig()
|
2013-10-22 14:26:45 +02:00
|
|
|
->setupErrorHandling()
|
|
|
|
->setupResourceFactory()
|
|
|
|
->setupModules()
|
|
|
|
->parseParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cliLoader()
|
|
|
|
{
|
|
|
|
if ($this->cliLoader === null) {
|
|
|
|
$this->cliLoader = new Loader($this);
|
|
|
|
}
|
|
|
|
return $this->cliLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup module loader
|
|
|
|
*
|
|
|
|
* TODO: This can be removed once broken bootstrapping has been fixed
|
|
|
|
* Loading the module manager and enabling all modules have former
|
|
|
|
* been two different tasks. CLI does NOT enable any module by default.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
protected function setupModules()
|
|
|
|
{
|
|
|
|
$this->moduleManager = new ModuleManager($this, $this->getConfigDir('enabledModules'));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for module manager
|
|
|
|
*
|
|
|
|
* TODO: This can also be removed once fixed. Making everything private
|
|
|
|
* made this duplication necessary
|
|
|
|
*
|
|
|
|
* @return ModuleManager
|
|
|
|
*/
|
|
|
|
public function getModuleManager()
|
|
|
|
{
|
|
|
|
return $this->moduleManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseParams()
|
|
|
|
{
|
|
|
|
$this->params = Params::parse();
|
|
|
|
if ($this->params->shift('help')) {
|
|
|
|
$this->params->unshift('help');
|
|
|
|
}
|
|
|
|
$watch = $this->params->shift('watch');
|
|
|
|
if ($watch === true) {
|
|
|
|
$watch = 5;
|
|
|
|
}
|
|
|
|
if (preg_match('~^\d+$~', $watch)) {
|
|
|
|
$this->watchTimeout = (int) $watch;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->showBenchmark = (bool) $this->params->shift('benchmark');
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getParams()
|
|
|
|
{
|
|
|
|
return $this->params;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dispatch()
|
|
|
|
{
|
|
|
|
Benchmark::measure('Dispatching CLI command');
|
|
|
|
|
|
|
|
if ($this->watchTimeout === null) {
|
|
|
|
$this->dispatchOnce();
|
|
|
|
} else {
|
|
|
|
$this->dispatchEndless();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dispatchOnce()
|
|
|
|
{
|
|
|
|
$loader = new Loader($this);
|
|
|
|
$loader->parseParams();
|
|
|
|
$loader->dispatch();
|
|
|
|
Benchmark::measure('All done');
|
|
|
|
if ($this->showBenchmark) {
|
|
|
|
Benchmark::dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dispatchEndless()
|
|
|
|
{
|
|
|
|
$loader = new Loader($this);
|
|
|
|
$loader->parseParams();
|
|
|
|
$screen = Screen::instance();
|
|
|
|
while (true) {
|
|
|
|
Benchmark::measure('Watch mode - loop begins');
|
|
|
|
echo $screen->clear();
|
|
|
|
$params = clone($this->params);
|
|
|
|
$loader->dispatch();
|
|
|
|
Benchmark::measure('Dispatch done');
|
|
|
|
if ($this->showBenchmark) {
|
|
|
|
Benchmark::dump();
|
|
|
|
}
|
|
|
|
Benchmark::reset();
|
|
|
|
$this->params = $params;
|
|
|
|
sleep($this->watchTimeout);
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fail if Icinga has not been called on CLI
|
|
|
|
*
|
2013-10-22 14:26:45 +02:00
|
|
|
* @throws ProgrammingError
|
|
|
|
* @return void
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2013-07-26 15:58:16 +02:00
|
|
|
private function assertRunningOnCli()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
if (Platform::isCli()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-30 10:47:50 +02:00
|
|
|
throw new ProgrammingError('Icinga is not running on CLI');
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
}
|