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