2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
2013-08-19 15:24:09 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - 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>
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2013-07-15 12:16:14 +02:00
|
|
|
namespace Monitoring;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2013-08-21 11:02:53 +02:00
|
|
|
use \Exception;
|
2013-08-12 15:02:25 +02:00
|
|
|
use \Icinga\Application\Config as IcingaConfig;
|
2013-08-19 15:24:09 +02:00
|
|
|
use \Icinga\Authentication\Manager as AuthManager;
|
|
|
|
use \Monitoring\Backend\AbstractBackend;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
|
|
|
* Container for monitoring backends
|
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
class Backend
|
|
|
|
{
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
|
|
|
* Array of backends
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
protected static $instances = array();
|
2013-08-19 15:24:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of configuration settings for backends
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
protected static $backendConfigs;
|
2013-06-27 13:04:47 +02:00
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
|
|
|
* Locked constructor
|
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
final protected function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
|
|
|
* Test if configuration key exist
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param string $name
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @return bool
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
public static function exists($name)
|
|
|
|
{
|
|
|
|
$configs = self::getBackendConfigs();
|
|
|
|
return array_key_exists($name, $configs);
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
|
|
|
* Get the first configuration name of all backends
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @return string
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @throws Exception
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
public static function getDefaultName()
|
|
|
|
{
|
|
|
|
$configs = self::getBackendConfigs();
|
|
|
|
if (empty($configs)) {
|
|
|
|
throw new Exception(
|
|
|
|
'Cannot get default backend as no backend has been configured'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
reset($configs);
|
|
|
|
return key($configs);
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
|
|
|
* Getter for backend configuration with lazy initializing
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-07-23 12:18:27 +02:00
|
|
|
public static function getBackendConfigs()
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
|
|
|
if (self::$backendConfigs === null) {
|
2013-08-20 15:45:04 +02:00
|
|
|
$resources = IcingaConfig::app('resources');
|
|
|
|
foreach ($resources as $resource) {
|
|
|
|
|
|
|
|
}
|
2013-08-19 15:24:09 +02:00
|
|
|
$backends = IcingaConfig::module('monitoring', 'backends');
|
2013-06-27 10:14:41 +02:00
|
|
|
foreach ($backends as $name => $config) {
|
|
|
|
self::$backendConfigs[$name] = $config;
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 15:24:09 +02:00
|
|
|
|
2013-06-27 10:14:41 +02:00
|
|
|
return self::$backendConfigs;
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
|
|
|
* Get a backend by name or a default one
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return AbstractBackend
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @throws Exception
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-07-23 12:18:27 +02:00
|
|
|
public static function getBackend($name = null)
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
|
|
|
if (! array_key_exists($name, self::$instances)) {
|
|
|
|
if ($name === null) {
|
|
|
|
$name = self::getDefaultName();
|
|
|
|
} else {
|
2013-09-02 09:41:19 +02:00
|
|
|
if (!self::exists($name)) {
|
2013-08-19 15:24:09 +02:00
|
|
|
throw new Exception(
|
|
|
|
sprintf(
|
|
|
|
'There is no such backend: "%s"',
|
|
|
|
$name
|
|
|
|
)
|
|
|
|
);
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = self::$backendConfigs[$name];
|
|
|
|
$type = $config->type;
|
|
|
|
$type[0] = strtoupper($type[0]);
|
2013-07-15 12:16:14 +02:00
|
|
|
$class = '\\Monitoring\\Backend\\' . $type;
|
2013-06-27 10:14:41 +02:00
|
|
|
self::$instances[$name] = new $class($config);
|
|
|
|
}
|
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:24:09 +02:00
|
|
|
/**
|
|
|
|
* Get backend by name or by user configuration
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param string $name
|
2013-08-19 15:24:09 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @return AbstractBackend
|
2013-08-19 15:24:09 +02:00
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
public static function getInstance($name = null)
|
|
|
|
{
|
|
|
|
if (array_key_exists($name, self::$instances)) {
|
|
|
|
return self::$instances[$name];
|
|
|
|
} else {
|
|
|
|
if ($name === null) {
|
|
|
|
// TODO: Remove this, will be chosen by Environment
|
2013-06-27 13:04:47 +02:00
|
|
|
$name = AuthManager::getInstance()->getSession()->get('backend');
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
return self::getBackend($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|