diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostgroupQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostgroupQuery.php index 512c3e7c0..a043626e3 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostgroupQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostgroupQuery.php @@ -8,99 +8,82 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query; */ class HostgroupQuery extends IdoQuery { - /** - * {@inheritdoc} - */ protected $allowCustomVars = true; - /** - * {@inheritdoc} - */ - protected $groupBase = array('hostgroups' => array('hg.hostgroup_id', 'hgo.object_id')); + protected $groupBase = array( + 'hostgroups' => array('hgo.object_id', 'hg.hostgroup_id'), + 'hoststatus' => array('hs.hoststatus_id'), + 'servicestatus' => array('ss.servicestatus_id') + ); - /** - * {@inheritdoc} - */ - protected $groupOrigin = array('hostobjects'); + protected $groupOrigin = array('members'); - /** - * {@inheritdoc} - */ protected $columnMap = array( 'hostgroups' => array( - 'hostgroup' => 'hgo.name1 COLLATE latin1_general_ci', 'hostgroup_alias' => 'hg.alias COLLATE latin1_general_ci', 'hostgroup_name' => 'hgo.name1' ), - 'hostobjects' => array( - 'host' => 'ho.name1 COLLATE latin1_general_ci', - 'host_name' => 'ho.name1' - ), - 'hosts' => array( - 'host_alias' => 'h.alias', - 'host_display_name' => 'h.display_name COLLATE latin1_general_ci', + 'hoststatus' => array( + 'host_handled' => 'CASE WHEN (hs.problem_has_been_acknowledged + hs.scheduled_downtime_depth) > 0 THEN 1 ELSE 0 END', + 'host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE hs.current_state END' ), 'instances' => array( 'instance_name' => 'i.instance_name' ), + 'members' => array( + 'host_name' => 'ho.name1' + ), 'servicegroups' => array( - 'servicegroup' => 'sgo.name1 COLLATE latin1_general_ci', - 'servicegroup_alias' => 'sg.alias COLLATE latin1_general_ci', - 'servicegroup_name' => 'sgo.name1' + 'servicegroup_name' => 'sgo.name1' ), 'services' => array( - 'service' => 'so.name2 COLLATE latin1_general_ci', - 'service_description' => 'so.name2', - 'service_display_name' => 's.display_name COLLATE latin1_general_ci' + 'service_description' => 'so.name2' + ), + 'servicestatus' => array( + 'service_handled' => 'CASE WHEN (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth + COALESCE(hs.current_state, 0)) > 0 THEN 1 ELSE 0 END', + 'service_state' => 'CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE ss.current_state END' ) ); - - /** - * {@inheritdoc} - */ protected function joinBaseTables() { $this->select->from( - array('hg' => $this->prefix . 'hostgroups'), + array('hgo' => $this->prefix . 'objects'), array() )->join( - array('hgo' => $this->prefix . 'objects'), - 'hgo.object_id = hg.hostgroup_object_id AND hgo.is_active = 1 AND hgo.objecttype_id = 3', + array('hg' => $this->prefix . 'hostgroups'), + 'hg.hostgroup_object_id = hgo.object_id AND hgo.is_active = 1 AND hgo.objecttype_id = 3', array() ); $this->joinedVirtualTables['hostgroups'] = true; } - /** - * Join host objects - */ - protected function joinHostobjects() - { - $this->select->joinLeft( - array('hgm' => $this->prefix . 'hostgroup_members'), - 'hgm.hostgroup_id = hg.hostgroup_id', - array() - )->joinLeft( - array('ho' => $this->prefix . 'objects'), - 'hgm.host_object_id = ho.object_id AND ho.is_active = 1 AND ho.objecttype_id = 1', - array() - ); - } - /** * Join hosts */ protected function joinHosts() { - $this->requireVirtualTable('hostobjects'); - $this->select->joinLeft( + $this->requireVirtualTable('members'); + $this->select->join( array('h' => $this->prefix . 'hosts'), 'h.host_object_id = ho.object_id', array() ); } + /** + * Join host status + */ + protected function joinHoststatus() + { + $this->requireVirtualTable('members'); + $this->select->join( + array('hs' => $this->prefix . 'hoststatus'), + 'hs.host_object_id = ho.object_id', + array() + ); + } + /** * Join instances */ @@ -113,21 +96,37 @@ class HostgroupQuery extends IdoQuery ); } + /** + * Join members + */ + protected function joinMembers() + { + $this->select->join( + array('hgm' => $this->prefix . 'hostgroup_members'), + 'hgm.hostgroup_id = hg.hostgroup_id', + array() + )->join( + array('ho' => $this->prefix . 'objects'), + 'hgm.host_object_id = ho.object_id AND ho.is_active = 1 AND ho.objecttype_id = 1', + array() + ); + } + /** * Join service groups */ protected function joinServicegroups() { $this->requireVirtualTable('services'); - $this->select->joinLeft( + $this->select->join( array('sgm' => $this->prefix . 'servicegroup_members'), 'sgm.service_object_id = s.service_object_id', array() - )->joinLeft( + )->join( array('sg' => $this->prefix . 'servicegroups'), - 'sgm.servicegroup_id = sg.' . $this->servicegroup_id, + 'sgm.servicegroup_id = sg.servicegroup_id', array() - )->joinLeft( + )->join( array('sgo' => $this->prefix . 'objects'), 'sgo.object_id = sg.servicegroup_object_id AND sgo.is_active = 1 AND sgo.objecttype_id = 4', array() @@ -140,14 +139,28 @@ class HostgroupQuery extends IdoQuery protected function joinServices() { $this->requireVirtualTable('hosts'); - $this->select->joinLeft( + $this->select->join( array('s' => $this->prefix . 'services'), 's.host_object_id = h.host_object_id', array() - )->joinLeft( + )->join( array('so' => $this->prefix . 'objects'), 'so.object_id = s.service_object_id AND so.is_active = 1 AND so.objecttype_id = 2', array() ); } + + /** + * Join service status + */ + protected function joinServicestatus() + { + $this->requireVirtualTable('services'); + $this->requireVirtualTable('hoststatus'); + $this->select->join( + array('ss' => $this->prefix . 'servicestatus'), + 'ss.service_object_id = so.object_id', + array() + ); + } } diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostgroupsummaryQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostgroupsummaryQuery.php index de7cc8dd8..4a6060bd9 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostgroupsummaryQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostgroupsummaryQuery.php @@ -3,52 +3,43 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query; -use Icinga\Data\Filter\Filter; use Zend_Db_Expr; use Zend_Db_Select; +use Icinga\Data\Filter\Filter; + /** * Query for host group summary */ class HostgroupsummaryQuery extends IdoQuery { - /** - * {@inheritdoc} - */ + protected $allowCustomVars = true; + protected $columnMap = array( - 'hoststatussummary' => array( - 'hostgroup' => 'hostgroup COLLATE latin1_general_ci', - 'hostgroup_alias' => 'hostgroup_alias COLLATE latin1_general_ci', + 'hostgroupsummary' => array( + 'hostgroup_alias' => 'hostgroup_alias', 'hostgroup_name' => 'hostgroup_name', - 'hosts_down' => 'SUM(CASE WHEN object_type = \'host\' AND state = 1 THEN 1 ELSE 0 END)', - 'hosts_down_handled' => 'SUM(CASE WHEN object_type = \'host\' AND state = 1 AND handled != 0 THEN 1 ELSE 0 END)', - 'hosts_down_handled_last_state_change' => 'MAX(CASE WHEN object_type = \'host\' AND state = 1 AND handled != 0 THEN state_change ELSE 0 END)', - 'hosts_down_unhandled' => 'SUM(CASE WHEN object_type = \'host\' AND state = 1 AND handled = 0 THEN 1 ELSE 0 END)', - 'hosts_down_unhandled_last_state_change' => 'MAX(CASE WHEN object_type = \'host\' AND state = 1 AND handled = 0 THEN state_change ELSE 0 END)', - 'hosts_pending' => 'SUM(CASE WHEN object_type = \'host\' AND state = 99 THEN 1 ELSE 0 END)', - 'hosts_pending_last_state_change' => 'MAX(CASE WHEN object_type = \'host\' AND state = 99 THEN state_change ELSE 0 END)', - 'hosts_severity' => 'MAX(CASE WHEN object_type = \'host\' THEN severity ELSE 0 END)', - 'hosts_total' => 'SUM(CASE WHEN object_type = \'host\' THEN 1 ELSE 0 END)', - 'hosts_unreachable' => 'SUM(CASE WHEN object_type = \'host\' AND state = 2 THEN 1 ELSE 0 END)', - 'hosts_unreachable_handled' => 'SUM(CASE WHEN object_type = \'host\' AND state = 2 AND handled != 0 THEN 1 ELSE 0 END)', - 'hosts_unreachable_handled_last_state_change' => 'MAX(CASE WHEN object_type = \'host\' AND state = 2 AND handled != 0 THEN state_change ELSE 0 END)', - 'hosts_unreachable_unhandled' => 'SUM(CASE WHEN object_type = \'host\' AND state = 2 AND handled = 0 THEN 1 ELSE 0 END)', - 'hosts_unreachable_unhandled_last_state_change' => 'MAX(CASE WHEN object_type = \'host\' AND state = 2 AND handled = 0 THEN state_change ELSE 0 END)', - 'hosts_up' => 'SUM(CASE WHEN object_type = \'host\' AND state = 0 THEN 1 ELSE 0 END)', - 'hosts_up_last_state_change' => 'MAX(CASE WHEN object_type = \'host\' AND state = 0 THEN state_change ELSE 0 END)', - 'services_critical' => 'SUM(CASE WHEN object_type = \'service\' AND state = 2 THEN 1 ELSE 0 END)', - 'services_critical_handled' => 'SUM(CASE WHEN object_type = \'service\' AND state = 2 AND handled + host_state > 0 THEN 1 ELSE 0 END)', - 'services_critical_unhandled' => 'SUM(CASE WHEN object_type = \'service\' AND state = 2 AND handled + host_state = 0 THEN 1 ELSE 0 END)', - 'services_ok' => 'SUM(CASE WHEN object_type = \'service\' AND state = 0 THEN 1 ELSE 0 END)', - 'services_pending' => 'SUM(CASE WHEN object_type = \'service\' AND state = 99 THEN 1 ELSE 0 END)', - 'services_severity' => 'MAX(CASE WHEN object_type = \'service\' THEN severity ELSE 0 END)', - 'services_total' => 'SUM(CASE WHEN object_type = \'service\' THEN 1 ELSE 0 END)', - 'services_unknown' => 'SUM(CASE WHEN object_type = \'service\' AND state = 3 THEN 1 ELSE 0 END)', - 'services_unknown_handled' => 'SUM(CASE WHEN object_type = \'service\' AND state = 3 AND handled + host_state > 0 THEN 1 ELSE 0 END)', - 'services_unknown_unhandled' => 'SUM(CASE WHEN object_type = \'service\' AND state = 3 AND handled + host_state = 0 THEN 1 ELSE 0 END)', - 'services_warning' => 'SUM(CASE WHEN object_type = \'service\' AND state = 1 THEN 1 ELSE 0 END)', - 'services_warning_handled' => 'SUM(CASE WHEN object_type = \'service\' AND state = 1 AND handled + host_state > 0 THEN 1 ELSE 0 END)', - 'services_warning_unhandled' => 'SUM(CASE WHEN object_type = \'service\' AND state = 1 AND handled + host_state = 0 THEN 1 ELSE 0 END)', + 'hosts_down' => 'SUM(CASE WHEN host_state = 1 THEN 1 ELSE 0 END)', + 'hosts_down_handled' => 'SUM(CASE WHEN host_state = 1 AND host_handled = 1 THEN 1 ELSE 0 END)', + 'hosts_down_unhandled' => 'SUM(CASE WHEN host_state = 1 AND host_handled = 0 THEN 1 ELSE 0 END)', + 'hosts_pending' => 'SUM(CASE WHEN host_state = 99 THEN 1 ELSE 0 END)', + 'hosts_total' => 'SUM(CASE WHEN host_state IS NOT NULL THEN 1 ELSE 0 END)', + 'hosts_unreachable' => 'SUM(CASE WHEN host_state = 2 THEN 1 ELSE 0 END)', + 'hosts_unreachable_handled' => 'SUM(CASE WHEN host_state = 2 AND host_handled = 1 THEN 1 ELSE 0 END)', + 'hosts_unreachable_unhandled' => 'SUM(CASE WHEN host_state = 2 AND host_handled = 0 THEN 1 ELSE 0 END)', + 'hosts_up' => 'SUM(CASE WHEN host_state = 0 THEN 1 ELSE 0 END)', + 'services_critical' => 'SUM(CASE WHEN service_state = 2 THEN 1 ELSE 0 END)', + 'services_critical_handled' => 'SUM(CASE WHEN service_state = 2 AND service_handled = 1 THEN 1 ELSE 0 END)', + 'services_critical_unhandled' => 'SUM(CASE WHEN service_state = 2 AND service_handled = 0 THEN 1 ELSE 0 END)', + 'services_ok' => 'SUM(CASE WHEN service_state = 0 THEN 1 ELSE 0 END)', + 'services_pending' => 'SUM(CASE WHEN service_state = 99 THEN 1 ELSE 0 END)', + 'services_total' => 'SUM(CASE WHEN service_state IS NOT NULL THEN 1 ELSE 0 END)', + 'services_unknown' => 'SUM(CASE WHEN service_state = 3 THEN 1 ELSE 0 END)', + 'services_unknown_handled' => 'SUM(CASE WHEN service_state = 3 AND service_handled = 1 THEN 1 ELSE 0 END)', + 'services_unknown_unhandled' => 'SUM(CASE WHEN service_state = 3 AND service_handled = 0 THEN 1 ELSE 0 END)', + 'services_warning' => 'SUM(CASE WHEN service_state = 1 THEN 1 ELSE 0 END)', + 'services_warning_handled' => 'SUM(CASE WHEN service_state = 1 AND service_handled = 1 THEN 1 ELSE 0 END)', + 'services_warning_unhandled' => 'SUM(CASE WHEN service_state = 1 AND service_handled = 0 THEN 1 ELSE 0 END)', ) ); @@ -67,94 +58,63 @@ class HostgroupsummaryQuery extends IdoQuery protected $subQueries = array(); /** - * {@inheritdoc} + * Count query + * + * @var IdoQuery */ - public function allowsCustomVars() - { - foreach ($this->subQueries as $query) { - if (! $query->allowsCustomVars()) { - return false; - } - } + protected $countQuery; - return true; - } - - /** - * {@inheritdoc} - */ public function addFilter(Filter $filter) { foreach ($this->subQueries as $sub) { $sub->applyFilter(clone $filter); } + $this->countQuery->applyFilter(clone $filter); return $this; } - /** - * {@inheritdoc} - */ protected function joinBaseTables() { - // TODO(el): Allow to switch between hard and soft states + $this->countQuery = $this->createSubQuery( + 'Hostgroup', + array() + ); $hosts = $this->createSubQuery( - 'Hoststatus', + 'Hostgroup', array( - 'handled' => 'host_handled', - 'host_state' => new Zend_Db_Expr('NULL'), 'hostgroup_alias', 'hostgroup_name', - 'object_type', - 'severity' => 'host_severity', - 'state' => 'host_state', - 'state_change' => 'host_last_state_change' + 'host_handled', + 'host_state', + 'service_handled' => new Zend_Db_Expr('NULL'), + 'service_state' => new Zend_Db_Expr('NULL'), ) ); - $hosts->select()->where('hgo.name1 IS NOT NULL'); // TODO(9458): Should be possible using our filters! $this->subQueries[] = $hosts; $services = $this->createSubQuery( - 'Servicestatus', + 'Hostgroup', array( - 'handled' => 'service_handled', - 'host_state' => 'host_hard_state', 'hostgroup_alias', 'hostgroup_name', - 'object_type', - 'severity' => new Zend_Db_Expr('NULL'), - 'state' => 'service_state', - 'state_change' => 'service_last_state_change' + 'host_handled' => new Zend_Db_Expr('NULL'), + 'host_state' => new Zend_Db_Expr('NULL'), + 'service_handled', + 'service_state' ) ); - $services->select()->where('hgo.name1 IS NOT NULL'); // TODO(9458): Should be possible using our filters! $this->subQueries[] = $services; $this->summaryQuery = $this->db->select()->union(array($hosts, $services), Zend_Db_Select::SQL_UNION_ALL); - $this->select->from(array('statussummary' => $this->summaryQuery), array()); - $this->group(array('statussummary.hostgroup_name', 'statussummary.hostgroup_alias')); - $this->joinedVirtualTables['hoststatussummary'] = true; + $this->select->from(array('hostgroupsummary' => $this->summaryQuery), array()); + $this->group(array('hostgroup_name', 'hostgroup_alias')); + $this->joinedVirtualTables['hostgroupsummary'] = true; } - /** - * {@inheritdoc} - */ - public function order($columnOrAlias, $dir = null) + public function getCountQuery() { - if (! $this->hasAliasName($columnOrAlias)) { - foreach ($this->subQueries as $sub) { - $sub->requireColumn($columnOrAlias); - } - } - return parent::order($columnOrAlias, $dir); - } - - /** - * {@inheritdoc} - */ - public function where($condition, $value = null) - { - $this->requireColumn($condition); - foreach ($this->subQueries as $sub) { - $sub->where($condition, $value); - } - return $this; + $count = $this->countQuery->select(); + $this->countQuery->applyFilterSql($count); + $count->columns(array('hgo.object_id')); + $count->group(array('hgo.object_id')); + return $this->db->select()->from($count, array('cnt' => 'COUNT(*)')); } }