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
|
|
|
|
2013-10-07 16:46:20 +02:00
|
|
|
use Zend_Config;
|
2013-09-24 15:26:10 +02:00
|
|
|
use Icinga\Application\Config as IcingaConfig;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
use Icinga\Data\DatasourceInterface;
|
|
|
|
use Icinga\Data\ResourceFactory;
|
|
|
|
use Icinga\Util\ConfigAwareFactory;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2013-09-24 15:26:10 +02:00
|
|
|
class Backend implements ConfigAwareFactory, DatasourceInterface
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* Resource config
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @var Zend_config
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
private $config;
|
2013-08-19 15:24:09 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* The resource the backend utilizes
|
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
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
private $resource;
|
|
|
|
|
|
|
|
private static $backendInstances = array();
|
|
|
|
|
|
|
|
private static $backendConfigs = array();
|
2013-06-27 13:04:47 +02:00
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* Create a new backend from the given resource config
|
|
|
|
*
|
2013-10-07 16:46:20 +02:00
|
|
|
* @param Zend_Config $backendConfig
|
|
|
|
* @param Zend_Config $resourceConfig
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-10-07 16:46:20 +02:00
|
|
|
public function __construct(Zend_Config $backendConfig, Zend_Config $resourceConfig)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-10-07 16:46:20 +02:00
|
|
|
$this->config = $backendConfig;
|
|
|
|
$this->resource = ResourceFactory::createResource($resourceConfig);
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* Set backend configs
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @param Zend_Config $backendConfigs
|
|
|
|
*/
|
|
|
|
public static function setConfig($backendConfigs)
|
|
|
|
{
|
|
|
|
foreach ($backendConfigs as $name => $config) {
|
|
|
|
self::$backendConfigs[$name] = $config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend entry point
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +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
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* Create query to retrieve columns and rows from the the given table
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @param string $table
|
|
|
|
* @param array $columns
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @return Query
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-10-07 16:46:20 +02:00
|
|
|
public function from($table, array $columns = null)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-09-24 15:26:10 +02:00
|
|
|
$queryClass = '\\Icinga\\Module\\Monitoring\\Backend\\'
|
|
|
|
. ucfirst($this->config->type)
|
|
|
|
. '\\Query\\'
|
|
|
|
. ucfirst($table)
|
|
|
|
. 'Query';
|
|
|
|
return new $queryClass($this->resource, $columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the resource which was created in the constructor
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getResource()
|
|
|
|
{
|
|
|
|
return $this->resource;
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* Get backend configs
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @return Zend_Config
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-07-23 12:18:27 +02:00
|
|
|
public static function getBackendConfigs()
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-09-24 15:26:10 +02:00
|
|
|
if (empty(self::$backendConfigs)) {
|
|
|
|
self::setConfig(IcingaConfig::module('monitoring', 'backends'));
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
return self::$backendConfigs;
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* Retrieve the name of the default backend which is the INI's first entry
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @return string
|
|
|
|
* @throws ConfigurationError When no backend has been configured
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
public static function getDefaultBackendName()
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-09-24 15:26:10 +02:00
|
|
|
$configs = self::getBackendConfigs();
|
|
|
|
if (empty($configs)) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'Cannot get default backend as no backend has been configured'
|
|
|
|
);
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
2013-09-24 15:26:10 +02:00
|
|
|
reset($configs);
|
|
|
|
return key($configs);
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
2013-09-24 15:26:10 +02:00
|
|
|
* Create the backend with the given name
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @param $name
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-09-24 15:26:10 +02:00
|
|
|
* @return Backend
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-09-24 15:26:10 +02:00
|
|
|
public static function createBackend($name)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-09-24 15:26:10 +02:00
|
|
|
if (array_key_exists($name, self::$backendInstances)) {
|
|
|
|
return self::$backendInstances[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name === null) {
|
|
|
|
$name = self::getDefaultBackendName();
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = self::$backendConfigs[$name];
|
2013-10-07 16:46:20 +02:00
|
|
|
self::$backendInstances[$name] = $backend = new self($config, ResourceFactory::getResourceConfig($config->resource));
|
2013-09-24 15:26:10 +02:00
|
|
|
switch (strtolower($config->type)) {
|
|
|
|
case 'ido':
|
|
|
|
if ($backend->getResource()->getDbType() !== 'oracle') {
|
|
|
|
$backend->getResource()->setTablePrefix('icinga_');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
2013-09-24 15:26:10 +02:00
|
|
|
return $backend;
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
}
|