2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
2013-08-19 15:24:09 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-10-23 15:10:33 +02:00
|
|
|
/**
|
|
|
|
* This file is part of Icinga Web 2.
|
|
|
|
*
|
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*
|
|
|
|
*/
|
2013-08-19 15:24:09 +02:00
|
|
|
// {{{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-19 20:09:17 +02:00
|
|
|
use Icinga\Module\Monitoring\Exception\UnsupportedBackendException;
|
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';
|
2013-10-19 20:09:17 +02:00
|
|
|
if (!class_exists($queryClass)) {
|
|
|
|
throw new UnsupportedBackendException('Query '
|
|
|
|
. ucfirst($table)
|
|
|
|
. ' Is Not Available For Backend '
|
|
|
|
. ucfirst($this->config->type)
|
|
|
|
);
|
|
|
|
}
|
2013-09-24 15:26:10 +02:00
|
|
|
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-10-22 16:04:38 +02:00
|
|
|
|
|
|
|
// We won't have disabled backends
|
|
|
|
foreach ($configs as $name => $config) {
|
|
|
|
if (!$config->get('disabled') == '1') {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'All backends are disabled'
|
|
|
|
);
|
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();
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:04:38 +02:00
|
|
|
$config = null;
|
2013-10-22 22:46:55 +02:00
|
|
|
self::getBackendConfigs();
|
2013-10-22 12:31:28 +02:00
|
|
|
if (isset(self::$backendConfigs[$name])) {
|
2013-10-22 16:04:38 +02:00
|
|
|
/** @var Zend_Config $config */
|
2013-10-22 12:31:28 +02:00
|
|
|
$config = self::$backendConfigs[$name];
|
2013-10-22 16:04:38 +02:00
|
|
|
if ($config->get('disabled') == '1') {
|
|
|
|
$config = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($config === null) {
|
2013-10-22 12:31:28 +02:00
|
|
|
throw new ConfigurationError(
|
2013-10-22 16:04:38 +02:00
|
|
|
'No configuration for backend:' . $name
|
2013-10-22 12:31:28 +02:00
|
|
|
);
|
|
|
|
}
|
2013-10-19 20:09:17 +02:00
|
|
|
|
2013-10-22 22:46:55 +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
|
|
|
}
|
|
|
|
}
|