2013-10-10 14:40:13 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 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;
|
2017-10-05 18:02:43 +02:00
|
|
|
use Icinga\Util\Json;
|
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
|
|
|
{
|
2017-10-17 16:05:07 +02:00
|
|
|
$desiredContentType = $this->getRequest()->getHeader('Accept');
|
|
|
|
if ($desiredContentType === 'application/json') {
|
|
|
|
$desiredFormat = 'json';
|
|
|
|
} elseif ($desiredContentType === 'text/csv') {
|
|
|
|
$desiredFormat = 'csv';
|
|
|
|
} else {
|
|
|
|
$desiredFormat = strtolower($this->params->get('format', 'html'));
|
2013-10-10 14:40:13 +02:00
|
|
|
}
|
2017-10-17 16:05:07 +02:00
|
|
|
|
|
|
|
if ($desiredFormat !== 'html' && ! $this->params->has('limit')) {
|
|
|
|
$query->limit(); // Resets any default limit and offset
|
2013-10-10 14:40:13 +02:00
|
|
|
}
|
2017-10-17 16:05:07 +02:00
|
|
|
|
2017-11-08 10:26:41 +01:00
|
|
|
switch ($desiredFormat) {
|
2017-10-17 16:05:07 +02:00
|
|
|
case 'sql':
|
|
|
|
echo '<pre>'
|
|
|
|
. htmlspecialchars(wordwrap($query->dump()))
|
|
|
|
. '</pre>';
|
|
|
|
exit;
|
|
|
|
case 'json':
|
|
|
|
$response = $this->getResponse();
|
|
|
|
$response
|
|
|
|
->setHeader('Content-Type', 'application/json')
|
|
|
|
->setHeader('Cache-Control', 'no-store')
|
2017-11-08 10:26:41 +01:00
|
|
|
->setHeader(
|
|
|
|
'Content-Disposition',
|
|
|
|
'inline; filename=' . $this->getRequest()->getActionName() . '.json'
|
|
|
|
)
|
2018-06-20 18:03:21 +02:00
|
|
|
->appendBody(Json::sanitize($query->fetchAll()))
|
2017-10-17 16:05:07 +02:00
|
|
|
->sendResponse();
|
|
|
|
exit;
|
|
|
|
case 'csv':
|
|
|
|
$response = $this->getResponse();
|
|
|
|
$response
|
|
|
|
->setHeader('Content-Type', 'text/csv')
|
|
|
|
->setHeader('Cache-Control', 'no-store')
|
2017-11-08 10:26:41 +01:00
|
|
|
->setHeader(
|
|
|
|
'Content-Disposition',
|
|
|
|
'attachment; filename=' . $this->getRequest()->getActionName() . '.csv'
|
|
|
|
)
|
2017-10-17 16:05:07 +02:00
|
|
|
->appendBody((string) Csv::fromQuery($query))
|
|
|
|
->sendResponse();
|
|
|
|
exit;
|
2013-10-10 14:40:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-27 14:22:37 +01:00
|
|
|
|
|
|
|
/**
|
2015-08-20 13:07:57 +02:00
|
|
|
* Apply a restriction of the authenticated on the given filterable
|
2015-01-27 14:22:37 +01:00
|
|
|
*
|
2015-08-20 13:07:57 +02:00
|
|
|
* @param string $name Name of the restriction
|
|
|
|
* @param Filterable $filterable Filterable to restrict
|
2015-01-27 14:22:37 +01:00
|
|
|
*
|
2015-08-20 13:07:57 +02:00
|
|
|
* @return Filterable The filterable having the restriction applied
|
2015-01-27 14:22:37 +01:00
|
|
|
*/
|
2015-08-20 13:07:57 +02:00
|
|
|
protected function applyRestriction($name, Filterable $filterable)
|
2015-01-27 14:22:37 +01:00
|
|
|
{
|
2015-08-20 14:24:06 +02:00
|
|
|
$filterable->applyFilter($this->getRestriction($name));
|
2015-08-20 13:07:57 +02:00
|
|
|
return $filterable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a restriction of the authenticated
|
|
|
|
*
|
|
|
|
* @param string $name Name of the restriction
|
|
|
|
*
|
|
|
|
* @return Filter|null Filter object or null if the authenticated user is not restricted
|
|
|
|
* @throws ConfigurationError If the restriction contains invalid filter columns
|
|
|
|
*/
|
|
|
|
protected function getRestriction($name)
|
|
|
|
{
|
|
|
|
$restriction = Filter::matchAny();
|
|
|
|
$restriction->setAllowedFilterColumns(array(
|
2015-06-18 16:42:27 +02:00
|
|
|
'host_name',
|
|
|
|
'hostgroup_name',
|
2015-09-07 11:33:44 +02:00
|
|
|
'instance_name',
|
2015-06-18 16:42:27 +02:00
|
|
|
'service_description',
|
|
|
|
'servicegroup_name',
|
|
|
|
function ($c) {
|
2015-09-28 16:01:13 +02:00
|
|
|
return preg_match('/^_(?:host|service)_/i', $c);
|
2015-06-18 16:42:27 +02:00
|
|
|
}
|
|
|
|
));
|
2015-08-20 13:07:57 +02:00
|
|
|
foreach ($this->getRestrictions($name) as $filter) {
|
2015-08-04 16:28:45 +02:00
|
|
|
if ($filter === '*') {
|
2015-08-20 14:24:06 +02:00
|
|
|
return Filter::matchAny();
|
2015-08-04 16:28:45 +02:00
|
|
|
}
|
2015-06-18 16:42:27 +02:00
|
|
|
try {
|
2015-08-20 13:07:57 +02:00
|
|
|
$restriction->addFilter(Filter::fromQueryString($filter));
|
2015-06-18 16:42:27 +02:00
|
|
|
} catch (QueryException $e) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
$this->translate(
|
|
|
|
'Cannot apply restriction %s using the filter %s. You can only use the following columns: %s'
|
|
|
|
),
|
2015-08-20 13:07:57 +02:00
|
|
|
$name,
|
2015-06-18 16:42:27 +02:00
|
|
|
$filter,
|
|
|
|
implode(', ', array(
|
2015-08-25 16:39:40 +02:00
|
|
|
'instance_name',
|
2015-06-18 16:42:27 +02:00
|
|
|
'host_name',
|
|
|
|
'hostgroup_name',
|
|
|
|
'service_description',
|
|
|
|
'servicegroup_name',
|
|
|
|
'_(host|service)_<customvar-name>'
|
|
|
|
)),
|
|
|
|
$e
|
|
|
|
);
|
|
|
|
}
|
2015-01-27 14:22:37 +01:00
|
|
|
}
|
2015-08-20 13:07:57 +02:00
|
|
|
return $restriction;
|
2015-01-27 14:22:37 +01:00
|
|
|
}
|
2013-10-10 14:40:13 +02:00
|
|
|
}
|