From f72f61c49edf461bdd26242b292b5f7269cb6a63 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Thu, 1 Jun 2017 16:54:51 +0200 Subject: [PATCH 1/2] Added new functionality to create alerts on modules without alerts --- pandora_console/extensions/agents_alerts.php | 475 +++++++++++++------ pandora_console/include/functions_ui.php | 10 +- 2 files changed, 338 insertions(+), 147 deletions(-) diff --git a/pandora_console/extensions/agents_alerts.php b/pandora_console/extensions/agents_alerts.php index 72e5db9e40..91dc8a630c 100755 --- a/pandora_console/extensions/agents_alerts.php +++ b/pandora_console/extensions/agents_alerts.php @@ -60,9 +60,31 @@ function mainAgentsAlerts() { } $updated_time = $updated_info; + $create_alert = (int)get_parameter ("create_alert",0); + if($create_alert){ + $template2 = get_parameter("template"); + $module_action_threshold = get_parameter("module_action_threshold"); + + $id_alert = alerts_create_alert_agent_module ($create_alert, $template2); + + if ($id_alert !== false) { + $action_select = get_parameter("action_select",0); + + if ($action_select != 0) { + $values = array(); + $values['fires_min'] = 0; + $values['fires_max'] = 0; + $values['module_action_threshold'] = + (int)get_parameter ('module_action_threshold'); + + alerts_add_alert_agent_module_action ($id_alert, $action_select, $values); + } + } + + } $refr = get_parameter('refr', 30); // By default 30 seconds - + $show_modules = (bool) get_parameter ("show_modules",0); $group_id = get_parameter('group_id', 0); $offset = get_parameter('offset', 0); $hor_offset = get_parameter('hor_offset', 0); @@ -70,10 +92,12 @@ function mainAgentsAlerts() { $groups = users_get_groups (); - $filter_groups = '
'; $filter_groups .= ''.__('Group').''; - $filter_groups .= html_print_select_groups(false, "AR", true, 'group_id', $group_id, 'this.form.submit()', '', '', true, false, true, '', false , 'width: 100px; margin-right: 10px;; margin-top: 5px;'); - $filter_groups .= '
'; + $filter_groups .= html_print_select_groups(false, "AR", true, 'group_id', $group_id, false, '', '', true, false, true, '', false , 'width: 100px; margin-right: 10px;; margin-top: 5px;'); + + $check = ''.__('Show modules without alerts').''; + $check .= html_print_checkbox('slides_ids[]', $d['id'], $show_modules, true, false, '', true); + $comborefr = '
'; $comborefr .= ''.__('Refresh').''; @@ -87,12 +111,12 @@ function mainAgentsAlerts() { $comborefr .= "
"; if ($config["pure"] == 0) { - $fullscreen['text'] = '' + $fullscreen['text'] = '' . html_print_image ("images/full_screen.png", true, array ("title" => __('Full screen mode'))) . ""; } else { - $fullscreen['text'] = '' + $fullscreen['text'] = '' . html_print_image ("images/normal_screen.png", true, array ("title" => __('Back to normal mode'))) . ""; $config['refr'] = $refr; @@ -113,161 +137,288 @@ function mainAgentsAlerts() { echo ''; echo ""; echo ""; + echo ""; if ($config['pure'] == 1) echo ""; echo ""; echo ""; echo "
" . $filter_groups . "" . $check . "" . $comborefr . " " . __("Full screen") . "" . $fullscreen['text'] . "
"; - $filter = array ('offset' => (int) $offset, - 'limit' => (int) $config['block_size']); - $filter_count = array(); - - if ($group_id > 0) { - $filter['id_grupo'] = $group_id; - $filter_count['id_grupo'] = $group_id; - } - - // Get the id of all agents with alerts - $sql = 'SELECT DISTINCT(id_agente) - FROM tagente_modulo - WHERE id_agente_modulo IN - (SELECT id_agent_module - FROM talert_template_modules)'; - $agents_with_alerts_raw = db_get_all_rows_sql($sql); - - if ($agents_with_alerts_raw === false) { - $agents_with_alerts_raw = array(); - } - - $agents_with_alerts = array(); - foreach ($agents_with_alerts_raw as $awar) { - $agents_with_alerts[] = $awar['id_agente']; - } - - $filter['id_agente'] = $agents_with_alerts; - $filter_count['id_agente'] = $agents_with_alerts; - - $agents = agents_get_agents ($filter); - - $nagents = count(agents_get_agents ($filter_count)); - - if ($agents == false) { - ui_print_info_message ( array('no_close'=>true, 'message'=> __('There are no agents with alerts') ) ); - return; - } - - $all_alerts = agents_get_alerts_simple (); - - if($config["pure"] == 1) { - $block = count($all_alerts); - } - - $templates = array(); - $agent_alerts = array(); - foreach ($all_alerts as $alert) { - $templates[$alert['id_alert_template']] = ''; - $agent_alerts[$alert['agent_name']][$alert['id_alert_template']][] = $alert; - } - - // Prepare pagination - ui_pagination ($nagents); - - echo ''; - echo ""; - - if ($hor_offset > 0) { - $new_hor_offset = $hor_offset-$block; - echo ""; - } - - $templates_raw = array(); - if (!empty($templates)) { - $sql = sprintf('SELECT id, name - FROM talert_templates - WHERE id IN (%s)',implode(',',array_keys($templates))); + if($show_modules){ - $templates_raw = db_get_all_rows_sql($sql); - } - - if (empty($templates_raw)) - $templates_raw = array(); - - $alerts = array(); - $ntemplates = 0; - foreach ($templates_raw as $temp) { - if (isset($templates[$temp['id']]) && $templates[$temp['id']] == '') { - $ntemplates++; - if ($ntemplates <= $hor_offset || $ntemplates > ($hor_offset+$block)) { - continue; + + if($group_id > 0){ + $grupo = " AND tagente.id_grupo = $group_id"; + } else { + $grupo =''; + } + + $offset_modules = get_parameter ("offset",0); + $sql_count = "SELECT COUNT(tagente_modulo.nombre) FROM tagente_modulo + INNER JOIN tagente ON tagente.id_agente = tagente_modulo.id_agente + WHERE id_agente_modulo NOT IN (SELECT id_agent_module FROM talert_template_modules) + $grupo"; + $count_agent_module = db_get_all_rows_sql($sql_count); + + $sql = "SELECT tagente.alias, tagente_modulo.nombre, + tagente_modulo.id_agente_modulo FROM tagente_modulo + INNER JOIN tagente ON tagente.id_agente = tagente_modulo.id_agente + WHERE id_agente_modulo NOT IN (SELECT id_agent_module FROM talert_template_modules) + $grupo LIMIT 20 OFFSET $offset_modules"; + $agent_modules = db_get_all_rows_sql($sql); + + ui_pagination ($count_agent_module[0]['COUNT(tagente_modulo.nombre)'], + ui_get_url_refresh(),0,0,false,'offset',true,'', + '',false,'alerts_modules'); + + $table->width = '100%'; + $table->class = "databox data"; + $table->id = "table_agent_module"; + $table->data = array (); + + $table->head[0] = __('Agents'); + $table->head[1] = __('Modules'); + $table->head[2] = __('Actions'); + + $table->style[0]= 'width: 25%;'; + $table->style[1]= 'width: 33%;'; + $table->style[2]= 'width: 33%;'; + + foreach($agent_modules as $agent_module) { + $data[0] = io_safe_output($agent_module['alias']); + $data[1] = io_safe_output($agent_module['nombre']); + $uniqid = $agent_module['id_agente_modulo']; + $data[2] = "".html_print_image('images/add_mc.png', true).""; + array_push ($table->data, $data); + + $table2->width = '100%'; + $table2->id = "table_add_alert"; + $table2->class = 'databox filters'; + $table2->data = array (); + // $data[0] = + $table2->data[0][0] = __('Actions'); + + $groups_user = users_get_groups($config["id_user"]); + if (!empty($groups_user)) { + $groups = implode(',', array_keys($groups_user)); + $sql = "SELECT id, name FROM talert_actions WHERE id_group IN ($groups)"; + $actions = db_get_all_rows_sql($sql); + } + + $table2->data[0][1] = html_print_select( + index_array($actions, 'id', 'name'), 'action_select', '', '', + __('Default action'), '0', true, '', true, '', false, + 'width: 250px;'); + $table2->data[0][1] .= ''; + if (check_acl ($config['id_user'], 0, "LM")) { + + $table2->data[0][1] .= ''; + $table2->data[0][1] .= html_print_image ('images/add.png', true); + $table2->data[0][1] .= ''.__('Create Action').''; + $table2->data[0][1] .= ''; + } + + $table2->data[1][0] = __('Template'); + $own_info = get_user_info ($config['id_user']); + if ($own_info['is_admin'] || check_acl ($config['id_user'], 0, "PM")) + $templates = alerts_get_alert_templates (false, array ('id', 'name')); + else { + $usr_groups = users_get_groups($config['id_user'], 'LW', true); + $filter_groups = ''; + $filter_groups = implode(',', array_keys($usr_groups)); + $templates = alerts_get_alert_templates (array ('id_group IN (' . $filter_groups . ')'), array ('id', 'name')); + } + + $table2->data[1][1] = html_print_select (index_array ($templates, 'id', 'name'), + 'template', '', '', __('Select'), 0, true, false, true, '', false, 'width: 250px;'); + $table2->data[1][1] .= ' '; + if (check_acl ($config['id_user'], 0, "LM")) { + $table2->data[1][1] .= ''; + $table2->data[1][1] .= html_print_image ('images/add.png', true); + $table2->data[1][1] .= ''.__('Create Template').''; + $table2->data[1][1] .= ''; + } + $table2->data[2][0] = __('Threshold'); + $table2->data[2][1] = html_print_input_text ('module_action_threshold', '0', '', 5, 7, true); + $table2->data[2][1] .= ' ' . __('seconds') . ui_print_help_icon ('action_threshold', true); + + $content2 = ''; + $content2 .= html_print_table($table2,true); + + $content2 .= '
'; + $content2 .= html_print_submit_button (__('Add alert'), 'add', false, 'class="sub wand"',true); + $content2 .= html_print_input_hidden ('create_alert', $uniqid,true); + $content2 .= '
'; + + $module_name = ui_print_truncate_text(io_safe_output($agent_module['nombre']), 40, false, true, false, '…', false); + echo ''; + } + + html_print_table($table); + + } else { + + $filter = array ('offset' => (int) $offset, + 'limit' => (int) $config['block_size']); + $filter_count = array(); + + if ($group_id > 0) { + $filter['id_grupo'] = $group_id; + $filter_count['id_grupo'] = $group_id; + } + + // Get the id of all agents with alerts + $sql = 'SELECT DISTINCT(id_agente) + FROM tagente_modulo + WHERE id_agente_modulo IN + (SELECT id_agent_module + FROM talert_template_modules)'; + $agents_with_alerts_raw = db_get_all_rows_sql($sql); + + if ($agents_with_alerts_raw === false) { + $agents_with_alerts_raw = array(); + } + + $agents_with_alerts = array(); + foreach ($agents_with_alerts_raw as $awar) { + $agents_with_alerts[] = $awar['id_agente']; + } + + $filter['id_agente'] = $agents_with_alerts; + $filter_count['id_agente'] = $agents_with_alerts; + + $agents = agents_get_agents ($filter); + + $nagents = count(agents_get_agents ($filter_count)); + + if ($agents == false) { + ui_print_info_message ( array('no_close'=>true, 'message'=> __('There are no agents with alerts') ) ); + return; + } + + $all_alerts = agents_get_alerts_simple (); + + if($config["pure"] == 1) { + $block = count($all_alerts); + } + + $templates = array(); + $agent_alerts = array(); + foreach ($all_alerts as $alert) { + $templates[$alert['id_alert_template']] = ''; + $agent_alerts[$alert['agent_name']][$alert['id_alert_template']][] = $alert; + } + + // Prepare pagination + ui_pagination ($nagents,false,0,0,false,'offset',true,'', + '',array('count' => '', 'offset' => 'offset_param'),'alerts_agents'); + + echo '
".__("Agents")." / ".__("Alert templates")." - ". - html_print_image("images/darrowleft.png",true, array('title' => __('Previous templates')))."
'; + echo ""; + + if ($hor_offset > 0) { + $new_hor_offset = $hor_offset-$block; + echo ""; + } + + $templates_raw = array(); + if (!empty($templates)) { + $sql = sprintf('SELECT id, name + FROM talert_templates + WHERE id IN (%s)',implode(',',array_keys($templates))); + + $templates_raw = db_get_all_rows_sql($sql); + } + + if (empty($templates_raw)) + $templates_raw = array(); + + $alerts = array(); + $ntemplates = 0; + foreach ($templates_raw as $temp) { + if (isset($templates[$temp['id']]) && $templates[$temp['id']] == '') { + $ntemplates++; + if ($ntemplates <= $hor_offset || $ntemplates > ($hor_offset+$block)) { + continue; + } + $templates[$temp['id']] = $temp['name']; } - $templates[$temp['id']] = $temp['name']; } - } - - foreach ($templates as $tid => $tname) { - if ($tname == '') { - continue; - } - echo '"; - } - - if (($hor_offset + $block) < $ntemplates) { - $new_hor_offset = $hor_offset+$block; - echo ""; - } - - foreach ($agents as $agent) { - $alias = db_get_row ('tagente', 'id_agente', $agent['id_agente']); - echo ''; - // Name of the agent - echo ''; - // Alerts of the agent - $anyfired = false; foreach ($templates as $tid => $tname) { if ($tname == '') { continue; } - if (isset($agent_alerts[$agent['nombre']][$tid])) { - foreach($agent_alerts[$agent['nombre']][$tid] as $alert) { - if($alert["times_fired"] > 0) { - $anyfired = true; - } - } - - $cellstyle = ''; - if($anyfired) { - $cellstyle = 'background:'.COL_ALERTFIRED.';'; - } - - echo ''; + echo '"; } - echo ''; + + if (($hor_offset + $block) < $ntemplates) { + $new_hor_offset = $hor_offset+$block; + echo ""; + } + + foreach ($agents as $agent) { + $alias = db_get_row ('tagente', 'id_agente', $agent['id_agente']); + echo ''; + // Name of the agent + echo ''; + + // Alerts of the agent + $anyfired = false; + foreach ($templates as $tid => $tname) { + if ($tname == '') { + continue; + } + if (isset($agent_alerts[$agent['nombre']][$tid])) { + foreach($agent_alerts[$agent['nombre']][$tid] as $alert) { + if($alert["times_fired"] > 0) { + $anyfired = true; + } + } + + $cellstyle = ''; + if($anyfired) { + $cellstyle = 'background:'.COL_ALERTFIRED.';'; + } + + echo ''; + } + echo ''; + } + + echo '
".__("Agents")." / ".__("Alert templates")." + ". + html_print_image("images/darrowleft.png",true, array('title' => __('Previous templates')))." '. io_safe_output($tname) . html_print_image('images/information_alerts.png', true, array('title' => io_safe_output($tname),'style' => 'margin-left:5px' )) ." - ".html_print_image("images/darrowright.png",true, array('title' => __('More templates')))."
'.$alias['alias'].' '; - - $uniqid = uniqid(); - echo "
"; - - echo count($agent_alerts[$agent['nombre']][$tid])." ".__('Alerts')." "; - - echo "".html_print_image('images/zoom.png', true).""; - - echo "
"; - - print_alerts_summary_modal_window($uniqid, $agent_alerts[$agent['nombre']][$tid]); - } - else { - echo '
'; - } - echo ''. io_safe_output($tname) . html_print_image('images/information_alerts.png', true, array('title' => io_safe_output($tname),'style' => 'margin-left:5px' )) ."
+ ".html_print_image("images/darrowright.png",true, array('title' => __('More templates')))."
'.$alias['alias'].' '; + + $uniqid = uniqid(); + echo "
"; + + echo count($agent_alerts[$agent['nombre']][$tid])." ".__('Alerts')." "; + + echo "".html_print_image('images/zoom.png', true).""; + + echo "
"; + + print_alerts_summary_modal_window($uniqid, $agent_alerts[$agent['nombre']][$tid]); + } + else { + echo '
'; + } + echo '
'; } - echo ''; + } // Print the modal window for the summary of each alerts group @@ -366,4 +517,40 @@ ui_require_jquery_file('pandora'); } }); } + + function show_add_alerts(id) { + $("#add_alerts_dialog_"+id).dialog({ + resizable: true, + draggable: true, + modal: true, + height: 235, + width: 600, + overlay: { + opacity: 0.5, + background: "black" + } + }); + } + + // checkbox-slides_ids + $(document).ready(function () { + $('#checkbox-slides_ids').click(function(){ + if ($('#checkbox-slides_ids').prop('checked')){ + var url = location.href.replace("&show_modules=true", ""); + location.href = url+"&show_modules=true"; + } else { + var url = location.href.replace("&show_modules=true", ""); + var re = /&offset=\d*/g; + location.href = url.replace(re, ""); + } + }); + + $('#group_id').change(function(){ + var regx = /&group_id=\d*/g; + var url = location.href.replace(regx, ""); + location.href = url+"&group_id="+$("#group_id").val(); + }); + + }); + diff --git a/pandora_console/include/functions_ui.php b/pandora_console/include/functions_ui.php index 3a1583b0da..84dcdee6b7 100755 --- a/pandora_console/include/functions_ui.php +++ b/pandora_console/include/functions_ui.php @@ -1585,6 +1585,7 @@ function ui_process_page_body ($string, $bitfield) { * @param bool $return Whether to return or print this * @param string $offset_name The name of parameter for the offset. * @param bool $print_total_items Show the text with the total items. By default true. + * @param string $set_id Set id of div. * * @return string The pagination div or nothing if no pagination needs to be done */ @@ -1592,7 +1593,7 @@ function ui_pagination ($count, $url = false, $offset = 0, $pagination = 0, $return = false, $offset_name = 'offset', $print_total_items = true, $other_class = '', $script = "", - $parameter_script = array('count' => '', 'offset' => 'offset_param')) { + $parameter_script = array('count' => '', 'offset' => 'offset_param'), $set_id = '') { global $config; @@ -1613,6 +1614,9 @@ function ui_pagination ($count, $url = false, $offset = 0, $url = ui_get_url_refresh (array ($offset_name => false)); } + if(!empty($set_id)){ + $set_id= " id = '$set_id'"; + } /* URL passed render links with some parameter &offset - Offset records passed to next page @@ -1624,7 +1628,7 @@ function ui_pagination ($count, $url = false, $offset = 0, if ($count <= $pagination) { if ($print_total_items) { - $output = "