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:
parent
09bba2a0c3
commit
aa6d231ead
134
index.php
134
index.php
@ -22,72 +22,87 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
|||||||
$metric['submetrics'] = [$metric];
|
$metric['submetrics'] = [$metric];
|
||||||
}
|
}
|
||||||
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
||||||
|
$pid = pcntl_fork();
|
||||||
if (isset($submetric['command'])) {
|
if ($pid > 0) {
|
||||||
$output = null;
|
$pids[] = $pid;
|
||||||
$retval = null;
|
} else {
|
||||||
try {
|
if (isset($submetric['command'])) {
|
||||||
exec($submetric['command'], $output, $retval);
|
$output = null;
|
||||||
$output = implode("\n", $output);
|
$retval = null;
|
||||||
} catch (Exception $e) {
|
try {
|
||||||
$output = '';
|
exec($submetric['command'], $output, $retval);
|
||||||
}
|
$output = implode("\n", $output);
|
||||||
} elseif (isset($submetric['http'])) {
|
} catch (Exception $e) {
|
||||||
$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 = '';
|
$output = '';
|
||||||
}
|
}
|
||||||
} elseif ((isset($submetric['http']['statuscode'])) && (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse')) {
|
} elseif (isset($submetric['http'])) {
|
||||||
if ($hasresponse) {
|
$client = new GuzzleHttp\Client();
|
||||||
$output = $output = (int)in_array($res->getStatusCode(), $submetric['http']['statuscode']);;
|
|
||||||
} else {
|
$options = [];
|
||||||
$output = 0;
|
$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));
|
foreach ($pids as $pid) {
|
||||||
} else {
|
pcntl_waitpid($pid, $none);
|
||||||
$submetric['value'] = $output;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
|
|
||||||
|
|
||||||
|
|
||||||
if(!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
|
foreach ($config as $key => &$metric) {
|
||||||
if(!isset($submetric['fallback']['maxage']) || time() < ($oldconfig[$key]['submetrics'][$subkey]['time'] + $submetric['fallback']['maxage'])) {
|
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
||||||
if($submetric['fallback']['type'] == 'previous') {
|
$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'];
|
$submetric['value'] = $oldconfig[$key]['submetrics'][$subkey]['value'];
|
||||||
} elseif ($submetric['fallback']['type'] == 'static') {
|
} elseif ($submetric['fallback']['type'] == 'static') {
|
||||||
$submetric['value'] = $submetric['fallback']['value'];
|
$submetric['value'] = $submetric['fallback']['value'];
|
||||||
@ -100,6 +115,7 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
|||||||
} else {
|
} else {
|
||||||
$submetric['time'] = time();
|
$submetric['time'] = time();
|
||||||
}
|
}
|
||||||
|
unlink('/tmp/.metrics.' . $key . '.' . $subkey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user