2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
2013-08-19 15:24:09 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2013-08-20 15:32:25 +02:00
|
|
|
namespace Icinga\Module\Monitoring;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2014-04-17 15:53:23 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
use Icinga\Data\Selectable;
|
|
|
|
use Icinga\Data\Queryable;
|
2014-06-06 09:05:21 +02:00
|
|
|
use Icinga\Data\ConnectionInterface;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2014-05-06 17:01:43 +02:00
|
|
|
use Icinga\Application\Config as IcingaConfig;
|
|
|
|
use Icinga\Data\ResourceFactory;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
|
2014-04-17 15:53:23 +02:00
|
|
|
/**
|
|
|
|
* Data view and query loader tied to a backend type
|
|
|
|
*/
|
2014-06-06 09:05:21 +02:00
|
|
|
class Backend implements Selectable, Queryable, ConnectionInterface
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2014-04-17 15:53:23 +02:00
|
|
|
* Resource
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @var mixed
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2014-04-17 15:53:23 +02:00
|
|
|
protected $resource;
|
2013-06-27 13:04:47 +02:00
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2014-04-17 15:53:23 +02:00
|
|
|
* Type
|
2013-09-24 15:26:10 +02:00
|
|
|
*
|
2014-04-17 15:53:23 +02:00
|
|
|
* @var string
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2014-04-17 15:53:23 +02:00
|
|
|
protected $type;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2014-06-21 04:25:02 +02:00
|
|
|
protected $name;
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2014-04-17 15:53:23 +02:00
|
|
|
* Create a new backend
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2014-04-17 15:53:23 +02:00
|
|
|
* @param mixed $resource
|
|
|
|
* @param string $type
|
2013-09-24 15:26:10 +02:00
|
|
|
*/
|
2014-04-17 15:53:23 +02:00
|
|
|
public function __construct($resource, $type)
|
2013-09-24 15:26:10 +02:00
|
|
|
{
|
2014-04-17 15:53:23 +02:00
|
|
|
$this->resource = $resource;
|
|
|
|
$this->type = $type;
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 04:25:02 +02:00
|
|
|
// Temporary workaround, we have no way to know our name
|
|
|
|
protected function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2014-05-06 17:01:43 +02:00
|
|
|
/**
|
|
|
|
* Create a backend
|
|
|
|
*
|
|
|
|
* @param string $backendName Name of the backend or null for creating the default backend which is the first INI
|
|
|
|
* configuration entry not being disabled
|
|
|
|
*
|
|
|
|
* @return Backend
|
|
|
|
* @throws ConfigurationError When no backend has been configured or all backends are disabled or the
|
|
|
|
* configuration for the requested backend does either not exist or it's disabled
|
|
|
|
*/
|
|
|
|
public static function createBackend($backendName = null)
|
|
|
|
{
|
|
|
|
$allBackends = array();
|
|
|
|
$defaultBackend = null;
|
|
|
|
foreach (IcingaConfig::module('monitoring', 'backends') as $name => $config) {
|
|
|
|
if (!(bool) $config->get('disabled', false) && $defaultBackend === null) {
|
|
|
|
$defaultBackend = $config;
|
|
|
|
}
|
|
|
|
$allBackends[$name] = $config;
|
|
|
|
}
|
|
|
|
if (empty($allBackends)) {
|
|
|
|
throw new ConfigurationError('No backend has been configured');
|
|
|
|
}
|
|
|
|
if ($defaultBackend === null) {
|
|
|
|
throw new ConfigurationError('All backends are disabled');
|
|
|
|
}
|
|
|
|
if ($backendName === null) {
|
|
|
|
$backendConfig = $defaultBackend;
|
|
|
|
} else {
|
|
|
|
if (!array_key_exists($backendName, $allBackends)) {
|
|
|
|
throw new ConfigurationError('No configuration for backend ' . $backendName);
|
|
|
|
}
|
|
|
|
$backendConfig = $allBackends[$backendName];
|
|
|
|
if ((bool) $backendConfig->get('disabled', false)) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'Configuration for backend ' . $backendName . ' available but backend is disabled'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$resource = ResourceFactory::createResource(ResourceFactory::getResourceConfig($backendConfig->resource));
|
|
|
|
if ($backendConfig->type === 'ido' && $resource->getDbType() !== 'oracle') {
|
|
|
|
// TODO(el): The resource should set the table prefix
|
|
|
|
$resource->setTablePrefix('icinga_');
|
|
|
|
}
|
2014-06-21 04:25:02 +02:00
|
|
|
$backend = new Backend($resource, $backendConfig->type);
|
|
|
|
$backend->setName($backendName);
|
|
|
|
return $backend;
|
2014-05-06 17:01:43 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 09:05:21 +02:00
|
|
|
public function getResource()
|
|
|
|
{
|
|
|
|
return $this->resource;
|
|
|
|
}
|
|
|
|
|
2013-09-24 15:26:10 +02:00
|
|
|
/**
|
|
|
|
* Backend entry point
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2014-04-17 15:53:23 +02:00
|
|
|
* @return self
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
public function select()
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-09-24 15:26:10 +02:00
|
|
|
return $this;
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2014-04-17 15:53:23 +02:00
|
|
|
* Create a data view to fetch data from
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2014-04-17 15:53:23 +02:00
|
|
|
* @param string $viewName
|
2013-09-24 15:26:10 +02:00
|
|
|
* @param array $columns
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2014-04-17 15:53:23 +02:00
|
|
|
* @return DataView
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2014-04-17 15:53:23 +02:00
|
|
|
public function from($viewName, array $columns = null)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2014-04-17 15:53:23 +02:00
|
|
|
$viewClass = $this->resolveDataViewName($viewName);
|
2014-06-06 09:05:21 +02:00
|
|
|
return new $viewClass($this, $columns);
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-17 15:53:23 +02:00
|
|
|
* View name to class name resolution
|
2013-09-24 15:26:10 +02:00
|
|
|
*
|
2014-04-17 15:53:23 +02:00
|
|
|
* @param string $viewName
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @return string
|
2014-04-17 15:53:23 +02:00
|
|
|
* @throws ProgrammingError When the view does not exist
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2014-04-17 15:53:23 +02:00
|
|
|
protected function resolveDataViewName($viewName)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2014-04-17 15:53:23 +02:00
|
|
|
$viewClass = '\\Icinga\\Module\\Monitoring\\DataView\\' . ucfirst($viewName);
|
|
|
|
if (!class_exists($viewClass)) {
|
|
|
|
throw new ProgrammingError('DataView ' . ucfirst($viewName) . ' does not exist');
|
2013-10-22 16:04:38 +02:00
|
|
|
}
|
2014-04-17 15:53:23 +02:00
|
|
|
return $viewClass;
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 09:05:21 +02:00
|
|
|
public function getQueryClass($name)
|
|
|
|
{
|
|
|
|
return $this->resolveQueryName($name);
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2014-04-17 15:53:23 +02:00
|
|
|
* Query name to class name resolution
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2014-04-17 15:53:23 +02:00
|
|
|
* @param string $queryName
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2014-04-17 15:53:23 +02:00
|
|
|
* @return string
|
|
|
|
* @throws ProgrammingError When the query does not exist for this backend
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2014-04-17 15:53:23 +02:00
|
|
|
protected function resolveQueryName($queryName)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2014-04-17 15:53:23 +02:00
|
|
|
$queryClass = '\\Icinga\\Module\\Monitoring\\Backend\\'
|
|
|
|
. ucfirst($this->type)
|
|
|
|
. '\\Query\\'
|
|
|
|
. ucfirst($queryName)
|
|
|
|
. 'Query';
|
|
|
|
if (!class_exists($queryClass)) {
|
|
|
|
throw new ProgrammingError(
|
2014-06-06 09:05:21 +02:00
|
|
|
'Query "' . ucfirst($queryName) . '" does not exist for backend ' . ucfirst($this->type)
|
2013-10-22 12:31:28 +02:00
|
|
|
);
|
|
|
|
}
|
2014-04-17 15:53:23 +02:00
|
|
|
return $queryClass;
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
}
|