Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
enh(config): broker stats working on remote server (#8591)
Browse files Browse the repository at this point in the history
* MON-5286 broker stats working on remote server

* MON-5286: resolve review

* MON-5286: resolve PR reviews
  • Loading branch information
garnier-quentin authored Apr 15, 2020
1 parent 583f0f9 commit 627bc07
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 0 deletions.
101 changes: 101 additions & 0 deletions www/class/config-generate-remote/Broker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/*
* Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

namespace ConfigGenerateRemote;

use \Exception;
use \PDO;
use ConfigGenerateRemote\Abstracts\AbstractObject;

class Broker extends AbstractObject
{
protected $table = 'cfg_centreonbroker';
protected $generateFilename = 'cfg_centreonbroker.infile';

protected $attributesSelect = '
config_id,
config_name,
config_filename,
config_write_timestamp,
config_write_thread_id,
config_activate,
ns_nagios_server,
event_queue_max_size,
command_file,
cache_directory,
stats_activate,
daemon
';
protected $attributesWrite = [
'config_id',
'config_name',
'config_filename',
'config_write_timestamp',
'config_write_thread_id',
'config_activate',
'ns_nagios_server',
'event_queue_max_size',
'command_file',
'cache_directory',
'stats_activate',
'daemon'
];
protected $stmtBroker = null;

/**
* Generate broker configuration from poller id
*
* @param int $poller
* @return void
*/
private function generate(int $pollerId)
{
if (is_null($this->stmtEngine)) {
$this->stmtBroker = $this->backendInstance->db->prepare(
"SELECT $this->attributesSelect FROM cfg_centreonbroker " .
"WHERE ns_nagios_server = :poller_id AND config_activate = '1'"
);
}
$this->stmtBroker->bindParam(':poller_id', $pollerId, PDO::PARAM_INT);
$this->stmtBroker->execute();

$results = $this->stmtBroker->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row) {
Relations\BrokerInfo::getInstance($this->dependencyInjector)->getBrokerInfoByConfigId($row['config_id']);
$this->generateObjectInFile(
$row,
$row['config_id']
);
}
}

/**
* Generate engine configuration from poller
*
* @param array $poller
* @return void
*/
public function generateFromPoller(array $poller)
{
Resource::getInstance($this->dependencyInjector)->generateFromPollerId($poller['id']);
$this->generate($poller['id']);
}
}
3 changes: 3 additions & 0 deletions www/class/config-generate-remote/Engine.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright 2005 - 2019 Centreon (https://www.centreon.com/)
*
Expand Down Expand Up @@ -35,6 +36,7 @@ class Engine extends AbstractObject
protected $attributesSelect = '
nagios_server_id,
nagios_id,
nagios_name,
use_timezone,
cfg_dir,
cfg_file,
Expand Down Expand Up @@ -142,6 +144,7 @@ class Engine extends AbstractObject
protected $attributesWrite = [
'nagios_server_id',
'nagios_id',
'nagios_name',
'use_timezone',
'log_file',
'status_file',
Expand Down
3 changes: 3 additions & 0 deletions www/class/config-generate-remote/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
require_once __DIR__ . '/ServiceCategory.php';
require_once __DIR__ . '/Resource.php';
require_once __DIR__ . '/Engine.php';
require_once __DIR__ . '/Broker.php';
require_once __DIR__ . '/Graph.php';
require_once __DIR__ . '/Manifest.php';
require_once __DIR__ . '/HostCategory.php';
Expand Down Expand Up @@ -193,6 +194,7 @@ private function configPoller($username = 'unknown')
);

Engine::getInstance($this->dependencyInjector)->generateFromPoller($this->currentPoller);
Broker::getInstance($this->dependencyInjector)->generateFromPoller($this->currentPoller);
}

/**
Expand Down Expand Up @@ -335,6 +337,7 @@ private function resetObjects()
ContactGroup::getInstance($this->dependencyInjector)->reset();
Curves::getInstance($this->dependencyInjector)->reset();
Engine::getInstance($this->dependencyInjector)->reset();
Broker::getInstance($this->dependencyInjector)->reset();
Graph::getInstance($this->dependencyInjector)->reset();
HostCategory::getInstance($this->dependencyInjector)->reset();
HostGroup::getInstance($this->dependencyInjector)->reset();
Expand Down
146 changes: 146 additions & 0 deletions www/class/config-generate-remote/Relations/BrokerInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

/*
* Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

namespace ConfigGenerateRemote\Relations;

use \PDO;
use ConfigGenerateRemote\Abstracts\AbstractObject;

class BrokerInfo extends AbstractObject
{
private $useCache = 1;
private $doneCache = 0;

private $brokerInfoCache = [];

protected $table = 'cfg_centreonbroker_info';
protected $generateFilename = 'cfg_centreonbroker_info.infile';
protected $stmtBrokerInfo = null;

protected $attributesWrite = [
'config_id',
'config_key',
'config_value',
'config_group',
'config_group_id',
'grp_level',
'subgrp_id',
'parent_grp_id',
'fieldIndex'
];

/**
* Constructor
*
* @param \Pimple\Container $dependencyInjector
*/
public function __construct(\Pimple\Container $dependencyInjector)
{
parent::__construct($dependencyInjector);
$this->buildCache();
}

/**
* Build cache of broker info
*
* @return void
*/
private function cacheBrokerInfo()
{
$stmt = $this->backendInstance->db->prepare(
"SELECT *
FROM cfg_centreonbroker_info"
);

$stmt->execute();
$values = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($values as &$value) {
if (!isset($this->brokerInfoCache[$value['config_id']])) {
$this->brokerInfoCache[$value['config_id']] = [];
}
$this->brokerInfoCache[$value['config_id']][] = $value;
}
}

/**
* Build cache
*
* @return void
*/
private function buildCache()
{
if ($this->doneCache === 0) {
$this->cacheBrokerInfo();
$this->doneCache = 1;
}
}

/**
* Generate broker info configs
*
* @param integer $configId
* @param array $brokerInfoCache
* @return void
*/
public function generateObject(int $configId, array $brokerInfoCache)
{
foreach ($brokerInfoCache[$configId] as $value) {
$this->generateObjectInFile($value);
}
}

/**
* Get broker information config
*
* @param integer $configId
* @return array
*/
public function getBrokerInfoByConfigId(int $configId)
{
// Get from the cache
if (isset($this->brokerInfoCache[$configId])) {
$this->generateObject($configId, $this->brokerInfoCache);
return $this->brokerInfoCache[$configId];
} elseif ($this->useCache === 1) {
return [];
}

// We get unitary
if (is_null($this->stmtBrokerInfo)) {
$this->stmtBrokerInfo = $this->backendInstance->db->prepare(
"SELECT *
FROM cfg_centreonbroker_info
WHERE config_id = :config_id"
);
}

$this->stmtBrokerInfo->bindParam(':config_id', $configId, PDO::PARAM_INT);
$this->stmtBrokerInfo->execute();
$brokerInfoCache = [ $config_id => [] ];
foreach ($this->stmtBrokerInfo->fetchAll(PDO::FETCH_ASSOC) as &$value) {
$brokerInfoCache[$config_id] = $value;
}

$this->generateObject($configId, $brokerInfoCache);

return $brokerInfoCache;
}
}

0 comments on commit 627bc07

Please sign in to comment.