From e70ecd461edac18a241a32b087ee951f6fbe54a8 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Fri, 12 Nov 2021 13:11:49 +0100 Subject: [PATCH] WIP new report alert fired actions --- .../reporting_builder.item_editor.php | 291 ++++++++++++++++-- .../godmode/reporting/reporting_builder.php | 60 ++++ pandora_console/include/functions_alerts.php | 97 ++++++ pandora_console/include/functions_html.php | 56 +++- .../include/functions_reporting.php | 59 ++++ .../include/functions_reporting_html.php | 10 + pandora_console/include/functions_reports.php | 6 + pandora_console/include/styles/pandora.css | 29 ++ 8 files changed, 573 insertions(+), 35 deletions(-) diff --git a/pandora_console/godmode/reporting/reporting_builder.item_editor.php b/pandora_console/godmode/reporting/reporting_builder.item_editor.php index b0caf02af0..2c2802d31c 100755 --- a/pandora_console/godmode/reporting/reporting_builder.item_editor.php +++ b/pandora_console/godmode/reporting/reporting_builder.item_editor.php @@ -748,6 +748,38 @@ switch ($action) { $idAgentModule = $module; break; + case 'alert_report_actions': + hd('1'); + $description = $item['description']; + $es = json_decode($item['external_source'], true); + $id_agents = $es['id_agents']; + + $selection_a_m = get_parameter('selection'); + $recursion = $item['recursion']; + + if ((count($es['module']) == 1) && ($es['module'][0] == 0)) { + $module = ''; + } else { + $module = $es['module']; + } + + $group = $item['id_group']; + $modulegroup = $item['id_module_group']; + $idAgentModule = $module; + + $alert_templates_selected = $es['templates']; + $alert_actions_selected = $es['actions']; + + $show_summary = $es['show_summary']; + + $group_by = $es['group_by']; + + $period = $item['period']; + + $lapse = $item['lapse']; + $lapse_calc = 1; + break; + case 'agents_inventory': $description = $item['description']; $es = json_decode($item['external_source'], true); @@ -1648,7 +1680,7 @@ $class = 'databox filters'; $group], ['id_agente', 'alias']); foreach ($all_agent_log as $key => $value) { $agents2[$value['id_agente']] = $value['alias']; } @@ -1680,7 +1712,18 @@ $class = 'databox filters'; true, '', false, - 'min-width: 180px' + 'min-width: 500px; max-height: 100px', + false, + false, + false, + '', + false, + false, + false, + false, + true, + true, + true ); ?> @@ -1721,7 +1764,7 @@ $class = 'databox filters'; $all_modules = ''; } else { $all_modules = db_get_all_rows_sql( - 'SELECT DISTINCT nombre FROM + 'SELECT DISTINCT nombre, id_agente_modulo FROM tagente_modulo WHERE id_agente IN ('.implode(',', array_values($id_agents)).')' ); } @@ -1730,12 +1773,12 @@ $class = 'databox filters'; $all_modules = []; } - $modules_select = []; - $all_modules_structured = []; + $modules_select = []; + $all_modules_structured = []; if (is_array($idAgentModule) || is_object($idAgentModule)) { - foreach ($idAgentModule as $id) { - foreach ($all_modules as $key => $a) { - if ($a['id_agente_modulo'] == (int) $id) { + foreach ($all_modules as $key => $a) { + foreach ($idAgentModule as $id) { + if ((int) $a['id_agente_modulo'] === (int) $id) { $modules_select[$a['id_agente_modulo']] = $a['id_agente_modulo']; } } @@ -1751,14 +1794,115 @@ $class = 'databox filters'; 'module[]', $modules_select, $script = '', - __('None'), + '', 0, false, true, true, '', false, - 'min-width: 180px' + 'min-width: 500px; max-height: 100px', + false, + false, + false, + '', + false, + false, + false, + false, + true, + true, + true + ); + ?> + + + + + + + + + + + + + + @@ -3108,7 +3252,60 @@ $class = 'databox filters'; ?> - + + + + + + + + + + + + + + + + 'Agent', + 'module' => 'Module', + 'action' => 'Action', + 'template' => 'Template', + ]; + html_print_select( + $valuesGroupBy, + 'group_by', + $group_by, + '', + __('None'), + '0', + false, + false, + false, + '', + false, + false, + false, + false, + false, + '' + ); + ?> + + + '; echo html_print_select( @@ -4252,12 +4449,12 @@ $(document).ready (function () { // Load selected modules by default $("#id_agents2").trigger('click'); - $('#combo_server').change (function (){ + $('#combo_server').change(function () { $("#id_agents").html(''); - $("#id_agents2").html(''); - $("#module").html(''); - $("#inventory_modules").html(''); - }) + $("#id_agents2").html(''); + $("#module").html(''); + $("#inventory_modules").html(''); + }); $("#text-url").keyup ( function () { @@ -4273,7 +4470,6 @@ $(document).ready (function () { $("#combo_group").change ( function () { - // Alert report group must show all matches when selecting All group // ignoring 'recursion' option. #6497. if ($("#combo_group").val() == 0) { @@ -4284,7 +4480,12 @@ $(document).ready (function () { } $("#id_agents2").html(''); + // Check agent all. + $("#checkbox-id_agents2-check-all").prop('checked', false); $("#module").html(''); + // Check module all. + $("#checkbox-module-check-all").prop('checked', false); + $("#inventory_modules").html(''); jQuery.post ("ajax.php", {"page" : "operation/agentes/ver_agente", @@ -4310,7 +4511,6 @@ $(document).ready (function () { ); } ); - $("#combo_group").change(); $("#checkbox-recursion").change ( function () { @@ -4324,6 +4524,11 @@ $(document).ready (function () { }, function (data, status) { $("#id_agents2").html(''); + // Check agent all. + $("#checkbox-id_agents2-check-all").prop('checked', false); + $("#module").html(''); + // Check module all. + $("#checkbox-module-check-all").prop('checked', false); jQuery.each (data, function (id, value) { // Remove keys_prefix from the index id = id.substring(1); @@ -4351,6 +4556,8 @@ $(document).ready (function () { }, function (data, status) { $("#module").html(''); + // Check module all. + $("#checkbox-module-check-all").prop('checked', false); jQuery.each (data, function (id, value) { option = $("") .attr ("value", value["id_agente_modulo"]) @@ -4374,6 +4581,8 @@ $(document).ready (function () { }, function (data, status) { $("#module").html(''); + // Check module all. + $("#checkbox-module-check-all").prop('checked', false); if(data){ jQuery.each (data, function (id, value) { option = $("") @@ -4399,6 +4608,8 @@ $(document).ready (function () { }, function (data, status) { $("#module").html(''); + // Check module all. + $("#checkbox-module-check-all").prop('checked', false); if(data){ jQuery.each (data, function (id, value) { option = $("") @@ -5538,6 +5749,8 @@ function chooseType() { $('#row_hide_notinit_agents').hide(); $('#row_priority_mode').hide(); $("#row_module_group").hide(); + $("#row_alert_templates").hide(); + $("#row_alert_actions").hide(); $("#row_servers").hide(); $("#row_sort").hide(); $("#row_date").hide(); @@ -5896,6 +6109,18 @@ function chooseType() { $("#row_historical_db_check").hide(); break; + case 'alert_report_actions': + $("#row_description").show(); + $("#row_group").show(); + $("#select_agent_modules").show(); + $("#agents_modules_row").show(); + $("#modules_row").show(); + $("#row_alert_templates").show(); + $("#row_alert_actions").show(); + $("#row_period").show(); + $("#row_lapse").show(); + break; + case 'event_report_group': $("#row_description").show(); $("#row_period").show(); diff --git a/pandora_console/godmode/reporting/reporting_builder.php b/pandora_console/godmode/reporting/reporting_builder.php index 5d1dbb6dff..d09b67c2f5 100755 --- a/pandora_console/godmode/reporting/reporting_builder.php +++ b/pandora_console/godmode/reporting/reporting_builder.php @@ -1692,6 +1692,36 @@ switch ($action) { $good_format = true; break; + case 'alert_report_actions': + hd('3'); + $agents_to_report = get_parameter('id_agents2'); + $modules_to_report = get_parameter('module', ''); + $alert_templates_to_report = get_parameter('alert_templates'); + $alert_actions_to_report = get_parameter('alert_actions'); + $show_summary = get_parameter('show_summary', 0); + $group_by = get_parameter('group_by', 0); + + $es['module'] = get_same_modules( + $agents_to_report, + $modules_to_report + ); + $es['id_agents'] = $agents_to_report; + $es['templates'] = $alert_templates_to_report; + $es['actions'] = $alert_actions_to_report; + $es['show_summary'] = $show_summary; + $es['group_by'] = $group_by; + + $values['external_source'] = json_encode($es); + + $values['period'] = get_parameter('period'); + $values['lapse_calc'] = get_parameter( + 'lapse_calc' + ); + $values['lapse'] = get_parameter('lapse'); + + $good_format = true; + break; + case 'inventory': $values['period'] = 0; $es['date'] = get_parameter('date'); @@ -2436,6 +2466,36 @@ switch ($action) { $good_format = true; break; + case 'alert_report_actions': + hd('2'); + $agents_to_report = get_parameter('id_agents2'); + $modules_to_report = get_parameter('module', ''); + $alert_templates_to_report = get_parameter('alert_templates'); + $alert_actions_to_report = get_parameter('alert_actions'); + $show_summary = get_parameter('show_summary', 0); + $group_by = get_parameter('group_by', 0); + + $es['module'] = get_same_modules( + $agents_to_report, + $modules_to_report + ); + $es['id_agents'] = $agents_to_report; + $es['templates'] = $alert_templates_to_report; + $es['actions'] = $alert_actions_to_report; + $es['show_summary'] = $show_summary; + $es['group_by'] = $group_by; + + $values['external_source'] = json_encode($es); + + $values['period'] = get_parameter('period'); + $values['lapse_calc'] = get_parameter( + 'lapse_calc' + ); + $values['lapse'] = get_parameter('lapse'); + + $good_format = true; + break; + case 'inventory_changes': $values['period'] = get_parameter('period'); $es['id_agents'] = get_parameter('id_agents'); diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index 6ca454695d..a7c05a9003 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -2949,3 +2949,100 @@ function alerts_get_agent_modules( return $agent_modules; } + + +function alerts_get_alert_fired($filters=[], $groupsBy=[]) +{ + $filter_group = ''; + if (isset($filters['group']) === true) { + $filter_group = sprintf('AND id_grupo = %d', $filters['group']); + } + + $filter_agents = ''; + if (isset($filters['agents']) === true) { + $filter_agents = sprintf( + 'AND id_agente IN ("%s")', + implode(',', $filters['agents']) + ); + } + + $filter_modules = ''; + if (isset($filters['modules']) === true) { + $filter_modules = sprintf( + 'AND id_agentmodule IN ("%s")', + implode(',', $filters['modules']) + ); + } + + $filter_actions = ''; + if (isset($filters['actions']) === true) { + $filter_actions = ''; + } + + $filter_templates = ''; + if (isset($filters['templates']) === true) { + $filter_templates = ''; + } + + $filter_period = ''; + if (isset($filters['period']) === true) { + $filter_period = ''; + } + + $group_by = ''; + if (isset($groupsBy['group_by']) === true) { + switch ($groupsBy['group_by']) { + case 'module': + $group_by = sprintf('GROUP BY id_agentmodule'); + break; + + case 'action': + $group_by = sprintf('GROUP BY 1'); + break; + + case 'template': + $group_by = sprintf('GROUP BY id_alert_am'); + break; + + case 'agent': + $group_by = sprintf('GROUP BY id_agente'); + break; + + default: + // Nothing. + break; + } + } + + // TODO: group by periods $groupsBy['lapse']. + $query = sprintf( + 'SELECT id_agente, id_grupo, id_agentmodule, count(*) as fired + FROM tevento + WHERE custom_data != "" + AND event_type="alert_fired" + %s + %s + %s + %s + %s + %s + AND ( + JSON_CONTAINS(custom_data, "Mail to Admin", "$.actions") = 1 + OR JSON_CONTAINS(custom_data, "Restart agent", "$.actions") = 1 + ) + %s', + $filter_group, + $filter_agents, + $filter_modules, + $filter_actions, + $filter_templates, + $filter_period, + $group_by + ); + + hd($query, '', true); + + $alert_fired = db_get_all_rows_sql($query); + + hd($alert_fired); +} diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index d7136bb7ce..920262be42 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -728,7 +728,9 @@ function html_print_select( $simple_multiple_options=false, $required=false, $truncate_size=false, - $select2_enable=true + $select2_enable=true, + $select2_multiple_enable=false, + $select2_multiple_enable_all=false ) { $output = "\n"; @@ -790,6 +792,12 @@ function html_print_select( $required = 'required'; } + if ($select2_multiple_enable === true + && $select2_multiple_enable_all === true + ) { + $output .= '
'; + } + $output .= ''; + + if ($select2_multiple_enable === true + && $select2_multiple_enable_all === true + ) { + $output .= '
'; + $output .= ''.__('All').''; + $output .= html_print_checkbox_switch( + $id.'-check-all', + 1, + false, + true, + $disabled, + 'checkMultipleAll('.$id.')' + ); + $output .= '
'; + $output .= '
'; + } + if ($modal && !enterprise_installed()) { $output .= "
@@ -900,7 +926,7 @@ function html_print_select( $select2 = 'select2_dark.min'; } - if ($multiple === false && $select2_enable === true) { + if (($multiple === false || $select2_multiple_enable === true) && $select2_enable === true) { if (is_ajax()) { $output .= ''; } diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index a1a5c3b68e..cd8a2ddcee 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -734,6 +734,13 @@ function reporting_make_reporting_data( ); break; + case 'alert_report_actions': + $report['contents'][] = reporting_alert_report_actions( + $report, + $content + ); + break; + case 'agents_inventory': $report['contents'][] = reporting_agents_inventory( $report, @@ -2658,6 +2665,58 @@ function reporting_inventory($report, $content, $type) } +function reporting_alert_report_actions($report, $content) +{ + global $config; + $return = []; + + $return['type'] = 'alert_report_actions'; + if (empty($content['name']) === true) { + $content['name'] = __('Alert actions'); + } + + $return['title'] = io_safe_output($content['name']); + $return['landscape'] = $content['landscape']; + $return['pagebreak'] = $content['pagebreak']; + + $return['subtitle'] = __('Actions'); + $return['description'] = io_safe_output($content['description']); + $return['date'] = reporting_get_date_text($report, $content); + + $return['data'] = []; + + $es = json_decode($content['external_source'], true); + + $period = $content['period']; + $id_group = $content['id_group']; + $modules = $es['modules']; + $agents = $es['id_agents']; + $templates = $es['templates']; + $actions = $es['actions']; + $show_summary = $es['show_summary']; + $group_by = $es['group_by']; + $lapse = $content['lapse']; + + $filters = [ + 'group' => $id_group, + 'agents' => $agents, + 'modules' => $modules, + 'templates' => $templates, + 'actions' => $actions, + 'period' => $period, + ]; + + $goupsBy = [ + 'group_by' => $group_by, + 'lapse' => $lapse, + ]; + + alerts_get_alert_fired($filters, $goupsBy); + + return reporting_check_structure_content($return); +} + + function reporting_agent_module($report, $content) { global $config; diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php index d93848c3f1..40463ddcee 100644 --- a/pandora_console/include/functions_reporting_html.php +++ b/pandora_console/include/functions_reporting_html.php @@ -377,6 +377,10 @@ function reporting_html_print_report($report, $mini=false, $report_info=1) reporting_html_agent_module($table, $item); break; + case 'alert_report_actions': + reporting_html_alert_report_actions($table, $item); + break; + case 'agents_inventory': reporting_html_agents_inventory($table, $item); break; @@ -2729,6 +2733,12 @@ function reporting_html_group_configuration($table, $item, $pdf=0) } +function reporting_html_alert_report_actions($table, $item, $pdf=0) +{ + hd($item); +} + + /** * This type of report element will generate the interface graphs * of all those devices that belong to the selected group. diff --git a/pandora_console/include/functions_reports.php b/pandora_console/include/functions_reports.php index 09e3340e70..4460a34ae5 100755 --- a/pandora_console/include/functions_reports.php +++ b/pandora_console/include/functions_reports.php @@ -834,6 +834,7 @@ function reports_get_report_types($template=false, $not_editor=false) 'optgroup' => __('Alerts'), 'name' => __('Agent alert report '), ]; + if (!$template) { $types['alert_report_group'] = [ 'optgroup' => __('Alerts'), @@ -841,6 +842,11 @@ function reports_get_report_types($template=false, $not_editor=false) ]; } + $types['alert_report_actions'] = [ + 'optgroup' => __('Alerts'), + 'name' => __('Actions alert report '), + ]; + $types['event_report_module'] = [ 'optgroup' => __('Events'), 'name' => __('Module event report'), diff --git a/pandora_console/include/styles/pandora.css b/pandora_console/include/styles/pandora.css index e541c8ff97..334a48d2ea 100644 --- a/pandora_console/include/styles/pandora.css +++ b/pandora_console/include/styles/pandora.css @@ -8348,3 +8348,32 @@ div.stat-win-spinner img { #license_error_msg_dialog { min-height: 350px !important; } + +.select2-container--default + .select2-selection--multiple + .select2-selection__rendered { + padding: 5px 10px 10px !important; +} + +.select2-container--default + .select2-selection--multiple + .select2-selection__choice { + background-color: #82b92e !important; + border: 1px solid #82b92e !important; + padding: 0.3em 0.6em !important; + color: #fff; + font-size: 1em; + display: flex; + flex-direction: row; + justify-content: flex-start; + align-items: center; + align-content: center; +} + +.select2-container--default + .select2-selection--multiple + .select2-selection__choice__remove { + color: #fff !important; + font-size: 1.2em; + margin-right: 5px !important; +}