From efcea15ab619caafbf94600f79a3d1e3d09ea5a7 Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Thu, 30 Apr 2020 15:55:49 +0200 Subject: [PATCH] Clean the scripts and resolve the comments Clean the scripts to pass the phpcodesniffer tests and resolve the comments provided by Eric. --- application/controllers/HostController.php | 46 +++--- application/controllers/NodeController.php | 3 +- application/controllers/ProcessController.php | 5 +- application/controllers/ServiceController.php | 53 +++---- application/forms/AddNodeForm.php | 5 +- application/forms/DeleteNodeForm.php | 5 +- application/forms/EditNodeForm.php | 6 +- library/Businessprocess/BpConfig.php | 5 +- library/Businessprocess/Common/EnumList.php | 2 +- .../Common/IcingadbDatabase.php | 1 - library/Businessprocess/HostNode.php | 7 +- library/Businessprocess/IcingaDbBackend.php | 30 ++-- .../MonitoringRestrictions.php | 4 +- library/Businessprocess/ServiceNode.php | 7 +- .../Businessprocess/State/IcingaDbState.php | 134 +++++++----------- .../Web/Component/Dashboard.php | 3 +- 16 files changed, 130 insertions(+), 186 deletions(-) diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 6672cfc..176a317 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -3,55 +3,45 @@ namespace Icinga\Module\Businessprocess\Controllers; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; -use Icinga\Module\Businessprocess\Web\Controller; +//use Icinga\Module\Businessprocess\Web\Controller; +use Icinga\Module\Businessprocess\IcingaDbBackend; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Monitoring\Backend; +use Icinga\Module\Monitoring\Controller; use Icinga\Web\Url; class HostController extends Controller { use IcingadbDatabase; - protected $backend; - - protected $allParams; - - public function showAction() { - $this->allParams = $this->getAllParams(); + $hostName = $this->params->get('host'); + $icingadb = $this->params->get('icingadb'); - $host = $this->params->getRequired('host'); + if ($icingadb) { + $host = Host::on($this->getDb()); + $host->getSelectBase() + ->where(['host.name = ?' => $hostName]); + IcingaDbBackend::applyMonitoringRestriction($host); - if (array_key_exists('backend', $this->allParams)) { - if ($this->allParams['backend'] === '_icingadb') { - $this->backend = $this->getDb(); + $rs = $host->columns('host.name')->first(); + + $this->params->add('name', $hostName); + + if ($rs !== false) { + $this->redirectNow(Url::fromPath('icingadb/host')->setParams($this->params)); } - $query = Host::on($this->backend); - $query->getSelectBase() - ->where(['host.name = ?' => $host]); - $this->applyMonitoringRestriction($query); - - $queryHost = $query->columns('host.name')->assembleSelect(); - $queryHost = $this->backend->select($queryHost)->fetch(); - - $this->params->add('name', $host); - - if ($queryHost !== false) { - $this->redirectNow(Url::fromPath('icingadb/host/index')->setParams($this->params)); - } - } else { - $this->backend = Backend::createBackend($this->_getParam('backend')); $query = $this->backend->select() ->from('hoststatus', array('host_name')) - ->where('host_name', $host); + ->where('host_name', $hostName); if ($this->applyRestriction('monitoring/filter/objects', $query)->fetchRow() !== false) { $this->redirectNow(Url::fromPath('monitoring/host/show')->setParams($this->params)); } } - $this->view->host = $host; + $this->view->host = $hostName; } } diff --git a/application/controllers/NodeController.php b/application/controllers/NodeController.php index ac7c1f1..4a4876a 100644 --- a/application/controllers/NodeController.php +++ b/application/controllers/NodeController.php @@ -84,8 +84,7 @@ class NodeController extends Controller } if ($config->getBackendName() === '_icingadb') { IcingaDbState::apply($config); - } - else { + } else { MonitoringState::apply($config); } $config->applySimulation($simulation); diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index 719266a..2b3eea7 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -84,8 +84,7 @@ class ProcessController extends Controller if ($bp->getBackendName() === '_icingadb') { IcingaDbState::apply($bp); - } - else { + } else { MonitoringState::apply($bp); } @@ -93,7 +92,6 @@ class ProcessController extends Controller $this->setTitle($this->translate('Business Process "%s"'), $bp->getTitle()); - $renderer = $this->prepareRenderer($bp, $node); if (! $this->showFullscreen && ($node === null || ! $renderer->rendersImportedNode())) { @@ -177,6 +175,7 @@ class ProcessController extends Controller } $renderer->setUrl($this->url()) ->setPath($this->params->getValues('path')); + $this->renderer = $renderer; } diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 3838c04..2313291 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -3,58 +3,49 @@ namespace Icinga\Module\Businessprocess\Controllers; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; -use Icinga\Module\Businessprocess\Web\Controller; +use Icinga\Module\Businessprocess\IcingaDbBackend; use Icinga\Module\Icingadb\Model\Service; use Icinga\Module\Monitoring\Backend; +use Icinga\Module\Monitoring\Controller; use Icinga\Web\Url; class ServiceController extends Controller { use IcingadbDatabase; - protected $backend; - - protected $allParams; - public function showAction() { - $this->allParams = $this->getAllParams(); + $hostName = $this->params->get('host'); + $serviceName = $this->params->get('service'); + $icingadb = $this->params->get('icingadb'); - $host = $this->params->getRequired('host'); - $service = $this->params->getRequired('service'); + if ($icingadb) { + $service = Service::on($this->getDb())->with('host'); + $service->getSelectBase() + ->where(['service_host.name = ?' => $hostName, 'service.name = ?' => $serviceName]); - if (array_key_exists('backend', $this->allParams)) { - if ($this->allParams['backend'] === '_icingadb') { - $this->backend = $this->getDb(); + IcingaDbBackend::applyMonitoringRestriction($service); + + $rs = $service->columns('host.name')->first(); + + $this->params->add('name', $serviceName); + $this->params->add('host.name', $hostName); + + if ($rs !== false) { + $this->redirectNow(Url::fromPath('icingadb/service')->setParams($this->params)); } - $query = Service::on($this->backend)->with('host'); - $query->getSelectBase() - ->where(['service_host.name = ?' => $host, 'service.name = ?' => $service]); - $this->applyMonitoringRestriction($query); - - $query = $query->columns('host.name')->assembleSelect(); - $query = $this->backend->select($query)->fetch(); - - $this->params->add('name', $service); - $this->params->add('host.name', $host); - - if ($query !== false) { - $this->redirectNow(Url::fromPath('icingadb/service/index')->setParams($this->params)); - } - } else { - $this->backend = Backend::createBackend($this->_getParam('backend')); $query = $this->backend->select() ->from('servicestatus', array('service_description')) - ->where('host_name', $host) - ->where('service_description', $service); + ->where('host_name', $hostName) + ->where('service_description', $serviceName); if ($this->applyRestriction('monitoring/filter/objects', $query)->fetchRow() !== false) { $this->redirectNow(Url::fromPath('monitoring/service/show')->setParams($this->params)); } } - $this->view->host = $host; - $this->view->service = $service; + $this->view->host = $hostName; + $this->view->service = $serviceName; } } diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index d86f228..94786c0 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -9,7 +9,6 @@ use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\Common\EnumList; use Icinga\Module\Businessprocess\ImportedNode; use Icinga\Module\Businessprocess\Modification\ProcessChanges; -use Icinga\Module\Businessprocess\MonitoringRestrictions; use Icinga\Module\Businessprocess\Storage\Storage; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator; @@ -18,11 +17,9 @@ use Icinga\Web\Session\SessionNamespace; class AddNodeForm extends QuickForm { - use MonitoringRestrictions; - use EnumList; - /** @var MonitoringBackend */ + /** @var MonitoringBackend|IcingadbDatabase */ protected $backend; /** @var Storage */ diff --git a/application/forms/DeleteNodeForm.php b/application/forms/DeleteNodeForm.php index a36731f..3d41bf4 100644 --- a/application/forms/DeleteNodeForm.php +++ b/application/forms/DeleteNodeForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Businessprocess\Forms; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BpConfig; +use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Node; use Icinga\Module\Businessprocess\Web\Form\QuickForm; @@ -12,7 +13,7 @@ use Icinga\Web\Session\SessionNamespace; class DeleteNodeForm extends QuickForm { - /** @var MonitoringBackend */ + /** @var MonitoringBackend|IcingadbDatabase */ protected $backend; /** @var BpConfig */ @@ -79,7 +80,7 @@ class DeleteNodeForm extends QuickForm } /** - * @param MonitoringBackend $backend + * @param MonitoringBackend|IcingadbDatabase $backend * @return $this */ public function setBackend($backend) diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index cf57c66..0e14d70 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -7,7 +7,6 @@ use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\Common\EnumList; use Icinga\Module\Businessprocess\Modification\ProcessChanges; -use Icinga\Module\Businessprocess\MonitoringRestrictions; use Icinga\Module\Businessprocess\Node; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator; @@ -16,13 +15,12 @@ use Icinga\Web\Session\SessionNamespace; class EditNodeForm extends QuickForm { - use MonitoringRestrictions; - use EnumList; - /** @var MonitoringBackend */ + /** @var MonitoringBackend|IcingadbDatabase */ protected $backend; + /** @var string $backendName */ protected $backendName; /** @var BpConfig */ diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index 98f3622..80488fb 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -29,6 +29,8 @@ class BpConfig /** * Backend to retrieve states from + * + * @var MonitoringBackend|IcingadbDatabase */ protected $backend; @@ -293,8 +295,7 @@ class BpConfig if ($this->backend === null) { if ($this->getBackendName() === '_icingadb') { $this->backend = $this->getDb(); - } - else { + } else { $this->backend = MonitoringBackend::instance( $this->getBackendName() ); diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index 4717223..3520793 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -82,4 +82,4 @@ trait EnumList { return $this->backendName === '_icingadb'; } -} \ No newline at end of file +} diff --git a/library/Businessprocess/Common/IcingadbDatabase.php b/library/Businessprocess/Common/IcingadbDatabase.php index fe5d93a..d35d2c8 100644 --- a/library/Businessprocess/Common/IcingadbDatabase.php +++ b/library/Businessprocess/Common/IcingadbDatabase.php @@ -23,7 +23,6 @@ trait IcingadbDatabase */ public function getDb() { - if ($this->db === null) { $config = new SqlConfig(ResourceFactory::getResourceConfig( AppConfig::module('icingadb')->get('icingadb', 'resource', 'icingadb') diff --git a/library/Businessprocess/HostNode.php b/library/Businessprocess/HostNode.php index 8229e95..cc5315e 100644 --- a/library/Businessprocess/HostNode.php +++ b/library/Businessprocess/HostNode.php @@ -57,7 +57,12 @@ class HostNode extends MonitoredNode ); if ($this->getBpConfig()->hasBackendName()) { - $params['backend'] = $this->getBpConfig()->getBackendName(); + $backendName = $this->getBpConfig()->getBackendName(); + if ($backendName === '_icingadb') { + $params['icingadb'] = 1; + } else { + $params['backend'] = $this->getBpConfig()->getBackendName(); + } } return Url::fromPath('businessprocess/host/show', $params); diff --git a/library/Businessprocess/IcingaDbBackend.php b/library/Businessprocess/IcingaDbBackend.php index 11f40cc..625127f 100644 --- a/library/Businessprocess/IcingaDbBackend.php +++ b/library/Businessprocess/IcingaDbBackend.php @@ -5,28 +5,28 @@ namespace Icinga\Module\Businessprocess; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; +use Icinga\Module\Monitoring\Backend\MonitoringBackend; use ipl\Orm\Compat\FilterProcessor; use ipl\Orm\Query; class IcingaDbBackend { - use MonitoringRestrictions; - use IcingadbDatabase; /** @var BpConfig */ protected $config; - protected $backend; + /** @var IcingadbDatabase */ + protected $conn; + public function __construct() { - $this->backend = $this->getDb(); + $this->conn = $this->getDb(); } public function fetchHosts() { - - $hosts = Host::on($this->getDb()) + $hosts = Host::on($this->conn) ->orderBy('host.name'); $this->applyMonitoringRestriction($hosts); @@ -36,18 +36,14 @@ class IcingaDbBackend public function fetchServices($host) { - $query = Service::on($this->backend) + $services = Service::on($this->conn) ->with('host'); - $query->getSelectBase() + $services->getSelectBase() ->where(['service_host.name = ?' => $host]) ->orderBy('service.name'); - $this->applyMonitoringRestriction($query); - - $queryServices = $query->assembleSelect(); - $services = $this->backend->select($queryServices)->fetchAll(); - var_dump($services); + $this->applyMonitoringRestriction($services); return $services; } @@ -66,13 +62,13 @@ class IcingaDbBackend } } - protected function applyMonitoringRestriction(Query $query) + public static function applyMonitoringRestriction(Query $query) { - FilterProcessor::apply( - $this->getRestriction('monitoring/filter/objects'), + $restriction = FilterProcessor::apply( + MonitoringRestrictions::getRestriction('monitoring/filter/objects'), $query ); - return $this; + return $restriction; } } diff --git a/library/Businessprocess/MonitoringRestrictions.php b/library/Businessprocess/MonitoringRestrictions.php index 4dd4caa..c7d2cef 100644 --- a/library/Businessprocess/MonitoringRestrictions.php +++ b/library/Businessprocess/MonitoringRestrictions.php @@ -7,7 +7,7 @@ use Icinga\Data\Filter\Filter; use Icinga\Exception\ConfigurationError; use Icinga\Exception\QueryException; -trait MonitoringRestrictions +class MonitoringRestrictions { /** * Return a filter for the given restriction @@ -17,7 +17,7 @@ trait MonitoringRestrictions * @return Filter|null Filter object or null if the authenticated user is not restricted * @throws ConfigurationError If the restriction contains invalid filter columns */ - protected function getRestriction($name) + public static function getRestriction($name) { // Borrowed from Icinga\Module\Monitoring\Controller $restriction = Filter::matchAny(); diff --git a/library/Businessprocess/ServiceNode.php b/library/Businessprocess/ServiceNode.php index 6160bce..ba9234a 100644 --- a/library/Businessprocess/ServiceNode.php +++ b/library/Businessprocess/ServiceNode.php @@ -76,7 +76,12 @@ class ServiceNode extends MonitoredNode ); if ($this->getBpConfig()->hasBackendName()) { - $params['backend'] = $this->getBpConfig()->getBackendName(); + $backendName = $this->getBpConfig()->getBackendName(); + if ($backendName === '_icingadb') { + $params['icingadb'] = 1; + } else { + $params['backend'] = $this->getBpConfig()->getBackendName(); + } } return Url::fromPath('businessprocess/service/show', $params); diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 25229bf..af6929d 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -11,10 +11,6 @@ use Icinga\Module\Businessprocess\ServiceNode; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; -ini_set("xdebug.var_display_max_children", -1); -ini_set("xdebug.var_display_max_data", -1); -ini_set("xdebug.var_display_max_depth", -1); - class IcingaDbState extends IcingaDbBackend { /** @var BpConfig */ @@ -54,57 +50,62 @@ class IcingaDbState extends IcingaDbBackend { $config = $this->config; - Benchmark::measure('Retrieving states for business process ' . $config->getName()); - $backend = $this->backend; + Benchmark::measure('Retrieving states for business process ' . $config->getName());; $hosts = $config->listInvolvedHostNames(); if (empty($hosts)) { return $this; } - $queryHost = Host::on($backend)->with('state'); + $queryHost = Host::on($this->backend)->with('state'); + $queryHost->getSelectBase() ->where(['host.name IN (?)' => $hosts]); - $columns = $queryHost->assembleSelect()->getColumns(); - $resetHostCols = []; - foreach ($columns as $column) - { - $tmpKey = str_replace('.','_',$column); - $resetHostCols[] = $tmpKey; - } +// $resetHostCols = []; +// foreach ($columns as $column) { +// $tmpKey = str_replace('.', '_', $column); +// $resetHostCols[] = $tmpKey; +// } $this->applyMonitoringRestriction($queryHost); // /** @var Host $host */ - $hostList = $queryHost->assembleSelect(); - $hostList = $backend->select($hostList)->fetchAll(); - - foreach ($hostList as $idx => $hst) - { - $hst = get_object_vars($hst); - $hostColVals = array_values($hst); - $hst = array_combine($resetHostCols, $hostColVals); - $hostList[$idx] = $hst; - if($hst['host_state_state_type'] === 'hard') { - $hostStateCol = 'host_state_hard_state'; - } else { - $hostStateCol = 'host_state_soft_state'; - } +// $hostList = $queryHost->assembleSelect(); +// $hostList = $backend->select($hostList)->fetchAll(); +// +// foreach ($hostList as $idx => $hst) { +// $hst = get_object_vars($hst); +// $hostColVals = array_values($hst); +// $hst = array_combine($resetHostCols, $hostColVals); +// $hostList[$idx] = $hst; +// if ($hst['host_state_state_type'] === 'hard') { +// $hostStateCol = 'host_state_hard_state'; +// } else { +// $hostStateCol = 'host_state_soft_state'; +// } +// } + if ($this->config->usesHardStates()) { + $stateCol = 'state.hard_state'; + } else { + $stateCol = 'state.soft_state'; } $hostStatusCols = array( - 'hostname' => 'host_name', - 'last_state_change' => 'host_state_last_state_change', - 'in_downtime' => 'host_state_in_downtime', - 'ack' => 'host_state_is_acknowledged', - 'state' => $hostStateCol, - 'display_name' =>'host_display_name' + 'hostname' => 'host.name', + 'last_state_change' => 'state.last_state_change', + 'in_downtime' => 'state.in_downtime', + 'ack' => 'state.is_acknowledged', + 'state' => $stateCol, + 'display_name' =>'host.display_name' ); - $hostStatus = $this->selectArrayCols($hostList,$hostStatusCols); + + $queryHost = $queryHost->columns($hostStatusCols)->assembleSelect(); + + $hostStatus = $this->backend->select($queryHost)->fetchAll(); Benchmark::measure('Retrieved states for ' . count($hostStatus) . ' hosts in ' . $config->getName()); - $queryService = Service::on($backend)->with([ + $queryService = Service::on($this->backend)->with([ 'state', 'host', 'host.state' @@ -112,43 +113,22 @@ class IcingaDbState extends IcingaDbBackend $queryService->getSelectBase() ->where(['service_host.name IN (?)' => $hosts]); - $columns = $queryService->assembleSelect()->getColumns(); - $resetServiceCols = []; - foreach ($columns as $column) - { - $tmpKey = str_replace('.','_',$column); - $resetServiceCols[] = $tmpKey; - } $this->applyMonitoringRestriction($queryService); - $serviceList = $queryService->assembleSelect(); - - $serviceList = $backend->select($serviceList)->fetchAll(); - - foreach ($serviceList as $idx => $srvc) - { - $srvc = get_object_vars($srvc); - $serviceColVals = array_values($srvc); - $srvc = array_combine($resetServiceCols, $serviceColVals); - $serviceList[$idx] = $srvc; - if($srvc['service_state_state_type'] === 'hard') { - $serviceStateCol = 'service_state_hard_state'; - } else { - $serviceStateCol = 'service_state_soft_state'; - } - } - $serviceStatusCols = array( - 'hostname' => 'service_host_name', - 'service' => 'service_name', - 'last_state_change' => 'service_state_last_state_change', - 'in_downtime' => 'service_state_in_downtime', - 'ack' => 'service_host_state_is_acknowledged', - 'state' => $serviceStateCol, - 'display_name' => 'service_display_name', - 'host_display_name' => 'service_host_display_name' + 'hostname' => 'host.name', + 'service' => 'service.name', + 'last_state_change' => 'state.last_state_change', + 'in_downtime' => 'state.in_downtime', + 'ack' => 'host.state.is_acknowledged', + 'state' => $stateCol, + 'display_name' => 'service.display_name', + 'host_display_name' => 'host.display_name' ); - $serviceStatus = $this->selectArrayCols($serviceList,$serviceStatusCols); + + $queryService = $queryService->columns($serviceStatusCols)->assembleSelect(); + + $serviceStatus = $this->backend->select($queryService)->fetchAll(); Benchmark::measure('Retrieved states for ' . count($serviceStatus) . ' services in ' . $config->getName()); @@ -171,22 +151,6 @@ class IcingaDbState extends IcingaDbBackend return $this; } - protected function selectArrayCols ($array, $cols) - { - $selectArrayCols = []; - foreach ($array as $idx => $subArray) - { - $tmpArray = []; - foreach ($cols as $colKey => $colVal) - { - $tmpArray[$colKey] = $subArray[$colVal]; - } - $selectArrayCols[$idx] = (object) $tmpArray; - } - - return $selectArrayCols; - } - protected function handleDbRow($row, BpConfig $config) { $key = $row->hostname; diff --git a/library/Businessprocess/Web/Component/Dashboard.php b/library/Businessprocess/Web/Component/Dashboard.php index 1b66d34..c57b48a 100644 --- a/library/Businessprocess/Web/Component/Dashboard.php +++ b/library/Businessprocess/Web/Component/Dashboard.php @@ -94,8 +94,7 @@ class Dashboard extends BaseHtmlElement $bp = $storage->loadProcess($name); if ($bp->getBackendName() === '_icingadb') { IcingaDbState::apply($bp); - } - else { + } else { MonitoringState::apply($bp); }