2013-09-24 15:26:10 +02:00
|
|
|
<?php
|
|
|
|
// {{{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-09-24 15:26:10 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Data;
|
|
|
|
|
2013-11-06 10:20:15 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2013-10-07 16:46:20 +02:00
|
|
|
use Zend_Config;
|
2013-09-24 15:26:10 +02:00
|
|
|
use Icinga\Util\ConfigAwareFactory;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
use Icinga\Data\Db\Connection as DbConnection;
|
2013-10-22 22:21:03 +02:00
|
|
|
use Icinga\Protocol\Livestatus\Connection as LivestatusConnection;
|
2013-10-07 16:46:20 +02:00
|
|
|
use Icinga\Protocol\Statusdat\Reader as StatusdatReader;
|
2013-11-06 10:20:15 +01:00
|
|
|
use Icinga\Protocol\Ldap\Connection as LdapConnection;
|
2014-03-19 16:57:11 +01:00
|
|
|
use Icinga\Protocol\File\Reader as FileReader;
|
2013-09-24 15:26:10 +02:00
|
|
|
|
|
|
|
class ResourceFactory implements ConfigAwareFactory
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Zend_Config
|
|
|
|
*/
|
|
|
|
private static $resources;
|
|
|
|
|
|
|
|
public static function setConfig($config)
|
|
|
|
{
|
|
|
|
self::$resources = $config;
|
|
|
|
}
|
|
|
|
|
2013-11-06 13:31:07 +01:00
|
|
|
/**
|
|
|
|
* Get the configuration for a specific resource
|
|
|
|
*
|
2014-02-21 10:16:16 +01:00
|
|
|
* @param $resourceName String The resource's name
|
2013-11-06 13:31:07 +01:00
|
|
|
*
|
|
|
|
* @return Zend_Config The configuration of the resource
|
|
|
|
* @throws \Icinga\Exception\ConfigurationError
|
|
|
|
*/
|
2013-10-07 16:46:20 +02:00
|
|
|
public static function getResourceConfig($resourceName)
|
2013-11-06 13:31:07 +01:00
|
|
|
{
|
|
|
|
self::assertResourcesExist();
|
|
|
|
if (($resourceConfig = self::$resources->get($resourceName)) === null) {
|
2014-02-21 10:16:16 +01:00
|
|
|
throw new ConfigurationError(
|
|
|
|
'Cannot load resource config "' . $resourceName . '". Resource does not exist'
|
|
|
|
);
|
2013-11-06 13:31:07 +01:00
|
|
|
}
|
|
|
|
return $resourceConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-06 19:02:30 +01:00
|
|
|
* Return the configuration of all existing resources, or get all resources of a given type.
|
2013-11-06 13:31:07 +01:00
|
|
|
*
|
2013-11-06 19:02:30 +01:00
|
|
|
* @param String|null $type Fetch only resources that have the given type.
|
|
|
|
*
|
|
|
|
* @return Zend_Config The configuration containing all resources
|
2013-11-06 13:31:07 +01:00
|
|
|
*/
|
2013-11-06 19:02:30 +01:00
|
|
|
public static function getResourceConfigs($type = null)
|
2013-11-06 13:31:07 +01:00
|
|
|
{
|
|
|
|
self::assertResourcesExist();
|
2013-11-06 19:02:30 +01:00
|
|
|
if (!isset($type)) {
|
|
|
|
return self::$resources;
|
|
|
|
} else {
|
|
|
|
$resources = array();
|
|
|
|
foreach (self::$resources as $name => $resource) {
|
|
|
|
if (strtolower($resource->type) === $type) {
|
|
|
|
$resources[$name] = $resource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Zend_Config($resources);
|
|
|
|
}
|
2013-11-06 13:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the existing resources are set. If not, throw an error.
|
|
|
|
*
|
|
|
|
* @throws \Icinga\Exception\ProgrammingError
|
|
|
|
*/
|
|
|
|
private static function assertResourcesExist()
|
2013-09-24 15:26:10 +02:00
|
|
|
{
|
2013-11-06 10:20:15 +01:00
|
|
|
if (!isset(self::$resources)) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
"The ResourceFactory must be initialised by setting a config, before it can be used"
|
|
|
|
);
|
|
|
|
}
|
2013-10-07 16:46:20 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 13:31:07 +01:00
|
|
|
/**
|
|
|
|
* Create a single resource from the given configuration.
|
|
|
|
*
|
|
|
|
* NOTE: The factory does not test if the given configuration is valid and the resource is accessible, this
|
|
|
|
* depends entirely on the implementation of the returned resource.
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config The configuration for the created resource.
|
|
|
|
*
|
|
|
|
* @return DbConnection|LdapConnection|LivestatusConnection|StatusdatReader An objects that can be used to access
|
|
|
|
* the given resource. The returned class depends on the configuration property 'type'.
|
|
|
|
* @throws \Icinga\Exception\ConfigurationError When an unsupported type is given
|
|
|
|
*/
|
2013-10-07 16:46:20 +02:00
|
|
|
public static function createResource(Zend_Config $config)
|
|
|
|
{
|
|
|
|
switch (strtolower($config->type)) {
|
2013-09-24 15:26:10 +02:00
|
|
|
case 'db':
|
2013-10-07 16:46:20 +02:00
|
|
|
$resource = new DbConnection($config);
|
|
|
|
break;
|
2013-11-06 10:20:15 +01:00
|
|
|
case 'ldap':
|
|
|
|
$resource = new LdapConnection($config);
|
|
|
|
break;
|
2013-10-07 16:46:20 +02:00
|
|
|
case 'statusdat':
|
|
|
|
$resource = new StatusdatReader($config);
|
2013-09-24 15:26:10 +02:00
|
|
|
break;
|
2013-10-22 22:21:03 +02:00
|
|
|
case 'livestatus':
|
|
|
|
$resource = new LivestatusConnection($config->socket);
|
|
|
|
break;
|
2014-03-19 16:57:11 +01:00
|
|
|
case 'file':
|
|
|
|
$resource = new FileReader($config);
|
|
|
|
break;
|
2013-09-24 15:26:10 +02:00
|
|
|
default:
|
2013-10-22 16:04:38 +02:00
|
|
|
throw new ConfigurationError('Unsupported resource type "' . $config->type . '"');
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|
|
|
|
return $resource;
|
|
|
|
}
|
2014-04-28 16:45:37 +02:00
|
|
|
|
|
|
|
public static function ldapAvailable()
|
|
|
|
{
|
|
|
|
return extension_loaded('ldap');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function pgsqlAvailable()
|
|
|
|
{
|
|
|
|
return extension_loaded('pgsql');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function mysqlAvailable()
|
|
|
|
{
|
|
|
|
return extension_loaded('mysql');
|
|
|
|
}
|
2013-09-24 15:26:10 +02:00
|
|
|
}
|