Merge branch '1-acciones-masivas' into 'develop'

Add selection modules in "Bulk operations: Add action alerts and delete action alerts".

#1

See merge request !15
This commit is contained in:
mlopez 2017-01-12 15:53:16 +01:00
parent 40676d44ff
commit 001c01a885
4 changed files with 244 additions and 78 deletions

View File

@ -52,7 +52,6 @@ $id_group = (int) get_parameter ('id_group');
$id_agents = get_parameter ('id_agents'); $id_agents = get_parameter ('id_agents');
$id_alert_templates = (array) get_parameter ('id_alert_templates'); $id_alert_templates = (array) get_parameter ('id_alert_templates');
$recursion = get_parameter ('recursion'); $recursion = get_parameter ('recursion');
$add = (bool) get_parameter_post ('add'); $add = (bool) get_parameter_post ('add');
if ($add) { if ($add) {
@ -64,45 +63,61 @@ if ($add) {
$fires_max = (int) get_parameter ('fires_max'); $fires_max = (int) get_parameter ('fires_max');
if (!empty($actions)) { if (!empty($actions)) {
$agent_alerts = agents_get_alerts($id_agents); $modules = get_parameter ('module');
$cont = 0; $modules_id = array();
$agent_alerts_id = array(); if (!empty($modules)) {
foreach ($agent_alerts['simple'] as $agent_alert) { foreach ($modules as $module) {
if (in_array($agent_alert['id_alert_template'], $id_alert_templates)) { foreach ($id_agents as $id_agent) {
$agent_alerts_id[$cont] = $agent_alert['id']; $module_id = modules_get_agentmodule_id($module, $id_agent);
$cont = $cont + 1; $modules_id[] = $module_id['id_agente_modulo'];
}
}
$options = array();
if ($fires_min > 0)
$options['fires_min'] = $fires_min;
if ($fires_max > 0)
$options['fires_max'] = $fires_max;
if (empty($agent_alerts_id)) {
ui_print_result_message (false, '', __('Could not be added').". ".__('No alerts selected'));
}
else {
$results = true;
foreach ($agent_alerts_id as $agent_alert_id) {
foreach ($actions as $action) {
$result = alerts_add_alert_agent_module_action($agent_alert_id, $action, $options);
if ($result === false)
$results = false;
} }
} }
db_pandora_audit("Massive management", "Add alert action " . json_encode($id_agents), false, false, 'Agents: ' . $agent_alerts = agents_get_alerts($id_agents);
json_encode($id_agents) . ' Alerts : ' . json_encode($agent_alerts) . $cont = 0;
' Fires Min: ' . $fires_min . ' Fires Max: ' . $fires_max . ' Actions: ' . implode(',',$actions)); $agent_alerts_id = array();
ui_print_result_message ($results, __('Successfully added'), __('Could not be added')); foreach ($agent_alerts['simple'] as $agent_alert) {
if ((in_array($agent_alert['id_alert_template'], $id_alert_templates)) && (in_array($agent_alert['id_agent_module'], $modules_id))) {
$agent_alerts_id[$cont] = $agent_alert['id'];
$cont += 1;
}
}
$options = array();
if ($fires_min > 0)
$options['fires_min'] = $fires_min;
if ($fires_max > 0)
$options['fires_max'] = $fires_max;
if (empty($agent_alerts_id)) {
ui_print_result_message (false, '', __('Could not be added').". ".__('No alerts selected'));
}
else {
$results = true;
foreach ($agent_alerts_id as $agent_alert_id) {
foreach ($actions as $action) {
$result = alerts_add_alert_agent_module_action($agent_alert_id, $action, $options);
if ($result === false)
$results = false;
}
}
db_pandora_audit("Massive management", "Add alert action " . json_encode($id_agents), false, false, 'Agents: ' .
json_encode($id_agents) . ' Alerts : ' . json_encode($agent_alerts) .
' Fires Min: ' . $fires_min . ' Fires Max: ' . $fires_max . ' Actions: ' . implode(',',$actions));
ui_print_result_message ($results, __('Successfully added'), __('Could not be added'));
}
}
else {
ui_print_result_message (false, '', __('Could not be added').". ".__('No modules selected'));
} }
} }
else { else {
ui_print_result_message (false, '', __('Could not be added').". ".__('No action selected')); ui_print_result_message (false, '', __('Could not be added').". ".__('No actions selected'));
} }
} }
@ -150,6 +165,15 @@ $table->data[2][0] .= '<span id="template_loading" class="invisible">';
$table->data[2][0] .= html_print_image('images/spinner.png', true); $table->data[2][0] .= html_print_image('images/spinner.png', true);
$table->data[2][0] .= '</span>'; $table->data[2][0] .= '</span>';
$table->data[2][1] = html_print_select (array(), 'id_alert_templates[]', '', '', '', '', true, true, true, '', $alert_templates == 0); $table->data[2][1] = html_print_select (array(), 'id_alert_templates[]', '', '', '', '', true, true, true, '', $alert_templates == 0);
$table->data[2][2] = __('When select agents');
$table->data[2][2] .= '<br>';
$table->data[2][2] .= html_print_select (
array('common' => __('Show common modules'),
'all' => __('Show all modules'),'unknown' => __('Show unknown and not init modules')),
'modules_selection_mode',
'common', false, '', '', true);
$table->data[2][3] = html_print_select (array(), 'module[]',
$modules_select, false, '', '', true, true, false);
$actions = alerts_get_alert_actions (); $actions = alerts_get_alert_actions ();
$table->data[3][0] = __('Action'); $table->data[3][0] = __('Action');
@ -233,6 +257,12 @@ $(document).ready (function () {
update_alerts(); update_alerts();
}); });
$("#id_alert_templates").change(alert_templates_changed_by_multiple_agents_with_alerts);
$("#modules_selection_mode").click(function () {
$("#id_alert_templates").trigger("change");
});
function update_alerts() { function update_alerts() {
var idAgents = Array(); var idAgents = Array();
jQuery.each ($("#id_agents option:selected"), function (i, val) { jQuery.each ($("#id_agents option:selected"), function (i, val) {

View File

@ -58,52 +58,65 @@ if ($delete) {
$actions = get_parameter ('action'); $actions = get_parameter ('action');
if (!empty($actions)) { if (!empty($actions)) {
$agent_alerts = agents_get_alerts($id_agents); $modules = (array) get_parameter ('module');
$modules_id = array();
$alerts_agent_modules = array(); if (!empty($modules)) {
foreach ($agent_alerts['simple'] as $agent_alert) { foreach ($modules as $module) {
if (in_array($agent_alert['id_alert_template'], $id_alert_templates)) { foreach ($id_agents as $id_agent) {
$alerts_agent_modules = array_merge($alerts_agent_modules, alerts_get_alerts_agent_module ($agent_alert['id_agent_module'], true, false, 'id')); $module_id = modules_get_agentmodule_id($module, $id_agent);
} $modules_id[] = $module_id['id_agente_modulo'];
}
if (empty($alerts_agent_modules)) {
ui_print_result_message (false, '',
__('Could not be deleted. No alerts selected'));
}
else {
$results = true;
$agent_module_actions = array();
foreach ($alerts_agent_modules as $alert_agent_module) {
$agent_module_actions = alerts_get_alert_agent_module_actions ($alert_agent_module['id'], array('id','id_alert_action'));
foreach ($agent_module_actions as $agent_module_action) {
foreach ($actions as $action) {
if ($agent_module_action['id_alert_action'] == $action) {
$result = alerts_delete_alert_agent_module_action ($agent_module_action['id']);
if ($result === false)
$results = false;
}
}
} }
} }
if ($results) { $agent_alerts = agents_get_alerts($id_agents);
db_pandora_audit("Massive management", "Delete alert action", false, false, $alerts_agent_modules = array();
'Agent: ' . json_encode($id_agents) . ' Alert templates: ' . json_encode($id_alert_templates) . foreach ($agent_alerts['simple'] as $agent_alert) {
' Actions: ' . implode(',',$actions)); if ((in_array($agent_alert['id_alert_template'], $id_alert_templates)) && (in_array($agent_alert['id_agent_module'], $modules_id))) {
} $alerts_agent_modules = array_merge($alerts_agent_modules, alerts_get_alerts_agent_module ($agent_alert['id_agent_module'], true, false, 'id'));
else { }
db_pandora_audit("Massive management", "Fail try to delete alert action", false, false,
'Agent: ' . json_encode($id_agents) . ' Alert templates: ' . json_encode($id_alert_templates) .
' Actions: ' . implode(',',$actions));
} }
ui_print_result_message ($results, if (empty($alerts_agent_modules)) {
__('Successfully deleted'), ui_print_result_message (false, '',
__('Could not be deleted')); __('Could not be deleted. No alerts selected'));
}
else {
$results = true;
$agent_module_actions = array();
foreach ($alerts_agent_modules as $alert_agent_module) {
$agent_module_actions = alerts_get_alert_agent_module_actions ($alert_agent_module['id'], array('id','id_alert_action'));
foreach ($agent_module_actions as $agent_module_action) {
foreach ($actions as $action) {
if ($agent_module_action['id_alert_action'] == $action) {
$result = alerts_delete_alert_agent_module_action ($agent_module_action['id']);
if ($result === false)
$results = false;
}
}
}
}
if ($results) {
db_pandora_audit("Massive management", "Delete alert action", false, false,
'Agent: ' . json_encode($id_agents) . ' Alert templates: ' . json_encode($id_alert_templates) .
' Actions: ' . implode(',',$actions));
}
else {
db_pandora_audit("Massive management", "Fail try to delete alert action", false, false,
'Agent: ' . json_encode($id_agents) . ' Alert templates: ' . json_encode($id_alert_templates) .
' Actions: ' . implode(',',$actions));
}
ui_print_result_message ($results,
__('Successfully deleted'),
__('Could not be deleted'));
}
}
else {
ui_print_result_message (false, '', __('Could not be added').". ".__('No modules selected'));
} }
} }
else { else {
@ -143,9 +156,6 @@ $table->data[0][3] = html_print_checkbox ("recursion", 1, $recursion,
true, false); true, false);
$table->data[1][0] = __('Agents with templates'); $table->data[1][0] = __('Agents with templates');
$table->data[1][0] .= '<span id="agent_loading" class="invisible">';
$table->data[1][0] .= html_print_image('images/spinner.png', true);
$table->data[1][0] .= '</span>';
$table->data[1][1] = html_print_select (array(),'id_agents[]', 0, false, '', '', true, true); $table->data[1][1] = html_print_select (array(),'id_agents[]', 0, false, '', '', true, true);
if (empty($id_agents)) { if (empty($id_agents)) {
@ -155,10 +165,17 @@ else {
$alert_templates = agents_get_alerts_simple ($id_agents); $alert_templates = agents_get_alerts_simple ($id_agents);
} }
$table->data[2][0] = __('Alert templates'); $table->data[2][0] = __('Alert templates');
$table->data[2][0] .= '<span id="template_loading" class="invisible">';
$table->data[2][0] .= html_print_image('images/spinner.png', true);
$table->data[2][0] .= '</span>';
$table->data[2][1] = html_print_select (index_array ($alert_templates, 'id_alert_template', 'template_name'), 'id_alert_templates[]', '', '', '', '', true, true, true, '', $alert_templates == 0); $table->data[2][1] = html_print_select (index_array ($alert_templates, 'id_alert_template', 'template_name'), 'id_alert_templates[]', '', '', '', '', true, true, true, '', $alert_templates == 0);
$table->data[2][2] = __('When select agents');
$table->data[2][2] .= '<br>';
$table->data[2][2] .= html_print_select (
array('common' => __('Show common modules'),
'all' => __('Show all modules'),'unknown' => __('Show unknown and not init modules')),
'modules_selection_mode',
'common', false, '', '', true);
$table->data[2][3] = html_print_select (array(), 'module[]',
$modules_select, false, '', '', true, true, false);
$actions = alerts_get_alert_actions (); $actions = alerts_get_alert_actions ();
$table->data[3][0] = __('Action'); $table->data[3][0] = __('Action');
@ -221,6 +238,12 @@ $(document).ready (function () {
update_alerts(); update_alerts();
}); });
$("#id_alert_templates").change(alert_templates_changed_by_multiple_agents_with_alerts);
$("#modules_selection_mode").click(function () {
$("#id_alert_templates").trigger("change");
});
function update_alerts() { function update_alerts() {
var idAgents = Array(); var idAgents = Array();
jQuery.each ($("#id_agents option:selected"), function (i, val) { jQuery.each ($("#id_agents option:selected"), function (i, val) {

View File

@ -373,6 +373,78 @@ function agent_changed_by_multiple_agents_with_alerts (event, id_agent, selected
); );
} }
/**
* Fill up select box with id "module" with modules with alerts of one or more templates
* before agent has been selected, but this not empty the select box.s
*
* @param event that has been triggered
* @param id_agent Agent ID that has been selected
* @param selected Which module(s) have to be selected
*/
function alert_templates_changed_by_multiple_agents_with_alerts (event, id_agent, selected) {
var idAgents = Array();
jQuery.each ($("#id_agents option:selected"), function (i, val) {
//val() because the var is same <option val="NNN"></option>
idAgents.push($(val).val());
});
var selection_mode = $('#modules_selection_mode').val();
if(selection_mode == undefined) {
selection_mode = 'common';
}
templates = Array();
jQuery.each ($("#id_alert_templates option:selected"), function (i, val) {
//val() because the var is same <option val="NNN"></option>
templates.push($(val).val());
});
console.log(templates);
$('#module').attr ('disabled', 1);
$('#module').empty ();
$('#module').append ($('<option></option>').html ("Loading...").attr ("value", 0));
jQuery.post ('ajax.php',
{"page": "operation/agentes/ver_agente",
"get_agent_modules_alerts_json_for_multiple_agents": 1,
"templates[]": templates,
"id_agent[]": idAgents,
"selection_mode": selection_mode
},
function (data) {
$('#module').empty ();
if (typeof($(document).data('text_for_module')) != 'undefined') {
$('#module').append ($('<option></option>').html ($(document).data('text_for_module')).attr("value", 0).prop('selected', true));
}
else {
if (typeof(data['any_text']) != 'undefined') {
$('#module').append ($('<option></option>').html (data['any_text']).attr ("value", 0).prop('selected', true));
}
else {
var anyText = $("#any_text").html(); //Trick for catch the translate text.
if (anyText == null) {
anyText = 'Any';
}
$('#module').append ($('<option></option>').html (anyText).attr ("value", 0).prop('selected', true));
}
}
jQuery.each (data, function (i, val) {
s = js_html_entity_decode(val);
$('#module').append ($('<option></option>').html (s).attr ("value", val));
$('#module').fadeIn ('normal');
});
if (selected != undefined)
$('#module').attr ('value', selected);
$('#module').removeAttr('disabled');
},
"json"
);
}
/** /**
* Fill up select box with id "agent" with agents after module has been selected, but this not empty the select box.s * Fill up select box with id "agent" with agents after module has been selected, but this not empty the select box.s
* *

View File

@ -37,6 +37,7 @@ if (is_ajax ()) {
$get_agents_group_json = (bool) get_parameter ("get_agents_group_json"); $get_agents_group_json = (bool) get_parameter ("get_agents_group_json");
$get_agent_modules_json_for_multiple_agents = (bool) get_parameter("get_agent_modules_json_for_multiple_agents"); $get_agent_modules_json_for_multiple_agents = (bool) get_parameter("get_agent_modules_json_for_multiple_agents");
$get_agent_modules_alerts_json_for_multiple_agents = (bool) get_parameter("get_agent_modules_alerts_json_for_multiple_agents"); $get_agent_modules_alerts_json_for_multiple_agents = (bool) get_parameter("get_agent_modules_alerts_json_for_multiple_agents");
$get_agent_modules_multiple_alerts_json_for_multiple_agents = (bool) get_parameter("get_agent_modules_multiple_alerts_json_for_multiple_agents");
$get_agents_json_for_multiple_modules = (bool) get_parameter("get_agents_json_for_multiple_modules"); $get_agents_json_for_multiple_modules = (bool) get_parameter("get_agents_json_for_multiple_modules");
$get_agent_modules_json_for_multiple_agents_id = (bool) get_parameter("get_agent_modules_json_for_multiple_agents_id"); $get_agent_modules_json_for_multiple_agents_id = (bool) get_parameter("get_agent_modules_json_for_multiple_agents_id");
$get_agentmodule_status_tooltip = (bool) get_parameter ("get_agentmodule_status_tooltip"); $get_agentmodule_status_tooltip = (bool) get_parameter ("get_agentmodule_status_tooltip");
@ -206,6 +207,46 @@ if (is_ajax ()) {
} }
if ($get_agent_modules_alerts_json_for_multiple_agents) { if ($get_agent_modules_alerts_json_for_multiple_agents) {
$idAgents = (array) get_parameter('id_agent');
$templates = (array) get_parameter('templates');
$selection_mode = get_parameter('selection_mode','common');
$sql = 'SELECT DISTINCT(nombre)
FROM tagente_modulo t1, talert_template_modules t2
WHERE t2.id_agent_module = t1.id_agente_modulo
AND delete_pending = 0
AND id_alert_template IN (' . implode(',', $templates) . ')
AND id_agente IN (' . implode(',', $idAgents) . ')';
if ($selection_mode == 'common') {
$sql .= ' AND (
SELECT count(nombre)
FROM tagente_modulo t3, talert_template_modules t4
WHERE t4.id_agent_module = t3.id_agente_modulo
AND delete_pending = 0 AND t1.nombre = t3.nombre
AND id_agente IN (' . implode(',', $idAgents) . ')
AND id_alert_template IN (' . implode(',', $templates) . ')) = (' . count($idAgents) . ')';
}
$sql .= ' ORDER BY t1.nombre';
$nameModules = db_get_all_rows_sql($sql);
if ($nameModules == false) {
$nameModules = array();
}
$result = array();
foreach($nameModules as $nameModule) {
$result[] = io_safe_output($nameModule['nombre']);
}
echo json_encode($result);
return;
}
if ($get_agent_modules_multiple_alerts_json_for_multiple_agents) {
$idAgents = get_parameter('id_agent'); $idAgents = get_parameter('id_agent');
$id_template = get_parameter('template'); $id_template = get_parameter('template');