icingaweb2/modules/monitoring/library/Monitoring/Controller.php

102 lines
3.1 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring;
use Icinga\Application\Config as IcingaConfig;
use Icinga\Module\Monitoring\DataView\ServiceStatus as ServiceStatusView;
use Icinga\Web\Controller\ActionController;
/**
* Base class for all monitoring action controller
*/
class Controller extends ActionController
{
/**
* Retrieve services from either given parameters or request
*
* @param array $params
*
* @return \Zend_Paginator
*/
protected function fetchServices(array $params = null)
{
$columns = array(
'host_name',
'host_state',
'host_state_type',
'host_last_state_change',
'host_address',
'host_handled',
'service_description',
'service_display_name',
'service_state',
'service_in_downtime',
'service_acknowledged',
'service_handled',
'service_output',
2013-10-19 20:09:17 +02:00
'service_attempt',
'service_last_state_change',
'service_icon_image',
'service_long_output',
'service_is_flapping',
'service_state_type',
'service_handled',
'service_severity',
'service_last_check',
'service_notifications_enabled',
'service_action_url',
'service_notes_url',
'service_last_comment',
'service_active_checks_enabled',
'service_passive_checks_enabled',
'current_check_attempt' => 'service_current_check_attempt',
'max_check_attempts' => 'service_max_check_attempts'
);
if ($params === null) {
$query = ServiceStatusView::fromRequest(
$this->_request,
$columns
)->getQuery();
} else {
$params['backend'] = $this->_request->getParam('backend');
$query = ServiceStatusView::fromParams(
$params,
$columns
)->getQuery();
}
$this->handleFormatRequest($query);
return $query->paginate();
}
protected function handleFormatRequest($query)
{
if ($this->compactView !== null && ($this->_getParam('view', false) === 'compact')) {
$this->_helper->viewRenderer($this->compactView);
}
if ($this->_getParam('format') === 'sql'
&& IcingaConfig::app()->global->get('environment', 'production') === 'development') {
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') {
header('Content-type: application/json');
echo json_encode($query->fetchAll());
exit;
}
if ($this->_getParam('format') === 'csv'
|| $this->_request->getHeader('Accept') === 'text/csv') {
Csv::fromQuery($query)->dump();
exit;
}
}
}
// @codingStandardsIgnoreEnd