2013-10-10 08:40:13 -04:00
|
|
|
<?php
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Monitoring;
|
|
|
|
|
|
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;
|
2014-06-22 07:49:21 -04:00
|
|
|
use Icinga\Web\Controller\ModuleActionController;
|
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
|
|
|
|
|
*/
|
2014-06-22 07:49:21 -04:00
|
|
|
class Controller extends ModuleActionController
|
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;
|
|
|
|
|
|
2013-10-23 09:38:06 -04:00
|
|
|
/**
|
|
|
|
|
* Compact layout name
|
|
|
|
|
*
|
|
|
|
|
* Set to a string containing the compact layout name to use when
|
|
|
|
|
* 'compact' is set as the layout parameter, otherwise null
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $compactView;
|
2013-10-17 13:48:46 -04:00
|
|
|
|
2014-06-17 08:55:43 -04:00
|
|
|
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
|
|
|
{
|
2013-10-22 09:57:30 -04:00
|
|
|
if ($this->compactView !== null && ($this->_getParam('view', false) === 'compact')) {
|
|
|
|
|
$this->_helper->viewRenderer($this->compactView);
|
|
|
|
|
}
|
|
|
|
|
|
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-01-27 08:24:56 -05:00
|
|
|
* @param Filterable $filterable 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
|
|
|
{
|
|
|
|
|
foreach ($this->getRestrictions($restriction) as $filter) {
|
|
|
|
|
$view->applyFilter(Filter::fromQueryString($filter));
|
|
|
|
|
}
|
|
|
|
|
return $view;
|
|
|
|
|
}
|
2013-10-10 08:40:13 -04:00
|
|
|
}
|
2014-07-16 07:59:43 -04:00
|
|
|
|