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\Cli\Params;
|
|
|
|
use Icinga\Cli\Loader;
|
|
|
|
use Icinga\Cli\Screen;
|
2014-10-31 10:27:17 +01:00
|
|
|
use Icinga\Application\Logger;
|
2013-10-22 14:26:45 +02:00
|
|
|
use Icinga\Application\Benchmark;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2013-10-22 14:26:45 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
2014-03-31 18:35:28 +02:00
|
|
|
require_once __DIR__ . '/ApplicationBootstrap.php';
|
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-10-22 16:15:11 +02:00
|
|
|
protected $verbose;
|
|
|
|
|
|
|
|
protected $debug;
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
protected function bootstrap()
|
|
|
|
{
|
|
|
|
$this->assertRunningOnCli();
|
2014-02-26 11:19:52 +01:00
|
|
|
$this->setupLogging()
|
2014-01-29 16:25:08 +01:00
|
|
|
->setupErrorHandling()
|
2014-02-12 14:51:04 +01:00
|
|
|
->loadConfig()
|
|
|
|
->setupTimezone()
|
2014-02-26 11:19:52 +01:00
|
|
|
->setupInternationalization()
|
|
|
|
->parseBasicParams()
|
2014-02-14 10:48:17 +01:00
|
|
|
->setupLogger()
|
2014-01-29 16:25:08 +01:00
|
|
|
->setupResourceFactory()
|
2014-12-30 09:27:18 +01:00
|
|
|
->setupModuleManager()
|
|
|
|
->loadSetupModuleIfNecessary();
|
2013-10-22 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2014-04-17 23:09:30 +02:00
|
|
|
protected function setupLogging()
|
2013-10-22 16:15:11 +02:00
|
|
|
{
|
2014-04-17 23:09:30 +02:00
|
|
|
Logger::create(
|
2014-11-18 13:11:52 +01:00
|
|
|
new ConfigObject(
|
2014-04-17 23:09:30 +02:00
|
|
|
array(
|
2014-10-16 15:59:35 +02:00
|
|
|
'level' => Logger::INFO,
|
2014-11-11 19:43:22 +01:00
|
|
|
'log' => 'stdout',
|
2014-04-17 23:09:30 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2013-10-22 16:15:11 +02:00
|
|
|
return $this;
|
2013-10-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cliLoader()
|
|
|
|
{
|
|
|
|
if ($this->cliLoader === null) {
|
|
|
|
$this->cliLoader = new Loader($this);
|
|
|
|
}
|
|
|
|
return $this->cliLoader;
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:15:11 +02:00
|
|
|
protected function parseBasicParams()
|
2013-10-22 14:26:45 +02:00
|
|
|
{
|
|
|
|
$this->params = Params::parse();
|
|
|
|
if ($this->params->shift('help')) {
|
|
|
|
$this->params->unshift('help');
|
|
|
|
}
|
2014-01-22 12:45:42 +01:00
|
|
|
if ($this->params->shift('autocomplete')) {
|
|
|
|
$this->params->unshift('autocomplete');
|
|
|
|
}
|
2013-10-22 14:26:45 +02:00
|
|
|
$watch = $this->params->shift('watch');
|
|
|
|
if ($watch === true) {
|
|
|
|
$watch = 5;
|
|
|
|
}
|
|
|
|
if (preg_match('~^\d+$~', $watch)) {
|
|
|
|
$this->watchTimeout = (int) $watch;
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:15:11 +02:00
|
|
|
$this->debug = (int) $this->params->get('debug');
|
|
|
|
$this->verbose = (int) $this->params->get('verbose');
|
|
|
|
|
2013-10-22 14:26:45 +02:00
|
|
|
$this->showBenchmark = (bool) $this->params->shift('benchmark');
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getParams()
|
|
|
|
{
|
|
|
|
return $this->params;
|
|
|
|
}
|
|
|
|
|
2014-06-05 01:39:12 +02:00
|
|
|
public function dispatchModule($name, $basedir = null)
|
|
|
|
{
|
|
|
|
$this->getModuleManager()->loadModule($name, $basedir);
|
|
|
|
$this->cliLoader()->setModuleName($name);
|
|
|
|
$this->dispatch();
|
|
|
|
}
|
|
|
|
|
2013-10-22 14:26:45 +02:00
|
|
|
public function dispatch()
|
|
|
|
{
|
|
|
|
Benchmark::measure('Dispatching CLI command');
|
|
|
|
|
|
|
|
if ($this->watchTimeout === null) {
|
|
|
|
$this->dispatchOnce();
|
|
|
|
} else {
|
|
|
|
$this->dispatchEndless();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dispatchOnce()
|
|
|
|
{
|
2014-06-05 00:52:38 +02:00
|
|
|
$loader = $this->cliLoader();
|
2013-10-22 14:26:45 +02:00
|
|
|
$loader->parseParams();
|
|
|
|
$loader->dispatch();
|
|
|
|
Benchmark::measure('All done');
|
|
|
|
if ($this->showBenchmark) {
|
|
|
|
Benchmark::dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dispatchEndless()
|
|
|
|
{
|
2014-06-05 00:52:38 +02:00
|
|
|
$loader = $this->cliLoader();
|
2013-10-22 14:26:45 +02:00
|
|
|
$loader->parseParams();
|
|
|
|
$screen = Screen::instance();
|
2014-02-14 14:19:56 +01:00
|
|
|
|
2013-10-22 14:26:45 +02:00
|
|
|
while (true) {
|
|
|
|
Benchmark::measure('Watch mode - loop begins');
|
2014-02-07 11:23:29 +01:00
|
|
|
ob_start();
|
2013-10-22 14:26:45 +02:00
|
|
|
$params = clone($this->params);
|
2014-02-14 14:19:56 +01:00
|
|
|
$loader->dispatch($params);
|
2013-10-22 14:26:45 +02:00
|
|
|
Benchmark::measure('Dispatch done');
|
|
|
|
if ($this->showBenchmark) {
|
|
|
|
Benchmark::dump();
|
|
|
|
}
|
|
|
|
Benchmark::reset();
|
2014-02-07 11:23:29 +01:00
|
|
|
$out = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
echo $screen->clear() . $out;
|
2013-10-22 14:26:45 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|