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

Commit

Permalink
enh(anomalydetection): add lua parameters from options table (#8439)
Browse files Browse the repository at this point in the history
* enh(anomalydetection): add lua parameters from options table
* fix(broker): uninitialized var
* fix(generation): correct proxy url build
  • Loading branch information
lpinsivy authored Apr 8, 2020
1 parent 7a53d86 commit 1109da4
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions www/class/config-generate/broker.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private function generate($poller_id, $localhost)
}

$watchdog = [];
$anomalyDetectionLuaOutputGroupID = -1;

$result = $this->stmt_broker->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
Expand Down Expand Up @@ -233,6 +234,18 @@ private function generate($poller_id, $localhost)
$object[$key][$subvalue['config_group_id']][$res[0]][(int)$subvalue['fieldIndex']][$res[1]] =
$subvalue['config_value'];
$subValuesToCastInArray[$subvalue['config_group_id']][] = $res[0];

if ((strcmp(
$object[$key][$subvalue['config_group_id']]['name'],
"forward-to-anomaly-detection"
) == 0)
&& (strcmp(
$object[$key][$subvalue['config_group_id']]['path'],
"/usr/share/centreon-broker/lua/centreon-anomaly-detection.lua"
) == 0)
) {
$anomalyDetectionLuaOutputGroupID = $subvalue['config_group_id'];
}
}
}

Expand Down Expand Up @@ -267,11 +280,20 @@ private function generate($poller_id, $localhost)
];
}

if ($anomalyDetectionLuaOutputGroupID >= 0) {
$object["output"][$anomalyDetectionLuaOutputGroupID]['lua_parameter'] = array_merge_recursive(
$object["output"][$anomalyDetectionLuaOutputGroupID]['lua_parameter'],
$this->generateAnomalyDetectionLuaParameters()
);
$anomalyDetectionLuaOutputGroupID = -1;
}

// gRPC parameters
$object['grpc'] = [
'port' => 51000 + (int) $row['config_id']
];


// Generate file
$this->generateFile($object);
$this->writeFile($this->backend_instance->getPath());
Expand Down Expand Up @@ -431,4 +453,68 @@ private function rpnOperation($result, $item)
}
return $result;
}

/**
* Generate complete proxy url
*
* @return array with lua parameters
*/
private function generateAnomalyDetectionLuaParameters(): array
{
global $pearDB;

$luaParameters = [];

$stmt = $pearDB->query(
"SELECT * FROM options WHERE options.key IN ('saas_token', 'saas_use_proxy', 'saas_url')"
);
while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($item['key'] == "saas_token") {
$luaParameters[] = [
"type" => "string",
"name" => "token",
"value" => $item['value']
];
} elseif ($item['key'] == "saas_url") {
$luaParameters[] = [
"type" => "string",
"name" => "destination",
"value" => $item['value']
];
} elseif (($item['key'] == "saas_use_proxy") && ($item['value'] == "1")) {
$proxyInfo = [];
$stmtProxy = $pearDB->query(
"SELECT * FROM options WHERE options.key IN ('proxy_url', 'proxy_port', 'proxy_user', 'proxy_password')"
);
while ($data = $stmtProxy->fetch(PDO::FETCH_ASSOC)) {
$proxyInfo[$data['key']] = $data['value'];
}

// Generate proxy URL
$proxy = '';
if (
!empty($proxyInfo['proxy_user'])
&& !empty($proxyInfo['proxy_password'])
) {
$proxy = $proxyInfo['proxy_user'] . ':' . $proxyInfo['proxy_password'] . '@';
}

$proxy = (parse_url($proxyInfo['proxy_url'], PHP_URL_SCHEME)
? (parse_url($proxyInfo['proxy_url'], PHP_URL_SCHEME) . '//:')
: 'http://'
) . $proxy . parse_url($proxyInfo['proxy_url'], PHP_URL_HOST);
if (isset($proxyInfo['proxy_port']) && !empty($proxyInfo['proxy_port'])) {
$proxy .= ':' . $proxyInfo['proxy_port'];
}

$luaParameters[] = [
"type" => "string",
"name" => "proxy",
"value" => $proxy
];
}
}

return $luaParameters;
}
}

0 comments on commit 1109da4

Please sign in to comment.