From e52037b84f144af47d5b9f954807ae44b8920de0 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 19 Nov 2015 14:46:36 +0100 Subject: [PATCH] cli/check: add --details and --colors --- application/clicommands/CheckCommand.php | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/application/clicommands/CheckCommand.php b/application/clicommands/CheckCommand.php index 707794f..b6ddd50 100644 --- a/application/clicommands/CheckCommand.php +++ b/application/clicommands/CheckCommand.php @@ -4,11 +4,28 @@ namespace Icinga\Module\Businessprocess\Clicommands; use Icinga\Cli\Command; use Icinga\Module\Businessprocess\Storage\LegacyStorage; +use Icinga\Module\Businessprocess\BpNode; +use Icinga\Module\Businessprocess\HostNode; class CheckCommand extends Command { protected $storage; + protected $hostColors = array( + 0 => array('black', 'lightgreen'), + 1 => array('black', 'lightred'), + 2 => array('black', 'brown'), + 99 => array('black', 'lightgray'), + ); + + protected $serviceColors = array( + 0 => array('black', 'lightgreen'), + 1 => array('black', 'yellow'), + 2 => array('black', 'lightred'), + 3 => array('black', 'lightpurple'), + 99 => array('black', 'lightgray'), + ); + public function init() { $this->storage = new LegacyStorage($this->Config()->getSection('global')); @@ -49,10 +66,46 @@ class CheckCommand extends Command ); exit(3); } + printf("Business Process %s: %s\n", $node->getStateName(), $node->getAlias()); + if ($this->params->shift('details')) { + echo $this->renderProblemTree($node->getProblemTree(), $this->params->shift('colors')); + } + exit($node->getState()); } + protected function renderProblemTree($tree, $useColors = false, $depth = 0) + { + $output = ''; + + foreach ($tree as $name => $subtree) { + $node = $subtree['node']; + + if ($node instanceof HostNode) { + $colors = $this->hostColors[$node->getState()]; + } else { + $colors = $this->serviceColors[$node->getState()]; + } + + $state = sprintf('[%s]', $node->getStateName()); + if ($useColors) { + $state = $this->screen->colorize($state, $colors[0], $colors[1]); + } + + $output .= sprintf( + "%s%s %s %s\n", + str_repeat(' ', $depth), + $node instanceof BpNode ? $node->getOperator() : '-', + $state, + $node->getAlias() + ); + $output .= $this->renderProblemTree($subtree['children'], $useColors, $depth + 1); + } + + return $output; + } + protected function getFirstProcessName() { $list = $this->storage->listProcesses();