2013-06-03 16:36:10 +02:00
|
|
|
<?php
|
2013-06-06 16:52:54 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-22 16:03:36 +02:00
|
|
|
/**
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring frontends
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
2013-06-06 16:52:54 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2013-06-03 16:36:10 +02:00
|
|
|
namespace Icinga\Backend\Statusdat;
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
use Icinga\Backend\Criteria\Order;
|
2013-06-03 16:36:10 +02:00
|
|
|
use Icinga\Backend\MonitoringObjectList as MList;
|
|
|
|
use Icinga\Protocol\Statusdat;
|
|
|
|
use Icinga\Exception;
|
2013-06-06 16:52:54 +02:00
|
|
|
use Icinga\Backend\Query as BaseQuery;
|
2013-06-03 16:36:10 +02:00
|
|
|
|
|
|
|
/**
|
2013-06-06 16:52:54 +02:00
|
|
|
* Class Query
|
|
|
|
* @package Icinga\Backend\Statusdat
|
2013-06-03 16:36:10 +02:00
|
|
|
*/
|
2013-06-06 16:52:54 +02:00
|
|
|
abstract class Query extends BaseQuery
|
2013-06-03 16:36:10 +02:00
|
|
|
{
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @var null
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
protected $cursor = null;
|
2013-06-06 16:52:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
protected $view = 'Icinga\Backend\Statusdat\DataView\StatusdatServiceView';
|
2013-06-06 16:52:54 +02:00
|
|
|
|
2013-06-03 16:36:10 +02:00
|
|
|
/**
|
|
|
|
* @var array Mapping of order to field names
|
|
|
|
* @todo Is not complete right now
|
|
|
|
*/
|
|
|
|
protected $orderColumns = array(
|
2013-06-06 16:52:54 +02:00
|
|
|
Order::SERVICE_STATE => "status.current_state",
|
|
|
|
Order::STATE_CHANGE => "status.last_state_change",
|
|
|
|
Order::HOST_STATE => "status.current_state",
|
|
|
|
Order::HOST_NAME => "host_name",
|
|
|
|
Order::SERVICE_NAME => "service_description"
|
2013-06-03 16:36:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the apply%Filtername%Filter() method for the given filter, or simply calls
|
|
|
|
* where(), if the method is not available.
|
|
|
|
*
|
|
|
|
* @see \Icinga\Backend\Query For the parent definition
|
|
|
|
*
|
|
|
|
* @param array $filters An array of "filtername"=>"value" definitions
|
|
|
|
*
|
|
|
|
* @return Query Returns the query object to allow fluent calls
|
|
|
|
*/
|
|
|
|
public function applyFilters(array $filters = array())
|
|
|
|
{
|
|
|
|
foreach ($filters as $filter => $value) {
|
|
|
|
$filter[0] = strtoupper($filter[0]);
|
|
|
|
$filterMethod = "apply" . $filter . "Filter";
|
2013-06-06 16:52:54 +02:00
|
|
|
if (method_exists($this, $filterMethod)) {
|
2013-06-03 16:36:10 +02:00
|
|
|
$this->$filterMethod($filter, $value);
|
2013-06-06 16:52:54 +02:00
|
|
|
} else {
|
2013-06-03 16:36:10 +02:00
|
|
|
$this->where($filter, $value);
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:36:10 +02:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a filter to only show open problems, or non problems, depending whether value is true or false
|
|
|
|
*
|
|
|
|
* @param $type ignored
|
|
|
|
* @param $value Whether problems should be shown (1) or non problems (0)
|
|
|
|
*/
|
|
|
|
public function applyProblemsFilter($type, $value)
|
|
|
|
{
|
|
|
|
if ($value) { // Status.dat only contains active downtimes
|
|
|
|
$value = array(1, 0);
|
|
|
|
$this->where("(status.current_state >= ? and COUNT{status.downtime} = ? )", $value);
|
|
|
|
} else {
|
|
|
|
$value = array(0, 1);
|
|
|
|
$this->where("(status.current_state < 1 or COUNT{status.downtime} > ? )", $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic object search by host name, service description and plugin output
|
|
|
|
*
|
|
|
|
* @param $type ignored
|
|
|
|
* @param $value The string to search for
|
|
|
|
*/
|
|
|
|
public function applySearchFilter($type, $value)
|
|
|
|
{
|
|
|
|
$text = "%$value%";
|
|
|
|
$val = array($text, $text, $text);
|
|
|
|
|
|
|
|
$this->query->where("(host_name LIKE ? OR service_description LIKE ? OR status.plugin_output LIKE ?)", $val);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a hostgroup filter on this object
|
|
|
|
*
|
|
|
|
* @param $type ignored
|
|
|
|
* @param $value The hostgroup to filter for
|
|
|
|
*/
|
|
|
|
public function applyHostgroupsFilter($type, $value)
|
|
|
|
{
|
|
|
|
$filter = array($value);
|
|
|
|
$this->query->where("host.group IN ?", $filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a servicegroup filter on this object
|
|
|
|
*
|
|
|
|
* @param $type ignored
|
|
|
|
* @param $value The servicegroup to filter for
|
|
|
|
*/
|
|
|
|
public function applyServicegroupsFilter($type, $value)
|
|
|
|
{
|
|
|
|
$filter = array($value);
|
|
|
|
$this->query->where("group IN ?", $filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters by handled problems or unhandled
|
|
|
|
*
|
|
|
|
* @todo: Add downtime
|
|
|
|
* @param $type
|
|
|
|
* @param $value Whether to search for unhandled (0) or handled (1)
|
|
|
|
*/
|
|
|
|
public function applyHandledFilter($type, $value)
|
|
|
|
{
|
|
|
|
$val = array($value, $value);
|
|
|
|
$this->query->where("(status.problem_has_been_acknowledged = ? )", $val);
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @param $value
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function applyHostnameFilter($type, $value)
|
|
|
|
{
|
2013-06-06 16:52:54 +02:00
|
|
|
if (!is_array($value)) {
|
2013-06-03 16:36:10 +02:00
|
|
|
$value = array($value);
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:36:10 +02:00
|
|
|
$this->query->where("host_name LIKE ?", $value);
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @param $value
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function applyStateFilter($type, $value)
|
|
|
|
{
|
|
|
|
$this->query->where("status.current_state = $value");
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @param $value
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function applyHoststateFilter($type, $value)
|
|
|
|
{
|
|
|
|
$this->query->where("host.status.current_state = $value");
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @param $value
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function applyServiceDescriptionFilter($type, $value)
|
|
|
|
{
|
2013-06-06 16:52:54 +02:00
|
|
|
if (!is_array($value)) {
|
2013-06-03 16:36:10 +02:00
|
|
|
$value = array($value);
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:36:10 +02:00
|
|
|
$this->query->where("service_description LIKE ?", $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limits this query and offsets it
|
|
|
|
* @param null|integer $count The maximum element count to display
|
|
|
|
* @param null|integer $offset The offset to start counting
|
|
|
|
* @return Query This object, for fluent interface
|
|
|
|
*/
|
|
|
|
public function limit($count = null, $offset = null)
|
|
|
|
{
|
|
|
|
$this->query->limit($count, $offset);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Orders the resultset
|
|
|
|
*
|
|
|
|
* @param string $column Either a string in the 'FIELD ASC/DESC format or only the field
|
|
|
|
* @param null $dir 'asc' or 'desc'
|
|
|
|
* @return Query Returns this query,for fluent interface
|
|
|
|
*/
|
|
|
|
public function order($column = '', $dir = null)
|
|
|
|
{
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
if ($column) {
|
2013-06-03 16:36:10 +02:00
|
|
|
$this->query->order($this->orderColumns[$column], strtolower($dir));
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:36:10 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a filter on this query by calling the statusdat where() function
|
|
|
|
*
|
2013-06-06 16:52:54 +02:00
|
|
|
* @param $column The (statusdat!) column to filter in "field operator ?"
|
|
|
|
* format. (@example status.current_state > ?)
|
2013-06-03 16:36:10 +02:00
|
|
|
* @param mixed $value The value to filter for
|
|
|
|
* @return Query Returns this query,for fluent interface
|
|
|
|
*/
|
|
|
|
public function where($column, $value = null)
|
|
|
|
{
|
2013-06-06 16:52:54 +02:00
|
|
|
if (!is_array($value)) {
|
2013-06-03 16:36:10 +02:00
|
|
|
$value = array($value);
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:36:10 +02:00
|
|
|
$this->query->where($column, $value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @return MList|mixed|null
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function fetchAll()
|
|
|
|
{
|
|
|
|
$view = $this->view;
|
2013-06-06 16:52:54 +02:00
|
|
|
if (!$this->cursor) {
|
2013-06-03 16:36:10 +02:00
|
|
|
$this->cursor = new MList($this->query->getResult(), new $view($this->reader));
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|
2013-06-03 16:36:10 +02:00
|
|
|
return $this->cursor;
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function fetchRow()
|
|
|
|
{
|
|
|
|
return next($this->fetchAll());
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @return mixed|void
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function fetchPairs()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function fetchOne()
|
|
|
|
{
|
|
|
|
return next($this->fetchAll());
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:52:54 +02:00
|
|
|
/**
|
|
|
|
* @return int|mixed
|
|
|
|
*/
|
2013-06-03 16:36:10 +02:00
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->query->getResult());
|
|
|
|
}
|
2013-06-06 16:52:54 +02:00
|
|
|
}
|