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

enh(anomalydetection): add lua parameters from options table #8439

Merged
merged 7 commits into from
Apr 8, 2020
84 changes: 84 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"
lpinsivy marked this conversation as resolved.
Show resolved Hide resolved
) == 0
) {
$anomalyDetectionLuaOutputGroupID = $subvalue['config_group_id'];
}
}
}

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

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

// Generate file
$this->generateFile($object);
$this->writeFile($this->backend_instance->getPath());
Expand Down Expand Up @@ -426,4 +446,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) . '//:')
cgagnaire marked this conversation as resolved.
Show resolved Hide resolved
: 'http://'
) . $proxy . $proxyInfo['proxy_url'];
if (isset($proxyInfo['proxy_port']) && !empty($proxyInfo['proxy_port'])) {
$proxy .= ':' . $proxyInfo['proxy_port'];
}

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

return $luaParameters;
}
}