From e05c292d7dc0ca434ff75a8b1570e265ca3252d2 Mon Sep 17 00:00:00 2001 From: alejandro-campos Date: Mon, 23 Mar 2020 12:47:58 +0100 Subject: [PATCH] show last status change of module --- pandora_console/extras/mr/37.sql | 8 ++ .../agentes/status_monitor_custom_fields.php | 10 +- pandora_console/include/ajax/module.php | 5 +- pandora_console/include/functions_agents.php | 22 ++++ .../include/functions_treeview.php | 7 ++ pandora_console/include/functions_ui.php | 26 ++--- .../agentes/estado_generalagente.php | 8 ++ .../operation/agentes/status_monitor.php | 109 ++++++++++-------- pandora_console/pandoradb_data.sql | 2 +- 9 files changed, 129 insertions(+), 68 deletions(-) create mode 100644 pandora_console/extras/mr/37.sql diff --git a/pandora_console/extras/mr/37.sql b/pandora_console/extras/mr/37.sql new file mode 100644 index 0000000000..efd118bdd1 --- /dev/null +++ b/pandora_console/extras/mr/37.sql @@ -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; \ No newline at end of file diff --git a/pandora_console/godmode/agentes/status_monitor_custom_fields.php b/pandora_console/godmode/agentes/status_monitor_custom_fields.php index 7e9ae48bdd..ed52959a09 100644 --- a/pandora_console/godmode/agentes/status_monitor_custom_fields.php +++ b/pandora_console/godmode/agentes/status_monitor_custom_fields.php @@ -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) { diff --git a/pandora_console/include/ajax/module.php b/pandora_console/include/ajax/module.php index 3da3b46728..e4e8775e8f 100755 --- a/pandora_console/include/ajax/module.php +++ b/pandora_console/include/ajax/module.php @@ -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; diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 7548843ce8..570e245bd7 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -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']; +} diff --git a/pandora_console/include/functions_treeview.php b/pandora_console/include/functions_treeview.php index a15b0f93d9..b886e9be49 100755 --- a/pandora_console/include/functions_treeview.php +++ b/pandora_console/include/functions_treeview.php @@ -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); diff --git a/pandora_console/include/functions_ui.php b/pandora_console/include/functions_ui.php index 12d94d3cef..bd721f57a6 100755 --- a/pandora_console/include/functions_ui.php +++ b/pandora_console/include/functions_ui.php @@ -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.' '.$extra_line_title; - $options['data-title'] = empty($extra_line_title) ? $title : $title.'
'.$extra_line_title; + $options['title'] = empty($extra_info) ? $title : $title.' '.$extra_info; + $options['data-title'] = empty($extra_info) ? $title : $title.'
'.$extra_info; $options['data-use_title_for_force_title'] = 1; if (isset($options['class'])) { $options['class'] .= ' forced_title'; diff --git a/pandora_console/operation/agentes/estado_generalagente.php b/pandora_console/operation/agentes/estado_generalagente.php index 4ecd610c43..b7ad228a44 100755 --- a/pandora_console/operation/agentes/estado_generalagente.php +++ b/pandora_console/operation/agentes/estado_generalagente.php @@ -368,6 +368,14 @@ if (enterprise_installed()) { $table_contact->data[] = $data; } +$data = []; +$data[0] = ''.__('Last status change').''; +$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) : ''.__('N/A').''; +$data[1] = $time_elapsed; + +$table_contact->data[] = $data; + /* * END: TABLE CONTACT BUILD */ diff --git a/pandora_console/operation/agentes/status_monitor.php b/pandora_console/operation/agentes/status_monitor.php index 154f8194d9..76fd7285ab 100644 --- a/pandora_console/operation/agentes/status_monitor.php +++ b/pandora_console/operation/agentes/status_monitor.php @@ -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] = ''.__('Show monitors...').''; $table->data[2][3] = html_print_select($monitor_options, 'module_option', $module_option, '', '', '', true, false, true, '', false, 'width: 150px;'); -$table->data[2][4] = 'data[2][4] = ''.__('Min. hours in current status').''; +$table->data[2][5] = html_print_input_text('min_hours_status', $min_hours_val, '', 12, 20, true); + +$table->data[3][0] = 'data[2][4] .= 'style="display:none"'; + $table->data[3][0] .= 'style="display:none"'; } -$table->data[2][4] .= '>'.__('Data type').''; +$table->data[3][0] .= '>'.__('Data type').''; -$table->data[2][5] .= '
'; +$table->data[3][1] .= '
'; switch ($moduletype) { @@ -609,33 +623,33 @@ switch ($moduletype) { } $a = db_get_all_rows_sql($sql); -$table->data[2][5] .= '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] .= ''; +$table->data[3][1] .= ''; foreach ($a as $valor) { - $table->data[2][5] .= ''; + $table->data[3][1] .= '>'.$valor['descripcion'].''; } - $table->data[2][5] .= ''; + $table->data[3][1] .= ''; - $table->data[2][5] .= '
'; + $table->data[3][1] .= '
'; $table_custom_fields = new stdClass(); @@ -683,9 +697,9 @@ foreach ($custom_fields as $custom_field) { $filters = '
'; 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 .= '
'; 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&sec2=operation/agentes/status_monitor $url_server_type = 'index.php?sec=view&sec2=operation/agentes/status_monitor&datatype='.$datatype.'&moduletype='.$moduletype.'&refr='.$refr.'&modulegroup='.$modulegroup.'&offset='.$offset.'&ag_group='.$ag_group.'&ag_freestring='.$ag_freestring.'&ag_modulename='.$ag_modulename.'&status='.$status.$ag_custom_fields_params.'&sort_field=moduletype&sort='; $url_interval = 'index.php?sec=view&sec2=operation/agentes/status_monitor&datatype='.$datatype.'&moduletype='.$moduletype.'&refr='.$refr.'&modulegroup='.$modulegroup.'&offset='.$offset.'&ag_group='.$ag_group.'&ag_freestring='.$ag_freestring.'&ag_modulename='.$ag_modulename.'&status='.$status.$ag_custom_fields_params.'&sort_field=interval&sort='; $url_status = 'index.php?sec=view&sec2=operation/agentes/status_monitor&datatype='.$datatype.'&moduletype='.$moduletype.'&refr='.$refr.'&modulegroup='.$modulegroup.'&offset='.$offset.'&ag_group='.$ag_group.'&ag_freestring='.$ag_freestring.'&ag_modulename='.$ag_modulename.'&status='.$status.$ag_custom_fields_params.'&sort_field=status&sort='; +$url_status = 'index.php?sec=view&sec2=operation/agentes/status_monitor&datatype='.$datatype.'&moduletype='.$moduletype.'&refr='.$refr.'&modulegroup='.$modulegroup.'&offset='.$offset.'&ag_group='.$ag_group.'&ag_freestring='.$ag_freestring.'&ag_modulename='.$ag_modulename.'&status='.$status.$ag_custom_fields_params.'&sort_field=last_status_change&sort='; $url_data = 'index.php?sec=view&sec2=operation/agentes/status_monitor&datatype='.$datatype.'&moduletype='.$moduletype.'&refr='.$refr.'&modulegroup='.$modulegroup.'&offset='.$offset.'&ag_group='.$ag_group.'&ag_freestring='.$ag_freestring.'&ag_modulename='.$ag_modulename.'&status='.$status.$ag_custom_fields_params.'&sort_field=data&sort='; $url_timestamp_up = 'index.php?sec=view&sec2=operation/agentes/status_monitor&datatype='.$datatype.'&moduletype='.$moduletype.'&refr='.$refr.'&offset='.$offset.'&ag_group='.$ag_group.'&ag_freestring='.$ag_freestring.'&ag_modulename='.$ag_modulename.'&status='.$status.$ag_custom_fields_params.'&sort_field=timestamp&sort=up'; $url_timestamp_down = 'index.php?sec=view&sec2=operation/agentes/status_monitor&datatype='.$datatype.'&moduletype='.$moduletype.'&refr='.$refr.'&modulegroup='.$modulegroup.'&offset='.$offset.'&ag_group='.$ag_group.'&ag_freestring='.$ag_freestring.'&ag_modulename='.$ag_modulename.'&status='.$status.$ag_custom_fields_params.'&sort_field=timestamp&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] .= ''.html_print_image('images/chart_curve.png', true, ['border' => '0', 'alt' => '']).''; + $data[8] .= ''.html_print_image('images/chart_curve.png', true, ['border' => '0', 'alt' => '']).''; } - $data[7] .= ''.html_print_image( + $data[8] .= ''.html_print_image( 'images/binary.png', true, [ @@ -1513,13 +1535,13 @@ if (!empty($result)) { ] ).''; - $data[7] .= ''.$row['module_name'].''; } } 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); diff --git a/pandora_console/pandoradb_data.sql b/pandora_console/pandoradb_data.sql index dc07c6fbe8..3fa6522a8d 100644 --- a/pandora_console/pandoradb_data.sql +++ b/pandora_console/pandoradb_data.sql @@ -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),