2013-10-10 14:40:13 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-10-10 14:40:13 +02:00
|
|
|
|
|
|
|
namespace Icinga\Module\Monitoring;
|
|
|
|
|
2015-06-18 16:42:27 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
use Icinga\Exception\QueryException;
|
2015-01-27 14:22:37 +01:00
|
|
|
use Icinga\Data\Filter\Filter;
|
2015-01-27 14:24:56 +01:00
|
|
|
use Icinga\Data\Filterable;
|
2015-01-27 14:22:37 +01:00
|
|
|
use Icinga\File\Csv;
|
2015-04-08 15:08:14 +02:00
|
|
|
use Icinga\Web\Controller as IcingaWebController;
|
2014-06-20 12:26:00 +02:00
|
|
|
use Icinga\Web\Url;
|
2013-10-10 14:40:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for all monitoring action controller
|
|
|
|
*/
|
2015-04-08 15:08:14 +02:00
|
|
|
class Controller extends IcingaWebController
|
2013-10-10 14:40:13 +02:00
|
|
|
{
|
2014-06-17 14:55:43 +02:00
|
|
|
/**
|
|
|
|
* The backend used for this controller
|
|
|
|
*
|
|
|
|
* @var Backend
|
|
|
|
*/
|
|
|
|
protected $backend;
|
|
|
|
|
|
|
|
protected function moduleInit()
|
|
|
|
{
|
|
|
|
$this->backend = Backend::createBackend($this->_getParam('backend'));
|
2014-06-20 12:26:00 +02:00
|
|
|
$this->view->url = Url::fromRequest();
|
2014-06-17 14:55:43 +02:00
|
|
|
}
|
|
|
|
|
2013-10-22 15:57:30 +02:00
|
|
|
protected function handleFormatRequest($query)
|
2013-10-10 14:40:13 +02:00
|
|
|
{
|
2014-03-06 10:17:55 +01:00
|
|
|
if ($this->_getParam('format') === 'sql') {
|
2013-10-10 14:40:13 +02:00
|
|
|
echo '<pre>'
|
|
|
|
. htmlspecialchars(wordwrap($query->dump()))
|
|
|
|
. '</pre>';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if ($this->_getParam('format') === 'json'
|
2013-10-17 21:40:02 +02:00
|
|
|
|| $this->_request->getHeader('Accept') === 'application/json') {
|
2013-10-10 14:40:13 +02:00
|
|
|
header('Content-type: application/json');
|
2014-06-21 00:09:11 +02:00
|
|
|
echo json_encode($query->getQuery()->fetchAll());
|
2013-10-10 14:40:13 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if ($this->_getParam('format') === 'csv'
|
|
|
|
|| $this->_request->getHeader('Accept') === 'text/csv') {
|
|
|
|
Csv::fromQuery($query)->dump();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 14:22:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply a restriction on the given data view
|
|
|
|
*
|
|
|
|
* @param string $restriction The name of restriction
|
2015-06-18 16:42:27 +02:00
|
|
|
* @param Filterable $view The filterable to restrict
|
2015-01-27 14:22:37 +01:00
|
|
|
*
|
2015-01-27 14:24:56 +01:00
|
|
|
* @return Filterable The filterable
|
2015-01-27 14:22:37 +01:00
|
|
|
*/
|
2015-01-27 14:24:56 +01:00
|
|
|
protected function applyRestriction($restriction, Filterable $view)
|
2015-01-27 14:22:37 +01:00
|
|
|
{
|
2015-05-29 11:40:26 +02:00
|
|
|
$restrictions = Filter::matchAny();
|
2015-06-18 16:42:27 +02:00
|
|
|
$restrictions->setAllowedFilterColumns(array(
|
|
|
|
'host_name',
|
|
|
|
'hostgroup_name',
|
|
|
|
'service_description',
|
|
|
|
'servicegroup_name',
|
|
|
|
function ($c) {
|
|
|
|
return preg_match('/^_(?:host|service)_/', $c);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2015-01-27 14:22:37 +01:00
|
|
|
foreach ($this->getRestrictions($restriction) as $filter) {
|
2015-08-04 16:28:45 +02:00
|
|
|
if ($filter === '*') {
|
|
|
|
return $view;
|
|
|
|
}
|
2015-06-18 16:42:27 +02:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
2015-01-27 14:22:37 +01:00
|
|
|
}
|
2015-06-18 16:42:27 +02:00
|
|
|
|
2015-05-29 11:40:26 +02:00
|
|
|
$view->applyFilter($restrictions);
|
2015-01-27 14:22:37 +01:00
|
|
|
return $view;
|
|
|
|
}
|
2013-10-10 14:40:13 +02:00
|
|
|
}
|
2014-07-16 13:59:43 +02:00
|
|
|
|