monitoring: Use a sub select in the service status summary

refs #9009
This commit is contained in:
Eric Lippmann 2015-06-05 10:28:50 +02:00
parent 76e2d8f5ee
commit e0b117915a
1 changed files with 51 additions and 128 deletions

View File

@ -2,6 +2,7 @@
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Icinga\Data\Filter\Filter;
/**
* Query for service status summary
@ -13,152 +14,74 @@ class ServicestatussummaryQuery extends IdoQuery
/**
* {@inheritdoc}
*/
protected $allowCustomVars = true;
protected $columnMap = array(
'servicestatussummary' => array(
'services_critical' => 'SUM(CASE WHEN state = 2 THEN 1 ELSE 0 END)',
'services_critical_handled' => 'SUM(CASE WHEN state = 2 AND handled = 1 THEN 1 ELSE 0 END)',
// 'services_critical_handled_last_state_change' => 'MAX(CASE WHEN state = 2 AND handled = 1 THEN UNIX_TIMESTAMP(last_state_change) ELSE NULL END)',
'services_critical_unhandled' => 'SUM(CASE WHEN state = 2 AND handled = 0 THEN 1 ELSE 0 END)',
// 'services_critical_unhandled_last_state_change' => 'MAX(CASE WHEN state = 2 AND handled = 0 THEN UNIX_TIMESTAMP(last_state_change) ELSE NULL END)',
'services_ok' => 'SUM(CASE WHEN state = 0 THEN 1 ELSE 0 END)',
// 'services_ok_last_state_change' => 'MAX(CASE WHEN state = 0 THEN UNIX_TIMESTAMP(last_state_change) ELSE NULL END)',
'services_pending' => 'SUM(CASE WHEN state = 99 THEN 1 ELSE 0 END)',
// 'services_pending_last_state_change' => 'MAX(CASE WHEN state = 99 THEN UNIX_TIMESTAMP(last_state_change) ELSE NULL END)',
'services_total' => 'SUM(1)',
'services_unknown' => 'SUM(CASE WHEN state = 3 THEN 1 ELSE 0 END)',
'services_unknown_handled' => 'SUM(CASE WHEN state = 3 AND handled = 1 THEN 1 ELSE 0 END)',
// 'services_unknown_handled_last_state_change' => 'MAX(CASE WHEN state = 3 AND handled = 1 THEN UNIX_TIMESTAMP(last_state_change) ELSE NULL END)',
'services_unknown_unhandled' => 'SUM(CASE WHEN state = 3 AND handled = 0 THEN 1 ELSE 0 END)',
// 'services_unknown_unhandled_last_state_change' => 'MAX(CASE WHEN state = 3 AND handled = 0 THEN UNIX_TIMESTAMP(last_state_change) ELSE NULL END)',
'services_warning' => 'SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END)',
'services_warning_handled' => 'SUM(CASE WHEN state = 1 AND handled = 1 THEN 1 ELSE 0 END)',
// 'services_warning_handled_last_state_change' => 'MAX(CASE WHEN state = 1 AND handled = 1 THEN UNIX_TIMESTAMP(last_state_change) ELSE NULL END)',
'services_warning_unhandled' => 'SUM(CASE WHEN state = 1 AND handled = 0 THEN 1 ELSE 0 END)',
// 'services_warning_unhandled_last_state_change' => 'MAX(CASE WHEN state = 1 AND handled = 0 THEN UNIX_TIMESTAMP(last_state_change) ELSE NULL END)'
)
);
/**
* The service status sub select
*
* @var ServiceStatusQuery
*/
protected $subSelect;
/**
* {@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'
),
'servicegroups' => array(
'servicegroup' => 'sgo.name1 COLLATE latin1_general_ci',
'servicegroup_name' => 'sgo.name1',
'servicegroup_alias' => 'sg.alias COLLATE latin1_general_ci'
),
'servicestatussummary' => array(
'host' => 'so.name1 COLLATE latin1_general_ci',
'host_alias' => 'h.alias COLLATE latin1_general_ci',
'host_display_name' => 'h.display_name COLLATE latin1_general_ci',
'host_name' => 'so.name1',
'object_type' => '(\'service\')',
'service' => 'so.name2 COLLATE latin1_general_ci',
'service_description' => 'so.name2',
'service_display_name' => 's.display_name COLLATE latin1_general_ci',
'service_host' => 'so.name1 COLLATE latin1_general_ci',
'service_host_name' => 'so.name1',
'services_critical' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 2 THEN 1 ELSE 0 END END)',
'services_critical_handled' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 2 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) > 0 THEN 1 ELSE 0 END END)',
'services_critical_handled_last_state_change' => 'MAX(CASE WHEN ss.has_been_checked != 1 THEN NULL ELSE CASE WHEN ss.current_state = 2 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) > 0 THEN UNIX_TIMESTAMP(ss.last_state_change) ELSE NULL END END)',
'services_critical_unhandled' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 2 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) = 0 THEN 1 ELSE 0 END END)',
'services_critical_unhandled_last_state_change' => 'MAX(CASE WHEN ss.has_been_checked != 1 THEN NULL ELSE CASE WHEN ss.current_state = 2 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) = 0 THEN UNIX_TIMESTAMP(ss.last_state_change) ELSE NULL END END)',
'services_ok' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 0 THEN 1 ELSE 0 END END)',
'services_ok_last_state_change' => 'MAX(CASE WHEN ss.has_been_checked != 1 THEN NULL ELSE CASE WHEN ss.current_state = 0 THEN UNIX_TIMESTAMP(ss.last_state_change) ELSE NULL END END)',
'services_pending' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 1 ELSE 0 END)',
'services_pending_last_state_change' => 'MAX(CASE WHEN ss.has_been_checked != 1 THEN UNIX_TIMESTAMP(ss.last_state_change) ELSE NULL END)',
'services_total' => 'SUM(1)',
'services_unknown' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 3 THEN 1 ELSE 0 END END)',
'services_unknown_handled' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 3 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) > 0 THEN 1 ELSE 0 END END)',
'services_unknown_handled_last_state_change' => 'MAX(CASE WHEN ss.has_been_checked != 1 THEN NULL ELSE CASE WHEN ss.current_state = 3 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) > 0 THEN UNIX_TIMESTAMP(ss.last_state_change) ELSE NULL END END)',
'services_unknown_unhandled' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 3 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) = 0 THEN 1 ELSE 0 END END)',
'services_unknown_unhandled_last_state_change' => 'MAX(CASE WHEN ss.has_been_checked != 1 THEN NULL ELSE CASE WHEN ss.current_state = 3 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) = 0 THEN UNIX_TIMESTAMP(ss.last_state_change) ELSE NULL END END)',
'services_warning' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 1 THEN 1 ELSE 0 END END)',
'services_warning_handled' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 1 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) > 0 THEN 1 ELSE 0 END END)',
'services_warning_handled_last_state_change' => 'MAX(CASE WHEN ss.has_been_checked != 1 THEN NULL ELSE CASE WHEN ss.current_state = 1 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) > 0 THEN UNIX_TIMESTAMP(ss.last_state_change) ELSE NULL END END)',
'services_warning_unhandled' => 'SUM(CASE WHEN ss.has_been_checked != 1 THEN 0 ELSE CASE WHEN ss.current_state = 1 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) = 0 THEN 1 ELSE 0 END END)',
'services_warning_unhandled_last_state_change' => 'MAX(CASE WHEN ss.has_been_checked != 1 THEN NULL ELSE CASE WHEN ss.current_state = 1 AND (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth) = 0 THEN UNIX_TIMESTAMP(ss.last_state_change) ELSE NULL END END)'
)
);
public function addFilter(Filter $filter)
{
$this->subSelect->applyFilter(clone $filter);
return $this;
}
/**
* {@inheritdoc}
*/
protected function joinBaseTables()
{
// TODO(el): Allow to switch between hard and soft states
$this->subSelect = $this->createSubQuery(
'servicestatus',
array(
'handled' => 'service_handled',
'state' => 'service_state',
'state_change' => 'service_last_state_change'
)
);
$this->select->from(
array('so' => $this->prefix . 'objects'),
array('servicestatussummary' => $this->subSelect->setIsSubQuery(true)),
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',
array()
)->join(
array('h' => $this->prefix . 'hosts'),
'h.host_object_id = s.host_object_id',
array()
)->where(
'so.is_active = ?',
1
)->where(
'so.objecttype_id = ?',
2
);
$this->joinedVirtualTables['servicestatussummary'] = true;
// Provide table services for custom var joins
$this->joinedVirtualTables['services'] = true;
}
/**
* Join host groups
* {@inheritdoc}
*/
protected function joinHostgroups()
public function where($condition, $value = null)
{
$this->select->join(
array('hgm' => $this->prefix . 'hostgroup_members'),
'hgm.host_object_id = s.host_object_id',
array()
)->join(
array('hg' => $this->prefix . 'hostgroups'),
'hg.hostgroup_id = hgm.hostgroup_id',
array()
)->join(
array('hgo' => $this->prefix . 'objects'),
'hgo.object_id = hg.hostgroup_object_id',
array()
)->where(
'hgo.is_active = ?',
1
)->where(
'hgo.objecttype_id = ?',
3
);
}
/**
* Join host status
*/
protected function joinHoststatus()
{
$this->requireVirtualTable('services');
$this->select->join(
array('hs' => $this->prefix . 'hoststatus'),
'hs.host_object_id = so.object_id',
array()
);
}
/**
* Join service groups
*/
protected function joinServicegroups()
{
$this->select->join(
array('sgm' => $this->prefix . 'servicegroup_members'),
'sgm.service_object_id = so.object_id',
array()
)->join(
array('sg' => $this->prefix . 'servicegroups'),
'sgm.servicegroup_id = sg.' . $this->servicegroup_id,
array()
)->join(
array('sgo' => $this->prefix . 'objects'),
'sgo.object_id = sg.servicegroup_object_id',
array()
)->where(
'sgo.is_active = ?',
1
)
->where(
'sgo.objecttype_id = ?',
4
)
->group('sgo.name1');
$this->subSelect->where($condition, $value);
return $this;
}
}