2013-06-03 16:14:46 +02:00
|
|
|
<?php
|
2013-06-07 13:29:11 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-28 16:47:30 +02:00
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-06-28 16:47:30 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* 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.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* 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.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* 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.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* @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-06-28 16:47:30 +02:00
|
|
|
*/
|
2013-06-07 13:29:11 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2013-06-03 16:14:46 +02:00
|
|
|
namespace Icinga\Protocol\Statusdat;
|
|
|
|
|
2013-10-28 12:46:19 +01:00
|
|
|
use Icinga\Application\Logger;
|
2013-07-19 17:45:51 +02:00
|
|
|
use Icinga\Data\DatasourceInterface;
|
2013-10-20 15:15:30 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2013-09-02 17:33:12 +02:00
|
|
|
use Icinga\Exception;
|
|
|
|
use Icinga\Benchmark;
|
2013-07-19 11:29:51 +02:00
|
|
|
use Icinga\Protocol\Statusdat\View\MonitoringObjectList;
|
2013-09-02 17:33:12 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
|
|
|
* Class Reader
|
|
|
|
* @package Icinga\Protocol\Statusdat
|
|
|
|
*/
|
2013-07-19 17:45:51 +02:00
|
|
|
class Reader implements IReader, DatasourceInterface
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* The default lifetime of the cache in milliseconds
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-10-20 15:15:30 +02:00
|
|
|
const DEFAULT_CACHE_LIFETIME = 30;
|
2013-06-03 16:14:46 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* The folder for the statusdat cache
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-10-28 12:46:19 +01:00
|
|
|
const STATUSDAT_DEFAULT_CACHE_PATH = '/tmp';
|
2013-06-03 16:14:46 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* The last state from the cache
|
|
|
|
*
|
|
|
|
* @var array
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-10-20 15:15:30 +02:00
|
|
|
private $lastState;
|
2013-06-07 13:29:11 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* True when this reader has already acquired the current runtime state (i.e. Status.dat)
|
|
|
|
*
|
2013-06-07 13:29:11 +02:00
|
|
|
* @var bool
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private $hasRuntimeState = false;
|
2013-06-07 13:29:11 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* The representation of the object.cache file
|
|
|
|
*
|
|
|
|
* @var array
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-10-20 15:15:30 +02:00
|
|
|
private $objectCache ;
|
2013-06-07 13:29:11 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* The representation of the status.dat file
|
|
|
|
* @var array
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-10-20 15:15:30 +02:00
|
|
|
private $statusCache;
|
2013-06-07 13:29:11 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* True when the icinga state differs from the cache
|
|
|
|
*
|
2013-06-07 13:29:11 +02:00
|
|
|
* @var bool
|
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private $newState = false;
|
2013-06-07 13:29:11 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* The Parser object to use for parsing
|
|
|
|
*
|
|
|
|
* @var Parser
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-10-20 15:15:30 +02:00
|
|
|
private $parser;
|
2013-06-07 13:29:11 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Whether to disable the cache
|
|
|
|
*
|
2013-06-07 13:29:11 +02:00
|
|
|
* @var bool
|
|
|
|
*/
|
2013-10-20 15:15:30 +02:00
|
|
|
private $noCache;
|
2013-06-07 13:29:11 +02:00
|
|
|
|
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Create a new Reader from the given configuraion
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config The configuration to read the status.dat information from
|
|
|
|
* @param Parser $parser The parser to use (for testing)
|
|
|
|
* @param bool $noCache Whether to disable the cache
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
public function __construct($config = \Zend_Config, $parser = null, $noCache = false)
|
|
|
|
{
|
|
|
|
$this->noCache = $noCache;
|
2013-07-19 11:29:51 +02:00
|
|
|
if (isset($config->no_cache)) {
|
|
|
|
$this->noCache = $config->no_cache;
|
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->config = $config;
|
|
|
|
$this->parser = $parser;
|
2013-10-19 20:09:17 +02:00
|
|
|
|
2013-07-19 11:29:51 +02:00
|
|
|
if (!$this->noCache) {
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->cache = $this->initializeCaches($config);
|
2013-06-07 13:29:11 +02:00
|
|
|
if ($this->fromCache()) {
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->createHostServiceConnections();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-10-19 20:09:17 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
if (!$this->lastState) {
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->parseObjectsCacheFile();
|
2013-06-07 13:29:11 +02:00
|
|
|
}
|
|
|
|
if (!$this->hasRuntimeState) {
|
2013-07-19 11:29:51 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
}
|
|
|
|
$this->parseStatusDatFile();
|
|
|
|
if (!$noCache && $this->newState) {
|
2013-10-19 20:09:17 +02:00
|
|
|
$this->statusCache->save($this->parser->getRuntimeState(), 'object' . md5($this->config->object_file));
|
2013-06-07 13:29:11 +02:00
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->createHostServiceConnections();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Initialize the internal caches if enabled
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
|
|
|
private function initializeCaches()
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
2013-10-28 12:46:19 +01:00
|
|
|
$defaultCachePath = self::STATUSDAT_DEFAULT_CACHE_PATH;
|
|
|
|
$cachePath = $this->config->get('cache_path', $defaultCachePath);
|
|
|
|
$maxCacheLifetime = intval($this->config->get('cache_path', self::DEFAULT_CACHE_LIFETIME));
|
|
|
|
$cachingEnabled = true;
|
2013-06-07 13:29:11 +02:00
|
|
|
if (!is_writeable($cachePath)) {
|
2013-10-28 12:46:19 +01:00
|
|
|
Logger::warn(
|
|
|
|
'Can\'t cache Status.dat backend; make sure cachepath %s is writable by the web user. '
|
|
|
|
. 'Caching is now disabled',
|
|
|
|
$cachePath
|
2013-06-07 13:29:11 +02:00
|
|
|
);
|
2013-10-28 12:46:19 +01:00
|
|
|
$cachePath = null;
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
2013-06-07 13:29:11 +02:00
|
|
|
$backendOptions = array(
|
|
|
|
'cache_dir' => $cachePath
|
2013-06-03 16:14:46 +02:00
|
|
|
);
|
2013-10-19 20:09:17 +02:00
|
|
|
// the object cache might exist for months and is still valid
|
2013-10-28 12:46:19 +01:00
|
|
|
$this->objectCache = $this->initCache($this->config->object_file, $backendOptions, null, $cachingEnabled);
|
|
|
|
$this->statusCache = $this->initCache(
|
|
|
|
$this->config->status_file, $backendOptions, $maxCacheLifetime, $cachingEnabled
|
|
|
|
);
|
2013-06-03 16:14:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Init the Cache backend in Zend
|
|
|
|
*
|
|
|
|
* @param String $file The file to use as the cache master file
|
|
|
|
* @param Zend_Config $backend The backend configuration to use
|
|
|
|
* @param integer $lifetime The lifetime of the cache
|
|
|
|
*
|
2013-06-07 13:29:11 +02:00
|
|
|
* @return \Zend_Cache_Core|\Zend_Cache_Frontend
|
|
|
|
*/
|
2013-10-28 12:46:19 +01:00
|
|
|
private function initCache($file, $backendConfig, $lifetime)
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
2013-06-07 13:29:11 +02:00
|
|
|
$frontendOptions = array(
|
2013-10-28 12:46:19 +01:00
|
|
|
'lifetime' => $lifetime,
|
|
|
|
'automatic_serialization' => true,
|
|
|
|
'master_files' => array($file)
|
2013-06-07 13:29:11 +02:00
|
|
|
);
|
2013-10-28 12:46:19 +01:00
|
|
|
return \Zend_Cache::factory('Core', 'File', $frontendOptions, $backendConfig);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Read the current cache state
|
|
|
|
*
|
|
|
|
* @return bool True if the state is the same as the icinga state
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private function fromCache()
|
|
|
|
{
|
2013-06-07 13:29:11 +02:00
|
|
|
if (!$this->readObjectsCache()) {
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->newState = true;
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-07 13:29:11 +02:00
|
|
|
if (!$this->readStatusCache()) {
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->newState = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Read the object.cache file from the Zend_Cache backend
|
|
|
|
*
|
|
|
|
* @return bool True if the file could be loaded from cache
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private function readObjectsCache()
|
|
|
|
{
|
2013-10-19 20:09:17 +02:00
|
|
|
$this->lastState = $this->objectCache->load('object' . md5($this->config->object_file));
|
2013-06-07 13:29:11 +02:00
|
|
|
if ($this->lastState == false) {
|
2013-06-03 16:14:46 +02:00
|
|
|
return false;
|
2013-06-07 13:29:11 +02:00
|
|
|
}
|
2013-06-07 13:35:03 +02:00
|
|
|
|
|
|
|
return true;
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Read the status.dat file from the Zend_Cache backend
|
|
|
|
*
|
|
|
|
* @return bool True if the file could be loaded from cache
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
2013-06-03 16:14:46 +02:00
|
|
|
private function readStatusCache()
|
|
|
|
{
|
2013-06-10 17:45:13 +02:00
|
|
|
if (!isset($this->stateCache)) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-07 13:29:11 +02:00
|
|
|
$statusInfo = $this->stateCache->load('state' . md5($this->config->status_file));
|
|
|
|
if ($statusInfo == false) {
|
2013-06-03 16:14:46 +02:00
|
|
|
return false;
|
2013-06-07 13:29:11 +02:00
|
|
|
}
|
2013-06-10 17:45:13 +02:00
|
|
|
|
2013-06-03 16:14:46 +02:00
|
|
|
$this->hasRuntimeState = true;
|
2013-06-07 13:35:03 +02:00
|
|
|
return true;
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Take the status.dat and objects.cache and connect all services to hosts
|
2013-06-07 13:29:11 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function createHostServiceConnections()
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
2013-06-07 13:29:11 +02:00
|
|
|
if (!isset($this->lastState["service"])) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-19 20:09:17 +02:00
|
|
|
foreach ($this->lastState["host"] as &$host) {
|
|
|
|
$host->host = $host;
|
|
|
|
}
|
2013-06-07 13:29:11 +02:00
|
|
|
foreach ($this->lastState["service"] as &$service) {
|
2013-10-19 20:09:17 +02:00
|
|
|
$service->service = &$service; // allow easier querying
|
|
|
|
$host = &$this->lastState["host"][$service->host_name];
|
2013-06-07 13:29:11 +02:00
|
|
|
if (!isset($host->services)) {
|
|
|
|
$host->services = array();
|
|
|
|
}
|
|
|
|
$host->services[$service->service_description] = & $service;
|
|
|
|
$service->host = & $host;
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Parse the object.cache file and update the current state
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError If the object.cache couldn't be read
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
|
|
|
private function parseObjectsCacheFile()
|
|
|
|
{
|
2013-10-19 20:09:17 +02:00
|
|
|
if (!is_readable($this->config->object_file)) {
|
2013-10-20 15:15:30 +02:00
|
|
|
throw new ConfigurationError(
|
2013-10-19 20:09:17 +02:00
|
|
|
'Can\'t read object-file "' . $this->config->object_file . '", check your configuration'
|
2013-06-07 13:29:11 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!$this->parser) {
|
2013-10-19 20:09:17 +02:00
|
|
|
$this->parser = new Parser(fopen($this->config->object_file, "r"));
|
2013-06-07 13:29:11 +02:00
|
|
|
}
|
|
|
|
$this->parser->parseObjectsFile();
|
2013-07-23 12:18:27 +02:00
|
|
|
$this->lastState = $this->parser->getRuntimeState();
|
2013-06-07 13:29:11 +02:00
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Parse the status.dat file and update the current state
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError If the status.dat couldn't be read
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
|
|
|
private function parseStatusDatFile()
|
|
|
|
{
|
|
|
|
if (!is_readable($this->config->status_file)) {
|
2013-10-20 15:15:30 +02:00
|
|
|
throw new ConfigurationError(
|
2013-06-07 13:29:11 +02:00
|
|
|
"Can't read status-file {$this->config->status_file}, check your configuration"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!$this->parser) {
|
|
|
|
$this->parser = new Parser(fopen($this->config->status_file, "r"), $this->lastState);
|
|
|
|
}
|
|
|
|
$this->parser->parseRuntimeState(fopen($this->config->status_file, "r"));
|
2013-07-23 12:18:27 +02:00
|
|
|
$this->lastState = $this->parser->getRuntimeState();
|
2013-06-07 13:29:11 +02:00
|
|
|
if (!$this->noCache) {
|
2013-10-19 20:09:17 +02:00
|
|
|
$this->statusCache->save(array("true" => true), "state" . md5($this->config->object_file));
|
2013-06-07 13:29:11 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Create a new Query
|
|
|
|
*
|
|
|
|
* @return Query The query to operate on
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
|
|
|
public function select()
|
|
|
|
{
|
|
|
|
return new Query($this);
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Return the internal state of the status.dat
|
|
|
|
*
|
|
|
|
* @return mixed The internal status.dat representation
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
|
|
|
public function getState()
|
2013-06-03 16:14:46 +02:00
|
|
|
{
|
2013-06-07 13:29:11 +02:00
|
|
|
return $this->lastState;
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Return the object with the given name and type
|
|
|
|
*
|
|
|
|
* @param String $type The type of the object to return (service, host, servicegroup...)
|
|
|
|
* @param String $name The name of the object
|
|
|
|
*
|
|
|
|
* @return ObjectContainer An object container wrapping the result or null if the object doesn't exist
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
|
|
|
public function getObjectByName($type, $name)
|
|
|
|
{
|
|
|
|
if (isset($this->lastState[$type]) && isset($this->lastState[$type][$name])) {
|
|
|
|
return new ObjectContainer($this->lastState[$type][$name], $this);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
|
2013-06-07 13:29:11 +02:00
|
|
|
/**
|
2013-10-20 15:15:30 +02:00
|
|
|
* Get an array containing all names of monitoring objects with the given type
|
|
|
|
*
|
|
|
|
* @param String $type The type of object to get the names for
|
|
|
|
* @return array An array of names or null if the type does not exist
|
2013-06-07 13:29:11 +02:00
|
|
|
*/
|
|
|
|
public function getObjectNames($type)
|
|
|
|
{
|
|
|
|
return isset($this->lastState[$type]) ? array_keys($this->lastState[$type]) : null;
|
|
|
|
}
|
2013-06-03 16:14:46 +02:00
|
|
|
}
|