HealthCommand: add PluginOutputBeautifier

This commit is contained in:
Thomas Gelf 2018-01-22 10:01:29 +01:00
parent 510b55ab33
commit 2f3e8c406f
2 changed files with 99 additions and 6 deletions

View File

@ -5,6 +5,7 @@ namespace Icinga\Module\Director\Clicommands;
use Icinga\Module\Director\CheckPlugin\PluginState; use Icinga\Module\Director\CheckPlugin\PluginState;
use Icinga\Module\Director\Cli\Command; use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Health; use Icinga\Module\Director\Health;
use Icinga\Module\Director\Cli\PluginOutputBeautifier;
/** /**
* Check Icinga Director Health * Check Icinga Director Health
@ -24,9 +25,10 @@ class HealthCommand extends Command
* *
* OPTIONS * OPTIONS
* *
* --check <name> Run only a specific set of checks * --check <name> Run only a specific set of checks
* valid names: config, sync, import, job, deployment * valid names: config, sync, import, job, deployment
* --db <name> Use a specific Icinga Web DB resource * --db <name> Use a specific Icinga Web DB resource
* --watch <seconds> Refresh every <second>. For interactive use only
*/ */
public function checkAction() public function checkAction()
{ {
@ -37,7 +39,7 @@ class HealthCommand extends Command
if ($name = $this->params->get('check')) { if ($name = $this->params->get('check')) {
$check = $health->getCheck($name); $check = $health->getCheck($name);
echo $check->getOutput(); echo PluginOutputBeautifier::beautify($check->getOutput(), $this->screen);
exit($check->getState()->getNumeric()); exit($check->getState()->getNumeric());
} else { } else {
@ -55,8 +57,24 @@ class HealthCommand extends Command
} else { } else {
echo "Icinga Director: there are problems\n\n"; echo "Icinga Director: there are problems\n\n";
} }
echo implode("\n", $output);
exit($state->getNumeric()); $out = PluginOutputBeautifier::beautify(implode("\n", $output), $this->screen);
echo $out;
if (! $this->isBeingWatched()) {
exit($state->getNumeric());
}
} }
} }
/**
* Cli should provide this information, as it shifts the parameter
*
* @return bool
*/
protected function isBeingWatched()
{
global $argv;
return in_array('--watch', $argv);
}
} }

View File

@ -0,0 +1,75 @@
<?php
namespace Icinga\Module\Director\Cli;
use Icinga\Cli\Screen;
class PluginOutputBeautifier
{
/** @var Screen */
protected $screen;
protected $isTty;
protected $colorized;
public function __construct(Screen $screen)
{
$this->screen = $screen;
}
public static function beautify($string, Screen $screen)
{
$self = new static($screen);
if ($self->isTty()) {
return $self->colorizeStates($string);
} else {
return $string;
}
}
protected function colorizeStates($string)
{
$string = preg_replace_callback(
"/'([^']+)'/",
[$this, 'highlightNames'],
$string
);
$string = preg_replace_callback(
'/(OK|WARNING|CRITICAL|UNKNOWN)/',
[$this, 'getColorized'],
$string
);
return $string;
}
protected function isTty()
{
if ($this->isTty === null) {
$this->isTty = function_exists('posix_isatty') && posix_isatty(STDOUT);
}
return $this->isTty;
}
protected function highlightNames($match)
{
return "'" . $this->screen->colorize($match[1], 'darkgray') . "'";
}
protected function getColorized($match)
{
if ($this->colorized === null) {
$this->colorized = [
'OK' => $this->screen->colorize('OK', 'lightgreen'),
'WARNING' => $this->screen->colorize('WARNING', 'yellow'),
'CRITICAL' => $this->screen->colorize('CRITICAL', 'lightred'),
'UNKNOWN' => $this->screen->colorize('UNKNOWN', 'lightpurple'),
];
}
return $this->colorized[$match[1]];
}
}