show last status change of module

This commit is contained in:
alejandro-campos 2020-03-23 12:47:58 +01:00
parent 014688468c
commit e05c292d7d
9 changed files with 129 additions and 68 deletions

View File

@ -0,0 +1,8 @@
START TRANSACTION;
ALTER TABLE `tagente_estado` ADD COLUMN `last_status_change` bigint(20) NOT NULL default '0';
UPDATE `tconfig` SET `value`='policy,agent,data_type,module_name,server_type,interval,status,last_status_change,graph,warn,data,timestamp' WHERE `token` = 'status_monitor_fields';
COMMIT;

View File

@ -97,6 +97,10 @@ if ($fields_selected[0] != '') {
$result = __('Status');
break;
case 'last_status_change':
$result = __('Last status change');
break;
case 'graph':
$result = __('Graph');
break;
@ -112,10 +116,6 @@ if ($fields_selected[0] != '') {
case 'timestamp':
$result = __('Timestamp');
break;
case 'to_critical':
$result = __('Last status change');
break;
}
$result_selected[$field_selected] = $result;
@ -146,11 +146,11 @@ $fields_available['module_name'] = __('Module name');
$fields_available['server_type'] = __('Server type');
$fields_available['interval'] = __('Interval');
$fields_available['status'] = __('Status');
$fields_available['last_status_change'] = __('Last status change');
$fields_available['graph'] = __('Graph');
$fields_available['warn'] = __('Warn');
$fields_available['data'] = __('Data');
$fields_available['timestamp'] = __('Timestamp');
$fields_available['to_critical'] = __('Last status change');
// remove fields already selected
foreach ($fields_available as $key => $available) {

View File

@ -1028,7 +1028,10 @@ if (check_login()) {
$title
);
$data[5] = ui_print_status_image($status, htmlspecialchars($title), true, false, false, false, $module['last_status_change']);
$last_status_change_text = 'Time elapsed since last status change: ';
$last_status_change_text .= !empty($module['last_status_change']) ? human_time_comparation($module['last_status_change']) : '';
$data[5] = ui_print_status_image($status, htmlspecialchars($title), true, false, false, false, $last_status_change_text);
if (!$show_context_help_first_time) {
$show_context_help_first_time = true;

View File

@ -3622,3 +3622,25 @@ function agents_get_sap_agents($id_agent)
return false;
}
/**
* Return time at which last status change of a module occured.
*
* @param integer $id_agent.
* @return string timestamp.
*/
function agents_get_last_status_change($id_agent)
{
$sql = sprintf(
'SELECT *
FROM tagente_estado
WHERE id_agente = %d
ORDER BY last_status_change DESC',
$id_agent
);
$row = db_get_row_sql($sql);
return $row['last_status_change'];
}

View File

@ -325,6 +325,13 @@ function treeview_printModuleTable($id_module, $server_data=false, $no_head=fals
$row['data'] = $last_data_str;
$table->data['last_data'] = $row;
// Last status change.
$last_status_change = db_get_value('last_status_change', 'tagente_estado', 'id_agente_modulo', $module['id_agente_modulo']);
$row = [];
$row['title'] = __('Last status change');
$row['data'] = human_time_comparation($last_status_change);
$table->data['tags'] = $row;
// End of table
html_print_table($table);

View File

@ -2654,6 +2654,7 @@ function ui_get_status_images_path()
* @param array $options Options to set image attributes: I.E.: style.
* @param string $path Path of the image, if not provided use the status path.
* @param boolean $image_with_css Don't use an image. Draw an image with css styles.
* @param string $extra_text Text that is displayed after title (i.e. time elapsed since last status change of module).
*
* @return string HTML code if return parameter is true.
*/
@ -2664,7 +2665,7 @@ function ui_print_status_image(
$options=false,
$path=false,
$image_with_css=false,
$module_last_status_change=''
$extra_info=''
) {
if ($path === false) {
$imagepath_array = ui_get_status_images_path();
@ -2679,15 +2680,9 @@ function ui_print_status_image(
$imagepath .= '/'.$type;
$title_extra_line = '';
if (!empty($module_last_status_change)) {
$title_extra_line = 'Time elapsed since last status change: '.date('h', $module_last_status_change).' '.__('hour').' '.date('i', $module_last_status_change).' '.__('min').' '.date('s', $module_last_status_change).' '.__('sec');
}
if ($image_with_css === true) {
$shape_status = get_shape_status_set($type);
return ui_print_status_sets($type, $title, $return, $shape_status, $title_extra_line);
return ui_print_status_sets($type, $title, $return, $shape_status, $extra_info);
} else {
if ($options === false) {
$options = [];
@ -2775,10 +2770,11 @@ function get_shape_status_set($type)
/**
* Prints an image representing a status.
*
* @param string $status Module status.
* @param string $title Title.
* @param boolean $return Whether to return an output string or echo now (optional, echo by default).
* @param array $options Options to set image attributes: I.E.: style.
* @param string $status Module status.
* @param string $title Title.
* @param boolean $return Whether to return an output string or echo now (optional, echo by default).
* @param array $options Options to set image attributes: I.E.: style.
* @param string $extra_info Text that is displayed after title (i.e. time elapsed since last status change of module).
*
* @return string HTML.
*/
@ -2787,7 +2783,7 @@ function ui_print_status_sets(
$title='',
$return=false,
$options=false,
$extra_line_title=''
$extra_info=''
) {
global $config;
@ -2806,8 +2802,8 @@ function ui_print_status_sets(
}
if ($title != '') {
$options['title'] = empty($extra_line_title) ? $title : $title.'&#10'.$extra_line_title;
$options['data-title'] = empty($extra_line_title) ? $title : $title.'<br>'.$extra_line_title;
$options['title'] = empty($extra_info) ? $title : $title.'&#10'.$extra_info;
$options['data-title'] = empty($extra_info) ? $title : $title.'<br>'.$extra_info;
$options['data-use_title_for_force_title'] = 1;
if (isset($options['class'])) {
$options['class'] .= ' forced_title';

View File

@ -368,6 +368,14 @@ if (enterprise_installed()) {
$table_contact->data[] = $data;
}
$data = [];
$data[0] = '<b>'.__('Last status change').'</b>';
$last_status_change_agent = agents_get_last_status_change($agent['id_agente']);
$time_elapsed = !empty($last_status_change_agent) ? human_time_comparation($last_status_change_agent) : '<em>'.__('N/A').'</em>';
$data[1] = $time_elapsed;
$table_contact->data[] = $data;
/*
* END: TABLE CONTACT BUILD
*/

View File

@ -81,6 +81,7 @@ $offset = (int) get_parameter('offset', 0);
$status = (int) get_parameter('status', 4);
$modulegroup = (int) get_parameter('modulegroup', -1);
$tag_filter = (int) get_parameter('tag_filter', 0);
$min_hours_status = (string) get_parameter('min_hours_status', '');
// Sort functionality
$sortField = get_parameter('sort_field');
$sort = get_parameter('sort', 'none');
@ -95,6 +96,7 @@ if ($ag_freestring !== '' || $moduletype !== '' || $datatype !== ''
|| $ag_modulename !== '' || $refr !== 0 || $offset !== 0 || $status !== 4
|| $modulegroup !== -1 || $tag_filter !== 0 || $sortField !== ''
|| $sort !== 'none' || $id_module !== 0 || $module_option !== 1
|| $min_hours_status !== ''
) {
$autosearch = true;
}
@ -243,6 +245,13 @@ if ($status == AGENT_MODULE_STATUS_NORMAL) {
AND tagente_modulo.id_tipo_modulo NOT IN (21,22,23,100)';
}
if (!empty($min_hours_status)) {
$date = new DateTime(null, new DateTimeZone($config['timezone']));
$current_timestamp = $date->getTimestamp();
$max_time = ($current_timestamp - ((int) $min_hours_status * 3600));
$sql_conditions .= sprintf(' AND tagente_estado.last_status_change < %d', $max_time);
}
// Filter by agent custom fields
$sql_conditions_custom_fields = '';
if (!empty($ag_custom_fields)) {
@ -531,16 +540,21 @@ $table->data[2][2] = '<span>'.__('Show monitors...').'</span>';
$table->data[2][3] = html_print_select($monitor_options, 'module_option', $module_option, '', '', '', true, false, true, '', false, 'width: 150px;');
$table->data[2][4] = '<span id="datatypetittle" ';
$min_hours_val = empty($min_hours_status) ? '' : (int) $min_hours_status;
$table->data[2][4] = '<span>'.__('Min. hours in current status').'</span>';
$table->data[2][5] = html_print_input_text('min_hours_status', $min_hours_val, '', 12, 20, true);
$table->data[3][0] = '<span id="datatypetittle" ';
if (!$_GET['sort']) {
$table->data[2][4] .= 'style="display:none"';
$table->data[3][0] .= 'style="display:none"';
}
$table->data[2][4] .= '>'.__('Data type').'</span>';
$table->data[3][0] .= '>'.__('Data type').'</span>';
$table->data[2][5] .= '<div id="datatypebox">';
$table->data[3][1] .= '<div id="datatypebox">';
switch ($moduletype) {
@ -609,33 +623,33 @@ switch ($moduletype) {
}
$a = db_get_all_rows_sql($sql);
$table->data[2][5] .= '<select id="datatype" name="datatype" ';
$table->data[3][1] .= '<select id="datatype" name="datatype" ';
if (!$_GET['sort']) {
$table->data[2][5] .= 'style="display:none"';
$table->data[3][1] .= 'style="display:none"';
}
$table->data[2][5] .= '>';
$table->data[3][1] .= '>';
$table->data[2][5] .= '<option name="datatype" value="">'.__('All').'</option>';
$table->data[3][1] .= '<option name="datatype" value="">'.__('All').'</option>';
foreach ($a as $valor) {
$table->data[2][5] .= '<option name="datatype" value="'.$valor['id_tipo'].'" ';
$table->data[3][1] .= '<option name="datatype" value="'.$valor['id_tipo'].'" ';
if ($valor['id_tipo'] == $datatype) {
$table->data[2][5] .= 'selected';
$table->data[3][1] .= 'selected';
}
$table->data[2][5] .= '>'.$valor['descripcion'].'</option>';
$table->data[3][1] .= '>'.$valor['descripcion'].'</option>';
}
$table->data[2][5] .= '</select>';
$table->data[3][1] .= '</select>';
$table->data[2][5] .= '</div>';
$table->data[3][1] .= '</div>';
$table_custom_fields = new stdClass();
@ -683,9 +697,9 @@ foreach ($custom_fields as $custom_field) {
$filters = '<form method="post" action="index.php?sec=view&amp;sec2=operation/agentes/status_monitor&amp;refr='.$refr.'&amp;ag_group='.$ag_group.'&amp;ag_freestring='.$ag_freestring.'&amp;module_option='.$module_option.'&amp;ag_modulename='.$ag_modulename.'&amp;moduletype='.$moduletype.'&amp;datatype='.$datatype.'&amp;status='.$status.'&amp;sort_field='.$sortField.'&amp;sort='.$sort.'&amp;pure='.$config['pure'].$ag_custom_fields_params.'">';
if (is_metaconsole()) {
$table->colspan[3][0] = 7;
$table->cellstyle[3][0] = 'padding: 10px;';
$table->data[3][0] = ui_toggle(
$table->colspan[4][0] = 7;
$table->cellstyle[4][0] = 'padding: 10px;';
$table->data[4][0] = ui_toggle(
html_print_table($table_custom_fields, true),
__('Advanced Options'),
'',
@ -698,9 +712,9 @@ if (is_metaconsole()) {
$filters .= '</form>';
ui_toggle($filters, __('Show Options'), '', '', false);
} else {
$table->colspan[3][0] = 7;
$table->cellstyle[3][0] = 'padding-left: 10px;';
$table->data[3][0] = ui_toggle(
$table->colspan[4][0] = 7;
$table->cellstyle[4][0] = 'padding-left: 10px;';
$table->data[4][0] = ui_toggle(
html_print_table(
$table_custom_fields,
true
@ -954,6 +968,7 @@ $sql = 'SELECT
tagente_modulo.snmp_oid,
tagente_estado.datos,
tagente_estado.estado,
tagente_estado.last_status_change,
tagente_modulo.min_warning,
tagente_modulo.max_warning,
tagente_modulo.str_warning,
@ -1066,6 +1081,7 @@ $url_module_name = 'index.php?sec=view&amp;sec2=operation/agentes/status_monitor
$url_server_type = 'index.php?sec=view&amp;sec2=operation/agentes/status_monitor&amp;datatype='.$datatype.'&amp;moduletype='.$moduletype.'&amp;refr='.$refr.'&amp;modulegroup='.$modulegroup.'&amp;offset='.$offset.'&amp;ag_group='.$ag_group.'&amp;ag_freestring='.$ag_freestring.'&amp;ag_modulename='.$ag_modulename.'&amp;status='.$status.$ag_custom_fields_params.'&amp;sort_field=moduletype&amp;sort=';
$url_interval = 'index.php?sec=view&amp;sec2=operation/agentes/status_monitor&amp;datatype='.$datatype.'&amp;moduletype='.$moduletype.'&amp;refr='.$refr.'&amp;modulegroup='.$modulegroup.'&amp;offset='.$offset.'&amp;ag_group='.$ag_group.'&amp;ag_freestring='.$ag_freestring.'&amp;ag_modulename='.$ag_modulename.'&amp;status='.$status.$ag_custom_fields_params.'&amp;sort_field=interval&amp;sort=';
$url_status = 'index.php?sec=view&amp;sec2=operation/agentes/status_monitor&amp;datatype='.$datatype.'&amp;moduletype='.$moduletype.'&amp;refr='.$refr.'&amp;modulegroup='.$modulegroup.'&amp;offset='.$offset.'&amp;ag_group='.$ag_group.'&amp;ag_freestring='.$ag_freestring.'&amp;ag_modulename='.$ag_modulename.'&amp;status='.$status.$ag_custom_fields_params.'&amp;sort_field=status&amp;sort=';
$url_status = 'index.php?sec=view&amp;sec2=operation/agentes/status_monitor&amp;datatype='.$datatype.'&amp;moduletype='.$moduletype.'&amp;refr='.$refr.'&amp;modulegroup='.$modulegroup.'&amp;offset='.$offset.'&amp;ag_group='.$ag_group.'&amp;ag_freestring='.$ag_freestring.'&amp;ag_modulename='.$ag_modulename.'&amp;status='.$status.$ag_custom_fields_params.'&amp;sort_field=last_status_change&amp;sort=';
$url_data = 'index.php?sec=view&amp;sec2=operation/agentes/status_monitor&amp;datatype='.$datatype.'&amp;moduletype='.$moduletype.'&amp;refr='.$refr.'&amp;modulegroup='.$modulegroup.'&amp;offset='.$offset.'&amp;ag_group='.$ag_group.'&amp;ag_freestring='.$ag_freestring.'&amp;ag_modulename='.$ag_modulename.'&amp;status='.$status.$ag_custom_fields_params.'&amp;sort_field=data&amp;sort=';
$url_timestamp_up = 'index.php?sec=view&amp;sec2=operation/agentes/status_monitor&amp;datatype='.$datatype.'&amp;moduletype='.$moduletype.'&amp;refr='.$refr.'&amp;offset='.$offset.'&amp;ag_group='.$ag_group.'&amp;ag_freestring='.$ag_freestring.'&amp;ag_modulename='.$ag_modulename.'&amp;status='.$status.$ag_custom_fields_params.'&amp;sort_field=timestamp&amp;sort=up';
$url_timestamp_down = 'index.php?sec=view&amp;sec2=operation/agentes/status_monitor&amp;datatype='.$datatype.'&amp;moduletype='.$moduletype.'&amp;refr='.$refr.'&amp;modulegroup='.$modulegroup.'&amp;offset='.$offset.'&amp;ag_group='.$ag_group.'&amp;ag_freestring='.$ag_freestring.'&amp;ag_modulename='.$ag_modulename.'&amp;status='.$status.$ag_custom_fields_params.'&amp;sort_field=timestamp&amp;sort=down';
@ -1125,32 +1141,34 @@ if (!empty($result)) {
$table->align[6] = 'left';
}
if (in_array('graph', $show_fields) || is_metaconsole()) {
$table->head[7] = __('Graph');
if (in_array('last_status_change', $show_fields)) {
$table->head[7] = __('Last status change');
$table->head[7] .= ui_get_sorting_arrows($url_status.'up', $url_status.'down', $selectStatusUp, $selectStatusDown);
$table->align[7] = 'left';
}
if (in_array('warn', $show_fields) || is_metaconsole()) {
$table->head[8] = __('Warn');
if (in_array('graph', $show_fields) || is_metaconsole()) {
$table->head[8] = __('Graph');
$table->align[8] = 'left';
}
if (in_array('data', $show_fields) || is_metaconsole()) {
$table->head[9] = __('Data');
if (in_array('warn', $show_fields) || is_metaconsole()) {
$table->head[9] = __('Warn');
$table->align[9] = 'left';
}
if (in_array('data', $show_fields) || is_metaconsole()) {
$table->head[10] = __('Data');
$table->align[10] = 'left';
if (is_metaconsole()) {
$table->head[9] .= ui_get_sorting_arrows($url_data.'up', $url_data.'down', $selectDataUp, $selectDataDown);
$table->head[10] .= ui_get_sorting_arrows($url_data.'up', $url_data.'down', $selectDataUp, $selectDataDown);
}
}
if (in_array('timestamp', $show_fields) || is_metaconsole()) {
$table->head[10] = __('Timestamp');
$table->head[10] .= ui_get_sorting_arrows($url_timestamp_up, $url_timestamp_down, $selectTimestampUp, $selectTimestampDown);
$table->align[10] = 'left';
}
if (in_array('to_critical', $show_fields)) {
$table->head[11] = __('Last status change');
$table->head[11] = __('Timestamp');
$table->head[11] .= ui_get_sorting_arrows($url_timestamp_up, $url_timestamp_down, $selectTimestampUp, $selectTimestampDown);
$table->align[11] = 'left';
}
$id_type_web_content_string = db_get_value(
@ -1462,8 +1480,12 @@ if (!empty($result)) {
}
}
if (in_array('last_status_change', $show_fields) || is_metaconsole()) {
$data[7] = human_time_comparation($row['last_status_change']);
}
if (in_array('graph', $show_fields) || is_metaconsole()) {
$data[7] = '';
$data[8] = '';
$acl_graphs = false;
@ -1498,13 +1520,13 @@ if (!empty($result)) {
$link = 'winopeng(\''.$url.'?'.$graph_params_str.'\',\''.$win_handle.'\')';
$data[7] = get_module_realtime_link_graph($row);
$data[8] = get_module_realtime_link_graph($row);
if (!is_snapshot_data($row['datos'])) {
$data[7] .= '<a href="javascript:'.$link.'">'.html_print_image('images/chart_curve.png', true, ['border' => '0', 'alt' => '']).'</a>';
$data[8] .= '<a href="javascript:'.$link.'">'.html_print_image('images/chart_curve.png', true, ['border' => '0', 'alt' => '']).'</a>';
}
$data[7] .= '<a href="javascript: '.'show_module_detail_dialog('.$row['id_agente_modulo'].', '.$row['id_agent'].', \''.$row['server_name'].'\', 0, '.SECONDS_1DAY.', \''.$row['module_name'].'\')">'.html_print_image(
$data[8] .= '<a href="javascript: '.'show_module_detail_dialog('.$row['id_agente_modulo'].', '.$row['id_agent'].', \''.$row['server_name'].'\', 0, '.SECONDS_1DAY.', \''.$row['module_name'].'\')">'.html_print_image(
'images/binary.png',
true,
[
@ -1513,13 +1535,13 @@ if (!empty($result)) {
]
).'</a>';
$data[7] .= '<span id=\'hidden_name_module_'.$row['id_agente_modulo'].'\'
$data[8] .= '<span id=\'hidden_name_module_'.$row['id_agente_modulo'].'\'
style=\'display: none;\'>'.$row['module_name'].'</span>';
}
}
if (in_array('warn', $show_fields) || is_metaconsole()) {
$data[8] = ui_print_module_warn_value(
$data[9] = ui_print_module_warn_value(
$row['max_warning'],
$row['min_warning'],
$row['str_warning'],
@ -1661,7 +1683,7 @@ if (!empty($result)) {
}
if (in_array('data', $show_fields) || is_metaconsole()) {
$data[9] = $salida;
$data[10] = $salida;
}
if (in_array('timestamp', $show_fields) || is_metaconsole()) {
@ -1680,12 +1702,7 @@ if (!empty($result)) {
$option = ['style' => 'font-size:7pt;'];
}
$data[10] = ui_print_timestamp($row['utimestamp'], true, $option);
}
if (in_array('to_critical', $show_fields)) {
$change_status_timestamp = db_get_sql('SELECT utimestamp FROM tevento WHERE id_agentmodule='.$row['id_agente_modulo'].' ORDER BY utimestamp DESC');
$data[11] = ui_print_timestamp($change_status_timestamp, true, $option);
$data[11] = ui_print_timestamp($row['utimestamp'], true, $option);
}
array_push($table->data, $data);

View File

@ -84,7 +84,7 @@ INSERT INTO `tconfig` (`token`, `value`) VALUES
('netflow_nfdump', '/usr/bin/nfdump'),
('netflow_max_resolution', '50'),
('event_fields', 'mini_severity,evento,id_agente,estado,timestamp'),
('status_monitor_fields', 'policy,agent,data_type,module_name,server_type,interval,status,graph,warn,data,timestamp'),
('status_monitor_fields', 'policy,agent,data_type,module_name,server_type,interval,status,last_status_change,graph,warn,data,timestamp'),
('list_ACL_IPs_for_API', '127.0.0.1'),
('enable_pass_policy', 0),
('pass_size', 4),