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-11-07 13:53:03 +01:00
|
|
|
use Icinga\Application\Config;
|
2014-05-06 17:01:43 +02:00
|
|
|
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)
|
|
|
|
{
|
2014-11-07 13:53:03 +01:00
|
|
|
$config = Config::module('monitoring', 'backends');
|
2014-09-01 16:11:34 +02:00
|
|
|
if ($config->count() === 0) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new ConfigurationError(mt('monitoring', 'No backend has been configured'));
|
2014-05-06 17:01:43 +02:00
|
|
|
}
|
2014-09-01 16:11:34 +02:00
|
|
|
if ($backendName !== null) {
|
|
|
|
$backendConfig = $config->get($backendName);
|
|
|
|
if ($backendConfig === null) {
|
2014-08-22 11:46:11 +02:00
|
|
|
throw new ConfigurationError('No configuration for backend %s', $backendName);
|
2014-05-06 17:01:43 +02:00
|
|
|
}
|
2014-09-01 16:11:34 +02:00
|
|
|
if ((bool) $backendConfig->get('disabled', false) === true) {
|
2014-05-06 17:01:43 +02:00
|
|
|
throw new ConfigurationError(
|
2014-10-21 17:22:16 +02:00
|
|
|
mt('monitoring', 'Configuration for backend %s available but backend is disabled'),
|
2014-08-22 11:46:11 +02:00
|
|
|
$backendName
|
2014-05-06 17:01:43 +02:00
|
|
|
);
|
|
|
|
}
|
2014-09-01 16:11:34 +02:00
|
|
|
} else {
|
|
|
|
foreach ($config as $name => $backendConfig) {
|
2014-09-02 11:32:48 +02:00
|
|
|
if ((bool) $backendConfig->get('disabled', false) === false) {
|
2014-09-01 16:11:34 +02:00
|
|
|
$backendName = $name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($backendName === null) {
|
2014-10-21 17:22:16 +02:00
|
|
|
throw new ConfigurationError(mt('monitoring', 'All backends are disabled'));
|
2014-09-01 16:11:34 +02:00
|
|
|
}
|
2014-05-06 17:01:43 +02:00
|
|
|
}
|
2014-08-28 11:42:22 +02:00
|
|
|
$resource = ResourceFactory::create($backendConfig->resource);
|
2014-05-06 17:01:43 +02:00
|
|
|
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)) {
|
2014-08-26 11:15:19 +02:00
|
|
|
throw new ProgrammingError(
|
|
|
|
'DataView %s does not exist',
|
|
|
|
ucfirst($viewName)
|
|
|
|
);
|
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-08-26 11:15:19 +02:00
|
|
|
'Query "%s" does not exist for backend %s',
|
|
|
|
ucfirst($queryName),
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|