From 6560975fe2e3963bdfbad5ce0304d210798eb1b7 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 7 Aug 2015 15:35:13 +0200 Subject: [PATCH 1/9] Host overview: Do not select `host_unhandled_services' It's implemented as horribly slow subquery. refs #9864 --- modules/monitoring/application/controllers/ListController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 7c749a2a3..9a7199580 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -85,7 +85,6 @@ class Monitoring_ListController extends Controller 'host_last_check', 'host_last_state_change' => $stateChangeColumn, 'host_notifications_enabled', - 'host_unhandled_services', 'host_action_url', 'host_notes_url', 'host_last_comment', From 03d4386763197f3d754beb42ad5f75043cee0033 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 7 Aug 2015 15:36:24 +0200 Subject: [PATCH 2/9] Introduce HostservicestatussummaryQuery class refs #9864 --- .../Query/HostservicestatussummaryQuery.php | 108 ++++++++++++++++++ .../Backend/Ido/Query/HoststatusQuery.php | 5 +- .../DataView/Hostservicestatussummary.php | 21 ++++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 modules/monitoring/library/Monitoring/Backend/Ido/Query/HostservicestatussummaryQuery.php create mode 100644 modules/monitoring/library/Monitoring/DataView/Hostservicestatussummary.php diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostservicestatussummaryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostservicestatussummaryQuery.php new file mode 100644 index 000000000..5f49d07e3 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostservicestatussummaryQuery.php @@ -0,0 +1,108 @@ + array( + 'host_name' => 'so.name1', + 'unhandled_service_count' => 'SUM( + CASE + WHEN (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth + COALESCE(hs.current_state, 0)) > 0 + THEN 0 + ELSE 1 + END + )' + ) + ); + + /** + * {@inheritdoc} + */ + protected function joinBaseTables() + { + $this->hostQuery = $this->createSubQuery('Hoststatus', array('object_id')); + $this->hostQuery->setIsSubQuery(); // TODO: Why is this necessary??? + + $this->select->from( + array('so' => $this->prefix . 'objects'), + array() + )->join( + array('s' => $this->prefix . 'services'), + 's.service_object_id = so.object_id', + array() + )->join( + array('ss' => $this->prefix . 'servicestatus'), + 'ss.service_object_id = so.object_id AND ss.current_state > 0', + array() + )->join( + array('hs' => $this->prefix . 'hoststatus'), + 'hs.host_object_id = s.host_object_id', + array() + )->join( + array('h' => $this->hostQuery), + 'h.object_id = s.host_object_id', + array() + ); + + $this->select->where('so.is_active = 1'); + $this->select->group('so.name1'); + $this->select->having($this->getMappedField('unhandled_service_count') . ' > 0'); + $this->joinedVirtualTables['statussummary'] = true; + } + + /** + * {@inheritdoc} + */ + public function addFilter(Filter $filter) + { + $this->hostQuery->addFilter($filter); + return $this; + } + + /** + * {@inheritdoc} + */ + public function order($columnOrAlias, $dir = null) + { + $this->hostQuery->order($columnOrAlias, $dir); + return $this; + } + + /** + * {@inheritdoc} + */ + public function where($condition, $value = null) + { + $this->hostQuery->where($condition, $value); + return $this; + } + + /** + * {@inheritdoc} + */ + public function limit($count = null, $offset = null) + { + $this->hostQuery->limit($count, $offset); + return $this; + } +} diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php index 3feb37a9e..37d20e165 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php @@ -33,7 +33,8 @@ class HoststatusQuery extends IdoQuery 'host_name' => 'ho.name1', 'host_notes' => 'h.notes', 'host_notes_url' => 'h.notes_url', - 'object_type' => '(\'host\')' + 'object_type' => '(\'host\')', + 'object_id' => 'ho.object_id' ), 'hoststatus' => array( 'host_acknowledged' => 'hs.problem_has_been_acknowledged', @@ -448,4 +449,4 @@ SQL; return $group; } -} +} \ No newline at end of file diff --git a/modules/monitoring/library/Monitoring/DataView/Hostservicestatussummary.php b/modules/monitoring/library/Monitoring/DataView/Hostservicestatussummary.php new file mode 100644 index 000000000..c6b7224f8 --- /dev/null +++ b/modules/monitoring/library/Monitoring/DataView/Hostservicestatussummary.php @@ -0,0 +1,21 @@ + Date: Fri, 7 Aug 2015 15:37:35 +0200 Subject: [PATCH 3/9] Host Overview: Use a dedicated query to fetch service summaries refs #9864 --- .../application/controllers/ListController.php | 15 +++++++++++++++ .../application/views/scripts/list/hosts.phtml | 10 +++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 9a7199580..a06a09d8c 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -120,6 +120,21 @@ class Monitoring_ListController extends Controller 'host_address' => $this->translate('Address'), 'host_last_check' => $this->translate('Last Check') ), $query); + + $summary = $this->backend->select()->from('Hostservicestatussummary', array( + 'host_name', + 'unhandled_service_count' + )); + $summary->addFilter($query->getFilter()); + $this->setupSortControl(array( + 'host_severity' => $this->translate('Severity'), + 'host_state' => $this->translate('Current State'), + 'host_display_name' => $this->translate('Hostname'), + 'host_address' => $this->translate('Address'), + 'host_last_check' => $this->translate('Last Check') + ), $summary); + $summary->limit($query->getLimit(), $query->getOffset()); + $this->view->summary = $summary->fetchPairs(); } /** diff --git a/modules/monitoring/application/views/scripts/list/hosts.phtml b/modules/monitoring/application/views/scripts/list/hosts.phtml index 8938d5703..62ca65121 100644 --- a/modules/monitoring/application/views/scripts/list/hosts.phtml +++ b/modules/monitoring/application/views/scripts/list/hosts.phtml @@ -58,11 +58,11 @@ if (! $this->compact): ?> 'class' => 'rowaction' ) ); ?> - host_unhandled_services) && $host->host_unhandled_services > 0): ?> + host_name])): ?> (qlink( sprintf( - $this->translatePlural('%u unhandled service', '%u unhandled services', $host->host_unhandled_services), - $host->host_unhandled_services + $this->translatePlural('%u unhandled service', '%u unhandled services', $summary[$host->host_name]), + $summary[$host->host_name] ), 'monitoring/list/services', array( @@ -76,9 +76,9 @@ if (! $this->compact): ?> $this->translatePlural( 'List %s unhandled service problem on host %s', 'List %s unhandled service problems on host %s', - $host->host_unhandled_services + $summary[$host->host_name] ), - $host->host_unhandled_services, + $summary[$host->host_name], $host->host_name ) ) From 542d7e2db3b74c994abf43695caaf3e887e66d29 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 11 Aug 2015 11:29:25 +0200 Subject: [PATCH 4/9] HostcommentQuery: Fix services join --- .../library/Monitoring/Backend/Ido/Query/HostcommentQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostcommentQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostcommentQuery.php index d649f2739..6fae0c658 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostcommentQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostcommentQuery.php @@ -142,7 +142,7 @@ class HostcommentQuery extends IdoQuery { $this->select->joinLeft( array('s' => $this->prefix . 'services'), - 's.host_object_id = h.host_object_id', + 's.host_object_id = ho.object_id', array() )->joinLeft( array('so' => $this->prefix . 'objects'), From 589f0de063e1d97a6ebdfe808d759e0b4a9894a4 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 11 Aug 2015 11:29:48 +0200 Subject: [PATCH 5/9] HostdowntimeQuery: Fix services join --- .../library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php index 63acc17fc..2776bcdc2 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php @@ -148,7 +148,7 @@ class HostdowntimeQuery extends IdoQuery { $this->select->joinLeft( array('s' => $this->prefix . 'services'), - 's.host_object_id = h.host_object_id', + 's.host_object_id = ho.object_id', array() )->joinLeft( array('so' => $this->prefix . 'objects'), From 95da3772ce4677d4d2e44b4823301bc686bf1777 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 11 Aug 2015 13:48:08 +0200 Subject: [PATCH 6/9] Rename HostservicestatussummaryQuery... ...to HostserviceproblemsummaryQuery. refs #9864 --- ...estatussummaryQuery.php => HostserviceproblemsummaryQuery.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/monitoring/library/Monitoring/Backend/Ido/Query/{HostservicestatussummaryQuery.php => HostserviceproblemsummaryQuery.php} (100%) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostservicestatussummaryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostserviceproblemsummaryQuery.php similarity index 100% rename from modules/monitoring/library/Monitoring/Backend/Ido/Query/HostservicestatussummaryQuery.php rename to modules/monitoring/library/Monitoring/Backend/Ido/Query/HostserviceproblemsummaryQuery.php From 41a1c7ce98295b820225bb0855a1adc820fa5dd4 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 11 Aug 2015 13:48:41 +0200 Subject: [PATCH 7/9] Drop DataView\Hostservicestatussummary It's unnecessary bloat. refs #9864 --- .../DataView/Hostservicestatussummary.php | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 modules/monitoring/library/Monitoring/DataView/Hostservicestatussummary.php diff --git a/modules/monitoring/library/Monitoring/DataView/Hostservicestatussummary.php b/modules/monitoring/library/Monitoring/DataView/Hostservicestatussummary.php deleted file mode 100644 index c6b7224f8..000000000 --- a/modules/monitoring/library/Monitoring/DataView/Hostservicestatussummary.php +++ /dev/null @@ -1,21 +0,0 @@ - Date: Tue, 11 Aug 2015 13:51:30 +0200 Subject: [PATCH 8/9] ReFacTOR HostserviceproblemsummaryQuery refs #9864 --- .../Query/HostserviceproblemsummaryQuery.php | 132 +++++++++++------- 1 file changed, 82 insertions(+), 50 deletions(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostserviceproblemsummaryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostserviceproblemsummaryQuery.php index 5f49d07e3..b2fca387c 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostserviceproblemsummaryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostserviceproblemsummaryQuery.php @@ -3,9 +3,7 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query; -use Icinga\Data\Filter\Filter; - -class HostservicestatussummaryQuery extends IdoQuery +class HostserviceproblemsummaryQuery extends IdoQuery { /** * {@inheritdoc} @@ -13,18 +11,27 @@ class HostservicestatussummaryQuery extends IdoQuery protected $allowCustomVars = true; /** - * The HoststatusQuery + * The HoststatusQuery in use * * @var HoststatusQuery */ - protected $hostQuery; + protected $hostStatusQuery; /** * {@inheritdoc} */ protected $columnMap = array( - 'statussummary' => array( + 'services' => array( 'host_name' => 'so.name1', + 'service_description' => 'so.name2' + ), + 'hostgroups' => array( + 'hostgroup_name' => 'hgo.name1' + ), + 'servicegroups' => array( + 'servicegroup_name' => 'sgo.name1' + ), + 'problemsummary' => array( 'unhandled_service_count' => 'SUM( CASE WHEN (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth + COALESCE(hs.current_state, 0)) > 0 @@ -35,14 +42,26 @@ class HostservicestatussummaryQuery extends IdoQuery ) ); + /** + * Set the HoststatusQuery to use + * + * @param HoststatusQuery $query + * + * @return $this + */ + public function setHoststatusQuery(HoststatusQuery $query) + { + $this->hostStatusQuery = clone $query; + $this->hostStatusQuery->setIsSubQuery(); + $this->hostStatusQuery->columns(array('object_id')); + return $this; + } + /** * {@inheritdoc} */ protected function joinBaseTables() { - $this->hostQuery = $this->createSubQuery('Hoststatus', array('object_id')); - $this->hostQuery->setIsSubQuery(); // TODO: Why is this necessary??? - $this->select->from( array('so' => $this->prefix . 'objects'), array() @@ -50,7 +69,59 @@ class HostservicestatussummaryQuery extends IdoQuery array('s' => $this->prefix . 'services'), 's.service_object_id = so.object_id', array() - )->join( + ); + + $this->select->group(array('so.name1')); + $this->select->where('so.is_active = 1'); + $this->joinedVirtualTables['services'] = true; + } + + /** + * Join host groups + */ + protected function joinHostgroups() + { + $this->select->joinLeft( + array('hgm' => $this->prefix . 'hostgroup_members'), + 'hgm.host_object_id = s.host_object_id', + array() + )->joinLeft( + array('hg' => $this->prefix . 'hostgroups'), + 'hg.hostgroup_id = hgm.hostgroup_id', + array() + )->joinLeft( + array('hgo' => $this->prefix . 'objects'), + 'hgo.object_id = hg.hostgroup_object_id AND hgo.is_active = 1 AND hgo.objecttype_id = 3', + array() + ); + } + + /** + * Join service groups + */ + protected function joinServicegroups() + { + $this->select->joinLeft( + array('sgm' => $this->prefix . 'servicegroup_members'), + 'sgm.service_object_id = so.object_id', + array() + )->joinLeft( + array('sg' => $this->prefix . 'servicegroups'), + 'sgm.servicegroup_id = sg.servicegroup_id', + array() + )->joinLeft( + array('sgo' => $this->prefix . 'objects'), + 'sgo.object_id = sg.servicegroup_object_id AND sgo.is_active = 1 AND sgo.objecttype_id = 4', + array() + ); + } + + /** + * Join the statussummary + */ + protected function joinProblemsummary() + { + $this->select->join( array('ss' => $this->prefix . 'servicestatus'), 'ss.service_object_id = so.object_id AND ss.current_state > 0', array() @@ -59,50 +130,11 @@ class HostservicestatussummaryQuery extends IdoQuery 'hs.host_object_id = s.host_object_id', array() )->join( - array('h' => $this->hostQuery), + array('h' => $this->hostStatusQuery), 'h.object_id = s.host_object_id', array() ); - $this->select->where('so.is_active = 1'); - $this->select->group('so.name1'); $this->select->having($this->getMappedField('unhandled_service_count') . ' > 0'); - $this->joinedVirtualTables['statussummary'] = true; - } - - /** - * {@inheritdoc} - */ - public function addFilter(Filter $filter) - { - $this->hostQuery->addFilter($filter); - return $this; - } - - /** - * {@inheritdoc} - */ - public function order($columnOrAlias, $dir = null) - { - $this->hostQuery->order($columnOrAlias, $dir); - return $this; - } - - /** - * {@inheritdoc} - */ - public function where($condition, $value = null) - { - $this->hostQuery->where($condition, $value); - return $this; - } - - /** - * {@inheritdoc} - */ - public function limit($count = null, $offset = null) - { - $this->hostQuery->limit($count, $offset); - return $this; } } From 00d143cb140610bbc9e913d28f1612ba74fca4c2 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 11 Aug 2015 13:52:21 +0200 Subject: [PATCH 9/9] Fetch the service problem summary of a host with less redundant code refs #9864 --- .../application/controllers/ListController.php | 15 ++------------- .../Backend/Ido/Query/HoststatusQuery.php | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index a06a09d8c..e200266ad 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -121,19 +121,8 @@ class Monitoring_ListController extends Controller 'host_last_check' => $this->translate('Last Check') ), $query); - $summary = $this->backend->select()->from('Hostservicestatussummary', array( - 'host_name', - 'unhandled_service_count' - )); - $summary->addFilter($query->getFilter()); - $this->setupSortControl(array( - 'host_severity' => $this->translate('Severity'), - 'host_state' => $this->translate('Current State'), - 'host_display_name' => $this->translate('Hostname'), - 'host_address' => $this->translate('Address'), - 'host_last_check' => $this->translate('Last Check') - ), $summary); - $summary->limit($query->getLimit(), $query->getOffset()); + $summary = $query->getQuery()->queryServiceProblemSummary(); + $this->applyRestriction('monitoring/filter/objects', $summary); $this->view->summary = $summary->fetchPairs(); } diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php index 37d20e165..c164a8bcf 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php @@ -449,4 +449,20 @@ SQL; return $group; } -} \ No newline at end of file + + /** + * Query the service problem summary for all hosts of this query's result set + * + * @return HostserviceproblemsummaryQuery + */ + public function queryServiceProblemSummary() + { + return $this->createSubQuery('Hostserviceproblemsummary') + ->setHostStatusQuery($this) + ->columns(array( + 'host_name', + 'unhandled_service_count' + ) + ); + } +}