This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh(config): broker stats working on remote server (#8591)
* MON-5286 broker stats working on remote server * MON-5286: resolve review * MON-5286: resolve PR reviews
- Loading branch information
1 parent
583f0f9
commit 627bc07
Showing
4 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
www/class/config-generate-remote/Relations/BrokerInfo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |