Add parallel processing for HTTP requests and handle fallback values

- Implement parallel processing using pcntl_fork for HTTP requests
- Handle different types of HTTP data retrieval and response codes
- Set fallback values if necessary based on configuration
This commit is contained in:
Jeroen De Meerleer 2024-06-05 22:13:05 +02:00
parent 09bba2a0c3
commit aa6d231ead
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6

134
index.php
View File

@ -22,72 +22,87 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
$metric['submetrics'] = [$metric];
}
foreach ($metric['submetrics'] as $subkey => &$submetric) {
if (isset($submetric['command'])) {
$output = null;
$retval = null;
try {
exec($submetric['command'], $output, $retval);
$output = implode("\n", $output);
} catch (Exception $e) {
$output = '';
}
} elseif (isset($submetric['http'])) {
$client = new GuzzleHttp\Client();
$options = [];
$output = NULL;
if (isset($submetric['http']['proxy'])) {
$options['proxy'] = $submetric['http']['proxy'];
}
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsetime') {
$options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) {
$output = $stats->getTransferTime();
};
}
$hasresponse = NULL;
try {
$res = $client->request('GET', $submetric['http']['url'], $options);
$hasresponse = true;
} catch (GuzzleHttp\Exception\GuzzleException $e) {
if (method_exists($e, 'getResponse')) {
$res = $e->getResponse();
$hasresponse = true;
} else {
$hasresponse = false;
}
}
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsebody') {
if ($hasresponse) {
$output = $res->getBody()->getContents();
} else {
$pid = pcntl_fork();
if ($pid > 0) {
$pids[] = $pid;
} else {
if (isset($submetric['command'])) {
$output = null;
$retval = null;
try {
exec($submetric['command'], $output, $retval);
$output = implode("\n", $output);
} catch (Exception $e) {
$output = '';
}
} elseif ((isset($submetric['http']['statuscode'])) && (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse')) {
if ($hasresponse) {
$output = $output = (int)in_array($res->getStatusCode(), $submetric['http']['statuscode']);;
} else {
$output = 0;
} elseif (isset($submetric['http'])) {
$client = new GuzzleHttp\Client();
$options = [];
$output = NULL;
if (isset($submetric['http']['proxy'])) {
$options['proxy'] = $submetric['http']['proxy'];
}
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsetime') {
$options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) {
$output = $stats->getTransferTime();
};
}
$hasresponse = NULL;
try {
$res = $client->request('GET', $submetric['http']['url'], $options);
$hasresponse = true;
} catch (GuzzleHttp\Exception\GuzzleException $e) {
if (method_exists($e, 'getResponse')) {
$res = $e->getResponse();
$hasresponse = true;
} else {
$hasresponse = false;
}
}
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsebody') {
if ($hasresponse) {
$output = $res->getBody()->getContents();
} else {
$output = '';
}
} elseif ((isset($submetric['http']['statuscode'])) && (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse')) {
if ($hasresponse) {
$output = $output = (int)in_array($res->getStatusCode(), $submetric['http']['statuscode']);;
} else {
$output = 0;
}
} elseif (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse') {
$output = (int)$hasresponse;
}
} elseif (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse') {
$output = (int)$hasresponse;
}
if (isset($submetric['jsonelem'])) {
$submetric['value'] = getArrayValue($submetric['jsonelem'], json_decode($output, true));
} else {
$submetric['value'] = $output;
}
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
file_put_contents('/tmp/.metrics.' . $key . '.' . $subkey, $submetric['value']);
exit();
}
if (isset($submetric['jsonelem'])) {
$submetric['value'] = getArrayValue($submetric['jsonelem'], json_decode($output, true));
} else {
$submetric['value'] = $output;
}
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
}
}
foreach ($pids as $pid) {
pcntl_waitpid($pid, $none);
}
if(!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
if(!isset($submetric['fallback']['maxage']) || time() < ($oldconfig[$key]['submetrics'][$subkey]['time'] + $submetric['fallback']['maxage'])) {
if($submetric['fallback']['type'] == 'previous') {
foreach ($config as $key => &$metric) {
foreach ($metric['submetrics'] as $subkey => &$submetric) {
$submetric['value'] = file_get_contents('/tmp/.metrics.' . $key . '.' . $subkey);
if (!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
if (!isset($submetric['fallback']['maxage']) || time() < ($oldconfig[$key]['submetrics'][$subkey]['time'] + $submetric['fallback']['maxage'])) {
if ($submetric['fallback']['type'] == 'previous') {
$submetric['value'] = $oldconfig[$key]['submetrics'][$subkey]['value'];
} elseif ($submetric['fallback']['type'] == 'static') {
$submetric['value'] = $submetric['fallback']['value'];
@ -100,6 +115,7 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
} else {
$submetric['time'] = time();
}
unlink('/tmp/.metrics.' . $key . '.' . $subkey);
}
}