From 1a0ddfb08b62ec0d7435d4d0ede88cba2c01c6a1 Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Tue, 28 Apr 2020 01:08:24 +0200 Subject: [PATCH 01/40] Add IcingaDB MySQL backend in businessprocess IcingaDB MySQL backend is added to the businessprocess module to obtain information regarding monitored nodes. ref #276 --- application/controllers/HostController.php | 45 +++- application/controllers/NodeController.php | 9 +- application/controllers/ProcessController.php | 11 +- application/controllers/ServiceController.php | 47 +++- application/forms/AddNodeForm.php | 12 +- application/forms/DeleteNodeForm.php | 2 +- application/forms/EditNodeForm.php | 11 +- application/forms/ProcessForm.php | 8 +- library/Businessprocess/BpConfig.php | 20 +- library/Businessprocess/Common/EnumList.php | 85 +++++++ .../Common/IcingadbDatabase.php | 43 ++++ library/Businessprocess/IcingaDbBackend.php | 78 ++++++ .../Businessprocess/State/IcingaDbState.php | 225 ++++++++++++++++++ .../Web/Component/Dashboard.php | 8 +- library/Businessprocess/Web/Controller.php | 91 +++++++ .../Web/Form/BpConfigBaseForm.php | 4 +- 16 files changed, 665 insertions(+), 34 deletions(-) create mode 100644 library/Businessprocess/Common/EnumList.php create mode 100644 library/Businessprocess/Common/IcingadbDatabase.php create mode 100644 library/Businessprocess/IcingaDbBackend.php create mode 100644 library/Businessprocess/State/IcingaDbState.php diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 134ab5b..6672cfc 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -2,21 +2,54 @@ namespace Icinga\Module\Businessprocess\Controllers; -use Icinga\Module\Monitoring\Controller; +use Icinga\Module\Businessprocess\Common\IcingadbDatabase; +use Icinga\Module\Businessprocess\Web\Controller; +use Icinga\Module\Icingadb\Model\Host; +use Icinga\Module\Monitoring\Backend; use Icinga\Web\Url; class HostController extends Controller { + use IcingadbDatabase; + + protected $backend; + + protected $allParams; + + public function showAction() { + $this->allParams = $this->getAllParams(); + $host = $this->params->getRequired('host'); - $query = $this->backend->select() - ->from('hoststatus', array('host_name')) - ->where('host_name', $host); + if (array_key_exists('backend', $this->allParams)) { + if ($this->allParams['backend'] === '_icingadb') { + $this->backend = $this->getDb(); + } + $query = Host::on($this->backend); + $query->getSelectBase() + ->where(['host.name = ?' => $host]); + $this->applyMonitoringRestriction($query); - if ($this->applyRestriction('monitoring/filter/objects', $query)->fetchRow() !== false) { - $this->redirectNow(Url::fromPath('monitoring/host/show')->setParams($this->params)); + $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); + + if ($this->applyRestriction('monitoring/filter/objects', $query)->fetchRow() !== false) { + $this->redirectNow(Url::fromPath('monitoring/host/show')->setParams($this->params)); + } } $this->view->host = $host; diff --git a/application/controllers/NodeController.php b/application/controllers/NodeController.php index dc6c5fc..ac7c1f1 100644 --- a/application/controllers/NodeController.php +++ b/application/controllers/NodeController.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Businessprocess\Controllers; use Icinga\Module\Businessprocess\Renderer\Breadcrumb; use Icinga\Module\Businessprocess\Renderer\TileRenderer; use Icinga\Module\Businessprocess\Simulation; +use Icinga\Module\Businessprocess\State\IcingaDbState; use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Web\Controller; use Icinga\Module\Businessprocess\Web\Url; @@ -81,8 +82,12 @@ class NodeController extends Controller if (empty($parents)) { continue; } - - MonitoringState::apply($config); + if ($config->getBackendName() === '_icingadb') { + IcingaDbState::apply($config); + } + else { + MonitoringState::apply($config); + } $config->applySimulation($simulation); foreach ($parents as $parentAndPath) { diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index b4fb3ef..719266a 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -11,6 +11,7 @@ use Icinga\Module\Businessprocess\Renderer\Renderer; use Icinga\Module\Businessprocess\Renderer\TileRenderer; use Icinga\Module\Businessprocess\Renderer\TreeRenderer; use Icinga\Module\Businessprocess\Simulation; +use Icinga\Module\Businessprocess\State\IcingaDbState; use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\ConfigDiff; use Icinga\Module\Businessprocess\Storage\LegacyConfigRenderer; @@ -81,11 +82,18 @@ class ProcessController extends Controller $bp = $this->loadModifiedBpConfig(); $node = $this->getNode($bp); - MonitoringState::apply($bp); + if ($bp->getBackendName() === '_icingadb') { + IcingaDbState::apply($bp); + } + else { + MonitoringState::apply($bp); + } + $this->handleSimulations($bp); $this->setTitle($this->translate('Business Process "%s"'), $bp->getTitle()); + $renderer = $this->prepareRenderer($bp, $node); if (! $this->showFullscreen && ($node === null || ! $renderer->rendersImportedNode())) { @@ -169,7 +177,6 @@ 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 17bb474..3838c04 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -2,23 +2,56 @@ namespace Icinga\Module\Businessprocess\Controllers; -use Icinga\Module\Monitoring\Controller; +use Icinga\Module\Businessprocess\Common\IcingadbDatabase; +use Icinga\Module\Businessprocess\Web\Controller; +use Icinga\Module\Icingadb\Model\Service; +use Icinga\Module\Monitoring\Backend; use Icinga\Web\Url; class ServiceController extends Controller { + use IcingadbDatabase; + + protected $backend; + + protected $allParams; + public function showAction() { + $this->allParams = $this->getAllParams(); + $host = $this->params->getRequired('host'); $service = $this->params->getRequired('service'); - $query = $this->backend->select() - ->from('servicestatus', array('service_description')) - ->where('host_name', $host) - ->where('service_description', $service); + if (array_key_exists('backend', $this->allParams)) { + if ($this->allParams['backend'] === '_icingadb') { + $this->backend = $this->getDb(); + } + $query = Service::on($this->backend)->with('host'); + $query->getSelectBase() + ->where(['service_host.name = ?' => $host, 'service.name = ?' => $service]); + $this->applyMonitoringRestriction($query); - if ($this->applyRestriction('monitoring/filter/objects', $query)->fetchRow() !== false) { - $this->redirectNow(Url::fromPath('monitoring/service/show')->setParams($this->params)); + $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); + + if ($this->applyRestriction('monitoring/filter/objects', $query)->fetchRow() !== false) { + $this->redirectNow(Url::fromPath('monitoring/service/show')->setParams($this->params)); + } } $this->view->host = $host; diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index 1d87305..d86f228 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -5,6 +5,8 @@ namespace Icinga\Module\Businessprocess\Forms; use Icinga\Data\Filter\Filter; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BpConfig; +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; @@ -18,6 +20,8 @@ class AddNodeForm extends QuickForm { use MonitoringRestrictions; + use EnumList; + /** @var MonitoringBackend */ protected $backend; @@ -37,6 +41,9 @@ class AddNodeForm extends QuickForm /** @var SessionNamespace */ protected $session; + /** @var string $backendName */ + protected $backendName; + public function setup() { $view = $this->getView(); @@ -401,10 +408,10 @@ class AddNodeForm extends QuickForm } /** - * @param MonitoringBackend $backend + * @param MonitoringBackend|IcingadbDatabase $backend * @return $this */ - public function setBackend(MonitoringBackend $backend) + public function setBackend($backend) { $this->backend = $backend; return $this; @@ -427,6 +434,7 @@ class AddNodeForm extends QuickForm public function setProcess(BpConfig $process) { $this->bp = $process; + $this->backendName = $process->getBackendName(); $this->setBackend($process->getBackend()); return $this; } diff --git a/application/forms/DeleteNodeForm.php b/application/forms/DeleteNodeForm.php index d06f8df..a36731f 100644 --- a/application/forms/DeleteNodeForm.php +++ b/application/forms/DeleteNodeForm.php @@ -82,7 +82,7 @@ class DeleteNodeForm extends QuickForm * @param MonitoringBackend $backend * @return $this */ - public function setBackend(MonitoringBackend $backend) + public function setBackend($backend) { $this->backend = $backend; return $this; diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index 588c6de..cf57c66 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -4,6 +4,8 @@ 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\Common\EnumList; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\MonitoringRestrictions; use Icinga\Module\Businessprocess\Node; @@ -16,9 +18,13 @@ class EditNodeForm extends QuickForm { use MonitoringRestrictions; + use EnumList; + /** @var MonitoringBackend */ protected $backend; + protected $backendName; + /** @var BpConfig */ protected $bp; @@ -295,10 +301,10 @@ class EditNodeForm extends QuickForm } /** - * @param MonitoringBackend $backend + * @param MonitoringBackend|IcingadbDatabase $backend * @return $this */ - public function setBackend(MonitoringBackend $backend) + public function setBackend($backend) { $this->backend = $backend; return $this; @@ -311,6 +317,7 @@ class EditNodeForm extends QuickForm public function setProcess(BpConfig $process) { $this->bp = $process; + $this->backendName = $process->getBackendName(); $this->setBackend($process->getBackend()); return $this; } diff --git a/application/forms/ProcessForm.php b/application/forms/ProcessForm.php index 4cc3bcf..bd894e8 100644 --- a/application/forms/ProcessForm.php +++ b/application/forms/ProcessForm.php @@ -4,6 +4,8 @@ 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\IcingaDbBackend; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Monitoring\Backend\MonitoringBackend; @@ -12,7 +14,7 @@ use Icinga\Web\Session\SessionNamespace; class ProcessForm extends QuickForm { - /** @var MonitoringBackend */ + /** @var MonitoringBackend|IcingadbDatabase */ protected $backend; /** @var BpConfig */ @@ -110,10 +112,10 @@ class ProcessForm extends QuickForm } /** - * @param MonitoringBackend $backend + * @param MonitoringBackend|IcingadbDatabase $backend * @return $this */ - public function setBackend(MonitoringBackend $backend) + public function setBackend($backend) { $this->backend = $backend; return $this; diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index c49d262..98f3622 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -6,6 +6,7 @@ use Exception; use Icinga\Application\Config; use Icinga\Exception\IcingaException; use Icinga\Exception\NotFoundError; +use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\Exception\NestingError; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Storage\LegacyStorage; @@ -13,6 +14,8 @@ use Icinga\Module\Monitoring\Backend\MonitoringBackend; class BpConfig { + use IcingadbDatabase; + const SOFT_STATE = 0; const HARD_STATE = 1; @@ -25,9 +28,7 @@ class BpConfig protected $backendName; /** - * Monitoring backend to retrieve states from - * - * @var MonitoringBackend + * Backend to retrieve states from */ protected $backend; @@ -281,7 +282,7 @@ class BpConfig return $this->getMetadata()->has('Backend'); } - public function setBackend(MonitoringBackend $backend) + public function setBackend($backend) { $this->backend = $backend; return $this; @@ -290,9 +291,14 @@ class BpConfig public function getBackend() { if ($this->backend === null) { - $this->backend = MonitoringBackend::instance( - $this->getBackendName() - ); + if ($this->getBackendName() === '_icingadb') { + $this->backend = $this->getDb(); + } + else { + $this->backend = MonitoringBackend::instance( + $this->getBackendName() + ); + } } return $this->backend; diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php new file mode 100644 index 0000000..4717223 --- /dev/null +++ b/library/Businessprocess/Common/EnumList.php @@ -0,0 +1,85 @@ +useIcingaDbBackend()) { + $names = (new IcingaDbBackend())->yieldHostnames(); + } else { + $names = $this->backend + ->select() + ->from('hostStatus', ['hostname' => 'host_name']) + ->applyFilter($this->getRestriction('monitoring/filter/objects')) + ->order('host_name') + ->getQuery() + ->fetchColumn(); + } + + // fetchPairs doesn't seem to work when using the same column with + // different aliases twice + $res = array(); + foreach ($names as $name) { + $res[$name] = $name; + } + + return $res; + } + + protected function enumHostList() + { + if ($this->useIcingaDbBackend()) { + $names = (new IcingaDbBackend())->yieldHostnames(); + } else { + $names = $this->backend + ->select() + ->from('hostStatus', ['hostname' => 'host_name']) + ->applyFilter($this->getRestriction('monitoring/filter/objects')) + ->order('host_name') + ->getQuery() + ->fetchColumn(); + } + + // fetchPairs doesn't seem to work when using the same column with + // different aliases twice + $res = array(); + $suffix = ';Hoststatus'; + foreach ($names as $name) { + $res[$name . $suffix] = $name; + } + + return $res; + } + + protected function enumServiceList($host) + { + if ($this->useIcingaDbBackend()) { + $names = (new IcingaDbBackend())->yieldServicenames($host); + } else { + $names = $this->backend + ->select() + ->from('serviceStatus', ['service' => 'service_description']) + ->where('host_name', $host) + ->applyFilter($this->getRestriction('monitoring/filter/objects')) + ->order('service_description') + ->getQuery() + ->fetchColumn(); + } + + $services = array(); + foreach ($names as $name) { + $services[$host . ';' . $name] = $name; + } + + return $services; + } + + protected function useIcingaDbBackend() + { + return $this->backendName === '_icingadb'; + } +} \ No newline at end of file diff --git a/library/Businessprocess/Common/IcingadbDatabase.php b/library/Businessprocess/Common/IcingadbDatabase.php new file mode 100644 index 0000000..fe5d93a --- /dev/null +++ b/library/Businessprocess/Common/IcingadbDatabase.php @@ -0,0 +1,43 @@ +db === null) { + $config = new SqlConfig(ResourceFactory::getResourceConfig( + AppConfig::module('icingadb')->get('icingadb', 'resource', 'icingadb') + )); + + $config->options = [ + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, + PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION SQL_MODE='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE" + . ",ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'" + ]; + + $this->db = new Connection($config); + } + + return $this->db; + } +} diff --git a/library/Businessprocess/IcingaDbBackend.php b/library/Businessprocess/IcingaDbBackend.php new file mode 100644 index 0000000..11f40cc --- /dev/null +++ b/library/Businessprocess/IcingaDbBackend.php @@ -0,0 +1,78 @@ +backend = $this->getDb(); + } + + public function fetchHosts() + { + + $hosts = Host::on($this->getDb()) + ->orderBy('host.name'); + + $this->applyMonitoringRestriction($hosts); + + return $hosts; + } + + public function fetchServices($host) + { + $query = Service::on($this->backend) + ->with('host'); + + $query->getSelectBase() + ->where(['service_host.name = ?' => $host]) + ->orderBy('service.name'); + + $this->applyMonitoringRestriction($query); + + $queryServices = $query->assembleSelect(); + $services = $this->backend->select($queryServices)->fetchAll(); + var_dump($services); + + return $services; + } + + public function yieldHostnames() + { + foreach ($this->fetchHosts() as $host) { + yield $host->name; + } + } + + public function yieldServicenames($host) + { + foreach ($this->fetchServices($host) as $service) { + yield $service->name; + } + } + + protected function applyMonitoringRestriction(Query $query) + { + FilterProcessor::apply( + $this->getRestriction('monitoring/filter/objects'), + $query + ); + + return $this; + } +} diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php new file mode 100644 index 0000000..25229bf --- /dev/null +++ b/library/Businessprocess/State/IcingaDbState.php @@ -0,0 +1,225 @@ +config = $config; + $this->backend = $config->getBackend(); + } + + public static function apply(BpConfig $config) + { + $self = new static($config); + $self->retrieveStatesFromBackend(); + return $config; + } + + public function retrieveStatesFromBackend() + { + $config = $this->config; + + try { + $this->reallyRetrieveStatesFromBackend(); + } catch (Exception $e) { + $config->addError( + $config->translate('Could not retrieve process state: %s'), + $e->getMessage() + ); + } + } + + public function reallyRetrieveStatesFromBackend() + { + $config = $this->config; + + Benchmark::measure('Retrieving states for business process ' . $config->getName()); + $backend = $this->backend; + + $hosts = $config->listInvolvedHostNames(); + if (empty($hosts)) { + return $this; + } + + $queryHost = Host::on($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; + } + $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'; + } + } + + $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' + ); + $hostStatus = $this->selectArrayCols($hostList,$hostStatusCols); + + Benchmark::measure('Retrieved states for ' . count($hostStatus) . ' hosts in ' . $config->getName()); + + $queryService = Service::on($backend)->with([ + 'state', + 'host', + 'host.state' + ]); + $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' + ); + $serviceStatus = $this->selectArrayCols($serviceList,$serviceStatusCols); + + Benchmark::measure('Retrieved states for ' . count($serviceStatus) . ' services in ' . $config->getName()); + + $configs = $config->listInvolvedConfigs(); + $hostStatus = (object) $hostStatus; + $serviceStatus = (object) $serviceStatus; + + foreach ($configs as $cfg) { + foreach ($serviceStatus as $row) { + $this->handleDbRow($row, $cfg); + } + foreach ($hostStatus as $row) { + $this->handleDbRow($row, $cfg); + } + } + + // TODO: Union, single query? + Benchmark::measure('Got states for business process ' . $config->getName()); + + 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; + if (property_exists($row, 'service')) { + $key .= ';' . $row->service; + } else { + $key .= ';Hoststatus'; + } + + // We fetch more states than we need, so skip unknown ones + if (! $config->hasNode($key)) { + return; + } + + $node = $config->getNode($key); + + if ($row->state !== null) { + $node->setState($row->state)->setMissing(false); + } + if ($row->last_state_change !== null) { + $node->setLastStateChange($row->last_state_change); + } + if ((int) $row->in_downtime === 1) { + $node->setDowntime(true); + } + if ((int) $row->ack === 1) { + $node->setAck(true); + } + + $node->setAlias($row->display_name); + + if ($node instanceof ServiceNode) { + $node->setHostAlias($row->host_display_name); + } + } +} diff --git a/library/Businessprocess/Web/Component/Dashboard.php b/library/Businessprocess/Web/Component/Dashboard.php index eb3b092..1b66d34 100644 --- a/library/Businessprocess/Web/Component/Dashboard.php +++ b/library/Businessprocess/Web/Component/Dashboard.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Businessprocess\Web\Component; use Icinga\Authentication\Auth; +use Icinga\Module\Businessprocess\State\IcingaDbState; use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\Storage; use ipl\Html\BaseHtmlElement; @@ -91,7 +92,12 @@ class Dashboard extends BaseHtmlElement } $bp = $storage->loadProcess($name); - MonitoringState::apply($bp); + if ($bp->getBackendName() === '_icingadb') { + IcingaDbState::apply($bp); + } + else { + MonitoringState::apply($bp); + } $this->add(new BpDashboardTile( $bp, diff --git a/library/Businessprocess/Web/Controller.php b/library/Businessprocess/Web/Controller.php index d1104d8..3079fab 100644 --- a/library/Businessprocess/Web/Controller.php +++ b/library/Businessprocess/Web/Controller.php @@ -3,6 +3,10 @@ namespace Icinga\Module\Businessprocess\Web; use Icinga\Application\Icinga; +use Icinga\Data\Filter\Filter; +use Icinga\Data\Filterable; +use Icinga\Exception\ConfigurationError; +use Icinga\Exception\QueryException; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Storage\LegacyStorage; @@ -12,10 +16,14 @@ use Icinga\Module\Businessprocess\Web\Component\Controls; use Icinga\Module\Businessprocess\Web\Component\Content; use Icinga\Module\Businessprocess\Web\Component\Tabs; use Icinga\Module\Businessprocess\Web\Form\FormLoader; +use Icinga\Module\Icingadb\Compat\MonitoringRestrictions; +use Icinga\Module\Icingadb\Compat\UrlMigrator; use Icinga\Web\Controller as ModuleController; use Icinga\Web\Notification; use Icinga\Web\View; use ipl\Html\Html; +use ipl\Orm\Compat\FilterProcessor; +use ipl\Orm\Query; class Controller extends ModuleController { @@ -267,4 +275,87 @@ class Controller extends ModuleController return $this->storage; } + + /** + * Apply a restriction of the authenticated on the given filterable + * + * @param string $name Name of the restriction + * @param Filterable $filterable Filterable to restrict + * + * @return Filterable The filterable having the restriction applied + */ + protected function applyRestriction($name, Filterable $filterable) + { + $filterable->applyFilter($this->getRestriction($name)); + return $filterable; + } + + /** + * Get a restriction of the authenticated + * + * @param string $name Name of the restriction + * + * @return Filter Filter object + * @throws ConfigurationError If the restriction contains invalid filter columns + */ + protected function getRestriction($name) + { + $restriction = Filter::matchAny(); + $restriction->setAllowedFilterColumns(array( + 'host_name', + 'hostgroup_name', + 'instance_name', + 'service_description', + 'servicegroup_name', + function ($c) { + return preg_match('/^_(?:host|service)_/i', $c); + } + )); + foreach ($this->getRestrictions($name) as $filter) { + if ($filter === '*') { + return Filter::matchAll(); + } + try { + $restriction->addFilter(Filter::fromQueryString($filter)); + } catch (QueryException $e) { + throw new ConfigurationError( + $this->translate( + 'Cannot apply restriction %s using the filter %s. You can only use the following columns: %s' + ), + $name, + $filter, + implode(', ', array( + 'instance_name', + 'host_name', + 'hostgroup_name', + 'service_description', + 'servicegroup_name', + '_(host|service)_' + )), + $e + ); + } + } + + if ($restriction->isEmpty()) { + return Filter::matchAll(); + } + + return $restriction; + } + + public function applyMonitoringRestriction(Query $query, $queryTransformer = null) + { + if ($queryTransformer === null || UrlMigrator::hasQueryTransformer($queryTransformer)) { + $restriction = UrlMigrator::transformFilter( + MonitoringRestrictions::getRestriction('monitoring/filter/objects'), + $queryTransformer + ); + if ($restriction) { + FilterProcessor::apply($restriction, $query); + } + } + + return $this; + } } diff --git a/library/Businessprocess/Web/Form/BpConfigBaseForm.php b/library/Businessprocess/Web/Form/BpConfigBaseForm.php index b8bcbbd..437d68d 100644 --- a/library/Businessprocess/Web/Form/BpConfigBaseForm.php +++ b/library/Businessprocess/Web/Form/BpConfigBaseForm.php @@ -18,7 +18,9 @@ abstract class BpConfigBaseForm extends QuickForm protected function listAvailableBackends() { $keys = array_keys(Config::module('monitoring', 'backends')->toArray()); - return array_combine($keys, $keys); + $keys = array_combine($keys, $keys); + $keys['_icingadb'] = 'Icinga DB'; + return $keys; } public function setStorage(LegacyStorage $storage) 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 02/40] 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); } From 843a556f6e547273439a3ea581353d5ee3d0fdc8 Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Thu, 30 Apr 2020 15:58:10 +0200 Subject: [PATCH 03/40] Remove the unwanted comments in IcingaDbState Clean IcingaDbState class by removing the unwanted comments. --- .../Businessprocess/State/IcingaDbState.php | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index af6929d..c87e561 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -62,28 +62,8 @@ class IcingaDbState extends IcingaDbBackend $queryHost->getSelectBase() ->where(['host.name IN (?)' => $hosts]); -// $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'; -// } -// } if ($this->config->usesHardStates()) { $stateCol = 'state.hard_state'; } else { From c0f422d750e3e4b05769fa284805f197d24e3fde Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Thu, 30 Apr 2020 16:07:12 +0200 Subject: [PATCH 04/40] Resolve PHP CodeSniffer issue Remove the extra semi-colon in line 53 in IcingaDbState --- library/Businessprocess/State/IcingaDbState.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index c87e561..eecedfa 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -50,7 +50,7 @@ class IcingaDbState extends IcingaDbBackend { $config = $this->config; - Benchmark::measure('Retrieving states for business process ' . $config->getName());; + Benchmark::measure('Retrieving states for business process ' . $config->getName()); $hosts = $config->listInvolvedHostNames(); if (empty($hosts)) { From d494cf3ceeaf6417c2e5af9ee8faf49ea4f3e98d Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Mon, 4 May 2020 09:35:10 +0200 Subject: [PATCH 05/40] Each PHP statement must be on a line by itself Previously there was an error during php codesniffer test at line 53 of IcingaDbState. Changes are made to correct this error. --- library/Businessprocess/State/IcingaDbState.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index eecedfa..56b1a70 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -16,7 +16,7 @@ class IcingaDbState extends IcingaDbBackend /** @var BpConfig */ protected $config; - /** @var IcingaDbBackend */ + /** @var IcingadbDatabase */ protected $backend; public function __construct(BpConfig $config) @@ -50,7 +50,10 @@ class IcingaDbState extends IcingaDbBackend { $config = $this->config; - Benchmark::measure('Retrieving states for business process ' . $config->getName()); + Benchmark::measure(sprintf( + 'Retrieving states for business process %s', + $config->getName() + )); $hosts = $config->listInvolvedHostNames(); if (empty($hosts)) { From ce3bbde2890f702247f4a018893b219183ac8bbd Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Mon, 4 May 2020 11:22:47 +0200 Subject: [PATCH 06/40] Apply public static getRestriction and applyMonitoringRestriction changes Apply the public static getRestriction and applyMonitoringRestriction changes in the places were the corresponding methods are used. --- library/Businessprocess/Common/EnumList.php | 7 ++++--- library/Businessprocess/IcingaDbBackend.php | 4 ++-- library/Businessprocess/State/IcingaDbState.php | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index 3520793..b1446fb 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Businessprocess\Common; use Icinga\Module\Businessprocess\IcingaDbBackend; +use Icinga\Module\Businessprocess\MonitoringRestrictions; trait EnumList { @@ -14,7 +15,7 @@ trait EnumList $names = $this->backend ->select() ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) + ->applyFilter(MonitoringRestrictions::getRestriction('monitoring/filter/objects')) ->order('host_name') ->getQuery() ->fetchColumn(); @@ -38,7 +39,7 @@ trait EnumList $names = $this->backend ->select() ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) + ->applyFilter(MonitoringRestrictions::getRestriction('monitoring/filter/objects')) ->order('host_name') ->getQuery() ->fetchColumn(); @@ -64,7 +65,7 @@ trait EnumList ->select() ->from('serviceStatus', ['service' => 'service_description']) ->where('host_name', $host) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) + ->applyFilter(MonitoringRestrictions::getRestriction('monitoring/filter/objects')) ->order('service_description') ->getQuery() ->fetchColumn(); diff --git a/library/Businessprocess/IcingaDbBackend.php b/library/Businessprocess/IcingaDbBackend.php index 625127f..9dee1e0 100644 --- a/library/Businessprocess/IcingaDbBackend.php +++ b/library/Businessprocess/IcingaDbBackend.php @@ -29,7 +29,7 @@ class IcingaDbBackend $hosts = Host::on($this->conn) ->orderBy('host.name'); - $this->applyMonitoringRestriction($hosts); + self::applyMonitoringRestriction($hosts); return $hosts; } @@ -43,7 +43,7 @@ class IcingaDbBackend ->where(['service_host.name = ?' => $host]) ->orderBy('service.name'); - $this->applyMonitoringRestriction($services); + self::applyMonitoringRestriction($services); return $services; } diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 56b1a70..1a14df4 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -65,7 +65,7 @@ class IcingaDbState extends IcingaDbBackend $queryHost->getSelectBase() ->where(['host.name IN (?)' => $hosts]); - $this->applyMonitoringRestriction($queryHost); + IcingaDbBackend::applyMonitoringRestriction($queryHost); if ($this->config->usesHardStates()) { $stateCol = 'state.hard_state'; @@ -96,7 +96,7 @@ class IcingaDbState extends IcingaDbBackend $queryService->getSelectBase() ->where(['service_host.name IN (?)' => $hosts]); - $this->applyMonitoringRestriction($queryService); + IcingaDbBackend::applyMonitoringRestriction($queryService); $serviceStatusCols = array( 'hostname' => 'host.name', From d1526a36cbca35c070b0574ea0d2332dcc61ad6e Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Tue, 12 May 2020 23:20:01 +0200 Subject: [PATCH 07/40] Address the comments on IcingaDbState and the Controllers Address the comments from Eric in the pull request on IcingaDbState, Web/Controller, HostController and ServiceController. --- application/controllers/HostController.php | 9 +- application/controllers/ServiceController.php | 11 ++- .../Businessprocess/State/IcingaDbState.php | 12 +-- library/Businessprocess/Web/Controller.php | 91 ------------------- 4 files changed, 18 insertions(+), 105 deletions(-) diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 176a317..0115516 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -3,10 +3,8 @@ 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\Host; -use Icinga\Module\Monitoring\Backend; use Icinga\Module\Monitoring\Controller; use Icinga\Web\Url; @@ -16,10 +14,11 @@ class HostController extends Controller public function showAction() { - $hostName = $this->params->get('host'); - $icingadb = $this->params->get('icingadb'); + $icingadb = $this->params->shift('icingadb'); if ($icingadb) { + $hostName = $this->params->shift('host'); + $host = Host::on($this->getDb()); $host->getSelectBase() ->where(['host.name = ?' => $hostName]); @@ -33,6 +32,8 @@ class HostController extends Controller $this->redirectNow(Url::fromPath('icingadb/host')->setParams($this->params)); } } else { + $hostName = $this->params->get('host'); + $query = $this->backend->select() ->from('hoststatus', array('host_name')) ->where('host_name', $hostName); diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 2313291..5b03aed 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -5,7 +5,6 @@ namespace Icinga\Module\Businessprocess\Controllers; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; 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; @@ -15,11 +14,12 @@ class ServiceController extends Controller public function showAction() { - $hostName = $this->params->get('host'); - $serviceName = $this->params->get('service'); - $icingadb = $this->params->get('icingadb'); + $icingadb = $this->params->shift('icingadb'); if ($icingadb) { + $hostName = $this->params->shift('host'); + $serviceName = $this->params->shift('service'); + $service = Service::on($this->getDb())->with('host'); $service->getSelectBase() ->where(['service_host.name = ?' => $hostName, 'service.name = ?' => $serviceName]); @@ -35,6 +35,9 @@ class ServiceController extends Controller $this->redirectNow(Url::fromPath('icingadb/service')->setParams($this->params)); } } else { + $hostName = $this->params->get('host'); + $serviceName = $this->params->get('service'); + $query = $this->backend->select() ->from('servicestatus', array('service_description')) ->where('host_name', $hostName) diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 1a14df4..09af2ce 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -29,6 +29,7 @@ class IcingaDbState extends IcingaDbBackend { $self = new static($config); $self->retrieveStatesFromBackend(); + return $config; } @@ -51,7 +52,7 @@ class IcingaDbState extends IcingaDbBackend $config = $this->config; Benchmark::measure(sprintf( - 'Retrieving states for business process %s', + 'Retrieving states for business process %s using Icinga DB backend', $config->getName() )); @@ -73,14 +74,14 @@ class IcingaDbState extends IcingaDbBackend $stateCol = 'state.soft_state'; } - $hostStatusCols = array( + $hostStatusCols = [ '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' - ); + ]; $queryHost = $queryHost->columns($hostStatusCols)->assembleSelect(); @@ -98,7 +99,7 @@ class IcingaDbState extends IcingaDbBackend IcingaDbBackend::applyMonitoringRestriction($queryService); - $serviceStatusCols = array( + $serviceStatusCols = [ 'hostname' => 'host.name', 'service' => 'service.name', 'last_state_change' => 'state.last_state_change', @@ -107,7 +108,7 @@ class IcingaDbState extends IcingaDbBackend 'state' => $stateCol, 'display_name' => 'service.display_name', 'host_display_name' => 'host.display_name' - ); + ]; $queryService = $queryService->columns($serviceStatusCols)->assembleSelect(); @@ -128,7 +129,6 @@ class IcingaDbState extends IcingaDbBackend } } - // TODO: Union, single query? Benchmark::measure('Got states for business process ' . $config->getName()); return $this; diff --git a/library/Businessprocess/Web/Controller.php b/library/Businessprocess/Web/Controller.php index 3079fab..d1104d8 100644 --- a/library/Businessprocess/Web/Controller.php +++ b/library/Businessprocess/Web/Controller.php @@ -3,10 +3,6 @@ namespace Icinga\Module\Businessprocess\Web; use Icinga\Application\Icinga; -use Icinga\Data\Filter\Filter; -use Icinga\Data\Filterable; -use Icinga\Exception\ConfigurationError; -use Icinga\Exception\QueryException; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Storage\LegacyStorage; @@ -16,14 +12,10 @@ use Icinga\Module\Businessprocess\Web\Component\Controls; use Icinga\Module\Businessprocess\Web\Component\Content; use Icinga\Module\Businessprocess\Web\Component\Tabs; use Icinga\Module\Businessprocess\Web\Form\FormLoader; -use Icinga\Module\Icingadb\Compat\MonitoringRestrictions; -use Icinga\Module\Icingadb\Compat\UrlMigrator; use Icinga\Web\Controller as ModuleController; use Icinga\Web\Notification; use Icinga\Web\View; use ipl\Html\Html; -use ipl\Orm\Compat\FilterProcessor; -use ipl\Orm\Query; class Controller extends ModuleController { @@ -275,87 +267,4 @@ class Controller extends ModuleController return $this->storage; } - - /** - * Apply a restriction of the authenticated on the given filterable - * - * @param string $name Name of the restriction - * @param Filterable $filterable Filterable to restrict - * - * @return Filterable The filterable having the restriction applied - */ - protected function applyRestriction($name, Filterable $filterable) - { - $filterable->applyFilter($this->getRestriction($name)); - return $filterable; - } - - /** - * Get a restriction of the authenticated - * - * @param string $name Name of the restriction - * - * @return Filter Filter object - * @throws ConfigurationError If the restriction contains invalid filter columns - */ - protected function getRestriction($name) - { - $restriction = Filter::matchAny(); - $restriction->setAllowedFilterColumns(array( - 'host_name', - 'hostgroup_name', - 'instance_name', - 'service_description', - 'servicegroup_name', - function ($c) { - return preg_match('/^_(?:host|service)_/i', $c); - } - )); - foreach ($this->getRestrictions($name) as $filter) { - if ($filter === '*') { - return Filter::matchAll(); - } - try { - $restriction->addFilter(Filter::fromQueryString($filter)); - } catch (QueryException $e) { - throw new ConfigurationError( - $this->translate( - 'Cannot apply restriction %s using the filter %s. You can only use the following columns: %s' - ), - $name, - $filter, - implode(', ', array( - 'instance_name', - 'host_name', - 'hostgroup_name', - 'service_description', - 'servicegroup_name', - '_(host|service)_' - )), - $e - ); - } - } - - if ($restriction->isEmpty()) { - return Filter::matchAll(); - } - - return $restriction; - } - - public function applyMonitoringRestriction(Query $query, $queryTransformer = null) - { - if ($queryTransformer === null || UrlMigrator::hasQueryTransformer($queryTransformer)) { - $restriction = UrlMigrator::transformFilter( - MonitoringRestrictions::getRestriction('monitoring/filter/objects'), - $queryTransformer - ); - if ($restriction) { - FilterProcessor::apply($restriction, $query); - } - } - - return $this; - } } From f9be5f81d686b1934058bbdab318632c10f85a32 Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Mon, 25 May 2020 10:32:11 +0200 Subject: [PATCH 08/40] Change state calculation in IcingaDbState Since we are fetching the values directly using assembleSelect instead of using ORM, 'last_state_change', 'in_downtime' and 'ack' have different values compared to monitored nodes other than IcingaDb MySQL backend. This is addresses in this commit. --- library/Businessprocess/State/IcingaDbState.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 09af2ce..018ded4 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -148,18 +148,20 @@ class IcingaDbState extends IcingaDbBackend return; } + // Since we are fetching the values directly using assembleSelect instead of using ORM, + // the following changes for 'last_state_change', 'in_downtime' and 'ack' is required $node = $config->getNode($key); if ($row->state !== null) { $node->setState($row->state)->setMissing(false); } if ($row->last_state_change !== null) { - $node->setLastStateChange($row->last_state_change); + $node->setLastStateChange($row->last_state_change/1000); } - if ((int) $row->in_downtime === 1) { + if ($row->in_downtime === 'y') { $node->setDowntime(true); } - if ((int) $row->ack === 1) { + if ($row->ack !== 'n') { $node->setAck(true); } From 5d905426b61722f58f53ad13bdbf1e13490b6447 Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Mon, 25 May 2020 10:39:31 +0200 Subject: [PATCH 09/40] Use IcingaDbState for nodes from IcingaDb MySQL backend in ProcessCommand Since we have included monitored nodes from IcingaDB MySQL backend, the states for these nodes will be now calculated using IcingaDbState::apply. --- application/clicommands/ProcessCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/application/clicommands/ProcessCommand.php b/application/clicommands/ProcessCommand.php index a6d1dac..59c9d31 100644 --- a/application/clicommands/ProcessCommand.php +++ b/application/clicommands/ProcessCommand.php @@ -9,6 +9,7 @@ use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\HostNode; use Icinga\Module\Businessprocess\Node; +use Icinga\Module\Businessprocess\State\IcingaDbState; use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\LegacyStorage; From afff0bea344d6e6ce7c19b101dcfb8093ae72d63 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 12 Oct 2021 18:34:24 +0200 Subject: [PATCH 10/40] BpConfigBaseForm: Fix backend change Do not use the previously saved backend if the value has been changed to `Use the configured default backend` (null) --- application/forms/BpConfigForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/forms/BpConfigForm.php b/application/forms/BpConfigForm.php index 7895d00..531cf0f 100644 --- a/application/forms/BpConfigForm.php +++ b/application/forms/BpConfigForm.php @@ -167,7 +167,7 @@ class BpConfigForm extends BpConfigBaseForm } $meta = $config->getMetadata(); foreach ($this->getValues() as $key => $value) { - if ($value === null || $value === '') { + if ($key !== 'Backend' && ($value === null || $value === '')) { continue; } if ($meta->hasKey($key)) { From 08175ae652e217787bf1e0c24be1c0f6f72183fe Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 12 Nov 2021 12:02:33 +0100 Subject: [PATCH 11/40] Provide hook for `icingadbSupport` --- .../ProvidedHook/Icingadb/IcingadbSupport.php | 10 ++++++++++ run.php | 1 + 2 files changed, 11 insertions(+) create mode 100644 library/Businessprocess/ProvidedHook/Icingadb/IcingadbSupport.php diff --git a/library/Businessprocess/ProvidedHook/Icingadb/IcingadbSupport.php b/library/Businessprocess/ProvidedHook/Icingadb/IcingadbSupport.php new file mode 100644 index 0000000..1ff37d3 --- /dev/null +++ b/library/Businessprocess/ProvidedHook/Icingadb/IcingadbSupport.php @@ -0,0 +1,10 @@ +provideHook('monitoring/HostActions'); $this->provideHook('monitoring/ServiceActions'); +$this->provideHook('icingadb/icingadbSupport'); //$this->provideHook('director/shipConfigFiles'); From 55b0ddf8f61c196fd59dab04c9c3eb2a5424e139 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 12 Nov 2021 12:04:05 +0100 Subject: [PATCH 12/40] BpConfig: Use icingadb backend Use icingadb backend if configured in preferences or monitoring module is disabled --- library/Businessprocess/BpConfig.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index 80488fb..8436483 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -4,11 +4,13 @@ namespace Icinga\Module\Businessprocess; use Exception; use Icinga\Application\Config; +use Icinga\Application\Modules\Module; use Icinga\Exception\IcingaException; use Icinga\Exception\NotFoundError; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\Exception\NestingError; use Icinga\Module\Businessprocess\Modification\ProcessChanges; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Storage\LegacyStorage; use Icinga\Module\Monitoring\Backend\MonitoringBackend; @@ -293,7 +295,9 @@ class BpConfig public function getBackend() { if ($this->backend === null) { - if ($this->getBackendName() === '_icingadb') { + if ($this->getBackendName() === '_icingadb' || + (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) + ) { $this->backend = $this->getDb(); } else { $this->backend = MonitoringBackend::instance( From a6dad054454f16b027fe1ff00a5fbb5daf07383d Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 12 Nov 2021 12:07:40 +0100 Subject: [PATCH 13/40] BpConfigBaseForm: Only add backends of enabled modules --- .../Businessprocess/Web/Form/BpConfigBaseForm.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/library/Businessprocess/Web/Form/BpConfigBaseForm.php b/library/Businessprocess/Web/Form/BpConfigBaseForm.php index 437d68d..92f09ea 100644 --- a/library/Businessprocess/Web/Form/BpConfigBaseForm.php +++ b/library/Businessprocess/Web/Form/BpConfigBaseForm.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Businessprocess\Web\Form; use Icinga\Application\Config; +use Icinga\Application\Icinga; use Icinga\Authentication\Auth; use Icinga\Module\Businessprocess\Storage\LegacyStorage; use Icinga\Module\Businessprocess\BpConfig; @@ -17,9 +18,17 @@ abstract class BpConfigBaseForm extends QuickForm protected function listAvailableBackends() { - $keys = array_keys(Config::module('monitoring', 'backends')->toArray()); - $keys = array_combine($keys, $keys); - $keys['_icingadb'] = 'Icinga DB'; + $keys = []; + $moduleManager = Icinga::app()->getModuleManager(); + if ($moduleManager->hasEnabled('monitoring')) { + $keys = array_keys(Config::module('monitoring', 'backends')->toArray()); + $keys = array_combine($keys, $keys); + } + + if ($moduleManager->hasEnabled('icingadb')) { + $keys['_icingadb'] = 'Icinga DB'; + } + return $keys; } From df72ff65bee2c884a9e789e31e36c434714ecd9a Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Wed, 17 Nov 2021 10:10:00 +0100 Subject: [PATCH 14/40] Move enumHostListByFilter and enumServiceListByFilter to EnumList trait. Move enumHostListByFilter and enumServiceListByFilter to EnumList trait from AddNodeForm and change fetchServices and fetchHosts method to work with filters in IcingaDbBackEnd class. Also, applyMonitoringRestriction is changed to applyIcingaDbRestrictions in IcingaDbBackend. --- application/controllers/HostController.php | 13 +- application/controllers/ServiceController.php | 13 +- application/forms/AddNodeForm.php | 137 +++++------------- library/Businessprocess/Common/EnumList.php | 52 +++++++ library/Businessprocess/IcingaDbBackend.php | 55 ++++--- .../Businessprocess/State/IcingaDbState.php | 12 +- 6 files changed, 148 insertions(+), 134 deletions(-) diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 0115516..1249ed6 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -19,16 +19,17 @@ class HostController extends Controller if ($icingadb) { $hostName = $this->params->shift('host'); - $host = Host::on($this->getDb()); - $host->getSelectBase() - ->where(['host.name = ?' => $hostName]); - IcingaDbBackend::applyMonitoringRestriction($host); + $query = Host::on($this->getDb()); + IcingaDbBackend::applyIcingaDbRestrictions($query); - $rs = $host->columns('host.name')->first(); + $query->getSelectBase() + ->where(['host.name = ?' => $hostName]); + + $host = $query->first(); $this->params->add('name', $hostName); - if ($rs !== false) { + if ($host !== false) { $this->redirectNow(Url::fromPath('icingadb/host')->setParams($this->params)); } } else { diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 5b03aed..ddc717d 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -20,18 +20,19 @@ class ServiceController extends Controller $hostName = $this->params->shift('host'); $serviceName = $this->params->shift('service'); - $service = Service::on($this->getDb())->with('host'); - $service->getSelectBase() - ->where(['service_host.name = ?' => $hostName, 'service.name = ?' => $serviceName]); + $query = Service::on($this->getDb())->with('host'); + IcingaDbBackend::applyIcingaDbRestrictions($query); - IcingaDbBackend::applyMonitoringRestriction($service); + $query->getSelectBase() + ->where(['service.name = ?' => $serviceName]) + ->where(['service_host.name = ?' => $hostName]); - $rs = $service->columns('host.name')->first(); + $service = $query->first(); $this->params->add('name', $serviceName); $this->params->add('host.name', $hostName); - if ($rs !== false) { + if ($service !== false) { $this->redirectNow(Url::fromPath('icingadb/service')->setParams($this->params)); } } else { diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index 94786c0..e2125dc 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -2,7 +2,6 @@ namespace Icinga\Module\Businessprocess\Forms; -use Icinga\Data\Filter\Filter; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; @@ -464,43 +463,6 @@ class AddNodeForm extends QuickForm return $this; } - protected function enumHostForServiceList() - { - $names = $this->backend - ->select() - ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('host_name') - ->getQuery() - ->fetchColumn(); - - // fetchPairs doesn't seem to work when using the same column with - // different aliases twice - - return array_combine((array) $names, (array) $names); - } - - protected function enumHostList() - { - $names = $this->backend - ->select() - ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('host_name') - ->getQuery() - ->fetchColumn(); - - // fetchPairs doesn't seem to work when using the same column with - // different aliases twice - $res = array(); - $suffix = ';Hoststatus'; - foreach ($names as $name) { - $res[$name . $suffix] = $name; - } - - return $res; - } - protected function enumHostStateList() { $hostStateList = [ @@ -512,25 +474,6 @@ class AddNodeForm extends QuickForm return $hostStateList; } - protected function enumServiceList($host) - { - $names = $this->backend - ->select() - ->from('serviceStatus', ['service' => 'service_description']) - ->where('host_name', $host) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('service_description') - ->getQuery() - ->fetchColumn(); - - $services = array(); - foreach ($names as $name) { - $services[$host . ';' . $name] = $name; - } - - return $services; - } - protected function enumServiceStateList() { $serviceStateList = [ @@ -544,46 +487,46 @@ class AddNodeForm extends QuickForm return $serviceStateList; } - protected function enumHostListByFilter($filter) - { - $names = $this->backend - ->select() - ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter(Filter::fromQueryString($filter)) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('host_name') - ->getQuery() - ->fetchColumn(); - - // fetchPairs doesn't seem to work when using the same column with - // different aliases twice - $res = array(); - $suffix = ';Hoststatus'; - foreach ($names as $name) { - $res[$name . $suffix] = $name; - } - - return $res; - } - - protected function enumServiceListByFilter($filter) - { - $objects = $this->backend - ->select() - ->from('serviceStatus', ['host' => 'host_name', 'service' => 'service_description']) - ->applyFilter(Filter::fromQueryString($filter)) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('service_description') - ->getQuery() - ->fetchAll(); - - $services = array(); - foreach ($objects as $object) { - $services[$object->host . ';' . $object->service] = $object->host . ':' . $object->service; - } - - return $services; - } +// protected function enumHostListByFilter($filter) +// { +// $names = $this->backend +// ->select() +// ->from('hostStatus', ['hostname' => 'host_name']) +// ->applyFilter(Filter::fromQueryString($filter)) +// ->applyFilter($this->getRestriction('monitoring/filter/objects')) +// ->order('host_name') +// ->getQuery() +// ->fetchColumn(); +// +// // fetchPairs doesn't seem to work when using the same column with +// // different aliases twice +// $res = array(); +// $suffix = ';Hoststatus'; +// foreach ($names as $name) { +// $res[$name . $suffix] = $name; +// } +// +// return $res; +// } +// +// protected function enumServiceListByFilter($filter) +// { +// $objects = $this->backend +// ->select() +// ->from('serviceStatus', ['host' => 'host_name', 'service' => 'service_description']) +// ->applyFilter(Filter::fromQueryString($filter)) +// ->applyFilter($this->getRestriction('monitoring/filter/objects')) +// ->order('service_description') +// ->getQuery() +// ->fetchAll(); +// +// $services = array(); +// foreach ($objects as $object) { +// $services[$object->host . ';' . $object->service] = $object->host . ':' . $object->service; +// } +// +// return $services; +// } protected function hasProcesses() { diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index b1446fb..0940751 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Common; +use Icinga\Data\Filter\Filter; use Icinga\Module\Businessprocess\IcingaDbBackend; use Icinga\Module\Businessprocess\MonitoringRestrictions; @@ -79,6 +80,57 @@ trait EnumList return $services; } + protected function enumHostListByFilter($filter) + { + if ($this->useIcingaDbBackend()) { + $names = (new IcingaDbBackend())->yieldHostnames($filter); + } else { + $names = $this->backend + ->select() + ->from('hostStatus', ['hostname' => 'host_name']) + ->applyFilter(MonitoringRestrictions::getRestriction('monitoring/filter/objects')) + ->order('host_name') + ->getQuery() + ->fetchColumn(); + } + + // fetchPairs doesn't seem to work when using the same column with + // different aliases twice + $res = array(); + $suffix = ';Hoststatus'; + foreach ($names as $name) { + $res[$name . $suffix] = $name; + } + + return $res; + } + + protected function enumServiceListByFilter($filter) + { + $services = array(); + + if ($this->useIcingaDbBackend()) { + $objects = (new IcingaDbBackend())->fetchServices($filter); + foreach ($objects as $object) { + $services[$object->host->name . ';' . $object->name] = $object->host->name . ':' . $object->name; + } + } else { + $objects = $this->backend + ->select() + ->from('serviceStatus', ['host' => 'host_name', 'service' => 'service_description']) + ->applyFilter(Filter::fromQueryString($filter)) + ->applyFilter(MonitoringRestrictions::getRestriction('monitoring/filter/objects')) + ->order('service_description') + ->getQuery() + ->fetchAll(); + foreach ($objects as $object) { + $services[$object->host . ';' . $object->service] = $object->host . ':' . $object->service; + } + } + + return $services; + } + protected function useIcingaDbBackend() { return $this->backendName === '_icingadb'; diff --git a/library/Businessprocess/IcingaDbBackend.php b/library/Businessprocess/IcingaDbBackend.php index 9dee1e0..ba5c194 100644 --- a/library/Businessprocess/IcingaDbBackend.php +++ b/library/Businessprocess/IcingaDbBackend.php @@ -3,16 +3,17 @@ namespace Icinga\Module\Businessprocess; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; +use Icinga\Module\Icingadb\Common\Auth; 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; +use ipl\Web\Filter\QueryString; class IcingaDbBackend { use IcingadbDatabase; + use Auth; + /** @var BpConfig */ protected $config; @@ -24,51 +25,63 @@ class IcingaDbBackend $this->conn = $this->getDb(); } - public function fetchHosts() + public function fetchHosts($filter = null) { - $hosts = Host::on($this->conn) - ->orderBy('host.name'); - self::applyMonitoringRestriction($hosts); + $hosts = Host::on($this->conn); + + if ($filter !== null) { + $filterQuery = QueryString::parse($filter); + + $hosts->filter($filterQuery); + } + + $hosts->orderBy('host.name'); + + $this->applyIcingaDbRestrictions($hosts); return $hosts; } - public function fetchServices($host) + public function fetchServices($filter) { $services = Service::on($this->conn) ->with('host'); - $services->getSelectBase() - ->where(['service_host.name = ?' => $host]) - ->orderBy('service.name'); + if ($filter !== null) { + $filterQuery = QueryString::parse($filter); - self::applyMonitoringRestriction($services); + $services->filter($filterQuery); + } + + $services->orderBy('service.name'); + + $this->applyIcingaDbRestrictions($services); return $services; } - public function yieldHostnames() + public function yieldHostnames($filter = null) { - foreach ($this->fetchHosts() as $host) { + foreach ($this->fetchHosts($filter) as $host) { yield $host->name; } } public function yieldServicenames($host) { - foreach ($this->fetchServices($host) as $service) { + $filter = "host.name=$host"; + + foreach ($this->fetchServices($filter) as $service) { yield $service->name; } } - public static function applyMonitoringRestriction(Query $query) + public static function applyIcingaDbRestrictions($query) { - $restriction = FilterProcessor::apply( - MonitoringRestrictions::getRestriction('monitoring/filter/objects'), - $query - ); + $object = new self; + $object->applyRestrictions($query); - return $restriction; + return $object; } } diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 018ded4..a5dc0fd 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -8,11 +8,14 @@ use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\IcingaDbBackend; use Icinga\Module\Businessprocess\ServiceNode; +use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; class IcingaDbState extends IcingaDbBackend { + use Auth; + /** @var BpConfig */ protected $config; @@ -62,11 +65,12 @@ class IcingaDbState extends IcingaDbBackend } $queryHost = Host::on($this->backend)->with('state'); + IcingaDbBackend::applyIcingaDbRestrictions($queryHost); $queryHost->getSelectBase() ->where(['host.name IN (?)' => $hosts]); - IcingaDbBackend::applyMonitoringRestriction($queryHost); + IcingaDbBackend::applyIcingaDbRestrictions($queryHost); if ($this->config->usesHardStates()) { $stateCol = 'state.hard_state'; @@ -97,15 +101,15 @@ class IcingaDbState extends IcingaDbBackend $queryService->getSelectBase() ->where(['service_host.name IN (?)' => $hosts]); - IcingaDbBackend::applyMonitoringRestriction($queryService); + IcingaDbBackend::applyIcingaDbRestrictions($queryService); $serviceStatusCols = [ '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, + 'ack' => 'state.is_acknowledged', + 'state' => 'state.soft_state', 'display_name' => 'service.display_name', 'host_display_name' => 'host.display_name' ]; From 9b0c4cca345d66a6275cddd9605295f6de165b4a Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Wed, 17 Nov 2021 15:07:33 +0100 Subject: [PATCH 15/40] Remove IcingadbBackend class extension from IcingaDbState and clean IcingaDbState class. Do not assemble the query as there is no need to do that. Also, no need to have associative arrays for host and service state columns. --- .../Businessprocess/State/IcingaDbState.php | 87 +++++++------------ 1 file changed, 30 insertions(+), 57 deletions(-) diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index a5dc0fd..250b8ec 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -12,7 +12,7 @@ use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; -class IcingaDbState extends IcingaDbBackend +class IcingaDbState { use Auth; @@ -70,66 +70,35 @@ class IcingaDbState extends IcingaDbBackend $queryHost->getSelectBase() ->where(['host.name IN (?)' => $hosts]); + $hostObject = $queryHost->getModel()->getTableName(); + IcingaDbBackend::applyIcingaDbRestrictions($queryHost); - if ($this->config->usesHardStates()) { - $stateCol = 'state.hard_state'; - } else { - $stateCol = 'state.soft_state'; - } - - $hostStatusCols = [ - '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' - ]; - - $queryHost = $queryHost->columns($hostStatusCols)->assembleSelect(); - - $hostStatus = $this->backend->select($queryHost)->fetchAll(); - - Benchmark::measure('Retrieved states for ' . count($hostStatus) . ' hosts in ' . $config->getName()); + Benchmark::measure('Retrieved states for ' . $queryHost->count() . ' hosts in ' . $config->getName()); $queryService = Service::on($this->backend)->with([ 'state', 'host', 'host.state' ]); + $queryService->getSelectBase() ->where(['service_host.name IN (?)' => $hosts]); IcingaDbBackend::applyIcingaDbRestrictions($queryService); - $serviceStatusCols = [ - 'hostname' => 'host.name', - 'service' => 'service.name', - 'last_state_change' => 'state.last_state_change', - 'in_downtime' => 'state.in_downtime', - 'ack' => 'state.is_acknowledged', - 'state' => 'state.soft_state', - 'display_name' => 'service.display_name', - 'host_display_name' => 'host.display_name' - ]; - - $queryService = $queryService->columns($serviceStatusCols)->assembleSelect(); - - $serviceStatus = $this->backend->select($queryService)->fetchAll(); - - Benchmark::measure('Retrieved states for ' . count($serviceStatus) . ' services in ' . $config->getName()); + Benchmark::measure('Retrieved states for ' . $queryService->count() . ' services in ' . $config->getName()); $configs = $config->listInvolvedConfigs(); - $hostStatus = (object) $hostStatus; - $serviceStatus = (object) $serviceStatus; + + $serviceObject = $queryService->getModel()->getTableName(); foreach ($configs as $cfg) { - foreach ($serviceStatus as $row) { - $this->handleDbRow($row, $cfg); + foreach ($queryService as $row) { + $this->handleDbRow($row, $cfg, $serviceObject); } - foreach ($hostStatus as $row) { - $this->handleDbRow($row, $cfg); + foreach ($queryHost as $row) { + $this->handleDbRow($row, $cfg, $hostObject); } } @@ -138,13 +107,12 @@ class IcingaDbState extends IcingaDbBackend return $this; } - protected function handleDbRow($row, BpConfig $config) + protected function handleDbRow($row, BpConfig $config, $objectName) { - $key = $row->hostname; - if (property_exists($row, 'service')) { - $key .= ';' . $row->service; + if ($objectName === 'service') { + $key = $row->host->name . ';' . $row->name; } else { - $key .= ';Hoststatus'; + $key = $row->name . ';Hoststatus'; } // We fetch more states than we need, so skip unknown ones @@ -152,27 +120,32 @@ class IcingaDbState extends IcingaDbBackend return; } - // Since we are fetching the values directly using assembleSelect instead of using ORM, - // the following changes for 'last_state_change', 'in_downtime' and 'ack' is required $node = $config->getNode($key); - if ($row->state !== null) { - $node->setState($row->state)->setMissing(false); + if ($this->config->usesHardStates()) { + if ($row->state->hard_state !== null) { + $node->setState($row->state->hard_state)->setMissing(false); + } + } else { + if ($row->state->soft_state !== null) { + $node->setState($row->state->soft_state)->setMissing(false); + } } - if ($row->last_state_change !== null) { - $node->setLastStateChange($row->last_state_change/1000); + + if ($row->state->last_state_change !== null) { + $node->setLastStateChange($row->state->last_state_change/1000); } - if ($row->in_downtime === 'y') { + if ($row->state->in_downtime === 'y') { $node->setDowntime(true); } - if ($row->ack !== 'n') { + if ($row->state->is_acknowledged !== 'n') { $node->setAck(true); } $node->setAlias($row->display_name); if ($node instanceof ServiceNode) { - $node->setHostAlias($row->host_display_name); + $node->setHostAlias($row->host->display_name); } } } From 685f44d118221f3f3615468a5951922907018983 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Wed, 17 Nov 2021 16:22:22 +0100 Subject: [PATCH 16/40] Change class name IcingaDbBackend to IcingaDbObject --- application/controllers/HostController.php | 4 ++-- application/controllers/ServiceController.php | 4 ++-- application/forms/ProcessForm.php | 2 +- library/Businessprocess/Common/EnumList.php | 12 ++++++------ .../{IcingaDbBackend.php => IcingaDbObject.php} | 2 +- library/Businessprocess/State/IcingaDbState.php | 8 ++++---- 6 files changed, 16 insertions(+), 16 deletions(-) rename library/Businessprocess/{IcingaDbBackend.php => IcingaDbObject.php} (98%) diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 1249ed6..fea347e 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -3,7 +3,7 @@ namespace Icinga\Module\Businessprocess\Controllers; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; -use Icinga\Module\Businessprocess\IcingaDbBackend; +use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Monitoring\Controller; use Icinga\Web\Url; @@ -20,7 +20,7 @@ class HostController extends Controller $hostName = $this->params->shift('host'); $query = Host::on($this->getDb()); - IcingaDbBackend::applyIcingaDbRestrictions($query); + IcingaDbObject::applyIcingaDbRestrictions($query); $query->getSelectBase() ->where(['host.name = ?' => $hostName]); diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index ddc717d..9cee847 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -3,7 +3,7 @@ namespace Icinga\Module\Businessprocess\Controllers; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; -use Icinga\Module\Businessprocess\IcingaDbBackend; +use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Icingadb\Model\Service; use Icinga\Module\Monitoring\Controller; use Icinga\Web\Url; @@ -21,7 +21,7 @@ class ServiceController extends Controller $serviceName = $this->params->shift('service'); $query = Service::on($this->getDb())->with('host'); - IcingaDbBackend::applyIcingaDbRestrictions($query); + IcingaDbObject::applyIcingaDbRestrictions($query); $query->getSelectBase() ->where(['service.name = ?' => $serviceName]) diff --git a/application/forms/ProcessForm.php b/application/forms/ProcessForm.php index bd894e8..da68017 100644 --- a/application/forms/ProcessForm.php +++ b/application/forms/ProcessForm.php @@ -5,7 +5,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\IcingaDbBackend; +use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Monitoring\Backend\MonitoringBackend; diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index 0940751..93d052b 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -3,7 +3,7 @@ namespace Icinga\Module\Businessprocess\Common; use Icinga\Data\Filter\Filter; -use Icinga\Module\Businessprocess\IcingaDbBackend; +use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\MonitoringRestrictions; trait EnumList @@ -11,7 +11,7 @@ trait EnumList protected function enumHostForServiceList() { if ($this->useIcingaDbBackend()) { - $names = (new IcingaDbBackend())->yieldHostnames(); + $names = (new IcingaDbObject())->yieldHostnames(); } else { $names = $this->backend ->select() @@ -35,7 +35,7 @@ trait EnumList protected function enumHostList() { if ($this->useIcingaDbBackend()) { - $names = (new IcingaDbBackend())->yieldHostnames(); + $names = (new IcingaDbObject())->yieldHostnames(); } else { $names = $this->backend ->select() @@ -60,7 +60,7 @@ trait EnumList protected function enumServiceList($host) { if ($this->useIcingaDbBackend()) { - $names = (new IcingaDbBackend())->yieldServicenames($host); + $names = (new IcingaDbObject())->yieldServicenames($host); } else { $names = $this->backend ->select() @@ -83,7 +83,7 @@ trait EnumList protected function enumHostListByFilter($filter) { if ($this->useIcingaDbBackend()) { - $names = (new IcingaDbBackend())->yieldHostnames($filter); + $names = (new IcingaDbObject())->yieldHostnames($filter); } else { $names = $this->backend ->select() @@ -110,7 +110,7 @@ trait EnumList $services = array(); if ($this->useIcingaDbBackend()) { - $objects = (new IcingaDbBackend())->fetchServices($filter); + $objects = (new IcingaDbObject())->fetchServices($filter); foreach ($objects as $object) { $services[$object->host->name . ';' . $object->name] = $object->host->name . ':' . $object->name; } diff --git a/library/Businessprocess/IcingaDbBackend.php b/library/Businessprocess/IcingaDbObject.php similarity index 98% rename from library/Businessprocess/IcingaDbBackend.php rename to library/Businessprocess/IcingaDbObject.php index ba5c194..1ee6e3a 100644 --- a/library/Businessprocess/IcingaDbBackend.php +++ b/library/Businessprocess/IcingaDbObject.php @@ -8,7 +8,7 @@ use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; use ipl\Web\Filter\QueryString; -class IcingaDbBackend +class IcingaDbObject { use IcingadbDatabase; diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 250b8ec..440f882 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -6,7 +6,7 @@ use Exception; use Icinga\Application\Benchmark; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; -use Icinga\Module\Businessprocess\IcingaDbBackend; +use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\ServiceNode; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Host; @@ -65,14 +65,14 @@ class IcingaDbState } $queryHost = Host::on($this->backend)->with('state'); - IcingaDbBackend::applyIcingaDbRestrictions($queryHost); + IcingaDbObject::applyIcingaDbRestrictions($queryHost); $queryHost->getSelectBase() ->where(['host.name IN (?)' => $hosts]); $hostObject = $queryHost->getModel()->getTableName(); - IcingaDbBackend::applyIcingaDbRestrictions($queryHost); + IcingaDbObject::applyIcingaDbRestrictions($queryHost); Benchmark::measure('Retrieved states for ' . $queryHost->count() . ' hosts in ' . $config->getName()); @@ -85,7 +85,7 @@ class IcingaDbState $queryService->getSelectBase() ->where(['service_host.name IN (?)' => $hosts]); - IcingaDbBackend::applyIcingaDbRestrictions($queryService); + IcingaDbObject::applyIcingaDbRestrictions($queryService); Benchmark::measure('Retrieved states for ' . $queryService->count() . ' services in ' . $config->getName()); From 69e879a7459226de576b277a08b03f7c95aebcee Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Wed, 17 Nov 2021 17:28:05 +0100 Subject: [PATCH 17/40] Make changes to support the case when icingadb is set as the preferred backend. --- application/clicommands/ProcessCommand.php | 8 +++++++- application/controllers/NodeController.php | 3 ++- application/controllers/ProcessController.php | 3 ++- library/Businessprocess/Common/EnumList.php | 3 ++- library/Businessprocess/HostNode.php | 5 +++-- library/Businessprocess/ServiceNode.php | 6 ++++-- library/Businessprocess/Web/Component/Dashboard.php | 3 ++- 7 files changed, 22 insertions(+), 9 deletions(-) diff --git a/application/clicommands/ProcessCommand.php b/application/clicommands/ProcessCommand.php index 59c9d31..0baef23 100644 --- a/application/clicommands/ProcessCommand.php +++ b/application/clicommands/ProcessCommand.php @@ -9,6 +9,7 @@ use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\HostNode; use Icinga\Module\Businessprocess\Node; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\State\IcingaDbState; use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\LegacyStorage; @@ -133,7 +134,12 @@ class ProcessCommand extends Command /** @var BpNode $node */ try { $node = $bp->getNode($nodeName); - MonitoringState::apply($bp); + if ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { + IcingaDbState::apply($bp); + } else { + MonitoringState::apply($bp); + } + if ($bp->hasErrors()) { Logger::error("Checking Business Process '%s' failed: %s\n", $name, $bp->getErrors()); diff --git a/application/controllers/NodeController.php b/application/controllers/NodeController.php index 4a4876a..ec5e699 100644 --- a/application/controllers/NodeController.php +++ b/application/controllers/NodeController.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Controllers; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Renderer\Breadcrumb; use Icinga\Module\Businessprocess\Renderer\TileRenderer; use Icinga\Module\Businessprocess\Simulation; @@ -82,7 +83,7 @@ class NodeController extends Controller if (empty($parents)) { continue; } - if ($config->getBackendName() === '_icingadb') { + if ($config->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { IcingaDbState::apply($config); } else { MonitoringState::apply($config); diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index 2b3eea7..6bf4417 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -6,6 +6,7 @@ use Icinga\Date\DateFormatter; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\Node; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Renderer\Breadcrumb; use Icinga\Module\Businessprocess\Renderer\Renderer; use Icinga\Module\Businessprocess\Renderer\TileRenderer; @@ -82,7 +83,7 @@ class ProcessController extends Controller $bp = $this->loadModifiedBpConfig(); $node = $this->getNode($bp); - if ($bp->getBackendName() === '_icingadb') { + if ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { IcingaDbState::apply($bp); } else { MonitoringState::apply($bp); diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index 93d052b..508a662 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Businessprocess\Common; use Icinga\Data\Filter\Filter; use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\MonitoringRestrictions; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; trait EnumList { @@ -133,6 +134,6 @@ trait EnumList protected function useIcingaDbBackend() { - return $this->backendName === '_icingadb'; + return $this->backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend(); } } diff --git a/library/Businessprocess/HostNode.php b/library/Businessprocess/HostNode.php index cc5315e..f272f33 100644 --- a/library/Businessprocess/HostNode.php +++ b/library/Businessprocess/HostNode.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Web\Url; use ipl\Html\Html; @@ -56,9 +57,9 @@ class HostNode extends MonitoredNode 'host' => $this->getHostname(), ); - if ($this->getBpConfig()->hasBackendName()) { + if ($this->getBpConfig()->hasBackendName() || IcingadbSupport::useIcingaDbAsBackend()) { $backendName = $this->getBpConfig()->getBackendName(); - if ($backendName === '_icingadb') { + if ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { $params['icingadb'] = 1; } else { $params['backend'] = $this->getBpConfig()->getBackendName(); diff --git a/library/Businessprocess/ServiceNode.php b/library/Businessprocess/ServiceNode.php index ba9234a..6476281 100644 --- a/library/Businessprocess/ServiceNode.php +++ b/library/Businessprocess/ServiceNode.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Web\Url; class ServiceNode extends MonitoredNode @@ -75,9 +76,10 @@ class ServiceNode extends MonitoredNode 'service' => $this->getServiceDescription() ); - if ($this->getBpConfig()->hasBackendName()) { + if ($this->getBpConfig()->hasBackendName() || IcingadbSupport::useIcingaDbAsBackend()) { $backendName = $this->getBpConfig()->getBackendName(); - if ($backendName === '_icingadb') { + + if ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { $params['icingadb'] = 1; } else { $params['backend'] = $this->getBpConfig()->getBackendName(); diff --git a/library/Businessprocess/Web/Component/Dashboard.php b/library/Businessprocess/Web/Component/Dashboard.php index c57b48a..0f1acb0 100644 --- a/library/Businessprocess/Web/Component/Dashboard.php +++ b/library/Businessprocess/Web/Component/Dashboard.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Businessprocess\Web\Component; use Icinga\Authentication\Auth; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\State\IcingaDbState; use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\Storage; @@ -92,7 +93,7 @@ class Dashboard extends BaseHtmlElement } $bp = $storage->loadProcess($name); - if ($bp->getBackendName() === '_icingadb') { + if ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { IcingaDbState::apply($bp); } else { MonitoringState::apply($bp); From 7b419068815e0bce0bd8abec08dd0acfbafa681e Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 18 Nov 2021 12:19:05 +0100 Subject: [PATCH 18/40] Select default backend when icingadb is deactivated and the same when monitoring backend is deactivated --- application/clicommands/ProcessCommand.php | 5 ++++- application/controllers/HostController.php | 3 ++- application/controllers/NodeController.php | 10 +++++++++- application/controllers/ProcessController.php | 9 ++++++++- application/controllers/ServiceController.php | 3 ++- library/Businessprocess/BpConfig.php | 5 ++++- library/Businessprocess/Common/EnumList.php | 7 ++++++- library/Businessprocess/HostNode.php | 10 ++++++++-- library/Businessprocess/ServiceNode.php | 9 +++++++-- library/Businessprocess/Web/Component/Dashboard.php | 9 ++++++++- 10 files changed, 58 insertions(+), 12 deletions(-) diff --git a/application/clicommands/ProcessCommand.php b/application/clicommands/ProcessCommand.php index 0baef23..541d97f 100644 --- a/application/clicommands/ProcessCommand.php +++ b/application/clicommands/ProcessCommand.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Businessprocess\Clicommands; use Exception; use Icinga\Application\Logger; +use Icinga\Application\Modules\Module; use Icinga\Cli\Command; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\BpNode; @@ -134,7 +135,9 @@ class ProcessCommand extends Command /** @var BpNode $node */ try { $node = $bp->getNode($nodeName); - if ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { + if (Module::exists('icingadb') && + ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + ) { IcingaDbState::apply($bp); } else { MonitoringState::apply($bp); diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index fea347e..c03f8e8 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Controllers; +use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Icingadb\Model\Host; @@ -16,7 +17,7 @@ class HostController extends Controller { $icingadb = $this->params->shift('icingadb'); - if ($icingadb) { + if ($icingadb && Module::exists('icingadb')) { $hostName = $this->params->shift('host'); $query = Host::on($this->getDb()); diff --git a/application/controllers/NodeController.php b/application/controllers/NodeController.php index ec5e699..4aee8f4 100644 --- a/application/controllers/NodeController.php +++ b/application/controllers/NodeController.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Controllers; +use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Renderer\Breadcrumb; use Icinga\Module\Businessprocess\Renderer\TileRenderer; @@ -83,7 +84,14 @@ class NodeController extends Controller if (empty($parents)) { continue; } - if ($config->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { + + if (! Module::exists('icingadb')) { + $config->getMetadata()->set('Backend', null); + } + + if (Module::exists('icingadb') && + ($config->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + ) { IcingaDbState::apply($config); } else { MonitoringState::apply($config); diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index 6bf4417..97225d5 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Controllers; +use Icinga\Application\Modules\Module; use Icinga\Date\DateFormatter; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\BpNode; @@ -83,7 +84,13 @@ class ProcessController extends Controller $bp = $this->loadModifiedBpConfig(); $node = $this->getNode($bp); - if ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { + if (! Module::exists('icingadb')) { + $bp->getMetadata()->set('Backend', null); + } + + if (Module::exists('icingadb') && + ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + ) { IcingaDbState::apply($bp); } else { MonitoringState::apply($bp); diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 9cee847..33b4d52 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Controllers; +use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Icingadb\Model\Service; @@ -16,7 +17,7 @@ class ServiceController extends Controller { $icingadb = $this->params->shift('icingadb'); - if ($icingadb) { + if ($icingadb && Module::exists('icingadb')) { $hostName = $this->params->shift('host'); $serviceName = $this->params->shift('service'); diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index 8436483..c4ea04d 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -3,7 +3,6 @@ namespace Icinga\Module\Businessprocess; use Exception; -use Icinga\Application\Config; use Icinga\Application\Modules\Module; use Icinga\Exception\IcingaException; use Icinga\Exception\NotFoundError; @@ -295,6 +294,10 @@ class BpConfig public function getBackend() { if ($this->backend === null) { + if (! Module::exists('icingadb')) { + $this->getMetadata()->set('Backend', null); + } + if ($this->getBackendName() === '_icingadb' || (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) ) { diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index 508a662..8dcc367 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Common; +use Icinga\Application\Modules\Module; use Icinga\Data\Filter\Filter; use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\MonitoringRestrictions; @@ -134,6 +135,10 @@ trait EnumList protected function useIcingaDbBackend() { - return $this->backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend(); + if (Module::exists('icingadb')) { + return $this->backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend(); + } + + return false; } } diff --git a/library/Businessprocess/HostNode.php b/library/Businessprocess/HostNode.php index f272f33..b0b9a83 100644 --- a/library/Businessprocess/HostNode.php +++ b/library/Businessprocess/HostNode.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess; +use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Web\Url; use ipl\Html\Html; @@ -57,9 +58,14 @@ class HostNode extends MonitoredNode 'host' => $this->getHostname(), ); - if ($this->getBpConfig()->hasBackendName() || IcingadbSupport::useIcingaDbAsBackend()) { + if ($this->getBpConfig()->hasBackendName() || + (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) + ) { $backendName = $this->getBpConfig()->getBackendName(); - if ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { + + if (Module::exists('icingadb') && + ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + ) { $params['icingadb'] = 1; } else { $params['backend'] = $this->getBpConfig()->getBackendName(); diff --git a/library/Businessprocess/ServiceNode.php b/library/Businessprocess/ServiceNode.php index 6476281..47e8a1a 100644 --- a/library/Businessprocess/ServiceNode.php +++ b/library/Businessprocess/ServiceNode.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess; +use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Web\Url; @@ -76,10 +77,14 @@ class ServiceNode extends MonitoredNode 'service' => $this->getServiceDescription() ); - if ($this->getBpConfig()->hasBackendName() || IcingadbSupport::useIcingaDbAsBackend()) { + if ($this->getBpConfig()->hasBackendName() || + (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) + ) { $backendName = $this->getBpConfig()->getBackendName(); - if ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { + if (Module::exists('icingadb') && + ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + ) { $params['icingadb'] = 1; } else { $params['backend'] = $this->getBpConfig()->getBackendName(); diff --git a/library/Businessprocess/Web/Component/Dashboard.php b/library/Businessprocess/Web/Component/Dashboard.php index 0f1acb0..f8880b2 100644 --- a/library/Businessprocess/Web/Component/Dashboard.php +++ b/library/Businessprocess/Web/Component/Dashboard.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Businessprocess\Web\Component; +use Icinga\Application\Modules\Module; use Icinga\Authentication\Auth; use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\State\IcingaDbState; @@ -93,7 +94,13 @@ class Dashboard extends BaseHtmlElement } $bp = $storage->loadProcess($name); - if ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) { + if (! Module::exists('icingadb')) { + $bp->getMetadata()->set('Backend', null); + } + + if (Module::exists('icingadb') && + ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + ) { IcingaDbState::apply($bp); } else { MonitoringState::apply($bp); From 39047914387a384cab822136173b5fdbc7ec2347 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 18 Nov 2021 12:32:46 +0100 Subject: [PATCH 19/40] Cleanup the code Remove unnecessary comments and unused namespaces. --- application/forms/AddNodeForm.php | 41 ------------------------------- application/forms/ProcessForm.php | 1 - 2 files changed, 42 deletions(-) diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index e2125dc..9e37094 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -487,47 +487,6 @@ class AddNodeForm extends QuickForm return $serviceStateList; } -// protected function enumHostListByFilter($filter) -// { -// $names = $this->backend -// ->select() -// ->from('hostStatus', ['hostname' => 'host_name']) -// ->applyFilter(Filter::fromQueryString($filter)) -// ->applyFilter($this->getRestriction('monitoring/filter/objects')) -// ->order('host_name') -// ->getQuery() -// ->fetchColumn(); -// -// // fetchPairs doesn't seem to work when using the same column with -// // different aliases twice -// $res = array(); -// $suffix = ';Hoststatus'; -// foreach ($names as $name) { -// $res[$name . $suffix] = $name; -// } -// -// return $res; -// } -// -// protected function enumServiceListByFilter($filter) -// { -// $objects = $this->backend -// ->select() -// ->from('serviceStatus', ['host' => 'host_name', 'service' => 'service_description']) -// ->applyFilter(Filter::fromQueryString($filter)) -// ->applyFilter($this->getRestriction('monitoring/filter/objects')) -// ->order('service_description') -// ->getQuery() -// ->fetchAll(); -// -// $services = array(); -// foreach ($objects as $object) { -// $services[$object->host . ';' . $object->service] = $object->host . ':' . $object->service; -// } -// -// return $services; -// } - protected function hasProcesses() { return count($this->enumProcesses()) > 0; diff --git a/application/forms/ProcessForm.php b/application/forms/ProcessForm.php index da68017..de484da 100644 --- a/application/forms/ProcessForm.php +++ b/application/forms/ProcessForm.php @@ -5,7 +5,6 @@ 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\IcingaDbObject; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Monitoring\Backend\MonitoringBackend; From 407d4f98dbd8c5a0b113cdf45ca9ea2d77290b03 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 18 Nov 2021 12:37:09 +0100 Subject: [PATCH 20/40] Remove enumHostForServiceList, enumHostList, enumServiceList from EditNodeForm. These methods are present in the trait EnumList and EditNodeForm is already using that trait. --- application/forms/EditNodeForm.php | 56 ------------------------------ 1 file changed, 56 deletions(-) diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index 0e14d70..2f886fc 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -348,43 +348,6 @@ class EditNodeForm extends QuickForm return $this; } - protected function enumHostForServiceList() - { - $names = $this->backend - ->select() - ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('host_name') - ->getQuery() - ->fetchColumn(); - - // fetchPairs doesn't seem to work when using the same column with - // different aliases twice - - return array_combine((array) $names, (array) $names); - } - - protected function enumHostList() - { - $names = $this->backend - ->select() - ->from('hostStatus', ['hostname' => 'host_name']) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('host_name') - ->getQuery() - ->fetchColumn(); - - // fetchPairs doesn't seem to work when using the same column with - // different aliases twice - $res = array(); - $suffix = ';Hoststatus'; - foreach ($names as $name) { - $res[$name . $suffix] = $name; - } - - return $res; - } - protected function enumHostStateList() { $hostStateList = [ @@ -396,25 +359,6 @@ class EditNodeForm extends QuickForm return $hostStateList; } - protected function enumServiceList($host) - { - $names = $this->backend - ->select() - ->from('serviceStatus', ['service' => 'service_description']) - ->where('host_name', $host) - ->applyFilter($this->getRestriction('monitoring/filter/objects')) - ->order('service_description') - ->getQuery() - ->fetchColumn(); - - $services = array(); - foreach ($names as $name) { - $services[$host . ';' . $name] = $name; - } - - return $services; - } - protected function enumServiceStateList() { $serviceStateList = [ From 1afa7668eb9118500b874a5be9872af4255ed703 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 18 Nov 2021 12:40:32 +0100 Subject: [PATCH 21/40] Move enumServiceStateList and enumHostStateList to trait EnumList. The above methods in AddNodeForm and EditNodeForm is moved to the trait EnumList. --- application/forms/AddNodeForm.php | 24 --------------------- application/forms/EditNodeForm.php | 24 --------------------- library/Businessprocess/Common/EnumList.php | 24 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 48 deletions(-) diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index 9e37094..de81516 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -463,30 +463,6 @@ class AddNodeForm extends QuickForm return $this; } - protected function enumHostStateList() - { - $hostStateList = [ - 0 => $this->translate('UP'), - 1 => $this->translate('DOWN'), - 99 => $this->translate('PENDING') - ]; - - return $hostStateList; - } - - protected function enumServiceStateList() - { - $serviceStateList = [ - 0 => $this->translate('OK'), - 1 => $this->translate('WARNING'), - 2 => $this->translate('CRITICAL'), - 3 => $this->translate('UNKNOWN'), - 99 => $this->translate('PENDING'), - ]; - - return $serviceStateList; - } - protected function hasProcesses() { return count($this->enumProcesses()) > 0; diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index 2f886fc..fd77446 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -348,30 +348,6 @@ class EditNodeForm extends QuickForm return $this; } - protected function enumHostStateList() - { - $hostStateList = [ - 0 => $this->translate('UP'), - 1 => $this->translate('DOWN'), - 99 => $this->translate('PENDING') - ]; - - return $hostStateList; - } - - protected function enumServiceStateList() - { - $serviceStateList = [ - 0 => $this->translate('OK'), - 1 => $this->translate('WARNING'), - 2 => $this->translate('CRITICAL'), - 3 => $this->translate('UNKNOWN'), - 99 => $this->translate('PENDING'), - ]; - - return $serviceStateList; - } - protected function hasProcesses() { return count($this->enumProcesses()) > 0; diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index 8dcc367..5a2a4ae 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -133,6 +133,30 @@ trait EnumList return $services; } + protected function enumHostStateList() + { + $hostStateList = [ + 0 => $this->translate('UP'), + 1 => $this->translate('DOWN'), + 99 => $this->translate('PENDING') + ]; + + return $hostStateList; + } + + protected function enumServiceStateList() + { + $serviceStateList = [ + 0 => $this->translate('OK'), + 1 => $this->translate('WARNING'), + 2 => $this->translate('CRITICAL'), + 3 => $this->translate('UNKNOWN'), + 99 => $this->translate('PENDING'), + ]; + + return $serviceStateList; + } + protected function useIcingaDbBackend() { if (Module::exists('icingadb')) { From 8f68ffe2a845c5ebda282b78786a5b4ff3a7fd6e Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 10 Feb 2022 12:50:28 +0100 Subject: [PATCH 22/40] Drop library/Businessprocess/Common/IcingadbDatabase.php in favor of Icinga\Module\Icingadb\Common\Database --- .../Common/IcingadbDatabase.php | 42 ------------------- 1 file changed, 42 deletions(-) delete mode 100644 library/Businessprocess/Common/IcingadbDatabase.php diff --git a/library/Businessprocess/Common/IcingadbDatabase.php b/library/Businessprocess/Common/IcingadbDatabase.php deleted file mode 100644 index d35d2c8..0000000 --- a/library/Businessprocess/Common/IcingadbDatabase.php +++ /dev/null @@ -1,42 +0,0 @@ -db === null) { - $config = new SqlConfig(ResourceFactory::getResourceConfig( - AppConfig::module('icingadb')->get('icingadb', 'resource', 'icingadb') - )); - - $config->options = [ - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, - PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION SQL_MODE='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE" - . ",ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'" - ]; - - $this->db = new Connection($config); - } - - return $this->db; - } -} From df3f363f1b4164f412d6f81853fb4aca228434c6 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 10 Feb 2022 16:03:09 +0100 Subject: [PATCH 23/40] Add static fetchDb() method to library/Businessprocess/IcingaDbObject.php Use this static method to get backend in IcingaDbState. --- library/Businessprocess/IcingaDbObject.php | 8 +++++++- library/Businessprocess/State/IcingaDbState.php | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/library/Businessprocess/IcingaDbObject.php b/library/Businessprocess/IcingaDbObject.php index 1ee6e3a..2fc9347 100644 --- a/library/Businessprocess/IcingaDbObject.php +++ b/library/Businessprocess/IcingaDbObject.php @@ -2,8 +2,8 @@ namespace Icinga\Module\Businessprocess; -use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Icingadb\Common\Auth; +use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; use ipl\Web\Filter\QueryString; @@ -84,4 +84,10 @@ class IcingaDbObject return $object; } + + public static function fetchDb() + { + $object = new self; + return $object->getDb(); + } } diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 440f882..ec4234c 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -25,7 +25,7 @@ class IcingaDbState public function __construct(BpConfig $config) { $this->config = $config; - $this->backend = $config->getBackend(); + $this->backend = IcingaDbObject::fetchDb(); } public static function apply(BpConfig $config) From ed52d511312a2b6b05a15a1a74555aadcda5e0cc Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 10 Feb 2022 16:06:33 +0100 Subject: [PATCH 24/40] Remove param 'icingadb' and set param 'backend' as '_icingadb' when icingadb is used --- library/Businessprocess/HostNode.php | 2 +- library/Businessprocess/ServiceNode.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Businessprocess/HostNode.php b/library/Businessprocess/HostNode.php index b0b9a83..eccf1aa 100644 --- a/library/Businessprocess/HostNode.php +++ b/library/Businessprocess/HostNode.php @@ -66,7 +66,7 @@ class HostNode extends MonitoredNode if (Module::exists('icingadb') && ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) ) { - $params['icingadb'] = 1; + $params['backend'] = '_icingadb'; } else { $params['backend'] = $this->getBpConfig()->getBackendName(); } diff --git a/library/Businessprocess/ServiceNode.php b/library/Businessprocess/ServiceNode.php index 47e8a1a..a0972a6 100644 --- a/library/Businessprocess/ServiceNode.php +++ b/library/Businessprocess/ServiceNode.php @@ -85,7 +85,7 @@ class ServiceNode extends MonitoredNode if (Module::exists('icingadb') && ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) ) { - $params['icingadb'] = 1; + $params['backend'] = '_icingadb'; } else { $params['backend'] = $this->getBpConfig()->getBackendName(); } From 549ad9212242be9cfc3170f8b220677a64744325 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 10 Feb 2022 16:13:28 +0100 Subject: [PATCH 25/40] Do not set Backend in Metadata to null in case icingadb module doesnot exist in BpConfig In case icingadb module is not present Backend in Metadata is set to null which is incorrect. Also use static fetchDb() from IcingaDbObject class in case icingadb backend is being used. --- library/Businessprocess/BpConfig.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index c4ea04d..6ef03df 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -6,17 +6,15 @@ use Exception; use Icinga\Application\Modules\Module; use Icinga\Exception\IcingaException; use Icinga\Exception\NotFoundError; -use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\Exception\NestingError; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Storage\LegacyStorage; +use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Monitoring\Backend\MonitoringBackend; class BpConfig { - use IcingadbDatabase; - const SOFT_STATE = 0; const HARD_STATE = 1; @@ -294,18 +292,22 @@ class BpConfig public function getBackend() { if ($this->backend === null) { - if (! Module::exists('icingadb')) { - $this->getMetadata()->set('Backend', null); - } - if ($this->getBackendName() === '_icingadb' || (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) ) { - $this->backend = $this->getDb(); + if (! Module::exists('icingadb')) { + throw new Exception('Icingadb module is not enabled.'); + } + + $this->backend = IcingaDbObject::fetchDb(); } else { - $this->backend = MonitoringBackend::instance( - $this->getBackendName() - ); + if (! Module::exists('monitoring') && Module::exists('icingadb')) { + $this->backend = IcingaDbObject::fetchDb(); + } else { + $this->backend = MonitoringBackend::instance( + $this->getBackendName() + ); + } } } From 6552ae200478c4798aecbffbb9a81ac0a0429c28 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 10 Feb 2022 16:17:49 +0100 Subject: [PATCH 26/40] Use trait Icinga\Module\Icingadb\Common\Database as icingadb backend object --- application/forms/AddNodeForm.php | 2 +- application/forms/DeleteNodeForm.php | 2 +- application/forms/EditNodeForm.php | 2 +- application/forms/ProcessForm.php | 2 +- library/Businessprocess/State/IcingaDbState.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index de81516..40f533c 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -4,7 +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\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Businessprocess\Common\EnumList; use Icinga\Module\Businessprocess\ImportedNode; use Icinga\Module\Businessprocess\Modification\ProcessChanges; diff --git a/application/forms/DeleteNodeForm.php b/application/forms/DeleteNodeForm.php index 3d41bf4..f16154f 100644 --- a/application/forms/DeleteNodeForm.php +++ b/application/forms/DeleteNodeForm.php @@ -4,10 +4,10 @@ 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; +use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Session\SessionNamespace; diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index fd77446..e476b5e 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -4,12 +4,12 @@ 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\Common\EnumList; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Node; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator; +use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Session\SessionNamespace; diff --git a/application/forms/ProcessForm.php b/application/forms/ProcessForm.php index de484da..8559aaa 100644 --- a/application/forms/ProcessForm.php +++ b/application/forms/ProcessForm.php @@ -4,9 +4,9 @@ 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\Web\Form\QuickForm; +use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Notification; use Icinga\Web\Session\SessionNamespace; diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index ec4234c..55d64d1 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -5,10 +5,10 @@ namespace Icinga\Module\Businessprocess\State; use Exception; use Icinga\Application\Benchmark; use Icinga\Module\Businessprocess\BpConfig; -use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\ServiceNode; use Icinga\Module\Icingadb\Common\Auth; +use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; From 6ebaedccf44360f629e6feb29c2946e100689f3a Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 10 Feb 2022 16:25:12 +0100 Subject: [PATCH 27/40] Add moduleInit() method to HostController and ServiceController to check if icingadb is used. Use moduleInit() to check whether icingadb to be used. Also use ipl\Stdlib\Filter with $query->filter() instead $query->getSelectBase()->where() in case of icingadb. --- application/controllers/HostController.php | 27 ++++++++++++------ application/controllers/ServiceController.php | 28 +++++++++++++++---- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index c03f8e8..3ec0593 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -3,28 +3,39 @@ namespace Icinga\Module\Businessprocess\Controllers; use Icinga\Application\Modules\Module; -use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\IcingaDbObject; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Monitoring\Controller; use Icinga\Web\Url; +use ipl\Stdlib\Filter; class HostController extends Controller { - use IcingadbDatabase; + protected $isIcingadb; + + protected $explicitIcingadb; + + protected function moduleInit() + { + $this->isIcingadb = $this->params->shift('backend') === '_icingadb'; + $this->explicitIcingadb = Module::exists('icingadb') + && IcingadbSupport::useIcingaDbAsBackend(); + + if (! $this->isIcingadb) { + parent::moduleInit(); + } + } public function showAction() { - $icingadb = $this->params->shift('icingadb'); - - if ($icingadb && Module::exists('icingadb')) { + if ($this->isIcingadb || $this->explicitIcingadb) { $hostName = $this->params->shift('host'); - $query = Host::on($this->getDb()); + $query = Host::on(IcingaDbObject::fetchDb()); IcingaDbObject::applyIcingaDbRestrictions($query); - $query->getSelectBase() - ->where(['host.name = ?' => $hostName]); + $query->filter(Filter::equal('host.name', $hostName)); $host = $query->first(); diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 33b4d52..61a8fcd 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -2,16 +2,31 @@ namespace Icinga\Module\Businessprocess\Controllers; +use Dompdf\Exception; use Icinga\Application\Modules\Module; -use Icinga\Module\Businessprocess\Common\IcingadbDatabase; use Icinga\Module\Businessprocess\IcingaDbObject; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Icingadb\Model\Service; use Icinga\Module\Monitoring\Controller; use Icinga\Web\Url; +use ipl\Stdlib\Filter; class ServiceController extends Controller { - use IcingadbDatabase; + protected $isIcingadb; + + protected $explicitIcingadb; + + protected function moduleInit() + { + $this->isIcingadb = $this->params->shift('backend') === '_icingadb'; + $this->explicitIcingadb = Module::exists('icingadb') + && IcingadbSupport::useIcingaDbAsBackend(); + + if (! $this->isIcingadb) { + parent::moduleInit(); + } + } public function showAction() { @@ -21,12 +36,13 @@ class ServiceController extends Controller $hostName = $this->params->shift('host'); $serviceName = $this->params->shift('service'); - $query = Service::on($this->getDb())->with('host'); + $query = Service::on(IcingaDbObject::fetchDb())->with('host'); IcingaDbObject::applyIcingaDbRestrictions($query); - $query->getSelectBase() - ->where(['service.name = ?' => $serviceName]) - ->where(['service_host.name = ?' => $hostName]); + $query->filter(Filter::all( + Filter::equal('service.name', $serviceName), + Filter::equal('host.name', $hostName) + )); $service = $query->first(); From a5165ef4768123494a45a75e7c9536f72a51084d Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 10 Feb 2022 16:26:40 +0100 Subject: [PATCH 28/40] Do not set Backend in Metadata to null in case icingadb module doesnot exist. --- application/controllers/NodeController.php | 4 ---- application/controllers/ProcessController.php | 4 ---- library/Businessprocess/Web/Component/Dashboard.php | 3 --- 3 files changed, 11 deletions(-) diff --git a/application/controllers/NodeController.php b/application/controllers/NodeController.php index 4aee8f4..0613109 100644 --- a/application/controllers/NodeController.php +++ b/application/controllers/NodeController.php @@ -85,10 +85,6 @@ class NodeController extends Controller continue; } - if (! Module::exists('icingadb')) { - $config->getMetadata()->set('Backend', null); - } - if (Module::exists('icingadb') && ($config->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) ) { diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index 97225d5..272c671 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -84,10 +84,6 @@ class ProcessController extends Controller $bp = $this->loadModifiedBpConfig(); $node = $this->getNode($bp); - if (! Module::exists('icingadb')) { - $bp->getMetadata()->set('Backend', null); - } - if (Module::exists('icingadb') && ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) ) { diff --git a/library/Businessprocess/Web/Component/Dashboard.php b/library/Businessprocess/Web/Component/Dashboard.php index f8880b2..712c049 100644 --- a/library/Businessprocess/Web/Component/Dashboard.php +++ b/library/Businessprocess/Web/Component/Dashboard.php @@ -94,9 +94,6 @@ class Dashboard extends BaseHtmlElement } $bp = $storage->loadProcess($name); - if (! Module::exists('icingadb')) { - $bp->getMetadata()->set('Backend', null); - } if (Module::exists('icingadb') && ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) From 1d12701293c44f73e137294e1ead4724cdfa771f Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Mon, 14 Feb 2022 11:29:45 +0100 Subject: [PATCH 29/40] Remove IcingaDb from list of available backends. By default use monitoring backend if icingadb is preferred default backend is set to icingadb. --- library/Businessprocess/Web/Form/BpConfigBaseForm.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/library/Businessprocess/Web/Form/BpConfigBaseForm.php b/library/Businessprocess/Web/Form/BpConfigBaseForm.php index 92f09ea..ddfc851 100644 --- a/library/Businessprocess/Web/Form/BpConfigBaseForm.php +++ b/library/Businessprocess/Web/Form/BpConfigBaseForm.php @@ -25,10 +25,6 @@ abstract class BpConfigBaseForm extends QuickForm $keys = array_combine($keys, $keys); } - if ($moduleManager->hasEnabled('icingadb')) { - $keys['_icingadb'] = 'Icinga DB'; - } - return $keys; } From 605834e7c45ae1b1ee928e41bdb45dafc639af74 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 10:20:58 +0100 Subject: [PATCH 30/40] trait cannot be a type for property Replace the type of properties with type Icinga\Module\Icingadb\Common\Database with type ipl\Sql\Connection. --- application/forms/AddNodeForm.php | 6 +++--- application/forms/DeleteNodeForm.php | 6 +++--- application/forms/EditNodeForm.php | 6 +++--- application/forms/ProcessForm.php | 6 +++--- library/Businessprocess/BpConfig.php | 4 ++-- library/Businessprocess/IcingaDbObject.php | 3 ++- library/Businessprocess/State/IcingaDbState.php | 4 ++-- 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index 40f533c..063914c 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -4,7 +4,6 @@ namespace Icinga\Module\Businessprocess\Forms; use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BpConfig; -use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Businessprocess\Common\EnumList; use Icinga\Module\Businessprocess\ImportedNode; use Icinga\Module\Businessprocess\Modification\ProcessChanges; @@ -13,12 +12,13 @@ use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Session\SessionNamespace; +use ipl\Sql\Connection as IcingaDbConnection; class AddNodeForm extends QuickForm { use EnumList; - /** @var MonitoringBackend|IcingadbDatabase */ + /** @var MonitoringBackend|IcingaDbConnection*/ protected $backend; /** @var Storage */ @@ -404,7 +404,7 @@ class AddNodeForm extends QuickForm } /** - * @param MonitoringBackend|IcingadbDatabase $backend + * @param MonitoringBackend|IcingaDbConnection $backend * @return $this */ public function setBackend($backend) diff --git a/application/forms/DeleteNodeForm.php b/application/forms/DeleteNodeForm.php index f16154f..dada9d3 100644 --- a/application/forms/DeleteNodeForm.php +++ b/application/forms/DeleteNodeForm.php @@ -7,13 +7,13 @@ use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Node; use Icinga\Module\Businessprocess\Web\Form\QuickForm; -use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Session\SessionNamespace; +use ipl\Sql\Connection as IcingaDbConnection; class DeleteNodeForm extends QuickForm { - /** @var MonitoringBackend|IcingadbDatabase */ + /** @var MonitoringBackend|IcingaDbConnection */ protected $backend; /** @var BpConfig */ @@ -80,7 +80,7 @@ class DeleteNodeForm extends QuickForm } /** - * @param MonitoringBackend|IcingadbDatabase $backend + * @param MonitoringBackend|IcingaDbConnection $backend * @return $this */ public function setBackend($backend) diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index e476b5e..6f47ba9 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -9,15 +9,15 @@ use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Node; use Icinga\Module\Businessprocess\Web\Form\QuickForm; use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator; -use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Session\SessionNamespace; +use ipl\Sql\Connection as IcingaDbConnection; class EditNodeForm extends QuickForm { use EnumList; - /** @var MonitoringBackend|IcingadbDatabase */ + /** @var MonitoringBackend|IcingaDbConnection */ protected $backend; /** @var string $backendName */ @@ -299,7 +299,7 @@ class EditNodeForm extends QuickForm } /** - * @param MonitoringBackend|IcingadbDatabase $backend + * @param MonitoringBackend|IcingaDbConnection $backend * @return $this */ public function setBackend($backend) diff --git a/application/forms/ProcessForm.php b/application/forms/ProcessForm.php index 8559aaa..7cc81c2 100644 --- a/application/forms/ProcessForm.php +++ b/application/forms/ProcessForm.php @@ -6,14 +6,14 @@ use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\Web\Form\QuickForm; -use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Monitoring\Backend\MonitoringBackend; use Icinga\Web\Notification; use Icinga\Web\Session\SessionNamespace; +use ipl\Sql\Connection as IcingaDbConnection; class ProcessForm extends QuickForm { - /** @var MonitoringBackend|IcingadbDatabase */ + /** @var MonitoringBackend|IcingaDbConnection */ protected $backend; /** @var BpConfig */ @@ -111,7 +111,7 @@ class ProcessForm extends QuickForm } /** - * @param MonitoringBackend|IcingadbDatabase $backend + * @param MonitoringBackend|IcingaDbConnection $backend * @return $this */ public function setBackend($backend) diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index 6ef03df..e9089de 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -10,8 +10,8 @@ use Icinga\Module\Businessprocess\Exception\NestingError; use Icinga\Module\Businessprocess\Modification\ProcessChanges; use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Storage\LegacyStorage; -use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Monitoring\Backend\MonitoringBackend; +use ipl\Sql\Connection as IcingaDbConnection; class BpConfig { @@ -29,7 +29,7 @@ class BpConfig /** * Backend to retrieve states from * - * @var MonitoringBackend|IcingadbDatabase + * @var MonitoringBackend|IcingaDbConnection */ protected $backend; diff --git a/library/Businessprocess/IcingaDbObject.php b/library/Businessprocess/IcingaDbObject.php index 2fc9347..cad459f 100644 --- a/library/Businessprocess/IcingaDbObject.php +++ b/library/Businessprocess/IcingaDbObject.php @@ -6,6 +6,7 @@ use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; +use ipl\Sql\Connection as IcingaDbConnection; use ipl\Web\Filter\QueryString; class IcingaDbObject @@ -17,7 +18,7 @@ class IcingaDbObject /** @var BpConfig */ protected $config; - /** @var IcingadbDatabase */ + /** @var IcingaDbConnection */ protected $conn; public function __construct() diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 55d64d1..cd931ec 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -8,9 +8,9 @@ use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\ServiceNode; use Icinga\Module\Icingadb\Common\Auth; -use Icinga\Module\Icingadb\Common\Database as IcingadbDatabase; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; +use ipl\Sql\Connection as IcingaDbConnection; class IcingaDbState { @@ -19,7 +19,7 @@ class IcingaDbState /** @var BpConfig */ protected $config; - /** @var IcingadbDatabase */ + /** @var IcingaDbConnection */ protected $backend; public function __construct(BpConfig $config) From daf0384e66f3835b51a8360fec824f1e9211b81f Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 10:24:58 +0100 Subject: [PATCH 31/40] Use $isIcingadbPreferred to check if the businessproces preders icingadb as default backend. --- application/controllers/HostController.php | 17 ++++++++------ application/controllers/ServiceController.php | 22 +++++++++---------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 3ec0593..6b306b3 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -12,24 +12,27 @@ use ipl\Stdlib\Filter; class HostController extends Controller { - protected $isIcingadb; - - protected $explicitIcingadb; + /** + * True if business process prefers to use icingadb as backend for it's nodes + * + * @var bool + */ + protected $isIcingadbPreferred; protected function moduleInit() { - $this->isIcingadb = $this->params->shift('backend') === '_icingadb'; - $this->explicitIcingadb = Module::exists('icingadb') + $this->isIcingadbPreferred = Module::exists('icingadb') + && ! $this->params->has('backend') && IcingadbSupport::useIcingaDbAsBackend(); - if (! $this->isIcingadb) { + if (! $this->isIcingadbPreferred) { parent::moduleInit(); } } public function showAction() { - if ($this->isIcingadb || $this->explicitIcingadb) { + if ($this->isIcingadbPreferred) { $hostName = $this->params->shift('host'); $query = Host::on(IcingaDbObject::fetchDb()); diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 61a8fcd..29d40ce 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -2,7 +2,6 @@ namespace Icinga\Module\Businessprocess\Controllers; -use Dompdf\Exception; use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; @@ -13,30 +12,31 @@ use ipl\Stdlib\Filter; class ServiceController extends Controller { - protected $isIcingadb; - - protected $explicitIcingadb; + /** + * True if business process prefers to use icingadb as backend for it's nodes + * + * @var bool + */ + protected $isIcingadbPreferred; protected function moduleInit() { - $this->isIcingadb = $this->params->shift('backend') === '_icingadb'; - $this->explicitIcingadb = Module::exists('icingadb') + $this->isIcingadbPreferred = Module::exists('icingadb') + && ! $this->params->has('backend') && IcingadbSupport::useIcingaDbAsBackend(); - if (! $this->isIcingadb) { + if (! $this->isIcingadbPreferred) { parent::moduleInit(); } } public function showAction() { - $icingadb = $this->params->shift('icingadb'); - - if ($icingadb && Module::exists('icingadb')) { + if ($this->isIcingadbPreferred) { $hostName = $this->params->shift('host'); $serviceName = $this->params->shift('service'); - $query = Service::on(IcingaDbObject::fetchDb())->with('host'); + $query = Service::on(IcingaDbObject::fetchDb()); IcingaDbObject::applyIcingaDbRestrictions($query); $query->filter(Filter::all( From d4bd45233bef709d31f4eb922957038a4768616d Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 10:27:06 +0100 Subject: [PATCH 32/40] Remove unnecessary checks to check if backend name is '_icingadb'. Instead use (! $bp->hasBackendName()) check with 'And' condition. --- application/clicommands/ProcessCommand.php | 4 ++-- application/controllers/NodeController.php | 2 +- application/controllers/ProcessController.php | 2 +- library/Businessprocess/BpConfig.php | 19 +++++-------------- library/Businessprocess/Common/EnumList.php | 2 +- library/Businessprocess/HostNode.php | 17 ++--------------- library/Businessprocess/ServiceNode.php | 16 ++-------------- .../Web/Component/Dashboard.php | 2 +- 8 files changed, 15 insertions(+), 49 deletions(-) diff --git a/application/clicommands/ProcessCommand.php b/application/clicommands/ProcessCommand.php index 541d97f..3b470b8 100644 --- a/application/clicommands/ProcessCommand.php +++ b/application/clicommands/ProcessCommand.php @@ -135,8 +135,8 @@ class ProcessCommand extends Command /** @var BpNode $node */ try { $node = $bp->getNode($nodeName); - if (Module::exists('icingadb') && - ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + if (Module::exists('icingadb') + && (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) ) { IcingaDbState::apply($bp); } else { diff --git a/application/controllers/NodeController.php b/application/controllers/NodeController.php index 0613109..8addc07 100644 --- a/application/controllers/NodeController.php +++ b/application/controllers/NodeController.php @@ -86,7 +86,7 @@ class NodeController extends Controller } if (Module::exists('icingadb') && - ($config->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + (! $config->getBackendName() && IcingadbSupport::useIcingaDbAsBackend()) ) { IcingaDbState::apply($config); } else { diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index 272c671..475826f 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -85,7 +85,7 @@ class ProcessController extends Controller $node = $this->getNode($bp); if (Module::exists('icingadb') && - ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) ) { IcingaDbState::apply($bp); } else { diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index e9089de..1e3f119 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -292,22 +292,13 @@ class BpConfig public function getBackend() { if ($this->backend === null) { - if ($this->getBackendName() === '_icingadb' || - (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) - ) { - if (! Module::exists('icingadb')) { - throw new Exception('Icingadb module is not enabled.'); - } - + if (Module::exists('icingadb') + && (! $this->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())) { $this->backend = IcingaDbObject::fetchDb(); } else { - if (! Module::exists('monitoring') && Module::exists('icingadb')) { - $this->backend = IcingaDbObject::fetchDb(); - } else { - $this->backend = MonitoringBackend::instance( - $this->getBackendName() - ); - } + $this->backend = MonitoringBackend::instance( + $this->getBackendName() + ); } } diff --git a/library/Businessprocess/Common/EnumList.php b/library/Businessprocess/Common/EnumList.php index 5a2a4ae..a1e5b56 100644 --- a/library/Businessprocess/Common/EnumList.php +++ b/library/Businessprocess/Common/EnumList.php @@ -160,7 +160,7 @@ trait EnumList protected function useIcingaDbBackend() { if (Module::exists('icingadb')) { - return $this->backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend(); + return ! $this->bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend(); } return false; diff --git a/library/Businessprocess/HostNode.php b/library/Businessprocess/HostNode.php index eccf1aa..b66f66f 100644 --- a/library/Businessprocess/HostNode.php +++ b/library/Businessprocess/HostNode.php @@ -2,10 +2,7 @@ namespace Icinga\Module\Businessprocess; -use Icinga\Application\Modules\Module; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Web\Url; -use ipl\Html\Html; class HostNode extends MonitoredNode { @@ -58,18 +55,8 @@ class HostNode extends MonitoredNode 'host' => $this->getHostname(), ); - if ($this->getBpConfig()->hasBackendName() || - (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) - ) { - $backendName = $this->getBpConfig()->getBackendName(); - - if (Module::exists('icingadb') && - ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) - ) { - $params['backend'] = '_icingadb'; - } else { - $params['backend'] = $this->getBpConfig()->getBackendName(); - } + if ($this->getBpConfig()->hasBackendName()) { + $params['backend'] = $this->getBpConfig()->getBackendName(); } return Url::fromPath('businessprocess/host/show', $params); diff --git a/library/Businessprocess/ServiceNode.php b/library/Businessprocess/ServiceNode.php index a0972a6..6160bce 100644 --- a/library/Businessprocess/ServiceNode.php +++ b/library/Businessprocess/ServiceNode.php @@ -2,8 +2,6 @@ namespace Icinga\Module\Businessprocess; -use Icinga\Application\Modules\Module; -use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; use Icinga\Module\Businessprocess\Web\Url; class ServiceNode extends MonitoredNode @@ -77,18 +75,8 @@ class ServiceNode extends MonitoredNode 'service' => $this->getServiceDescription() ); - if ($this->getBpConfig()->hasBackendName() || - (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) - ) { - $backendName = $this->getBpConfig()->getBackendName(); - - if (Module::exists('icingadb') && - ($backendName === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) - ) { - $params['backend'] = '_icingadb'; - } else { - $params['backend'] = $this->getBpConfig()->getBackendName(); - } + if ($this->getBpConfig()->hasBackendName()) { + $params['backend'] = $this->getBpConfig()->getBackendName(); } return Url::fromPath('businessprocess/service/show', $params); diff --git a/library/Businessprocess/Web/Component/Dashboard.php b/library/Businessprocess/Web/Component/Dashboard.php index 712c049..58506df 100644 --- a/library/Businessprocess/Web/Component/Dashboard.php +++ b/library/Businessprocess/Web/Component/Dashboard.php @@ -96,7 +96,7 @@ class Dashboard extends BaseHtmlElement $bp = $storage->loadProcess($name); if (Module::exists('icingadb') && - ($bp->getBackendName() === '_icingadb' || IcingadbSupport::useIcingaDbAsBackend()) + (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) ) { IcingaDbState::apply($bp); } else { From 6c5c38d42594ab45d6635935d2ee410115d3da6b Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 10:29:19 +0100 Subject: [PATCH 33/40] Provide native implementations for the hooks: icingadb/HostActions and icingadb/ServiceActions --- .../ProvidedHook/Icingadb/HostActions.php | 22 ++++++++++++++++ .../ProvidedHook/Icingadb/ServiceActions.php | 26 +++++++++++++++++++ run.php | 2 ++ 3 files changed, 50 insertions(+) create mode 100644 library/Businessprocess/ProvidedHook/Icingadb/HostActions.php create mode 100644 library/Businessprocess/ProvidedHook/Icingadb/ServiceActions.php diff --git a/library/Businessprocess/ProvidedHook/Icingadb/HostActions.php b/library/Businessprocess/ProvidedHook/Icingadb/HostActions.php new file mode 100644 index 0000000..27f4551 --- /dev/null +++ b/library/Businessprocess/ProvidedHook/Icingadb/HostActions.php @@ -0,0 +1,22 @@ +name . ';Hoststatus') + ) + ); + } +} diff --git a/library/Businessprocess/ProvidedHook/Icingadb/ServiceActions.php b/library/Businessprocess/ProvidedHook/Icingadb/ServiceActions.php new file mode 100644 index 0000000..24e6829 --- /dev/null +++ b/library/Businessprocess/ProvidedHook/Icingadb/ServiceActions.php @@ -0,0 +1,26 @@ +host->name, $service->name) + ) + ) + ) + ); + } +} diff --git a/run.php b/run.php index 3ca9f7e..3f05d06 100644 --- a/run.php +++ b/run.php @@ -2,5 +2,7 @@ $this->provideHook('monitoring/HostActions'); $this->provideHook('monitoring/ServiceActions'); +$this->provideHook('icingadb/HostActions'); +$this->provideHook('icingadb/ServiceActions'); $this->provideHook('icingadb/icingadbSupport'); //$this->provideHook('director/shipConfigFiles'); From 26a7a50d2c216d7eba0a08f9a3f3748f4e3b390e Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 10:30:03 +0100 Subject: [PATCH 34/40] Add icingadb module to module dependencies --- module.info | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/module.info b/module.info index 1f80156..7484c03 100644 --- a/module.info +++ b/module.info @@ -1,14 +1,14 @@ Name: Businessprocess Version: 2.3.1 -Depends: monitoring (>= 2.4.1), ipl (>= 0.1.1) +Depends: monitoring (>= 2.4.1), icingadb (>= 1.0.0), ipl (>= 0.5.0) Description: A Business Process viewer and modeler Provides a web-based process modeler for. It integrates as a module into Icinga Web 2 and provides a plugin check command for Icinga. Tile and tree views can be shown inline, as dashlets or fullscreen. All of those for whole processes or just parts of them. - Hooks into the monitoring module to show Business Impact for a specific host - or service and provides a Business Impact Simulation mode to visualize the - influence of a potential outage. + Hooks into the monitoring or icingadb module to show Business Impact for a + specific host or service and provides a Business Impact Simulation mode to + visualize the influence of a potential outage. Supports legacy BPaddon config files. From 5cb389b0524c985ed4b2621b1bd0a251feb134d3 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 10:30:52 +0100 Subject: [PATCH 35/40] Add icingadb to Requirements in doc/02-Installation.md. --- doc/02-Installation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/02-Installation.md b/doc/02-Installation.md index 3be623e..5f8c417 100644 --- a/doc/02-Installation.md +++ b/doc/02-Installation.md @@ -7,7 +7,7 @@ Requirements * Icinga Web 2 (>= 2.9) * PHP (>= 7.2) * Icinga Web 2 modules: - * The `monitoring` module needs to be configured and enabled. + * The `monitoring` or `icingadb` module needs to be configured and enabled. Installation from .tar.gz ------------------------- From ab4728ad5430273f3ea3e0790b30274c124f4d9f Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 12:38:48 +0100 Subject: [PATCH 36/40] Use ipl\Stdlib\Filter to filter the $queryHost and $queryService --- library/Businessprocess/State/IcingaDbState.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index cd931ec..a55ffc3 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -11,6 +11,7 @@ use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; use ipl\Sql\Connection as IcingaDbConnection; +use ipl\Stdlib\Filter; class IcingaDbState { @@ -67,8 +68,7 @@ class IcingaDbState $queryHost = Host::on($this->backend)->with('state'); IcingaDbObject::applyIcingaDbRestrictions($queryHost); - $queryHost->getSelectBase() - ->where(['host.name IN (?)' => $hosts]); + $queryHost->filter(Filter::equal('host.name', $hosts)); $hostObject = $queryHost->getModel()->getTableName(); @@ -82,8 +82,7 @@ class IcingaDbState 'host.state' ]); - $queryService->getSelectBase() - ->where(['service_host.name IN (?)' => $hosts]); + $queryService->filter(Filter::equal('host.name', $hosts)); IcingaDbObject::applyIcingaDbRestrictions($queryService); From 34626cbec5ca4ccdf4e6042d1538804cd0ccc7b1 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 12:44:24 +0100 Subject: [PATCH 37/40] $row->state->in_downtime and $row->state->is_acknowledged are used as boolens in if condition --- library/Businessprocess/State/IcingaDbState.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index a55ffc3..34d6667 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -134,10 +134,10 @@ class IcingaDbState if ($row->state->last_state_change !== null) { $node->setLastStateChange($row->state->last_state_change/1000); } - if ($row->state->in_downtime === 'y') { + if ($row->state->in_downtime) { $node->setDowntime(true); } - if ($row->state->is_acknowledged !== 'n') { + if ($row->state->is_acknowledged) { $node->setAck(true); } From 6c2e1cfa0a61b118dd04b44dbb133f499860940d Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 15 Feb 2022 12:46:23 +0100 Subject: [PATCH 38/40] Remove $backendName property from AddNodeForm and EditNodeForm --- application/forms/AddNodeForm.php | 4 ---- application/forms/EditNodeForm.php | 4 ---- library/Businessprocess/State/IcingaDbState.php | 5 ----- 3 files changed, 13 deletions(-) diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index 063914c..d2a0b56 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -37,9 +37,6 @@ class AddNodeForm extends QuickForm /** @var SessionNamespace */ protected $session; - /** @var string $backendName */ - protected $backendName; - public function setup() { $view = $this->getView(); @@ -430,7 +427,6 @@ class AddNodeForm extends QuickForm public function setProcess(BpConfig $process) { $this->bp = $process; - $this->backendName = $process->getBackendName(); $this->setBackend($process->getBackend()); return $this; } diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index 6f47ba9..b313343 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -20,9 +20,6 @@ class EditNodeForm extends QuickForm /** @var MonitoringBackend|IcingaDbConnection */ protected $backend; - /** @var string $backendName */ - protected $backendName; - /** @var BpConfig */ protected $bp; @@ -315,7 +312,6 @@ class EditNodeForm extends QuickForm public function setProcess(BpConfig $process) { $this->bp = $process; - $this->backendName = $process->getBackendName(); $this->setBackend($process->getBackend()); return $this; } diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 34d6667..f33d4a4 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -7,7 +7,6 @@ use Icinga\Application\Benchmark; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\IcingaDbObject; use Icinga\Module\Businessprocess\ServiceNode; -use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; use ipl\Sql\Connection as IcingaDbConnection; @@ -15,8 +14,6 @@ use ipl\Stdlib\Filter; class IcingaDbState { - use Auth; - /** @var BpConfig */ protected $config; @@ -72,8 +69,6 @@ class IcingaDbState $hostObject = $queryHost->getModel()->getTableName(); - IcingaDbObject::applyIcingaDbRestrictions($queryHost); - Benchmark::measure('Retrieved states for ' . $queryHost->count() . ' hosts in ' . $config->getName()); $queryService = Service::on($this->backend)->with([ From a7b86f956cfd7bd83000ee0119b5a037d8fadc71 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 17 Feb 2022 12:01:03 +0100 Subject: [PATCH 39/40] Do not show Backend select item when monitoring module is disabled --- application/forms/BpConfigForm.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/application/forms/BpConfigForm.php b/application/forms/BpConfigForm.php index 531cf0f..64e5350 100644 --- a/application/forms/BpConfigForm.php +++ b/application/forms/BpConfigForm.php @@ -51,16 +51,18 @@ class BpConfigForm extends BpConfigBaseForm 'rows' => 4, )); - $this->addElement('select', 'Backend', array( - 'label' => $this->translate('Backend'), - 'description' => $this->translate( - 'Icinga Web Monitoring Backend where current object states for' - . ' this process should be retrieved from' - ), - 'multiOptions' => array( - null => $this->translate('Use the configured default backend'), - ) + $this->listAvailableBackends() - )); + if (! empty($this->listAvailableBackends())) { + $this->addElement('select', 'Backend', array( + 'label' => $this->translate('Backend'), + 'description' => $this->translate( + 'Icinga Web Monitoring Backend where current object states for' + . ' this process should be retrieved from' + ), + 'multiOptions' => array( + null => $this->translate('Use the configured default backend'), + ) + $this->listAvailableBackends() + )); + } $this->addElement('select', 'Statetype', array( 'label' => $this->translate('State Type'), From e2fa339f99297485e47083a8cdca3df6f364aef3 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Thu, 17 Feb 2022 12:02:28 +0100 Subject: [PATCH 40/40] 'Display Name' and 'Description' can be left empty. --- application/forms/BpConfigForm.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/application/forms/BpConfigForm.php b/application/forms/BpConfigForm.php index 64e5350..2de98e3 100644 --- a/application/forms/BpConfigForm.php +++ b/application/forms/BpConfigForm.php @@ -169,9 +169,11 @@ class BpConfigForm extends BpConfigBaseForm } $meta = $config->getMetadata(); foreach ($this->getValues() as $key => $value) { - if ($key !== 'Backend' && ($value === null || $value === '')) { + if (! in_array($key, ['Title', 'Description', 'Backend'], true) + && ($value === null || $value === '')) { continue; } + if ($meta->hasKey($key)) { $meta->set($key, $value); }