From de1ba2aa7cb9f0d4f7d1cefe7ee3a384de519bae Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sat, 7 Feb 2015 01:27:39 +0100 Subject: [PATCH] CheckCommand: code cleanup, remove legacy code --- application/clicommands/CheckCommand.php | 92 +++++++++--------------- 1 file changed, 32 insertions(+), 60 deletions(-) diff --git a/application/clicommands/CheckCommand.php b/application/clicommands/CheckCommand.php index ff9aee0..cbf2234 100644 --- a/application/clicommands/CheckCommand.php +++ b/application/clicommands/CheckCommand.php @@ -3,85 +3,57 @@ namespace Icinga\Module\Businessprocess\Clicommands; use Icinga\Cli\Command; -use Icinga\Module\Businessprocess\BusinessProcess; -use Icinga\Application\Config; -use Icinga\Module\Monitoring\Backend; +use Icinga\Module\Businessprocess\Storage\LegacyStorage; class CheckCommand extends Command { - protected $config; - protected $bpconf; - protected $bpname; - protected $views; - protected $filename; - protected $backend; + protected $storage; public function init() { + // WTF? I do not want to have to take care about this!! + $this->app->setupZendAutoloader(); + $this->app->getModuleManager()->loadModule('monitoring'); - $this->config = $this->Config(); - $this->readConfig(); - $this->prepareBackend(); + $this->storage = new LegacyStorage($this->Config()->getSection('global')); } /** * Check a specific process * - * Blabla + * USAGE + * + * icingacli businessprocess check process [--config ] */ public function processAction() - { - $bp = BusinessProcess::parse($this->filename); - $node = $bp->getNode($this->params->shift()); - if ($this->params->get('soft-states')) { - $bp->useSoftStates(); + { + $name = $this->params->get('config'); + if ($name === null) { + $name = $this->getFirstProcessName(); } - $bp->retrieveStatesFromBackend($this->backend); + + $bp = $this->storage->loadProcess($name); + + if (null !== ($stateType = $this->params->get('state-type'))) { + if ($stateType === 'soft') { + $bp->useSoftStates(); + } + if ($stateType === 'hard') { + $bp->useHardStates(); + } + } + + $bp->retrieveStatesFromBackend(); + + $node = $bp->getNode($this->params->shift()); + $bp->retrieveStatesFromBackend(); printf("Business Process %s: %s\n", $node->getStateName(), $node->getAlias()); exit($node->getState()); - } - // TODO: Remove this - protected function prepareBackend() + protected function getFirstProcessName() { - if ($this->backend === null) { - $name = $this->config->{'global'}->get('default_backend'); - if (isset($this->bpconf->backend)) { - $name = $this->bpconf->backend; - } - $this->backend = Backend::createBackend($name); - } - return $this->backend; - } - - // TODO: Remove this - protected function readConfig() - { - $this->views = array(); - $this->aliases = array(); - foreach ($this->config as $key => $val) { - if (! preg_match('~^view-(.+)$~', $key, $match)) continue; - $this->views[$match[1]] = (object) $val->toArray(); - $this->aliases[(string) $val->title] = $match[1]; - if ($val->aliases) { - foreach (preg_split('~\s*,\s*~', $val->aliases, -1, PREG_SPLIT_NO_EMPTY) as $alias) { - $this->aliases[$alias] = $match[1]; - } - } - } - $bpname = $this->params->get('bp', key($this->views)); - - if (array_key_exists($bpname, $this->aliases)) { - $bpname = $this->aliases[$bpname]; - } - if (! array_key_exists($bpname, $this->views)) { - throw new Exception('Got invalid bp name: ' . $bpname); - } - $this->bpconf = $this->views[$bpname]; - $this->bpname = $bpname; - - $this->filename = $this->config->global->bp_config_dir - . '/' . $this->bpconf->file . '.conf'; + $list = $this->storage->listProcesses(); + return key($list); } }