From 1e351a772482081f563b9f54dacc244fe4a09bf5 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 23 Nov 2016 15:20:46 +0100 Subject: [PATCH] ProcessCommand: replace and deprecate CheckCommand fixes #13295 --- application/clicommands/CheckCommand.php | 103 +----------------- application/clicommands/ProcessCommand.php | 118 +++++++++++++++++++++ 2 files changed, 123 insertions(+), 98 deletions(-) create mode 100644 application/clicommands/ProcessCommand.php diff --git a/application/clicommands/CheckCommand.php b/application/clicommands/CheckCommand.php index 8895959..d1c561f 100644 --- a/application/clicommands/CheckCommand.php +++ b/application/clicommands/CheckCommand.php @@ -2,37 +2,15 @@ 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 +class CheckCommand extends ProcessCommand { - 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() + public function listActions() { - $this->storage = new LegacyStorage($this->Config()->getSection('global')); + return array('process'); } /** - * Check a specific process + * 'check process' is DEPRECATED, please use 'process check' instead * * USAGE * @@ -40,77 +18,6 @@ class CheckCommand extends Command */ public function processAction() { - $name = $this->params->get('config'); - if ($name === null) { - $name = $this->getFirstProcessName(); - } - - $bp = $this->storage->loadProcess($name); - - if (null !== ($stateType = $this->params->get('state-type'))) { - if ($stateType === 'soft') { - $bp->useSoftStates(); - } - if ($stateType === 'hard') { - $bp->useHardStates(); - } - } - - $node = $bp->getNode($this->params->shift()); - $bp->retrieveStatesFromBackend(); - if ($bp->hasErrors()) { - printf( - "Checking Business Process %s failed: %s\n", - $node->getAlias(), - implode("\n", $bp->getErrors()) - ); - 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); - } - - $output = str_replace("|", "¦", $output); - - return $output; - } - - protected function getFirstProcessName() - { - $list = $this->storage->listProcesses(); - return key($list); + $this->checkAction(); } } diff --git a/application/clicommands/ProcessCommand.php b/application/clicommands/ProcessCommand.php new file mode 100644 index 0000000..a2974a5 --- /dev/null +++ b/application/clicommands/ProcessCommand.php @@ -0,0 +1,118 @@ + array('black', 'lightgreen'), + 1 => array('lightgray', 'lightred'), + 2 => array('black', 'brown'), + 99 => array('black', 'lightgray'), + ); + + protected $serviceColors = array( + 0 => array('black', 'lightgreen'), + 1 => array('black', 'yellow'), + 2 => array('lightgray', 'lightred'), + 3 => array('black', 'lightpurple'), + 99 => array('black', 'lightgray'), + ); + + public function init() + { + $this->storage = new LegacyStorage($this->Config()->getSection('global')); + } + + /** + * Check a specific process + * + * USAGE + * + * icingacli businessprocess process check [--config ] + */ + public function checkAction() + { + $name = $this->params->get('config'); + if ($name === null) { + $name = $this->getFirstProcessName(); + } + + $bp = $this->storage->loadProcess($name); + + if (null !== ($stateType = $this->params->get('state-type'))) { + if ($stateType === 'soft') { + $bp->useSoftStates(); + } + if ($stateType === 'hard') { + $bp->useHardStates(); + } + } + + /** @var BpNode $node */ + $node = $bp->getNode($this->params->shift()); + $bp->retrieveStatesFromBackend(); + if ($bp->hasErrors()) { + printf( + "Checking Business Process %s failed: %s\n", + $node->getAlias(), + implode("\n", $bp->getErrors()) + ); + 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(); + return key($list); + } +}