Ejemplo n.º 1
0
 /**
  * @return Filter
  */
 public function getFilter()
 {
     if ($this->filter === null) {
         $this->filter = Filter::matchAny();
     }
     return $this->filter;
 }
 /**
  * Apply a restriction on the given data view
  *
  * @param   string      $restriction    The name of restriction
  * @param   Filterable  $filterable     The filterable to restrict
  *
  * @return  Filterable  The filterable
  */
 protected static function applyRestriction($restriction, Filterable $filterable)
 {
     $restrictions = Filter::matchAny();
     foreach (Manager::getInstance()->getRestrictions($restriction) as $filter) {
         $restrictions->addFilter(Filter::fromQueryString($filter));
     }
     $filterable->applyFilter($restrictions);
     return $filterable;
 }
Ejemplo n.º 3
0
 /**
  * Return the corresponding filter-object
  *
  * @returns Filter
  */
 public function getFilter()
 {
     $baseFilter = Filter::matchAny(Filter::expression('type', '=', 'hard_state'));
     if ($this->getValue('objecttype', 'hosts') === 'hosts') {
         $objectTypeFilter = Filter::expression('object_type', '=', 'host');
     } else {
         $objectTypeFilter = Filter::expression('object_type', '=', 'service');
     }
     $states = array('cnt_down_hard' => Filter::expression('state', '=', '1'), 'cnt_unreachable_hard' => Filter::expression('state', '=', '2'), 'cnt_up' => Filter::expression('state', '=', '0'), 'cnt_critical_hard' => Filter::expression('state', '=', '2'), 'cnt_warning_hard' => Filter::expression('state', '=', '1'), 'cnt_unknown_hard' => Filter::expression('state', '=', '3'), 'cnt_ok' => Filter::expression('state', '=', '0'));
     $state = $this->getValue('state', 'cnt_critical_hard');
     $stateFilter = $states[$state];
     if (in_array($state, array('cnt_ok', 'cnt_up'))) {
         return Filter::matchAll($objectTypeFilter, $stateFilter);
     }
     return Filter::matchAll($baseFilter, $objectTypeFilter, $stateFilter);
 }
Ejemplo n.º 4
0
 /**
  * Apply a restriction on the given data view
  *
  * @param   string      $restriction    The name of restriction
  * @param   Filterable  $view           The filterable to restrict
  *
  * @return  Filterable  The filterable
  */
 protected function applyRestriction($restriction, Filterable $view)
 {
     $restrictions = Filter::matchAny();
     $restrictions->setAllowedFilterColumns(array('host_name', 'hostgroup_name', 'service_description', 'servicegroup_name', function ($c) {
         return preg_match('/^_(?:host|service)_/', $c);
     }));
     foreach ($this->getRestrictions($restriction) as $filter) {
         try {
             $restrictions->addFilter(Filter::fromQueryString($filter));
         } catch (QueryException $e) {
             throw new ConfigurationError($this->translate('Cannot apply restriction %s using the filter %s. You can only use the following columns: %s'), $restriction, $filter, implode(', ', array('host_name', 'hostgroup_name', 'service_description', 'servicegroup_name', '_(host|service)_<customvar-name>')), $e);
         }
     }
     $view->applyFilter($restrictions);
     return $view;
 }
 /**
  * {@inheritdoc}
  */
 public function onSuccess()
 {
     $cookie = new AnnouncementCookie();
     $repo = new AnnouncementIniRepository();
     $query = $repo->findActive();
     $filter = array();
     foreach ($cookie->getAcknowledged() as $hash) {
         $filter[] = Filter::expression('hash', '=', $hash);
     }
     $query->addFilter(Filter::matchAny($filter));
     $acknowledged = array();
     foreach ($query as $row) {
         $acknowledged[] = $row->hash;
     }
     $acknowledged[] = $this->getElement('hash')->getValue();
     $cookie->setAcknowledged($acknowledged);
     $this->getResponse()->setCookie($cookie);
     return true;
 }
Ejemplo n.º 6
0
 protected function applyChanges($changes)
 {
     $filter = $this->filter;
     $pairs = array();
     $addTo = null;
     $add = array();
     foreach ($changes as $k => $v) {
         if (preg_match('/^(column|value|sign|operator)((?:_new)?)_([\\d-]+)$/', $k, $m)) {
             if ($m[2] === '_new') {
                 if ($addTo !== null && $addTo !== $m[3]) {
                     throw new \Exception('F...U');
                 }
                 $addTo = $m[3];
                 $add[$m[1]] = $v;
             } else {
                 $pairs[$m[3]][$m[1]] = $v;
             }
         }
     }
     $operators = array();
     foreach ($pairs as $id => $fs) {
         if (array_key_exists('operator', $fs)) {
             $operators[$id] = $fs['operator'];
         } else {
             $f = $filter->getById($id);
             $f->setColumn($fs['column']);
             if ($f->getSign() !== $fs['sign']) {
                 if ($f->isRootNode()) {
                     $filter = $f->setSign($fs['sign']);
                 } else {
                     $filter->replaceById($id, $f->setSign($fs['sign']));
                 }
             }
             $f->setExpression($fs['value']);
         }
     }
     krsort($operators, version_compare(PHP_VERSION, '5.4.0') >= 0 ? SORT_NATURAL : SORT_REGULAR);
     foreach ($operators as $id => $operator) {
         $f = $filter->getById($id);
         if ($f->getOperatorName() !== $operator) {
             if ($f->isRootNode()) {
                 $filter = $f->setOperatorName($operator);
             } else {
                 $filter->replaceById($id, $f->setOperatorName($operator));
             }
         }
     }
     if ($addTo !== null) {
         if ($addTo === '0') {
             $filter = Filter::expression($add['column'], $add['sign'], $add['value']);
         } else {
             $parent = $filter->getById($addTo);
             $f = Filter::expression($add['column'], $add['sign'], $add['value']);
             if (isset($add['operator'])) {
                 switch ($add['operator']) {
                     case 'AND':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::matchAll(clone $parent, $f);
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::matchAll(clone $parent, $f));
                             }
                         } else {
                             $parent->addFilter(Filter::matchAll($f));
                         }
                         break;
                     case 'OR':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::matchAny(clone $parent, $f);
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::matchAny(clone $parent, $f));
                             }
                         } else {
                             $parent->addFilter(Filter::matchAny($f));
                         }
                         break;
                     case 'NOT':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::not(Filter::matchAll($parent, $f));
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::not(Filter::matchAll($parent, $f)));
                             }
                         } else {
                             $parent->addFilter(Filter::not($f));
                         }
                         break;
                 }
             } else {
                 $parent->addFilter($f);
             }
         }
     }
     return $filter;
 }
Ejemplo n.º 7
0
 public function flappingFilter()
 {
     return Filter::matchAny(Filter::expression('type', '=', 'flapping'), Filter::expression('type', '=', 'flapping_deleted'));
 }
Ejemplo n.º 8
0
 public function orFilter(Filter $filter)
 {
     return Filter::matchAny($this, $filter);
 }
Ejemplo n.º 9
0
 public function testManualFilterCreation()
 {
     $filter = Filter::matchAll(Filter::where('host', '*localhost*'), Filter::matchAny(Filter::where('service', 'ping'), Filter::matchAll(Filter::where('service', 'www.icinga.org'), Filter::where('state', '0'))));
     $this->assertTrue($filter->matches($this->row(0)));
     $this->assertTrue($filter->matches($this->row(1)));
     $this->assertFalse($filter->matches($this->row(2)));
 }
Ejemplo n.º 10
0
 /**
  * Get the timestamp of the next active announcement
  *
  * @return  int|null
  */
 public function findNextActive()
 {
     $now = new DateTime();
     $query = $this->select(array('start', 'end'))->setFilter(Filter::matchAny(array(Filter::expression('start', '>', $now), Filter::expression('end', '>', $now))));
     $refresh = null;
     foreach ($query as $row) {
         $min = min($row->start->getTimestamp(), $row->end->getTimestamp());
         if ($refresh === null) {
             $refresh = $min;
         } else {
             $refresh = min($refresh, $min);
         }
     }
     return $refresh;
 }
 public function reallyRetrieveStatesFromBackend()
 {
     Benchmark::measure('Retrieving states for business process ' . $this->getName());
     $backend = $this->getBackend();
     // TODO: Split apart, create a dedicated function.
     //       Separate "parse-logic" from "retrieve-state-logic"
     //       Allow DB-based backend
     //       Use IcingaWeb2 Multi-Backend-Support
     $check_results = array();
     $hostFilter = array_keys($this->hosts);
     if ($this->state_type === self::HARD_STATE) {
         $hostStateColumn = 'host_hard_state';
         $hostStateChangeColumn = 'host_last_hard_state_change';
         $serviceStateColumn = 'service_hard_state';
         $serviceStateChangeColumn = 'service_last_hard_state_change';
     } else {
         $hostStateColumn = 'host_state';
         $hostStateChangeColumn = 'host_last_state_change';
         $serviceStateColumn = 'service_state';
         $serviceStateChangeColumn = 'service_last_state_change';
     }
     $filter = Filter::matchAny();
     foreach ($hostFilter as $host) {
         $filter->addFilter(Filter::where('host_name', $host));
     }
     if ($filter->isEmpty()) {
         return $this;
     }
     $hostStatus = $backend->select()->from('hostStatus', array('hostname' => 'host_name', 'last_state_change' => $hostStateChangeColumn, 'in_downtime' => 'host_in_downtime', 'ack' => 'host_acknowledged', 'state' => $hostStateColumn))->applyFilter($filter)->getQuery()->fetchAll();
     $serviceStatus = $backend->select()->from('serviceStatus', array('hostname' => 'host_name', 'service' => 'service_description', 'last_state_change' => $serviceStateChangeColumn, 'in_downtime' => 'service_in_downtime', 'ack' => 'service_acknowledged', 'state' => $serviceStateColumn))->applyFilter($filter)->getQuery()->fetchAll();
     foreach ($serviceStatus as $row) {
         $this->handleDbRow($row);
     }
     foreach ($hostStatus as $row) {
         $this->handleDbRow($row);
     }
     ksort($this->root_nodes);
     Benchmark::measure('Got states for business process ' . $this->getName());
     return $this;
 }
 /**
  * Applies the name filter of EventType restrictions onto a Query
  *
  * @param RepositoryQuery $query
  */
 public function filterEventTypes(RepositoryQuery $query)
 {
     $allowedTypes = $this->canEventTypes();
     if (!empty($allowedTypes)) {
         $filter = Filter::matchAny();
         foreach ($allowedTypes as $type) {
             $here = Filter::where('name', $type);
             $filter->addFilter($here);
         }
         $query->addFilter($filter);
     }
 }