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

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kduret committed Dec 4, 2018
1 parent d1fa9fd commit 2713890
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function findCentralBrokerConfigId(): int
if ($row = $stmt->fetch()) {
$configId = $row['config_id'];
} else {
throw new NotFoundException('Central broker config id not found');
throw new NotFoundException(_('Central broker config id not found'));
}

return $configId;
Expand Down Expand Up @@ -68,7 +68,7 @@ public function findBrokerConfigIdByPollerId(int $pollerId): int
if ($row = $stmt->fetch()) {
$configId = $row['config_id'];
} else {
throw new NotFoundException('Poller broker config id not found');
throw new NotFoundException(_('Poller broker config id not found'));
}

return $configId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ public function postLinkCentreonRemoteServer(): array
$serverConfigurationService->setName($serverName);
$serverConfigurationService->setOpenBrokerFlow($openBrokerFlow);

$pollerConfigurationService->setOpenBrokerFlow($openBrokerFlow);

// set linked pollers
$pollerConfigurationBridge->collectDataFromRequest();

Expand All @@ -258,8 +260,7 @@ public function postLinkCentreonRemoteServer(): array
$serverConfigurationService->isLinkedToCentralServer();
}

$serverID = $serverConfigurationService->insert();
$pollerConfigurationBridge->setServerID($serverID);
$serverId = $serverConfigurationService->insert();
} catch (\Exception $e) {
return ['error' => true, 'message' => $e->getMessage()];
}
Expand All @@ -268,7 +269,7 @@ public function postLinkCentreonRemoteServer(): array
if ($isRemoteConnection) {
$taskId = null;

$remoteServer = $pollerConfigurationBridge->getRemoteServerForConfiguration();
$remoteServer = $pollerConfigurationBridge->getPollerFromId($serverId);

// set basic parameters to export task
$params = [
Expand All @@ -280,11 +281,10 @@ public function postLinkCentreonRemoteServer(): array

// If you want to link pollers to a remote
if ($pollerConfigurationBridge->hasPollersForUpdating()) {
$pollerServers = $pollerConfigurationBridge->getLinkedPollersSelectedForUpdate();
$pollerConfigurationService->setOpenBrokerFlow($openBrokerFlow);
$pollerConfigurationService->setPollersConfigurationWithServer($pollerServers, $remoteServer);
$pollers = $pollerConfigurationBridge->getLinkedPollersSelectedForUpdate();
$pollerConfigurationService->linkPollersToParentPoller($pollers, $remoteServer);

foreach ($pollerServers as $poller){
foreach ($pollers as $poller) {
$params['pollers'][] = $poller->getId();
}
}
Expand All @@ -296,12 +296,11 @@ public function postLinkCentreonRemoteServer(): array
$this->addServerToListOfRemotes($serverIP, $centreonPath);
$this->setCentreonInstanceAsCentral();

// if it is poller wizard and poller is linked to a another poller/remote server (instead of central)
// if it is poller wizard and poller is linked to another poller/remote server (instead of central)
} elseif ($pollerConfigurationBridge->hasPollersForUpdating()) {
$remoteServer = $pollerConfigurationBridge->getRemoteServerForConfiguration();
$pollerServers = $pollerConfigurationBridge->getLinkedPollersSelectedForUpdate();
$pollerConfigurationService->setOpenBrokerFlow($openBrokerFlow);
$pollerConfigurationService->setPollersConfigurationWithServer($pollerServers, $remoteServer);
$pollers = [$pollerConfigurationBridge->getPollerFromId($serverId)];
$parentPoller = $pollerConfigurationBridge->getLinkedPollersSelectedForUpdate()[0];
$pollerConfigurationService->linkPollersToParentPoller($pollers, $parentPoller);
}

return ['success' => true, 'task_id' => $taskId];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ public function setOpenBrokerFlow($openBrokerFlow)
}

/**
* Link a set of pollers to a parent poller by creating broker input/output
*
* @param PollerServer[] $pollers
* @param PollerServer $remote
*/
public function setPollersConfigurationWithServer(array $pollers, PollerServer $remote)
public function linkPollersToParentPoller(array $pollers, PollerServer $remote)
{
$pollerIds = array_map(function ($poller) {
return $poller->getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@ class PollerConfigurationRequestBridge
/** @var PollerServer[] */
private $pollers = [];

private $serverID = null;


public function __construct(Container $di)
{
$this->dbAdapter = $di['centreon.db-manager']->getAdapter('configuration_db');
}

public function setServerID($serverID)
{
$this->serverID = $serverID;
}

public function hasPollersForUpdating(): bool
{
return !empty($this->pollers);
Expand All @@ -42,16 +35,19 @@ public function getLinkedPollersSelectedForUpdate(): array
return $this->pollers;
}

public function collectDataFromRequest()
/**
* Set linked pollers regarding wizard type (poller/remote server)
*/
public function collectDataFromRequest(): void
{
$isRemoteConnection = (new ServerWizardIdentity)->requestConfigurationIsRemote();
$isRemoteServerWizard = (new ServerWizardIdentity)->requestConfigurationIsRemote();

if ($isRemoteConnection) { // configure remote server
$linkedPollers = $_POST['linked_pollers'] ?? ''; // pollers linked to the remote server
$linkedPollers = (array) $linkedPollers;
if ($isRemoteServerWizard) { // configure remote server
// pollers linked to the remote server
$linkedPollers = isset($_POST['linked_pollers']) ? (array) $_POST['linked_pollers'] : [];
} else { // configure poller
$remoteId = $_POST['linked_remote'] ?? ''; // if the poller is linked to a remote server
$linkedPollers = [$this->serverID];
// if the poller is linked to a remote server
$linkedPollers = isset($_POST['linked_remote']) ? [$_POST['linked_remote']] : [];
}

$this->pollers = $this->getPollersToLink($linkedPollers); // set and instantiate linked pollers
Expand All @@ -77,39 +73,34 @@ private function getPollersToLink(array $pollers)

$idBindString = str_repeat('?,', count($pollerIDs));
$idBindString = rtrim($idBindString, ',');
$queryPollers = "SELECT id, name, ns_ip_address as ip FROM nagios_server WHERE id IN({$idBindString})";

try {
$this->dbAdapter->query($queryPollers, $pollerIDs);
$results = $this->dbAdapter->results();
$data = [];
$queryPollers = "SELECT id, name, ns_ip_address as ip FROM nagios_server WHERE id IN ({$idBindString})";

foreach ($results as $result) {
$poller = new PollerServer;
$poller->setId($result->id);
$poller->setName($result->name);
$poller->setIp($result->ip);
$this->dbAdapter->query($queryPollers, $pollerIDs);
$results = $this->dbAdapter->results();
$data = [];

$data[] = $poller;
}
foreach ($results as $result) {
$poller = new PollerServer;
$poller->setId($result->id);
$poller->setName($result->name);
$poller->setIp($result->ip);

return $data;
} catch (\Exception $e) {
error_log($e->getMessage());
$data[] = $poller;
}

return $data;

return [];
}

public function getRemoteServerForConfiguration(): ?PollerServer
/**
* Get poller information from poller id
*/
public function getPollerFromId(int $pollerId): ?PollerServer
{
if (is_null($this->serverID)) {
return null;
}

$query = 'SELECT id, name, ns_ip_address as ip FROM nagios_server WHERE id = ?';

$this->dbAdapter->query($query, [$this->serverID]);
$this->dbAdapter->query($query, [$pollerId]);
$results = $this->dbAdapter->results();

if (count($results)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public function setBrokerConfigurationService(BrokerConfigurationService $broker
$this->brokerConfigurationService = $brokerConfigurationService;
}

protected function insertConfigCentreonBroker($serverID)
/**
* Insert centreon broker configuration to a given poller
* this configuration i only for broker module (not cbd)
*
* @param int $serverID the poller id
*/
protected function insertConfigCentreonBroker(int $serverID): void
{
$configCentreonBrokerData = CfgCentreonBroker::getConfiguration($serverID, $this->name);
$configCentreonBrokerInfoData = CfgCentreonBrokerInfo::getConfiguration($this->name, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class RemoteConnectionConfigurationService extends ServerConnectionConfigurationService
{

protected function insertConfigCentreonBroker($serverID)
protected function insertConfigCentreonBroker(int $serverID): void
{
$configCentreonBrokerData = CfgCentreonBroker::getConfiguration($serverID, $this->name);
$configCentreonBrokerInfoData = CfgCentreonBrokerInfo::getConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(CentreonDBAdapter $dbAdapter)
$this->dbAdapter = $dbAdapter;
}

abstract protected function insertConfigCentreonBroker($serverID);
abstract protected function insertConfigCentreonBroker(int $serverID): void;

public function setServerIp($ip)
{
Expand Down
35 changes: 22 additions & 13 deletions src/CentreonRemote/Domain/Service/NotifyMasterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace CentreonRemote\Domain\Service;

use Centreon\Domain\Repository\InformationsRepository;
use Centreon\Infrastructure\Service\CentreonDBManagerService;
use Curl\Curl;
use Pimple\Container;

class NotifyMasterService
{
Expand All @@ -26,16 +26,31 @@ class NotifyMasterService
const FAIL = 'fail';

/**
* @var Container
* @var CentreonDBManagerService
*/
private $di;
private $dbManager;

/**
* @var Curl
*/
private $curl;

/**
* @return Curl
*/
public function setCurl(Curl $curl): void
{
$this->curl = $curl;
}

/**
* NotifyMasterService constructor.
*
* @param CentreonDBManagerService $dbManager
*/
public function __construct(Container $di)
public function __construct(CentreonDBManagerService $dbManager)
{
$this->di = $di;
$this->dbManager = $dbManager;
}

/**
Expand All @@ -54,7 +69,7 @@ public function pingMaster($ip)
}

$url = "{$ip}/centreon/api/external.php?object=centreon_remote_server&action=addToWaitList";
$repository = $this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class);
$repository = $this->dbManager->getRepository(InformationsRepository::class);
$applicationKey = $repository->getOneByKey('appKey');
$version = $repository->getOneByKey('version');

Expand All @@ -70,8 +85,7 @@ public function pingMaster($ip)
'app_key' => $applicationKey->getValue(),
'version' => $version->getValue(),
];
$curl = new Curl();
$curl->post($url, $curlData);
$this->curl->post($url, $curlData);

if ($curl->error) {
switch ($curl->error_code) {
Expand Down Expand Up @@ -103,9 +117,4 @@ public function pingMaster($ip)

return ['status' => self::SUCCESS];
}

private function getDi(): Container
{
return $this->di;
}
}
4 changes: 3 additions & 1 deletion src/CentreonRemote/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CentreonRemote\Domain\Service\NotifyMasterService;
use CentreonRemote\Domain\Service\TaskService;
use CentreonRemote\Infrastructure\Service\PollerInteractionService;
use Curl\Curl;

class ServiceProvider implements AutoloadServiceProviderInterface
{
Expand All @@ -39,7 +40,8 @@ public function register(Container $pimple): void
$pimple['centreon.clapi']->add(Clapi\CentreonWorker::class);

$pimple['centreon.notifymaster'] = function (Container $pimple): NotifyMasterService {
$service = new NotifyMasterService($pimple);
$service = new NotifyMasterService($pimple['centreon.db-manager']);
$service->setCurl(new Curl);
return $service;
};

Expand Down

0 comments on commit 2713890

Please sign in to comment.