Core: Fix and optimized lag calculaton. Console: optimized and unified queries with server

This commit is contained in:
Calvo 2023-02-21 12:56:26 +01:00
parent 83a296b6c2
commit 47bd0081ef
2 changed files with 87 additions and 59 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Auxiliary functions to manage servers. * Auxiliary functions to manage servers.
* *
@ -44,7 +45,7 @@ function servers_get_server($id_server, $filter=false, $fields=false)
return false; return false;
} }
if (! is_array($filter)) { if (!is_array($filter)) {
$filter = []; $filter = [];
} }
@ -145,7 +146,6 @@ function servers_get_total_modules()
); );
return $modules; return $modules;
} }
@ -1017,40 +1017,57 @@ function servers_get_info($id_server=-1)
// Remote servers LAG Calculation (server_type != 0). // Remote servers LAG Calculation (server_type != 0).
if ($server['server_type'] != 0) { if ($server['server_type'] != 0) {
// MySQL 8.0 has function lag(). So, lag must be enclosed in quotations. // MySQL 8.0 has function lag(). So, lag must be enclosed in quotations.
$result = db_get_row_sql( $sql = sprintf(
'SELECT COUNT(tagente_modulo.id_agente_modulo) AS module_lag, 'SELECT COUNT(tam.id_agente_modulo) AS module_lag,
AVG(UNIX_TIMESTAMP() - utimestamp - current_interval) AS "lag" AVG(UNIX_TIMESTAMP() - tae.last_execution_try - tae.current_interval) AS "lag"
FROM tagente_estado, tagente_modulo, tagente FROM (
WHERE utimestamp > 0 SELECT tagente_estado.last_execution_try, tagente_estado.current_interval, tagente_estado.id_agente_modulo
AND tagente.disabled = 0 FROM tagente_estado
AND tagente.id_agente = tagente_estado.id_agente WHERE tagente_estado.current_interval > 0
AND tagente_modulo.disabled = 0 AND tagente_estado.last_execution_try > 0
AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_estado.running_by = %d
AND current_interval > 0 ) tae
AND running_by = '.$server['id_server'].' JOIN (
AND (UNIX_TIMESTAMP() - utimestamp) < ( current_interval * 10) SELECT tagente_modulo.id_agente_modulo
AND (UNIX_TIMESTAMP() - utimestamp) > current_interval' FROM tagente_modulo LEFT JOIN tagente
ON tagente_modulo.id_agente = tagente.id_agente
WHERE tagente.disabled = 0
AND tagente_modulo.disabled = 0
) tam
ON tae.id_agente_modulo = tam.id_agente_modulo
WHERE (UNIX_TIMESTAMP() - tae.last_execution_try) > (tae.current_interval)
AND (UNIX_TIMESTAMP() - tae.last_execution_try) < ( tae.current_interval * 10)',
$server['id_server']
); );
} else { } else {
// Local/Dataserver server LAG calculation. // Local/Dataserver server LAG calculation.
// MySQL 8.0 has function lag(). So, lag must be enclosed in quotations. // MySQL 8.0 has function lag(). So, lag must be enclosed in quotations.
$result = db_get_row_sql( $sql = sprintf(
'SELECT COUNT(tagente_modulo.id_agente_modulo) AS module_lag, 'SELECT COUNT(tam.id_agente_modulo) AS module_lag,
AVG(UNIX_TIMESTAMP() - utimestamp - current_interval) AS "lag" AVG(UNIX_TIMESTAMP() - tae.last_execution_try - tae.current_interval) AS "lag"
FROM tagente_estado, tagente_modulo, tagente FROM (
WHERE utimestamp > 0 SELECT tagente_estado.last_execution_try, tagente_estado.current_interval, tagente_estado.id_agente_modulo
AND tagente.disabled = 0 FROM tagente_estado
AND tagente.id_agente = tagente_estado.id_agente WHERE tagente_estado.current_interval > 0
AND tagente_modulo.disabled = 0 AND tagente_estado.last_execution_try > 0
AND tagente_modulo.id_tipo_modulo < 5 AND tagente_estado.running_by = %d
AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo ) tae
AND current_interval > 0 JOIN (
AND (UNIX_TIMESTAMP() - utimestamp) < ( current_interval * 10) SELECT tagente_modulo.id_agente_modulo
AND running_by = '.$server['id_server'].' FROM tagente_modulo LEFT JOIN tagente
AND (UNIX_TIMESTAMP() - utimestamp) > (current_interval * 1.1)' ON tagente_modulo.id_agente = tagente.id_agente
WHERE tagente.disabled = 0
AND tagente_modulo.disabled = 0
AND tagente_modulo.id_tipo_modulo < 5
) tam
ON tae.id_agente_modulo = tam.id_agente_modulo
WHERE (UNIX_TIMESTAMP() - tae.last_execution_try) > (tae.current_interval * 1.1)
AND (UNIX_TIMESTAMP() - tae.last_execution_try) < ( tae.current_interval * 10)',
$server['id_server']
); );
} }
$result = db_get_row_sql($sql);
// Lag over current_interval * 2 is not lag, // Lag over current_interval * 2 is not lag,
// it's a timed out module. // it's a timed out module.
if (!empty($result['lag'])) { if (!empty($result['lag'])) {
@ -1137,11 +1154,11 @@ function servers_check_remote_config($server_name)
$config['remote_config'] $config['remote_config']
).'/conf/'.$server_md5.'.srv.conf'; ).'/conf/'.$server_md5.'.srv.conf';
if (! isset($filenames['conf'])) { if (!isset($filenames['conf'])) {
return false; return false;
} }
if (! isset($filenames['md5'])) { if (!isset($filenames['md5'])) {
return false; return false;
} }

View File

@ -5629,37 +5629,48 @@ sub pandora_server_statistics ($$) {
if ($server->{"server_type"} != DATASERVER){ if ($server->{"server_type"} != DATASERVER){
$lag_row = get_db_single_row ($dbh, $lag_row = get_db_single_row ($dbh,
"SELECT COUNT(tam.id_agente_modulo) AS module_lag, AVG(UNIX_TIMESTAMP() - tae.last_execution_try - tae.current_interval) AS lag "SELECT COUNT(tam.id_agente_modulo) AS module_lag,
FROM ( AVG(UNIX_TIMESTAMP() - tae.last_execution_try - tae.current_interval) AS "lag"
SELECT tagente_estado.last_execution_try, tagente_estado.current_interval, tagente_estado.id_agente_modulo FROM (
FROM tagente_estado SELECT tagente_estado.last_execution_try, tagente_estado.current_interval, tagente_estado.id_agente_modulo
WHERE tagente_estado.current_interval > 0 FROM tagente_estado
AND tagente_estado.last_execution_try > 0 WHERE tagente_estado.current_interval > 0
AND tagente_estado.running_by = ? AND tagente_estado.last_execution_try > 0
) tae AND tagente_estado.running_by = ?
JOIN ( ) tae
SELECT tagente_modulo.id_agente_modulo, tagente_modulo.flag JOIN (
FROM tagente_modulo LEFT JOIN tagente SELECT tagente_modulo.id_agente_modulo
ON tagente_modulo.id_agente = tagente.id_agente FROM tagente_modulo LEFT JOIN tagente
WHERE tagente.disabled = 0 ON tagente_modulo.id_agente = tagente.id_agente
AND tagente_modulo.disabled = 0 WHERE tagente.disabled = 0
AND tagente_modulo.id_tipo_modulo < 5 AND tagente_modulo.disabled = 0
) tam ) tam
ON tae.id_agente_modulo = tam.id_agente_modulo ON tae.id_agente_modulo = tam.id_agente_modulo
WHERE (UNIX_TIMESTAMP() - tae.last_execution_try) < ( tae.current_interval * 10) WHERE (UNIX_TIMESTAMP() - tae.last_execution_try) > (tae.current_interval)
AND (tam.flag = 1 OR (UNIX_TIMESTAMP() - tae.last_execution_try) > tae.current_interval)", $server->{"id_server"}); AND (UNIX_TIMESTAMP() - tae.last_execution_try) < ( tae.current_interval * 10)", $server->{"id_server"});
} }
# Dataserver LAG calculation: # Dataserver LAG calculation:
else { else {
$lag_row = get_db_single_row ($dbh, "SELECT COUNT(tagente_modulo.id_agente_modulo) AS module_lag, AVG(UNIX_TIMESTAMP() - utimestamp - current_interval) AS lag $lag_row = get_db_single_row ($dbh, "SELECT COUNT(tam.id_agente_modulo) AS module_lag,
FROM tagente_estado, tagente_modulo AVG(UNIX_TIMESTAMP() - tae.last_execution_try - tae.current_interval) AS "lag"
WHERE utimestamp > 0 FROM (
AND tagente_modulo.disabled = 0 SELECT tagente_estado.last_execution_try, tagente_estado.current_interval, tagente_estado.id_agente_modulo
AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo FROM tagente_estado
AND current_interval > 0 WHERE tagente_estado.current_interval > 0
AND running_by = ? AND tagente_estado.last_execution_try > 0
AND (UNIX_TIMESTAMP() - utimestamp) < ( current_interval * 10) AND tagente_estado.running_by = ?
AND (UNIX_TIMESTAMP() - utimestamp) > current_interval", $server->{"id_server"}); ) tae
JOIN (
SELECT tagente_modulo.id_agente_modulo
FROM tagente_modulo LEFT JOIN tagente
ON tagente_modulo.id_agente = tagente.id_agente
WHERE tagente.disabled = 0
AND tagente_modulo.disabled = 0
AND tagente_modulo.id_tipo_modulo < 5
) tam
ON tae.id_agente_modulo = tam.id_agente_modulo
WHERE (UNIX_TIMESTAMP() - tae.last_execution_try) > (tae.current_interval * 1.1)
AND (UNIX_TIMESTAMP() - tae.last_execution_try) < ( tae.current_interval * 10)", $server->{"id_server"});
} }
$server->{"module_lag"} = $lag_row->{'module_lag'}; $server->{"module_lag"} = $lag_row->{'module_lag'};