mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-27 15:54:03 +02:00
HealthCommand: add PluginOutputBeautifier
This commit is contained in:
parent
510b55ab33
commit
2f3e8c406f
@ -5,6 +5,7 @@ namespace Icinga\Module\Director\Clicommands;
|
||||
use Icinga\Module\Director\CheckPlugin\PluginState;
|
||||
use Icinga\Module\Director\Cli\Command;
|
||||
use Icinga\Module\Director\Health;
|
||||
use Icinga\Module\Director\Cli\PluginOutputBeautifier;
|
||||
|
||||
/**
|
||||
* Check Icinga Director Health
|
||||
@ -27,6 +28,7 @@ class HealthCommand extends Command
|
||||
* --check <name> Run only a specific set of checks
|
||||
* valid names: config, sync, import, job, deployment
|
||||
* --db <name> Use a specific Icinga Web DB resource
|
||||
* --watch <seconds> Refresh every <second>. For interactive use only
|
||||
*/
|
||||
public function checkAction()
|
||||
{
|
||||
@ -37,7 +39,7 @@ class HealthCommand extends Command
|
||||
|
||||
if ($name = $this->params->get('check')) {
|
||||
$check = $health->getCheck($name);
|
||||
echo $check->getOutput();
|
||||
echo PluginOutputBeautifier::beautify($check->getOutput(), $this->screen);
|
||||
|
||||
exit($check->getState()->getNumeric());
|
||||
} else {
|
||||
@ -55,8 +57,24 @@ class HealthCommand extends Command
|
||||
} else {
|
||||
echo "Icinga Director: there are problems\n\n";
|
||||
}
|
||||
echo implode("\n", $output);
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
75
library/Director/Cli/PluginOutputBeautifier.php
Normal file
75
library/Director/Cli/PluginOutputBeautifier.php
Normal 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]];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user