monitoring: Apply host and service restrictions to downtimes

refs #9009
refs #9319
This commit is contained in:
Eric Lippmann 2015-05-26 17:05:47 +02:00
parent 788481e2b1
commit bb9b9bc754
1 changed files with 122 additions and 89 deletions

View File

@ -3,6 +3,11 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Zend_Db_Expr;
use Zend_Db_Select;
use Icinga\Authentication\Manager;
use Icinga\Data\Filter\Filter;
/**
* Query for host and service downtimes
*/
@ -12,127 +17,155 @@ class DowntimeQuery extends IdoQuery
* {@inheritdoc}
*/
protected $columnMap = array(
'downtime' => array(
'downtime_author_name' => 'sd.author_name',
'author' => 'sd.author_name COLLATE latin1_general_ci',
'downtime_comment' => 'sd.comment_data',
'downtime_entry_time' => 'UNIX_TIMESTAMP(sd.entry_time)',
'downtime_is_fixed' => 'sd.is_fixed',
'downtime_is_flexible' => 'CASE WHEN sd.is_fixed = 0 THEN 1 ELSE 0 END',
'downtime_triggered_by_id' => 'sd.triggered_by_id',
'downtime_scheduled_start' => 'UNIX_TIMESTAMP(sd.scheduled_start_time)',
'downtime_scheduled_end' => 'UNIX_TIMESTAMP(sd.scheduled_end_time)',
'downtime_start' => "UNIX_TIMESTAMP(CASE WHEN UNIX_TIMESTAMP(sd.trigger_time) > 0 then sd.trigger_time ELSE sd.scheduled_start_time END)",
'downtime_end' => 'CASE WHEN sd.is_fixed > 0 THEN UNIX_TIMESTAMP(sd.scheduled_end_time) ELSE UNIX_TIMESTAMP(sd.trigger_time) + sd.duration END',
'downtime_duration' => 'sd.duration',
'downtime_is_in_effect' => 'sd.is_in_effect',
'downtime_internal_id' => 'sd.internal_downtime_id',
'downtime_objecttype' => "CASE WHEN ho.object_id IS NULL THEN 'service' ELSE 'host' END",
'host' => 'CASE WHEN ho.name1 IS NULL THEN so.name1 ELSE ho.name1 END COLLATE latin1_general_ci',
'host_name' => 'CASE WHEN ho.name1 IS NULL THEN so.name1 ELSE ho.name1 END',
'service' => 'so.name2 COLLATE latin1_general_ci',
'service_description' => 'so.name2',
'service_host' => 'so.name1 COLLATE latin1_general_ci',
'service_host_name' => 'so.name1'
'downtimes' => array(
'downtime_author' => 'd.downtime_author',
'downtime_author_name' => 'd.downtime_author_name',
'downtime_comment' => 'd.downtime_comment',
'downtime_duration' => 'd.downtime_duration',
'downtime_end' => 'd.downtime_end',
'downtime_entry_time' => 'd.downtime_entry_time',
'downtime_internal_id' => 'd.downtime_internal_id',
'downtime_is_fixed' => 'd.downtime_is_fixed',
'downtime_is_flexible' => 'd.downtime_is_flexible',
'downtime_is_in_effect' => 'd.downtime_is_in_effect',
'downtime_scheduled_end' => 'd.downtime_scheduled_end',
'downtime_scheduled_start' => 'd.downtime_scheduled_start',
'downtime_start' => 'd.downtime_start',
'object_type' => 'd.object_type'
),
'hosts' => array(
'host_display_name' => 'CASE WHEN h.display_name IS NULL THEN sh.display_name ELSE h.display_name END'
),
'hoststatus' => array(
'downtime_host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE hs.current_state END'
'host_display_name' => 'd.host_display_name',
'host_name' => 'd.host_name',
'host_state' => 'd.host_state'
),
'services' => array(
'service_display_name' => 's.display_name'
),
'servicestatus' => array(
'downtime_service_state' => 'CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE ss.current_state END'
'service_description' => 'd.service_description',
'service_display_name' => 'd.service_display_name',
'service_host_name' => 'd.service_host_name',
'service_state' => 'd.service_state'
)
);
/**
* The union
*
* @var Zend_Db_Select
*/
protected $downtimeQuery;
/**
* Subqueries used for the downtime query
*
* @var IdoQuery[]
*/
protected $subQueries = array();
/**
* {@inheritdoc}
*/
public function addFilter(Filter $filter)
{
foreach ($this->subQueries as $sub) {
$sub->applyFilter(clone $filter);
}
return $this;
}
/**
* Apply host restrictions to a query
*
* @param IdoQuery $query
*/
protected function applyHostRestrictions(IdoQuery $query)
{
$hostRestrictions = Filter::matchAny();
foreach (Manager::getInstance()->getRestrictions('monitoring/hosts/filter') as $restriction) {
$hostRestrictions->addFilter(Filter::fromQueryString($restriction));
}
$query->addFilter($hostRestrictions);
}
/**
* Apply host and service restrictions to a query
*
* @param IdoQuery $query
*/
protected function applyServiceRestrictions(IdoQuery $query)
{
$hostAndServiceRestrictions = Filter::matchAll();
$hostRestrictions = Filter::matchAny();
$serviceRestrictions = Filter::matchAny();
foreach (Manager::getInstance()->getRestrictions('monitoring/hosts/filter') as $restriction) {
$hostRestrictions->addFilter(Filter::fromQueryString($restriction));
}
foreach (Manager::getInstance()->getRestrictions('monitoring/services/filter') as $restriction) {
$serviceRestrictions->addFilter(Filter::fromQueryString($restriction));
}
$hostAndServiceRestrictions->addFilter($hostRestrictions);
$hostAndServiceRestrictions->addFilter($serviceRestrictions);
$query->addFilter($hostAndServiceRestrictions);
}
/**
* {@inheritdoc}
*/
protected function joinBaseTables()
{
$this->downtimeQuery = $this->db->select();
$this->select->from(
array('sd' => $this->prefix . 'scheduleddowntime'),
array('d' => $this->downtimeQuery),
array()
);
$this->select->joinLeft(
array('ho' => $this->prefix . 'objects'),
'sd.object_id = ho.object_id AND ho.is_active = 1 AND ho.objecttype_id = 1',
array()
);
$this->select->joinLeft(
array('so' => $this->prefix . 'objects'),
'sd.object_id = so.object_id AND so.is_active = 1 AND so.objecttype_id = 2',
array()
);
$this->joinedVirtualTables = array('downtime' => true);
$this->joinedVirtualTables['downtimes'] = true;
}
/**
* Join downtimes' hosts
*
* @return $this
* Join hosts
*/
protected function joinHosts()
{
$this->select->joinLeft(
array('h' => $this->prefix . 'hosts'),
'h.host_object_id = ho.object_id',
array()
);
return $this;
$columns = array_keys($this->columnMap['downtimes'] + $this->columnMap['hosts']);
foreach (array_keys($this->columnMap['services']) as $column) {
$columns[$column] = new Zend_Db_Expr('NULL');
}
$hosts = $this->createSubQuery('hoststatus', $columns);
$this->applyHostRestrictions($hosts);
$this->subQueries[] = $hosts;
$this->downtimeQuery->union(array($hosts), Zend_Db_Select::SQL_UNION_ALL);
}
/**
* Join downtimes' hosts' status
*
* @return $this
*/
protected function joinHoststatus()
{
$this->select->joinLeft(
array('hs' => $this->prefix . 'hoststatus'),
'ho.object_id = hs.host_object_id',
array()
);
return $this;
}
/**
* Join downtimes' services
*
* @return $this
* Join services
*/
protected function joinServices()
{
$this->select->joinLeft(
array('s' => $this->prefix . 'services'),
's.service_object_id = so.object_id',
array()
);
$this->select->joinLeft(
array('sh' => $this->prefix . 'hosts'),
'sh.host_object_id = s.host_object_id',
array()
);
return $this;
$columns = array_keys($this->columnMap['downtimes'] + $this->columnMap['hosts'] + $this->columnMap['services']);
$services = $this->createSubQuery('servicestatus', $columns);
$this->applyServiceRestrictions($services);
$this->subQueries[] = $services;
$this->downtimeQuery->union(array($services), Zend_Db_Select::SQL_UNION_ALL);
}
/**
* Join downtimes' services' status
*
* @return $this
* {@inheritdoc}
*/
protected function joinServicestatus()
public function order($columnOrAlias, $dir = null)
{
$this->select->joinLeft(
array('ss' => $this->prefix . 'servicestatus'),
'so.object_id = ss.service_object_id',
array()
);
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;
}
}