diff --git a/modules/monitoring/application/controllers/HostsController.php b/modules/monitoring/application/controllers/HostsController.php index ec4e470a20..55f8a6e60b 100644 --- a/modules/monitoring/application/controllers/HostsController.php +++ b/modules/monitoring/application/controllers/HostsController.php @@ -110,16 +110,7 @@ public function showAction() $acknowledgedObjects = array(); $objectsInDowntime = array(); $downtimeFilterExpressions = array(); - $hostStates = array( - 'hosts_' . Host::getStateText(Host::STATE_UP) => 0, - 'hosts_' . Host::getStateText(Host::STATE_UP) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_DOWN) => 0, - 'hosts_' . Host::getStateText(Host::STATE_DOWN) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_UNREACHABLE) => 0, - 'hosts_' . Host::getStateText(Host::STATE_UNREACHABLE) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_PENDING) => 0, - 'hosts_' . Host::getStateText(Host::STATE_PENDING) . '_unhandled' => 0, - ); + foreach ($this->hostList as $host) { /** @var Host $host */ $unhandled = (bool) $host->problem === true && (bool) $host->handled === false; @@ -134,7 +125,6 @@ public function showAction() $objectsInDowntime[] = $host; $downtimeFilterExpressions[] = Filter::where('downtime_host', $host->getName()); } - ++$hostStates['hosts_' . $host::getStateText($host->state) . ($unhandled ? '_unhandled' : '')]; } if (! empty($acknowledgedObjects)) { $removeAckForm = new RemoveAcknowledgementCommandForm(); @@ -150,7 +140,7 @@ public function showAction() $this->view->processCheckResultAllLink = Url::fromRequest()->setPath('monitoring/hosts/process-check-result'); $this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/hosts/add-comment'); $this->view->deleteCommentLink = Url::fromRequest()->setPath('monitoring/hosts/delete-comment'); - $this->view->hostStates = (object)$hostStates; + $this->view->stats = (object)$this->hostList->getStateSummary(); $this->view->objects = $this->hostList; $this->view->unhandledObjects = $unhandledObjects; $unhandledFilterQueryString = Filter::matchAny($unhandledFilterExpressions)->toQueryString(); diff --git a/modules/monitoring/application/controllers/ServicesController.php b/modules/monitoring/application/controllers/ServicesController.php index 1514a0ea0c..dcd95184cb 100644 --- a/modules/monitoring/application/controllers/ServicesController.php +++ b/modules/monitoring/application/controllers/ServicesController.php @@ -132,35 +132,10 @@ public function showAction() $acknowledgedObjects = array(); $objectsInDowntime = array(); $downtimeFilterExpressions = array(); - $serviceStates = array( - 'services_' . Service::getStateText(Service::STATE_OK) => 0, - 'services_' . Service::getStateText(Service::STATE_OK) . '_unhandled' => 0, - 'services_' . Service::getStateText(Service::STATE_WARNING) => 0, - 'services_' . Service::getStateText(Service::STATE_WARNING) . '_unhandled' => 0, - 'services_' . Service::getStateText(Service::STATE_CRITICAL) => 0, - 'services_' . Service::getStateText(Service::STATE_CRITICAL) . '_unhandled' => 0, - 'services_' . Service::getStateText(Service::STATE_UNKNOWN) => 0, - 'services_' . Service::getStateText(Service::STATE_UNKNOWN) . '_unhandled' => 0, - 'services_' . Service::getStateText(Service::STATE_PENDING) => 0, - 'services_' . Service::getStateText(Service::STATE_PENDING) . '_unhandled' => 0 - ); - $knownHostStates = array(); - $hostStates = array( - 'hosts_' . Host::getStateText(Host::STATE_UP) => 0, - 'hosts_' . Host::getStateText(Host::STATE_UP) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_DOWN) => 0, - 'hosts_' . Host::getStateText(Host::STATE_DOWN) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_UNREACHABLE) => 0, - 'hosts_' . Host::getStateText(Host::STATE_UNREACHABLE) . '_unhandled' => 0, - 'hosts_' . Host::getStateText(Host::STATE_PENDING) => 0, - 'hosts_' . Host::getStateText(Host::STATE_PENDING) . '_unhandled' => 0 - ); - foreach ($this->serviceList as $service) { - $unhandled = false; + foreach ($this->serviceList as $service) { /** @var Service $service */ if ((bool) $service->problem === true && (bool) $service->handled === false) { - $unhandled = true; $unhandledObjects[] = $service; $unhandledFilterExpressions[] = Filter::matchAll( Filter::where('host', $service->getHost()->getName()), @@ -177,12 +152,6 @@ public function showAction() Filter::where('downtime_service', $service->getName()) ); } - - ++$serviceStates['services_' . $service::getStateText($service->state) . ($unhandled ? '_unhandled' : '')]; - if (! isset($knownHostStates[$service->getHost()->getName()])) { - $knownHostStates[$service->getHost()->getName()] = true; - ++$hostStates['hosts_' . $service->getHost()->getStateText($service->host_state)]; - } } if (! empty($acknowledgedObjects)) { $removeAckForm = new RemoveAcknowledgementCommandForm(); @@ -208,8 +177,7 @@ public function showAction() ); $this->view->addCommentLink = Url::fromRequest()->setPath('monitoring/services/add-comment'); $this->view->deleteCommentLink = Url::fromRequest()->setPath('monitoring/services/delete-comment'); - $this->view->hostStates = (object)$hostStates; - $this->view->serviceStates = (object)$serviceStates; + $this->view->stats = $this->serviceList->getStateSummary(); $this->view->objects = $this->serviceList; $this->view->unhandledObjects = $unhandledObjects; $unhandledFilterQueryString = Filter::matchAny($unhandledFilterExpressions)->toQueryString(); diff --git a/modules/monitoring/application/views/scripts/hosts/show.phtml b/modules/monitoring/application/views/scripts/hosts/show.phtml index f4ac409041..d56f92af49 100644 --- a/modules/monitoring/application/views/scripts/hosts/show.phtml +++ b/modules/monitoring/application/views/scripts/hosts/show.phtml @@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm; - render('partials/host/objects-tinysummary.phtml') ?> + render('list/components/hostssummary.phtml') ?> render('partials/host/objects-header.phtml'); ?> diff --git a/modules/monitoring/application/views/scripts/partials/host/objects-tinysummary.phtml b/modules/monitoring/application/views/scripts/partials/host/objects-tinysummary.phtml deleted file mode 100644 index fa841c6337..0000000000 --- a/modules/monitoring/application/views/scripts/partials/host/objects-tinysummary.phtml +++ /dev/null @@ -1,44 +0,0 @@ - - -

- qlink( - sprintf($this->translate('%d Hosts Selected:'), count($objects)), - $listAllLink - ); ?> - - - hosts_up): ?> - - - - hosts_up ?> - - - - - 'down', - 2 => 'unreachable', - 99 => 'pending' - ) as $stateId => $state) { - - $stateName = 'hosts_' . $state; - $unhandledStateName = $stateName . '_unhandled'; - if ($hostStates->$unhandledStateName) { - echo '' . $hostStates->$unhandledStateName . ''; - } - if ($hostStates->$stateName) { - echo '' . $hostStates->$stateName . ''; - } - if ($hostStates->$unhandledStateName) { - echo ''; - } - $stateName .= '_unhandled'; - }?> - - -

\ No newline at end of file diff --git a/modules/monitoring/application/views/scripts/partials/service/objects-tinysummary.phtml b/modules/monitoring/application/views/scripts/partials/service/objects-tinysummary.phtml deleted file mode 100644 index 7707651d2b..0000000000 --- a/modules/monitoring/application/views/scripts/partials/service/objects-tinysummary.phtml +++ /dev/null @@ -1,42 +0,0 @@ - - -

- qlink( - sprintf($this->translate('%d Services Selected:'), count($objects)), - $listAllLink - ); ?> - - - services_ok): ?> - - - services_ok ?> - - - - 'critical', - 3 => 'unknown', - 1 => 'warning', - 4 => 'pending' - ) as $stateId => $state) { - - $stateName = 'services_' . $state; - $unhandledStateName = $stateName . '_unhandled'; - if ($serviceStates->$unhandledStateName) { - echo '' . $serviceStates->$unhandledStateName . ''; - } - if ($serviceStates->$stateName) { - echo '' . $serviceStates->$stateName . ''; - } - if ($serviceStates->$unhandledStateName) { - echo ''; - } - $stateName .= '_unhandled'; - }?> - -

\ No newline at end of file diff --git a/modules/monitoring/application/views/scripts/services/show.phtml b/modules/monitoring/application/views/scripts/services/show.phtml index 08e1737f36..1daa1a956c 100644 --- a/modules/monitoring/application/views/scripts/services/show.phtml +++ b/modules/monitoring/application/views/scripts/services/show.phtml @@ -8,7 +8,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm; - render('partials/service/objects-tinysummary.phtml') ?> + render('list/components/servicesummary.phtml') ?> render('partials/service/objects-header.phtml'); ?> diff --git a/modules/monitoring/library/Monitoring/Object/HostList.php b/modules/monitoring/library/Monitoring/Object/HostList.php index f0adced24e..94157b6fcf 100644 --- a/modules/monitoring/library/Monitoring/Object/HostList.php +++ b/modules/monitoring/library/Monitoring/Object/HostList.php @@ -25,4 +25,31 @@ protected function fetchObjects() } return $hosts; } + + /** + * Create a state summary of all hosts that can be consumed by hostssummary.phtml + * + * @return object The summary + */ + public function getStateSummary() + { + $hostStates = $this->prepareStateNames('hosts_', array( + Host::getStateText(Host::STATE_UP), + Host::getStateText(Host::STATE_DOWN), + Host::getStateText(Host::STATE_UNREACHABLE), + Host::getStateText(Host::STATE_PENDING) + )); + + foreach ($this as $host) { + $unhandled = (bool) $host->problem === true && (bool) $host->handled === false; + + $stateName = 'hosts_' . $host::getStateText($host->state); + ++$hostStates[$stateName]; + ++$hostStates[$stateName. ($unhandled ? '_unhandled' : '_handled')]; + } + + $hostStates['hosts_total'] = count($this); + + return (object)$hostStates; + } } diff --git a/modules/monitoring/library/Monitoring/Object/ObjectList.php b/modules/monitoring/library/Monitoring/Object/ObjectList.php index 1464998050..234ccb9bb5 100644 --- a/modules/monitoring/library/Monitoring/Object/ObjectList.php +++ b/modules/monitoring/library/Monitoring/Object/ObjectList.php @@ -85,4 +85,15 @@ public function getComments() { return $this->backend->select()->from('comment')->applyFilter($this->filter); } + + protected function prepareStateNames($prefix, array $names) { + $new = array(); + foreach ($names as $name) { + $new[$prefix . $name] = 0; + $new[$prefix . $name . '_handled'] = 0; + $new[$prefix . $name . '_unhandled'] = 0; + } + $new[$prefix . 'total'] = 0; + return $new; + } } diff --git a/modules/monitoring/library/Monitoring/Object/ServiceList.php b/modules/monitoring/library/Monitoring/Object/ServiceList.php index d9ffa0d8e2..0625ae0f8b 100644 --- a/modules/monitoring/library/Monitoring/Object/ServiceList.php +++ b/modules/monitoring/library/Monitoring/Object/ServiceList.php @@ -25,4 +25,46 @@ protected function fetchObjects() } return $services; } + + /** + * Create a state summary of all services that can be consumed by servicesummary.phtml + * + * @return object The summary + */ + public function getStateSummary() + { + $serviceStates = $this->prepareStateNames('services_', array( + Service::getStateText(Service::STATE_OK), + Service::getStateText(Service::STATE_WARNING), + Service::getStateText(Service::STATE_CRITICAL), + Service::getStateText(Service::STATE_UNKNOWN), + Service::getStateText(Service::STATE_PENDING), + )); + + $hostStates = $this->prepareStateNames('hosts_', array( + Host::getStateText(Host::STATE_UP), + Host::getStateText(Host::STATE_DOWN), + Host::getStateText(Host::STATE_UNREACHABLE), + Host::getStateText(Host::STATE_PENDING), + )); + + foreach ($this as $service) { + $unhandled = false; + if ((bool) $service->problem === true && (bool) $service->handled === false) { + $unhandled = true; + } + + $stateName = 'services_' . $service::getStateText($service->state); + ++$serviceStates[$stateName]; + ++$serviceStates[$stateName . ($unhandled ? '_unhandled' : '_handled')]; + if (! isset($knownHostStates[$service->getHost()->getName()])) { + $knownHostStates[$service->getHost()->getName()] = true; + ++$hostStates['hosts_' . $service->getHost()->getStateText($service->host_state)]; + } + } + + $serviceStates['services_total'] = count($this); + + return (object)$serviceStates; + } }