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

View File

@ -3,6 +3,11 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query; 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 * Query for host and service downtimes
*/ */
@ -12,127 +17,155 @@ class DowntimeQuery extends IdoQuery
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $columnMap = array( protected $columnMap = array(
'downtime' => array( 'downtimes' => array(
'downtime_author_name' => 'sd.author_name', 'downtime_author' => 'd.downtime_author',
'author' => 'sd.author_name COLLATE latin1_general_ci', 'downtime_author_name' => 'd.downtime_author_name',
'downtime_comment' => 'sd.comment_data', 'downtime_comment' => 'd.downtime_comment',
'downtime_entry_time' => 'UNIX_TIMESTAMP(sd.entry_time)', 'downtime_duration' => 'd.downtime_duration',
'downtime_is_fixed' => 'sd.is_fixed', 'downtime_end' => 'd.downtime_end',
'downtime_is_flexible' => 'CASE WHEN sd.is_fixed = 0 THEN 1 ELSE 0 END', 'downtime_entry_time' => 'd.downtime_entry_time',
'downtime_triggered_by_id' => 'sd.triggered_by_id', 'downtime_internal_id' => 'd.downtime_internal_id',
'downtime_scheduled_start' => 'UNIX_TIMESTAMP(sd.scheduled_start_time)', 'downtime_is_fixed' => 'd.downtime_is_fixed',
'downtime_scheduled_end' => 'UNIX_TIMESTAMP(sd.scheduled_end_time)', 'downtime_is_flexible' => 'd.downtime_is_flexible',
'downtime_start' => "UNIX_TIMESTAMP(CASE WHEN UNIX_TIMESTAMP(sd.trigger_time) > 0 then sd.trigger_time ELSE sd.scheduled_start_time END)", 'downtime_is_in_effect' => 'd.downtime_is_in_effect',
'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_scheduled_end' => 'd.downtime_scheduled_end',
'downtime_duration' => 'sd.duration', 'downtime_scheduled_start' => 'd.downtime_scheduled_start',
'downtime_is_in_effect' => 'sd.is_in_effect', 'downtime_start' => 'd.downtime_start',
'downtime_internal_id' => 'sd.internal_downtime_id', 'object_type' => 'd.object_type'
'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'
), ),
'hosts' => array( 'hosts' => array(
'host_display_name' => 'CASE WHEN h.display_name IS NULL THEN sh.display_name ELSE h.display_name END' 'host_display_name' => 'd.host_display_name',
), 'host_name' => 'd.host_name',
'hoststatus' => array( 'host_state' => 'd.host_state'
'downtime_host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE hs.current_state END'
), ),
'services' => array( 'services' => array(
'service_display_name' => 's.display_name' 'service_description' => 'd.service_description',
), 'service_display_name' => 'd.service_display_name',
'servicestatus' => array( 'service_host_name' => 'd.service_host_name',
'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_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} * {@inheritdoc}
*/ */
protected function joinBaseTables() protected function joinBaseTables()
{ {
$this->downtimeQuery = $this->db->select();
$this->select->from( $this->select->from(
array('sd' => $this->prefix . 'scheduleddowntime'), array('d' => $this->downtimeQuery),
array() array()
); );
$this->select->joinLeft( $this->joinedVirtualTables['downtimes'] = true;
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);
} }
/** /**
* Join downtimes' hosts * Join hosts
*
* @return $this
*/ */
protected function joinHosts() protected function joinHosts()
{ {
$this->select->joinLeft( $columns = array_keys($this->columnMap['downtimes'] + $this->columnMap['hosts']);
array('h' => $this->prefix . 'hosts'), foreach (array_keys($this->columnMap['services']) as $column) {
'h.host_object_id = ho.object_id', $columns[$column] = new Zend_Db_Expr('NULL');
array() }
); $hosts = $this->createSubQuery('hoststatus', $columns);
return $this; $this->applyHostRestrictions($hosts);
$this->subQueries[] = $hosts;
$this->downtimeQuery->union(array($hosts), Zend_Db_Select::SQL_UNION_ALL);
} }
/** /**
* Join downtimes' hosts' status * Join services
*
* @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
*/ */
protected function joinServices() protected function joinServices()
{ {
$this->select->joinLeft( $columns = array_keys($this->columnMap['downtimes'] + $this->columnMap['hosts'] + $this->columnMap['services']);
array('s' => $this->prefix . 'services'), $services = $this->createSubQuery('servicestatus', $columns);
's.service_object_id = so.object_id', $this->applyServiceRestrictions($services);
array() $this->subQueries[] = $services;
); $this->downtimeQuery->union(array($services), Zend_Db_Select::SQL_UNION_ALL);
$this->select->joinLeft(
array('sh' => $this->prefix . 'hosts'),
'sh.host_object_id = s.host_object_id',
array()
);
return $this;
} }
/** /**
* Join downtimes' services' status * {@inheritdoc}
*
* @return $this
*/ */
protected function joinServicestatus() public function order($columnOrAlias, $dir = null)
{ {
$this->select->joinLeft( foreach ($this->subQueries as $sub) {
array('ss' => $this->prefix . 'servicestatus'), $sub->requireColumn($columnOrAlias);
'so.object_id = ss.service_object_id', }
array() 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; return $this;
} }
} }