Merge remote-tracking branch 'origin/develop' into ent-12607-faltan-botones-de-desacoplar-y-minimizar-en-consola-sonora-command-center

This commit is contained in:
daniel 2023-12-05 16:48:33 +01:00
commit 98f22dac72
4 changed files with 261 additions and 167 deletions

View File

@ -127,6 +127,58 @@ function events_translate_event_type($event_type)
}
/**
* Module status event_type into descriptive text.
*
* @param integer $event_type Event type.
*
* @return string Module status.
*/
function events_status_module_event_type($event_type)
{
$module_status = '';
switch ($event_type) {
case 'alert_fired':
case 'alert_recovered':
case 'alert_ceased':
case 'alert_manual_validation':
$module_status = AGENT_MODULE_STATUS_CRITICAL_ALERT;
break;
case 'going_down_normal':
case 'going_up_normal':
$module_status = AGENT_MODULE_STATUS_NORMAL;
break;
case 'going_unknown':
case 'unknown':
$module_status = AGENT_MODULE_STATUS_UNKNOWN;
break;
case 'going_up_warning':
case 'going_down_warning':
$module_status = AGENT_MODULE_STATUS_WARNING;
break;
case 'going_up_critical':
case 'going_down_critical':
$module_status = AGENT_MODULE_STATUS_CRITICAL_BAD;
break;
case 'recon_host_detected':
case 'system':
case 'error':
case 'new_agent':
case 'configuration_change':
default:
$module_status = AGENT_MODULE_STATUS_NOT_INIT;
break;
}
return $module_status;
}
/**
* Translates a numeric value event_status into descriptive text.
*

View File

@ -15986,171 +15986,174 @@ function reporting_module_histogram_graph($report, $content, $pdf=0)
return false;
}
$module_interval = modules_get_interval(
$content['id_agent_module']
);
$slice = ($content['period'] / $module_interval);
if ($metaconsole_on && $server_name != '') {
// Restore db connection.
metaconsole_restore_db();
}
$result_sla = reporting_advanced_sla(
$content['id_agent_module'],
($report['datetime'] - $content['period']),
$report['datetime'],
null,
null,
$filter = [
'id_agentmodule' => $content['id_agent_module'],
'group_rep' => EVENT_GROUP_REP_ALL,
'date_to' => date('Y-m-d', ($report['datetime'] - $content['period'])),
'time_to' => date('H:i:s', ($report['datetime'] - $content['period'])),
];
if ($metaconsole_on && $server_name != '') {
$filter['id_server'] = $connection['id'];
}
$previous_event = events_get_all(
['te.event_type, te.timestamp, te.utimestamp'],
$filter,
0,
null,
null,
null,
$slice,
1,
'desc',
'timestamp',
true
);
// Select Warning and critical values.
$agentmodule_info = modules_get_agentmodule($content['id_agent_module']);
$min_value_critical = ($agentmodule_info['min_critical'] == 0) ? null : $agentmodule_info['min_critical'];
$status = '';
if ($previous_event !== false) {
$previous_event = end($previous_event);
$status = $previous_event['event_type'];
}
// Check if module type is string.
$modules_is_string = modules_is_string($agentmodule_info['id_agente_modulo']);
$date_start = ($report['datetime'] - $content['period']);
$filter = [
'id_agentmodule' => $content['id_agent_module'],
'group_rep' => EVENT_GROUP_REP_ALL,
'date_from' => date('Y-m-d', $date_start),
'date_to' => date('Y-m-d', $report['datetime']),
'time_from' => date('H:i:s', $date_start),
'time_to' => date('H:i:s', $report['datetime']),
];
if ($modules_is_string === false) {
if ($agentmodule_info['max_critical'] == 0) {
$max_value_critical = null;
if ($agentmodule_info['min_critical'] == 0) {
if ((bool) $content['dinamic_proc'] === true) {
$max_value_critical = 0.01;
}
if ($metaconsole_on && $server_name != '') {
$filter['id_server'] = $connection['id'];
}
$events = events_get_all(
['te.event_type, te.timestamp, te.utimestamp'],
$filter,
null,
null,
null,
null,
true
);
// Metaconsole connection.
if ($metaconsole_on && $server_name != '') {
$connection = metaconsole_get_connection($server_name);
if (!metaconsole_load_external_db($connection)) {
ui_print_error_message('Error connecting to '.$server_name);
}
}
$not_init_data = [];
$previous_data = [
'event_type' => $status,
'utimestamp' => $date_start,
];
if (empty($status) === true) {
// Si viene de no iniciado busco el primer dato del modulo y si es de histórico.
$first_utimestamp = false;
$search_historydb = false;
$extract_first_data = modules_get_first_date($content['id_agent_module'], 0);
if (empty($extract_first_data) === false) {
$first_utimestamp = $extract_first_data['first_utimestamp'];
$search_historydb = (isset($extract_first_data['search_historydb']) === true) ? $extract_first_data['search_historydb'] : false;
}
// Si se encuentra algun dato.
// Si no hay eventos, la fecha del primer dato no sea mayor al fin del report (seria un bloque completo de no iniciado).
// Se comprueba que si existen eventos el dato no sea previo al evento.
if ($first_utimestamp !== false
&& ((empty($events) === true && $first_utimestamp < $report['datetime']) || (empty($events) === false && $first_utimestamp < $events[0]['utimestamp']))
) {
// Tenemos en cuenta si el modulo es de tipo string.
$module = modules_get_agentmodule($content['id_agent_module']);
$module_type = $module['id_tipo_modulo'];
$module_type_str = modules_get_type_name($module_type);
$table = 'tagente_datos';
if (strstr($module_type_str, 'string') !== false) {
$table = 'tagente_datos_string';
}
$query = sprintf(
'SELECT datos,utimestamp
FROM %s
WHERE id_agente_modulo = %d
AND utimestamp = %d
',
$table,
$content['id_agent_module'],
$first_utimestamp
);
$data = db_get_all_rows_sql($query, $search_historydb);
if ($data !== false) {
$not_init_data = [
'event_type' => $status,
'utimestamp' => $date_start,
];
$previous_data = [
'event_type' => 'going_up_normal',
'utimestamp' => $data[0]['utimestamp'],
];
}
} else {
$max_value_critical = $agentmodule_info['max_critical'];
}
} else {
if ($agentmodule_info['str_critical'] == '') {
$max_value_critical = null;
} else {
$max_value_critical = $agentmodule_info['str_critical'];
}
}
$inverse_critical = $agentmodule_info['critical_inverse'];
$min_value_warning = ($agentmodule_info['min_warning'] == 0) ? null : $agentmodule_info['min_warning'];
if ($modules_is_string === false) {
if ($agentmodule_info['max_warning'] == 0) {
$max_value_warning = null;
} else {
$max_value_warning = $agentmodule_info['max_warning'];
}
} else {
if ($agentmodule_info['str_warning'] == '') {
$max_value_warning = null;
} else {
$max_value_warning = $agentmodule_info['str_warning'];
}
$array_result = [];
$time_total = $content['period'];
$check_total = 0;
$check_ok = 0;
$time_ok = 0;
if (empty($events) === true) {
$events = [];
}
$inverse_warning = $agentmodule_info['warning_inverse'];
// Añadimos el dato previo.
array_unshift($events, $previous_data);
if (empty($not_init_data) === false) {
// Añadimos si viene de no iniciado el no iniciado.
array_unshift($events, $not_init_data);
}
$data = [];
$data['time_total'] = 0;
$data['time_ok'] = 0;
$data['time_error'] = 0;
$data['time_warning'] = 0;
$data['time_unknown'] = 0;
$data['time_not_init'] = 0;
$data['time_downtime'] = 0;
$data['checks_total'] = 0;
$data['checks_ok'] = 0;
$data['checks_error'] = 0;
$data['checks_warning'] = 0;
$data['checks_unknown'] = 0;
$data['checks_not_init'] = 0;
$array_graph = [];
$current_time = time();
$i = 0;
foreach ($result_sla as $value_sla) {
$data['time_total'] += $value_sla['time_total'];
$data['time_ok'] += $value_sla['time_ok'];
$data['time_error'] += $value_sla['time_error'];
$data['time_warning'] += $value_sla['time_warning'];
$data['time_unknown'] += $value_sla['time_unknown'];
$data['time_downtime'] += $value_sla['time_downtime'];
$data['time_not_init'] += $value_sla['time_not_init'];
$data['checks_total'] += $value_sla['checks_total'];
$data['checks_ok'] += $value_sla['checks_ok'];
$data['checks_error'] += $value_sla['checks_error'];
$data['checks_warning'] += $value_sla['checks_warning'];
$data['checks_unknown'] += $value_sla['checks_unknown'];
$data['checks_not_init'] += $value_sla['checks_not_init'];
// Generate raw data for graph.
if ($value_sla['time_total'] != 0) {
if ($value_sla['time_error'] > 0) {
// ERR.
$array_graph[$i]['data'] = 3;
} else if ($value_sla['time_unknown'] > 0) {
// UNKNOWN.
$array_graph[$i]['data'] = 4;
} else if ($value_sla['time_warning'] > 0) {
// Warning.
$array_graph[$i]['data'] = 2;
} else if ($value_sla['time_not_init'] == $value_sla['time_total']) {
// NOT INIT.
$array_graph[$i]['data'] = 6;
} else {
$array_graph[$i]['data'] = 1;
}
foreach ($events as $event) {
$array_result[$i]['data'] = events_status_module_event_type($event['event_type']);
if (isset($events[($i + 1)]) === true) {
$period = ($events[($i + 1)]['utimestamp'] - $event['utimestamp']);
$array_result[$i]['utimestamp'] = $period;
} else {
$array_graph[$i]['data'] = 7;
$period = ($report['datetime'] - $event['utimestamp']);
// El ultimo evento solo lo arrastramos hasta la fecha actual.
// Si pedimos mas fecha sera desconocido.
if ($report['datetime'] > $current_time) {
$period = ($current_time - $event['utimestamp']);
}
$array_result[$i]['utimestamp'] = $period;
}
$array_graph[$i]['utimestamp'] = ($value_sla['date_to'] - $value_sla['date_from']);
if ($array_result[$i]['data'] === AGENT_MODULE_STATUS_NORMAL) {
$check_ok++;
$time_ok += $period;
}
$check_total++;
$i++;
}
$data['sla_value'] = reporting_sla_get_compliance_from_array(
$data
);
$data['sla_fixed'] = sla_truncate(
$data['sla_value'],
$config['graph_precision']
);
$data_init = -1;
$acum = 0;
$sum = 0;
$array_result = [];
$i = 0;
foreach ($array_graph as $value) {
if ($data_init == -1) {
$data_init = $value['data'];
$acum = $value['utimestamp'];
} else {
if ($data_init == $value['data']) {
$acum = ($acum + $value['utimestamp']);
} else {
$array_result[$i]['data'] = $data_init;
$array_result[$i]['utimestamp'] = $acum;
$array_result[$i]['real_data'] = $sum;
$i++;
$data_init = $value['data'];
$acum = $value['utimestamp'];
}
}
// Bloque por si se pide mas fecha.
if ($report['datetime'] > $current_time) {
$array_result[$i]['data'] = AGENT_MODULE_STATUS_UNKNOWN;
$array_result[$i]['utimestamp'] = ($report['datetime'] - $current_time);
}
if (count($array_result) == 0) {
$array_result = $array_graph;
} else {
$array_result[$i]['data'] = $data_init;
$array_result[$i]['utimestamp'] = $acum;
$array_result[$i]['real_data'] = $sum;
}
$time_total = $data['time_total'];
// Slice graphs calculation.
$return['agent'] = modules_get_agentmodule_agent_alias(
$content['id_agent_module']
);
@ -16158,37 +16161,21 @@ function reporting_module_histogram_graph($report, $content, $pdf=0)
$content['id_agent_module']
);
$return['max_critical'] = $max_value_critical;
$return['min_critical'] = $min_value_critical;
$return['critical_inverse'] = $inverse_critical;
$return['max_warning'] = $max_value_warning;
$return['min_warning'] = $min_value_warning;
$return['warning_inverse'] = $inverse_warning;
$return['data_not_init'] = $data['checks_not_init'];
$return['data_unknown'] = $data['checks_unknown'];
$return['data_critical'] = $data['checks_error'];
$return['data_warning'] = $data['checks_warning'];
$return['data_ok'] = $data['checks_ok'];
$return['data_total'] = $data['checks_total'];
$return['time_not_init'] = $data['time_not_init'];
$return['time_unknown'] = $data['time_unknown'];
$return['time_critical'] = $data['time_error'];
$return['time_warning'] = $data['time_warning'];
$return['time_ok'] = $data['time_ok'];
if ($data['checks_total'] > 0) {
$return['percent_ok'] = (($data['checks_ok'] * 100) / $data['checks_total']);
$return['data_ok'] = $check_ok;
$return['data_total'] = $check_total;
if ($check_total > 0) {
$return['percent_ok'] = (($time_ok * 100) / $content['period']);
} else {
$return['percent_ok'] = 0;
}
$colors = [
1 => COL_NORMAL,
2 => COL_WARNING,
3 => COL_CRITICAL,
4 => COL_UNKNOWN,
5 => COL_DOWNTIME,
6 => COL_NOTINIT,
7 => COL_IGNORED,
AGENT_MODULE_STATUS_CRITICAL_ALERT => COL_ALERTFIRED,
AGENT_MODULE_STATUS_NORMAL => COL_NORMAL,
AGENT_MODULE_STATUS_UNKNOWN => COL_UNKNOWN,
AGENT_MODULE_STATUS_WARNING => COL_WARNING,
AGENT_MODULE_STATUS_CRITICAL_BAD => COL_CRITICAL,
AGENT_MODULE_STATUS_NOT_INIT => COL_NOTINIT,
];
$width_graph = 100;

View File

@ -120,6 +120,23 @@ final class EventsHistory extends Item
$moduleId = static::parseIntOr($linkedModule['moduleId'], null);
$legendColor = static::extractLegendColor($data);
$metaconsoleId = $linkedModule['metaconsoleId'];
// Maybe connect to node.
$nodeConnected = false;
if (\is_metaconsole() === true && $metaconsoleId !== null) {
$nodeConnected = \metaconsole_connect(
null,
$metaconsoleId
) === NOERR;
if ($nodeConnected === false) {
throw new \InvalidArgumentException(
'error connecting to the node'
);
}
}
if ($agentId === null) {
throw new \InvalidArgumentException('missing agent Id');
}
@ -166,6 +183,11 @@ final class EventsHistory extends Item
$data['html'] = $html;
// Restore connection.
if ($nodeConnected === true) {
\metaconsole_restore_db();
}
return $data;
}

View File

@ -1836,6 +1836,34 @@ sub pandora_execute_action ($$$$$$$$$;$$) {
. $base64_data . "\n";
}
# Image that comes from module macro substitution.
if ($field3 =~ /cid:moduledata_/) {
$content_type = 'multipart/related; boundary="'.$boundary.'"';
$boundary = "--" . $boundary;
$field3 = $boundary . "\n"
. "Content-Type: " . $html_content_type . "\n\n"
# "Content-Transfer-Encoding: quoted-printable\n\n"
. $field3 . "\n";
my @matches = ($field3 =~ /cid:moduledata_(\d+)/g);
foreach my $module_id (@matches) {
# Get base64 Image for the module.
my $module_data = get_db_value($dbh, 'SELECT datos FROM tagente_estado WHERE id_agente_modulo = ?', $module_id);
my $base64_data = substr($module_data, 23); # remove first 23 characters: 'data:image/png;base64, '
$cid = 'moduledata_'.$module_id;
my $filename = $cid . ".png";
$field3 .= $boundary . "\n"
. "Content-Type: image/png; name=\"" . $filename . "\"\n"
. "Content-Disposition: inline; filename=\"" . $filename . "\"\n"
. "Content-Transfer-Encoding: base64\n"
. "Content-ID: <" . $cid . ">\n"
. "Content-Location: " . $filename . "\n\n"
. $base64_data . "\n";
}
}
if ($pa_config->{"mail_in_separate"} != 0){
foreach my $address (split (',', $field1)) {
# Remove blanks
@ -5154,6 +5182,11 @@ sub on_demand_macro($$$$$$;$) {
elsif (defined($unit_mod) && $unit_mod ne '') {
$field_value .= $unit_mod;
}
if ($field_value =~ /^data:image\/png;base64, /) {
# macro _data_ substitution in case is image.
$field_value = '<img style="height: 150px;" src="cid:moduledata_' . $id_mod . '"/>';
}
return(defined($field_value)) ? $field_value : '';
} elsif ($macro eq '_secondarygroups_') {