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

fix(remote): fix enableremote parameters parsing and setting #7711

Merged
merged 3 commits into from
Jul 25, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 32 additions & 33 deletions src/CentreonRemote/Application/Clapi/CentreonRemoteServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CentreonRemote\Application\Clapi;

use DateTime;
cgagnaire marked this conversation as resolved.
Show resolved Hide resolved
use Centreon\Domain\Repository\InformationsRepository;
use Centreon\Domain\Repository\TopologyRepository;
use Centreon\Domain\Repository\OptionsRepository;
Expand Down Expand Up @@ -34,9 +35,9 @@ public static function getName() : string
/**
* Clapi command to enable remote server
*
* @param string $ip ip address of the central server
* @param string $parametersString parameters string
cgagnaire marked this conversation as resolved.
Show resolved Hide resolved
*/
public function enableRemote(string $string_ip)
public function enableRemote(string $parametersString)
{
/* Set default value */
$noCheckCertificate = false;
Expand All @@ -47,65 +48,65 @@ public function enableRemote(string $string_ip)
);

/* Check CLAPI */
$options = explode(';', $string_ip);
$options = explode(';', $parametersString);

if (count($options) === 6) {
$string_ip = $options[0];
$urlString = $options[0];
$noCheckCertificate = $options[1];
$data['remoteHttpMethod'] = $options[2];
$data['remoteHttpPort'] = $options[3];
$data['remoteNoCheckCertificate'] = $options[4];
$noProxy = $options[5];
} elseif (count($options) > 1) {
echo "6 arguments are needed, please check your arguments.";
echo "Expecting 6 parameters, received " . count($options) . "\n";
return 1;
}

/* Extract host from URI */
$aIPMaster = array();
$pattern_extract_host = '/^[a-z][a-z0-9+\-.]*:\/\/([a-z0-9\-._~%!$&\'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&\'()*+,;=:]+\])/';
$ipList = explode(',', $string_ip);
foreach ($ipList as $ip) {
if (preg_match($pattern_extract_host, $ip, $matches)) {
$ip = $matches[2];
$hostList = array();
$pattern_extract_host = '/^[htps:\/]*([a-z0-9.]+)[:0-9]*$/';
lpinsivy marked this conversation as resolved.
Show resolved Hide resolved
$urlList = explode(',', $urlString);
foreach ($urlList as $url) {
if (preg_match($pattern_extract_host, $url, $matches)) {
$hostList[] = $matches[1];
}
$aIPMaster[] = $ip;
}

echo "Starting Centreon Remote enable process: \n";
echo "Starting Centreon Remote enable process:\n";

echo "Limiting Menu Access...";
echo "Limiting Menu Access... ";
$result =
$this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
->getRepository(TopologyRepository::class)
->disableMenus();
echo (($result) ? 'Success' : 'Fail') . "\n";

echo "Limiting Actions...";
echo "Limiting Actions... ";
$this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
->getRepository(InformationsRepository::class)
->toggleRemote('yes');
echo "Done\n";

echo "Authorizing Master...";
echo "Authorizing Master... ";
$this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
->getRepository(InformationsRepository::class)
->authorizeMaster(
implode(',', $aIPMaster)
implode(',',$hostList)
);
echo "Done\n";

echo "Set 'remote' instance type...";
echo "Set 'remote' instance type... ";
system(
"sed -i -r 's/(\\\$instance_mode?\s+=?\s+\")([a-z]+)(\";)/\\1remote\\3/' " . _CENTREON_ETC_ . "/conf.pm"
);
echo "Done\n";

echo "Notifying Master...";
echo "Notifying Master...\n";
$result = "";
foreach ($ipList as $ip) {
foreach ($hostList as $host) {
echo " Trying host '$host'... ";
$result = $this->getDi()['centreon.notifymaster']->pingMaster(
$ip,
$host,
$data,
$noCheckCertificate,
$noProxy
Expand All @@ -114,9 +115,7 @@ public function enableRemote(string $string_ip)
echo "Success\n";
break;
}
}
if (empty($result['status']) || $result['status'] != 'success') {
printf("Fail: %s\n", $result['details']);
printf("Fail [Details: %s]\n", $result['details']);
}

echo "Centreon Remote enabling finished.\n";
Expand All @@ -127,22 +126,22 @@ public function enableRemote(string $string_ip)
*/
public function disableRemote(): void
{
echo "Starting Centreon Remote disable process: \n";
echo "Starting Centreon Remote disable process:\n";

echo "Restoring Menu Access...";
echo "Restoring Menu Access... ";
$result =
$this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
->getRepository(TopologyRepository::class)
->enableMenus();
echo ($result) ? 'Success' : 'Fail' . "\n";

echo "Restoring Actions...";
echo "Restoring Actions... ";
$this->getDi()[\Centreon\ServiceProvider::CENTREON_DB_MANAGER]
->getRepository(InformationsRepository::class)
->toggleRemote('no');
echo 'Done'. "\n";

echo "Restore 'central' instance type...";
echo "Restore 'central' instance type... ";
system(
"sed -i -r 's/(\\\$instance_mode?\s+=?\s+\")([a-z]+)(\";)/\\1central\\3/' " . _CENTREON_ETC_ . "/conf.pm"
);
Expand All @@ -156,17 +155,17 @@ public function disableRemote(): void
*/
public function import(): void
{
echo "Starting Centreon Remote import process: \n";
echo "Importing...";
echo (new \DateTime())->format("Y-m-d H:i:s") . " - INFO - Starting Centreon Remote import process...\n";

try {
$this->getDi()['centreon_remote.export']->import();
echo "Success\n";
echo (new \DateTime())->format("Y-m-d H:i:s") . " - INFO - Import succeed\n";
} catch (\Exception $e) {
echo "Fail: " . $e->getMessage() . "\n";
echo (new \DateTime())->format("Y-m-d H:i:s") . " - ERROR - Import failed\n";
echo (new \DateTime())->format("Y-m-d H:i:s") . " - ERROR - Error message: " . $e->getMessage() . "\n";
}

echo "Centreon Remote import finished.\n";
echo (new \DateTime())->format("Y-m-d H:i:s") . " - INFO - Centreon Remote import process finished.\n";
}

public function getDi(): Container
Expand Down