From 2f3e8c406f29fe275c028ea2a8e8c419d810aba8 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 22 Jan 2018 10:01:29 +0100 Subject: [PATCH] HealthCommand: add PluginOutputBeautifier --- application/clicommands/HealthCommand.php | 30 ++++++-- .../Director/Cli/PluginOutputBeautifier.php | 75 +++++++++++++++++++ 2 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 library/Director/Cli/PluginOutputBeautifier.php diff --git a/application/clicommands/HealthCommand.php b/application/clicommands/HealthCommand.php index 19e13e49..0c3cb240 100644 --- a/application/clicommands/HealthCommand.php +++ b/application/clicommands/HealthCommand.php @@ -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 @@ -24,9 +25,10 @@ class HealthCommand extends Command * * OPTIONS * - * --check Run only a specific set of checks - * valid names: config, sync, import, job, deployment - * --db Use a specific Icinga Web DB resource + * --check Run only a specific set of checks + * valid names: config, sync, import, job, deployment + * --db Use a specific Icinga Web DB resource + * --watch Refresh every . 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); - 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); + } } diff --git a/library/Director/Cli/PluginOutputBeautifier.php b/library/Director/Cli/PluginOutputBeautifier.php new file mode 100644 index 00000000..18a18f22 --- /dev/null +++ b/library/Director/Cli/PluginOutputBeautifier.php @@ -0,0 +1,75 @@ +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]]; + } +}