Optimize queries used for fetching the host group summaries

refs #1883
This commit is contained in:
Eric Lippmann 2017-07-27 11:43:13 +02:00
parent f5a9016dea
commit 30cc51a823
2 changed files with 127 additions and 154 deletions

View File

@ -8,99 +8,82 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query;
*/ */
class HostgroupQuery extends IdoQuery class HostgroupQuery extends IdoQuery
{ {
/**
* {@inheritdoc}
*/
protected $allowCustomVars = true; protected $allowCustomVars = true;
/** protected $groupBase = array(
* {@inheritdoc} 'hostgroups' => array('hgo.object_id', 'hg.hostgroup_id'),
*/ 'hoststatus' => array('hs.hoststatus_id'),
protected $groupBase = array('hostgroups' => array('hg.hostgroup_id', 'hgo.object_id')); 'servicestatus' => array('ss.servicestatus_id')
);
/** protected $groupOrigin = array('members');
* {@inheritdoc}
*/
protected $groupOrigin = array('hostobjects');
/**
* {@inheritdoc}
*/
protected $columnMap = array( protected $columnMap = array(
'hostgroups' => array( 'hostgroups' => array(
'hostgroup' => 'hgo.name1 COLLATE latin1_general_ci',
'hostgroup_alias' => 'hg.alias COLLATE latin1_general_ci', 'hostgroup_alias' => 'hg.alias COLLATE latin1_general_ci',
'hostgroup_name' => 'hgo.name1' 'hostgroup_name' => 'hgo.name1'
), ),
'hostobjects' => array( 'hoststatus' => array(
'host' => 'ho.name1 COLLATE latin1_general_ci', 'host_handled' => 'CASE WHEN (hs.problem_has_been_acknowledged + hs.scheduled_downtime_depth) > 0 THEN 1 ELSE 0 END',
'host_name' => 'ho.name1' 'host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE hs.current_state END'
),
'hosts' => array(
'host_alias' => 'h.alias',
'host_display_name' => 'h.display_name COLLATE latin1_general_ci',
), ),
'instances' => array( 'instances' => array(
'instance_name' => 'i.instance_name' 'instance_name' => 'i.instance_name'
), ),
'members' => array(
'host_name' => 'ho.name1'
),
'servicegroups' => array( 'servicegroups' => array(
'servicegroup' => 'sgo.name1 COLLATE latin1_general_ci', 'servicegroup_name' => 'sgo.name1'
'servicegroup_alias' => 'sg.alias COLLATE latin1_general_ci',
'servicegroup_name' => 'sgo.name1'
), ),
'services' => array( 'services' => array(
'service' => 'so.name2 COLLATE latin1_general_ci', 'service_description' => 'so.name2'
'service_description' => 'so.name2', ),
'service_display_name' => 's.display_name COLLATE latin1_general_ci' '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() protected function joinBaseTables()
{ {
$this->select->from( $this->select->from(
array('hg' => $this->prefix . 'hostgroups'), array('hgo' => $this->prefix . 'objects'),
array() array()
)->join( )->join(
array('hgo' => $this->prefix . 'objects'), array('hg' => $this->prefix . 'hostgroups'),
'hgo.object_id = hg.hostgroup_object_id AND hgo.is_active = 1 AND hgo.objecttype_id = 3', 'hg.hostgroup_object_id = hgo.object_id AND hgo.is_active = 1 AND hgo.objecttype_id = 3',
array() array()
); );
$this->joinedVirtualTables['hostgroups'] = true; $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 * Join hosts
*/ */
protected function joinHosts() protected function joinHosts()
{ {
$this->requireVirtualTable('hostobjects'); $this->requireVirtualTable('members');
$this->select->joinLeft( $this->select->join(
array('h' => $this->prefix . 'hosts'), array('h' => $this->prefix . 'hosts'),
'h.host_object_id = ho.object_id', 'h.host_object_id = ho.object_id',
array() 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 * 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 * Join service groups
*/ */
protected function joinServicegroups() protected function joinServicegroups()
{ {
$this->requireVirtualTable('services'); $this->requireVirtualTable('services');
$this->select->joinLeft( $this->select->join(
array('sgm' => $this->prefix . 'servicegroup_members'), array('sgm' => $this->prefix . 'servicegroup_members'),
'sgm.service_object_id = s.service_object_id', 'sgm.service_object_id = s.service_object_id',
array() array()
)->joinLeft( )->join(
array('sg' => $this->prefix . 'servicegroups'), array('sg' => $this->prefix . 'servicegroups'),
'sgm.servicegroup_id = sg.' . $this->servicegroup_id, 'sgm.servicegroup_id = sg.servicegroup_id',
array() array()
)->joinLeft( )->join(
array('sgo' => $this->prefix . 'objects'), array('sgo' => $this->prefix . 'objects'),
'sgo.object_id = sg.servicegroup_object_id AND sgo.is_active = 1 AND sgo.objecttype_id = 4', 'sgo.object_id = sg.servicegroup_object_id AND sgo.is_active = 1 AND sgo.objecttype_id = 4',
array() array()
@ -140,14 +139,28 @@ class HostgroupQuery extends IdoQuery
protected function joinServices() protected function joinServices()
{ {
$this->requireVirtualTable('hosts'); $this->requireVirtualTable('hosts');
$this->select->joinLeft( $this->select->join(
array('s' => $this->prefix . 'services'), array('s' => $this->prefix . 'services'),
's.host_object_id = h.host_object_id', 's.host_object_id = h.host_object_id',
array() array()
)->joinLeft( )->join(
array('so' => $this->prefix . 'objects'), array('so' => $this->prefix . 'objects'),
'so.object_id = s.service_object_id AND so.is_active = 1 AND so.objecttype_id = 2', 'so.object_id = s.service_object_id AND so.is_active = 1 AND so.objecttype_id = 2',
array() 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()
);
}
} }

View File

@ -3,52 +3,43 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query; namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Icinga\Data\Filter\Filter;
use Zend_Db_Expr; use Zend_Db_Expr;
use Zend_Db_Select; use Zend_Db_Select;
use Icinga\Data\Filter\Filter;
/** /**
* Query for host group summary * Query for host group summary
*/ */
class HostgroupsummaryQuery extends IdoQuery class HostgroupsummaryQuery extends IdoQuery
{ {
/** protected $allowCustomVars = true;
* {@inheritdoc}
*/
protected $columnMap = array( protected $columnMap = array(
'hoststatussummary' => array( 'hostgroupsummary' => array(
'hostgroup' => 'hostgroup COLLATE latin1_general_ci', 'hostgroup_alias' => 'hostgroup_alias',
'hostgroup_alias' => 'hostgroup_alias COLLATE latin1_general_ci',
'hostgroup_name' => 'hostgroup_name', 'hostgroup_name' => 'hostgroup_name',
'hosts_down' => 'SUM(CASE WHEN object_type = \'host\' AND state = 1 THEN 1 ELSE 0 END)', 'hosts_down' => 'SUM(CASE WHEN host_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' => 'SUM(CASE WHEN host_state = 1 AND host_handled = 1 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 host_state = 1 AND host_handled = 0 THEN 1 ELSE 0 END)',
'hosts_down_unhandled' => 'SUM(CASE WHEN object_type = \'host\' AND state = 1 AND handled = 0 THEN 1 ELSE 0 END)', 'hosts_pending' => 'SUM(CASE WHEN host_state = 99 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_total' => 'SUM(CASE WHEN host_state IS NOT NULL THEN 1 ELSE 0 END)',
'hosts_pending' => 'SUM(CASE WHEN object_type = \'host\' AND state = 99 THEN 1 ELSE 0 END)', 'hosts_unreachable' => 'SUM(CASE WHEN host_state = 2 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_unreachable_handled' => 'SUM(CASE WHEN host_state = 2 AND host_handled = 1 THEN 1 ELSE 0 END)',
'hosts_severity' => 'MAX(CASE WHEN object_type = \'host\' THEN severity ELSE 0 END)', 'hosts_unreachable_unhandled' => 'SUM(CASE WHEN host_state = 2 AND host_handled = 0 THEN 1 ELSE 0 END)',
'hosts_total' => 'SUM(CASE WHEN object_type = \'host\' THEN 1 ELSE 0 END)', 'hosts_up' => 'SUM(CASE WHEN host_state = 0 THEN 1 ELSE 0 END)',
'hosts_unreachable' => 'SUM(CASE WHEN object_type = \'host\' AND state = 2 THEN 1 ELSE 0 END)', 'services_critical' => 'SUM(CASE WHEN service_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)', 'services_critical_handled' => 'SUM(CASE WHEN service_state = 2 AND service_handled = 1 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)', 'services_critical_unhandled' => 'SUM(CASE WHEN service_state = 2 AND service_handled = 0 THEN 1 ELSE 0 END)',
'hosts_unreachable_unhandled' => 'SUM(CASE WHEN object_type = \'host\' AND state = 2 AND handled = 0 THEN 1 ELSE 0 END)', 'services_ok' => 'SUM(CASE WHEN service_state = 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)', 'services_pending' => 'SUM(CASE WHEN service_state = 99 THEN 1 ELSE 0 END)',
'hosts_up' => 'SUM(CASE WHEN object_type = \'host\' AND state = 0 THEN 1 ELSE 0 END)', 'services_total' => 'SUM(CASE WHEN service_state IS NOT NULL 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_unknown' => 'SUM(CASE WHEN service_state = 3 THEN 1 ELSE 0 END)',
'services_critical' => 'SUM(CASE WHEN object_type = \'service\' AND state = 2 THEN 1 ELSE 0 END)', 'services_unknown_handled' => 'SUM(CASE WHEN service_state = 3 AND service_handled = 1 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_unknown_unhandled' => 'SUM(CASE WHEN service_state = 3 AND service_handled = 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_warning' => 'SUM(CASE WHEN service_state = 1 THEN 1 ELSE 0 END)',
'services_ok' => 'SUM(CASE WHEN object_type = \'service\' AND state = 0 THEN 1 ELSE 0 END)', 'services_warning_handled' => 'SUM(CASE WHEN service_state = 1 AND service_handled = 1 THEN 1 ELSE 0 END)',
'services_pending' => 'SUM(CASE WHEN object_type = \'service\' AND state = 99 THEN 1 ELSE 0 END)', 'services_warning_unhandled' => 'SUM(CASE WHEN service_state = 1 AND service_handled = 0 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)',
) )
); );
@ -67,94 +58,63 @@ class HostgroupsummaryQuery extends IdoQuery
protected $subQueries = array(); protected $subQueries = array();
/** /**
* {@inheritdoc} * Count query
*
* @var IdoQuery
*/ */
public function allowsCustomVars() protected $countQuery;
{
foreach ($this->subQueries as $query) {
if (! $query->allowsCustomVars()) {
return false;
}
}
return true;
}
/**
* {@inheritdoc}
*/
public function addFilter(Filter $filter) public function addFilter(Filter $filter)
{ {
foreach ($this->subQueries as $sub) { foreach ($this->subQueries as $sub) {
$sub->applyFilter(clone $filter); $sub->applyFilter(clone $filter);
} }
$this->countQuery->applyFilter(clone $filter);
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
protected function joinBaseTables() protected function joinBaseTables()
{ {
// TODO(el): Allow to switch between hard and soft states $this->countQuery = $this->createSubQuery(
'Hostgroup',
array()
);
$hosts = $this->createSubQuery( $hosts = $this->createSubQuery(
'Hoststatus', 'Hostgroup',
array( array(
'handled' => 'host_handled',
'host_state' => new Zend_Db_Expr('NULL'),
'hostgroup_alias', 'hostgroup_alias',
'hostgroup_name', 'hostgroup_name',
'object_type', 'host_handled',
'severity' => 'host_severity', 'host_state',
'state' => 'host_state', 'service_handled' => new Zend_Db_Expr('NULL'),
'state_change' => 'host_last_state_change' '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; $this->subQueries[] = $hosts;
$services = $this->createSubQuery( $services = $this->createSubQuery(
'Servicestatus', 'Hostgroup',
array( array(
'handled' => 'service_handled',
'host_state' => 'host_hard_state',
'hostgroup_alias', 'hostgroup_alias',
'hostgroup_name', 'hostgroup_name',
'object_type', 'host_handled' => new Zend_Db_Expr('NULL'),
'severity' => new Zend_Db_Expr('NULL'), 'host_state' => new Zend_Db_Expr('NULL'),
'state' => 'service_state', 'service_handled',
'state_change' => 'service_last_state_change' 'service_state'
) )
); );
$services->select()->where('hgo.name1 IS NOT NULL'); // TODO(9458): Should be possible using our filters!
$this->subQueries[] = $services; $this->subQueries[] = $services;
$this->summaryQuery = $this->db->select()->union(array($hosts, $services), Zend_Db_Select::SQL_UNION_ALL); $this->summaryQuery = $this->db->select()->union(array($hosts, $services), Zend_Db_Select::SQL_UNION_ALL);
$this->select->from(array('statussummary' => $this->summaryQuery), array()); $this->select->from(array('hostgroupsummary' => $this->summaryQuery), array());
$this->group(array('statussummary.hostgroup_name', 'statussummary.hostgroup_alias')); $this->group(array('hostgroup_name', 'hostgroup_alias'));
$this->joinedVirtualTables['hoststatussummary'] = true; $this->joinedVirtualTables['hostgroupsummary'] = true;
} }
/** public function getCountQuery()
* {@inheritdoc}
*/
public function order($columnOrAlias, $dir = null)
{ {
if (! $this->hasAliasName($columnOrAlias)) { $count = $this->countQuery->select();
foreach ($this->subQueries as $sub) { $this->countQuery->applyFilterSql($count);
$sub->requireColumn($columnOrAlias); $count->columns(array('hgo.object_id'));
} $count->group(array('hgo.object_id'));
} return $this->db->select()->from($count, array('cnt' => 'COUNT(*)'));
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;
} }
} }