mirror of
https://github.com/Icinga/icingaweb2-module-graphite.git
synced 2026-05-28 04:34:57 -04:00
Use Icinga\Web\Controller as the base class for MonitoringAwareController instead of Icinga\Module\Monitoring\Controller
Graphite module should work also when monitoring module is not installed. Hence the `MonitoringAwareController` which is the base class for `ListController` and `MonitoringGraphController`should not use `Icinga\Module\Monitoring\Controller` as its base class (use class `Icinga\Web\Controller` instead). Add the required methods in `Icinga\Module\Monitoring\Controller` directly to `MonitoringGraphController`.
This commit is contained in:
parent
7a616caf65
commit
eb959fda73
1 changed files with 138 additions and 2 deletions
|
|
@ -2,10 +2,20 @@
|
|||
|
||||
namespace Icinga\Module\Graphite\Web\Controller;
|
||||
|
||||
use ArrayIterator;
|
||||
use Icinga\Application\Modules\Module;
|
||||
use Icinga\Data\Filter\Filter;
|
||||
use Icinga\Data\Filterable;
|
||||
use Icinga\Exception\ConfigurationError;
|
||||
use Icinga\Exception\QueryException;
|
||||
use Icinga\Module\Graphite\ProvidedHook\Icingadb\IcingadbSupport;
|
||||
use Icinga\Module\Monitoring\Controller;
|
||||
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
|
||||
use Icinga\Module\Monitoring\Data\CustomvarProtectionIterator;
|
||||
use Icinga\Module\Monitoring\DataView\DataView;
|
||||
use Icinga\Util\Json;
|
||||
use Icinga\File\Csv;
|
||||
use Icinga\Web\Controller;
|
||||
use Icinga\Web\Url;
|
||||
|
||||
abstract class MonitoringAwareController extends Controller
|
||||
{
|
||||
|
|
@ -34,6 +44,132 @@ abstract class MonitoringAwareController extends Controller
|
|||
return;
|
||||
}
|
||||
|
||||
parent::moduleInit();
|
||||
$this->backend = MonitoringBackend::instance($this->_getParam('backend'));
|
||||
$this->view->url = Url::fromRequest();
|
||||
}
|
||||
|
||||
|
||||
protected function handleFormatRequest($query)
|
||||
{
|
||||
$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'));
|
||||
}
|
||||
|
||||
if ($desiredFormat !== 'html' && ! $this->params->has('limit')) {
|
||||
$query->limit(); // Resets any default limit and offset
|
||||
}
|
||||
|
||||
switch ($desiredFormat) {
|
||||
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')
|
||||
->setHeader(
|
||||
'Content-Disposition',
|
||||
'inline; filename=' . $this->getRequest()->getActionName() . '.json'
|
||||
)
|
||||
->appendBody(
|
||||
Json::sanitize(
|
||||
iterator_to_array(
|
||||
new CustomvarProtectionIterator(
|
||||
new ArrayIterator($query->fetchAll())
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
->sendResponse();
|
||||
exit;
|
||||
case 'csv':
|
||||
$response = $this->getResponse();
|
||||
$response
|
||||
->setHeader('Content-Type', 'text/csv')
|
||||
->setHeader('Cache-Control', 'no-store')
|
||||
->setHeader(
|
||||
'Content-Disposition',
|
||||
'attachment; filename=' . $this->getRequest()->getActionName() . '.csv'
|
||||
)
|
||||
->appendBody((string) Csv::fromQuery(new CustomvarProtectionIterator($query)))
|
||||
->sendResponse();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a restriction of the authenticated on the given filterable
|
||||
*
|
||||
* @param string $name Name of the restriction
|
||||
* @param Filterable $filterable Filterable to restrict
|
||||
*
|
||||
* @return Filterable The filterable having the restriction applied
|
||||
*/
|
||||
protected function applyRestriction($name, Filterable $filterable)
|
||||
{
|
||||
$filterable->applyFilter($this->getRestriction($name));
|
||||
return $filterable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a restriction of the authenticated
|
||||
*
|
||||
* @param string $name Name of the restriction
|
||||
*
|
||||
* @return Filter Filter object
|
||||
* @throws ConfigurationError If the restriction contains invalid filter columns
|
||||
*/
|
||||
protected function getRestriction($name)
|
||||
{
|
||||
$restriction = Filter::matchAny();
|
||||
$restriction->setAllowedFilterColumns(array(
|
||||
'host_name',
|
||||
'hostgroup_name',
|
||||
'instance_name',
|
||||
'service_description',
|
||||
'servicegroup_name',
|
||||
function ($c) {
|
||||
return preg_match('/^_(?:host|service)_/i', $c);
|
||||
}
|
||||
));
|
||||
foreach ($this->getRestrictions($name) as $filter) {
|
||||
if ($filter === '*') {
|
||||
return Filter::matchAll();
|
||||
}
|
||||
try {
|
||||
$restriction->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'
|
||||
),
|
||||
$name,
|
||||
$filter,
|
||||
implode(', ', array(
|
||||
'instance_name',
|
||||
'host_name',
|
||||
'hostgroup_name',
|
||||
'service_description',
|
||||
'servicegroup_name',
|
||||
'_(host|service)_<customvar-name>'
|
||||
)),
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($restriction->isEmpty()) {
|
||||
return Filter::matchAll();
|
||||
}
|
||||
|
||||
return $restriction;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue