bulk operations agents in meta #8290

This commit is contained in:
Daniel Barbero Martin 2022-01-20 16:52:14 +01:00
parent 42d1ccbc13
commit 87bfd08bb9
2 changed files with 497 additions and 403 deletions

View File

@ -14,7 +14,7 @@
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
* Copyright (c) 2005-2022 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -29,7 +29,7 @@
// Begin.
check_login();
if (! check_acl($config['id_user'], 0, 'AW')) {
if ((bool) check_acl($config['id_user'], 0, 'AW') === false) {
db_pandora_audit(
'ACL Violation',
'Trying to access massive agent deletion section'
@ -38,10 +38,11 @@ if (! check_acl($config['id_user'], 0, 'AW')) {
return;
}
require_once 'include/functions_agents.php';
require_once 'include/functions_alerts.php';
require_once 'include/functions_modules.php';
require_once 'include/functions_users.php';
require_once $config['homedir'].'/include/functions_agents.php';
require_once $config['homedir'].'/include/functions_alerts.php';
require_once $config['homedir'].'/include/functions_modules.php';
require_once $config['homedir'].'/include/functions_users.php';
require_once $config['homedir'].'/include/functions_massive_operations.php';
function process_manage_delete($id_agents)
@ -99,19 +100,50 @@ $recursion = get_parameter('recursion');
$delete = (bool) get_parameter_post('delete');
if ($delete) {
if ($delete === true) {
$result = process_manage_delete($id_agents);
$info = '{"Agent":"'.implode(',', $id_agents).'"}';
if ($result) {
db_pandora_audit('Massive management', 'Delete agent ', false, false, $info);
if ($result === true) {
db_pandora_audit(
'Massive management',
'Delete agent ',
false,
false,
$info
);
} else {
db_pandora_audit('Massive management', 'Fail try to delete agent', false, false, $info);
db_pandora_audit(
'Massive management',
'Fail try to delete agent',
false,
false,
$info
);
}
}
$groups = users_get_groups();
if (is_metaconsole() === false && is_management_allowed() === false) {
if (\is_metaconsole() === false) {
$url_link = '<a target="_blank" href="'.ui_get_meta_url($url).'">';
$url_link .= __('metaconsole');
$url_link .= '</a>';
} else {
$url_link = __('any node');
}
\ui_print_warning_message(
__(
'This node is configured with centralized mode. All alert calendar information is read only. Go to %s to manage it.',
$url_link
)
);
}
// $groups = users_get_groups();
$table = new stdClass;
$table->id = 'delete_table';
$table->class = 'databox filters';
$table->width = '100%';
@ -177,31 +209,75 @@ $table->data[1][3] = html_print_select(
__('All'),
2,
true,
false,
true,
'',
'',
'',
'',
false,
'width:30%;'
);
$table->data[2][0] = __('Agents');
$table->data[2][0] .= '<span id="agent_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(
agents_get_group_agents(array_keys(users_get_groups($config['id_user'], 'AW', false)), false, 'none'),
if (is_metaconsole() === true) {
$servers = metaconsole_get_servers();
$server_fields = [];
foreach ($servers as $key => $server) {
$server_fields[$key] = $server['server_name'];
}
$table->data[2][2] = __('Node');
$table->data[2][3] = html_print_select(
$server_fields,
'node',
0,
'',
__('All'),
0,
true
);
}
$table->data[3][0] = __('Agents');
$table->data[3][0] .= '<span id="agent_loading" class="invisible">';
$table->data[3][0] .= html_print_image('images/spinner.png', true);
$table->data[3][0] .= '</span>';
$table->data[3][1] = html_print_select(
agents_get_agents_selected(
array_keys(users_get_groups($config['id_user'], 'AW', false))
),
'id_agents[]',
0,
false,
'',
'',
true,
true,
true,
'',
false,
'min-width: 500px; max-width: 500px; max-height: 100px',
false,
false,
false,
'',
false,
false,
false,
false,
true,
true,
true
);
echo '<form method="post" id="form_agents" action="index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=delete_agents">';
$url = 'index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=delete_agents';
if (is_metaconsole() === true) {
$ulr = 'index.php?sec=advanced&sec2=advanced/massive_operations&tab=massive_agents&pure=0&option=delete_agents';
}
echo '<form method="post" id="form_agents" action="'.$url.'">';
html_print_table($table);
if (is_management_allowed() === true) {
if (is_metaconsole() === true || is_management_allowed() === true) {
attachActionButton('delete', 'delete', $table->width);
}
@ -215,24 +291,18 @@ ui_require_jquery_file('pandora.controls');
<script type="text/javascript">
$(document).ready (function () {
var recursion;
$("#checkbox-recursion").click(function () {
recursion = this.checked ? 1 : 0;
$("#id_group").trigger("change");
});
var disabled;
$("#disabled").click(function () {
disabled = this.value;
$("#id_group").trigger("change");
disabled = this.value;
$("#id_group").trigger("change");
});
$("#id_group").pandoraSelectGroupAgent ({
status_agents: function () {
return $("#status_agents").val();
@ -246,14 +316,13 @@ ui_require_jquery_file('pandora.controls');
return disabled;
}
});
$("#status_agents").change(function() {
$("#id_group").trigger("change");
});
disabled = 2;
$("#id_group").trigger("change");
//$("#id_group").trigger("change");
});
</script>

View File

@ -1,371 +1,396 @@
/* globals jQuery, js_html_entity_decode */
(function($) {
var dummyFunc = function () {
return true;
};
var _pandoraSelectGroupAgent = function (disabled) {
var that = this;
this.defaults = {
agentSelect: "select#id_agent",
status_agents: -1,
recursion: 0,
filter_agents_json: "",
loading: "#agent_loading",
callbackBefore: dummyFunc,
callbackPre: dummyFunc,
callbackPost: dummyFunc,
callbackAfter: dummyFunc,
debug: false,
disabled: disabled || false,
privilege: "",
};
/* public methods */
this.construct = function (settings) {
return this.each (function() {
this.config = {};
this.config = $.extend (this.config, that.defaults, settings);
var config = this.config;
$(this).change (function () {
var $select = $(config.agentSelect).disable ();
$(config.loading).show ();
$("option[value!=0]", $select).remove ();
if (! config.callbackBefore (this))
return;
var dummyFunc = function() {
return true;
};
if (typeof config.recursion === "function") {
// Perform this for those cases where recursion parameter is obtained through a function that returns a variable that is set in the lexical environment where this constructor is called.
var recursion_value = config.recursion();
var _pandoraSelectGroupAgent = function(disabled) {
var that = this;
if (typeof recursion_value === "boolean") {
recursion_value = recursion_value ? 1 : 0;
}
} else {
var recursion_value = config.recursion;
}
this.defaults = {
agentSelect: "select#id_agent",
status_agents: -1,
recursion: 0,
filter_agents_json: "",
loading: "#agent_loading",
callbackBefore: dummyFunc,
callbackPre: dummyFunc,
callbackPost: dummyFunc,
callbackAfter: dummyFunc,
debug: false,
disabled: disabled || false,
privilege: "",
serialized: false,
serialized_separator: ""
};
var opts = {
"page" : "godmode/groups/group_list",
"get_group_agents" : 1,
"id_group" : this.value,
"recursion" : recursion_value,
"filter_agents_json" : config.filter_agents_json,
"disabled" : (typeof config.disabled === "function")
? (config.disabled())
: (config.disabled),
"status_agents" : config.status_agents,
"add_alert_bulk_op" : config.add_alert_bulk_op,
"privilege" : config.privilege,
// Add a key prefix to avoid auto sorting in js object conversion
"keys_prefix" : "_"
};
if(opts['disabled'] == 1 || opts['disabled'] == 0){
jQuery.post ("ajax.php",
opts,
function (data, status) {
jQuery.each (data, function (id, value) {
// Remove keys_prefix from the index
id = id.substring(1);
if (id !== 'keycount') {
config.callbackPre ();
option = $("<option></option>")
.attr ("value", id)
.html (value);
config.callbackPost (id, value, option);
$(config.agentSelect).append (option);
}
});
$(config.loading).hide ();
$select.enable ();
config.callbackAfter ();
},
"json"
);
}
else{
opts['disabled'] = 0;
jQuery.post ("ajax.php",
opts,
function (data, status) {
jQuery.each (data, function (id, value) {
// Remove keys_prefix from the index
id = id.substring(1);
if (id !== 'keycount') {
config.callbackPre ();
option = $("<option></option>")
.attr ("value", id)
.html (value);
config.callbackPost (id, value, option);
$(config.agentSelect).append (option);
}
});
$(config.loading).hide ();
$select.enable ();
config.callbackAfter ();
},
"json"
);
opts['disabled'] = 1;
jQuery.post ("ajax.php",
opts,
function (data, status) {
jQuery.each (data, function (id, value) {
// Remove keys_prefix from the index
id = id.substring(1);
if (id !== 'keycount') {
config.callbackPre ();
option = $("<option></option>")
.attr ("value", id)
.html (value);
config.callbackPost (id, value, option);
$(config.agentSelect).append (option);
}
});
$(config.loading).hide ();
$select.enable ();
config.callbackAfter ();
},
"json"
);
}
});
});
};
};
$.extend ({
pandoraSelectGroupAgent: new _pandoraSelectGroupAgent(),
pandoraSelectGroupAgentDisabled: new _pandoraSelectGroupAgent(true),
});
$.extend ({
pandoraSelectAgentModule: new function() {
this.defaults = {
moduleSelect: "select#id_agent_module",
loading: "#module_loading",
callbackBefore: dummyFunc,
callbackPre: dummyFunc,
callbackPost: dummyFunc,
callbackAfter: dummyFunc,
moduleFilter: {
disabled: 0,
deletePending: 0
},
debug: false
};
/* public methods */
this.construct = function (settings) {
return this.each (function() {
this.config = {};
this.config = $.extend (this.config, $.pandoraSelectAgentModule.defaults, settings);
var config = this.config;
$(this).change (function () {
var $select = $(config.moduleSelect).disable ();
$(config.loading).show ();
$("option[value!=0]", $select).remove ();
if (! config.callbackBefore (this))
return;
jQuery.post ('ajax.php',
{
"page": "operation/agentes/ver_agente",
"get_agent_modules_json": 1,
"id_agent": this.value,
"disabled": config.moduleFilter.disabled,
"delete_pending": config.moduleFilter.deletePending
},
function (data) {
jQuery.each (data, function (i, value) {
config.callbackPre ();
// Get the selected item from hidden field
selected = $('#hidden-'+config.moduleSelect.attr('id')+'_selected').val();
if (selected == i) {
option = $("<option></option>")
.prop ("selected", true)
.attr ("value", value['id_agente_modulo'])
.html (js_html_entity_decode (value['nombre']));
}
else {
option = $("<option></option>")
.attr ("value", value['id_agente_modulo'])
.html (js_html_entity_decode (value['nombre']));
}
config.callbackPost (i, value, option);
$(config.moduleSelect).append (option);
});
$(config.loading).hide ();
$select.enable ();
config.callbackAfter ();
},
"json"
);
});
});
};
}
});
$.extend ({
pandoraSelectAgentAlert: new function() {
this.defaults = {
alertSelect: "select#id_agent_module",
loading: "#alert_loading",
callbackBefore: dummyFunc,
callbackPre: dummyFunc,
callbackPost: dummyFunc,
callbackAfter: dummyFunc,
debug: false
};
/* public methods */
this.construct = function (settings) {
return this.each (function() {
this.config = {};
this.config = $.extend (this.config, $.pandoraSelectAgentAlert.defaults, settings);
var config = this.config;
$(this).change (function () {
var $select = $(config.alertSelect).disable ();
$(config.loading).show ();
$("option[value!=0]", $select).remove ();
if (! config.callbackBefore (this))
return;
jQuery.post ('ajax.php',
{"page": "include/ajax/alert_list.ajax",
"get_agent_alerts_simple": 1,
"id_agent": this.value
},
function (data) {
jQuery.each (data, function (i, value) {
config.callbackPre ();
option = $("<option></option>")
.attr ("value", value['id'])
.html (js_html_entity_decode (value['template']['name']))
.append (" ("+js_html_entity_decode (value['module_name'])+")");
config.callbackPost (i, value, option);
$(config.alertSelect).append (option);
});
$(config.loading).hide ();
$select.enable ();
config.callbackAfter ();
},
"json"
);
});
});
};
}
});
$.extend ({
pandoraSelectOS: new function() {
this.defaults = {
alertSelect: "select#id_os",
spanPreview: "#os_preview",
debug: false
};
/* public methods */
this.construct = function (settings) {
return this.each (function() {
this.config = {};
this.config = $.extend (this.config, $.pandoraSelectOS.defaults, settings);
var config = this.config;
$(this).change (function () {
var id_os = this.value;
var home_url;
if (typeof(settings) == 'undefined')
home_url = './';
else
home_url = settings.home_url;
$(config.spanPreview).fadeOut ('fast', function () {
$("img", config.spanPreview).remove ();
jQuery.post (home_url + 'ajax.php',
{"page": "godmode/setup/setup",
"get_os_icon": 1,
"id_os": id_os
},
function (data) {
$(config.spanPreview)
.append (data)
.fadeIn ('fast');
},
"html"
);
});
});
});
};
}
});
$.extend ({
pandoraSelectGroupIcon: new function() {
this.defaults = {
alertSelect: "select#id_group",
spanPreview: "#group_preview",
debug: false
};
/* public methods */
this.construct = function (settings) {
return this.each (function() {
this.config = {};
this.config = $.extend (this.config, $.pandoraSelectGroupIcon.defaults, settings);
var config = this.config;
$(this).change (function () {
var id_group = this.value;
$(config.spanPreview).fadeOut ('fast', function () {
$("img", config.spanPreview).remove ();
jQuery.post ('ajax.php',
{"page": "godmode/groups/group_list",
"get_group_json": 1,
"id_group": id_group
},
function (data) {
img = $("<img />").attr ("src", "images/groups_small/"+data["icon"]+".png");
$(config.spanPreview)
.append (img)
.fadeIn ('fast');
},
"json"
);
});
});
});
};
}
});
$.fn.extend({
pandoraSelectGroupAgent: $.pandoraSelectGroupAgent.construct,
pandoraSelectGroupAgentDisabled: $.pandoraSelectGroupAgentDisabled.construct,
pandoraSelectAgentModule: $.pandoraSelectAgentModule.construct,
pandoraSelectAgentAlert: $.pandoraSelectAgentAlert.construct,
pandoraSelectOS: $.pandoraSelectOS.construct,
pandoraSelectGroupIcon: $.pandoraSelectGroupIcon.construct
});
}) (jQuery);
/* public methods */
this.construct = function(settings) {
return this.each(function() {
this.config = {};
this.config = $.extend(this.config, that.defaults, settings);
var config = this.config;
$(this).change(function() {
var $select = $(config.agentSelect).disable();
$(config.loading).show();
$("option[value!=0]", $select).remove();
if (!config.callbackBefore(this)) return;
var recursion_value;
if (typeof config.recursion === "function") {
// Perform this for those cases where recursion parameter
// is obtained through a function that returns a variable
// that is set in the lexical environment
// where this constructor is called.
recursion_value = config.recursion();
if (typeof recursion_value === "boolean") {
recursion_value = recursion_value ? 1 : 0;
}
} else {
recursion_value = config.recursion;
}
var opts = {
page: "godmode/groups/group_list",
get_group_agents: 1,
id_group: this.value,
recursion: recursion_value,
filter_agents_json: config.filter_agents_json,
disabled:
typeof config.disabled === "function"
? config.disabled()
: config.disabled,
status_agents: config.status_agents,
add_alert_bulk_op: config.add_alert_bulk_op,
privilege: config.privilege,
// Add a key prefix to avoid auto sorting in js object conversion
keys_prefix: "_"
};
if (opts["disabled"] == 1 || opts["disabled"] == 0) {
jQuery.post(
"ajax.php",
opts,
function(data) {
jQuery.each(data, function(id, value) {
// Remove keys_prefix from the index.
id = id.substring(1);
if (id !== "keycount") {
config.callbackPre();
var option = $("<option></option>")
.attr("value", id)
.html(value);
config.callbackPost(id, value, option);
$(config.agentSelect).append(option);
}
});
$(config.loading).hide();
$select.enable();
config.callbackAfter();
},
"json"
);
} else {
opts["disabled"] = 0;
jQuery.post(
"ajax.php",
opts,
function(data) {
jQuery.each(data, function(id, value) {
// Remove keys_prefix from the index
id = id.substring(1);
if (id !== "keycount") {
config.callbackPre();
var option = $("<option></option>")
.attr("value", id)
.html(value);
config.callbackPost(id, value, option);
$(config.agentSelect).append(option);
}
});
$(config.loading).hide();
$select.enable();
config.callbackAfter();
},
"json"
);
opts["disabled"] = 1;
jQuery.post(
"ajax.php",
opts,
function(data) {
jQuery.each(data, function(id, value) {
// Remove keys_prefix from the index
id = id.substring(1);
if (id !== "keycount") {
config.callbackPre();
var option = $("<option></option>")
.attr("value", id)
.html(value);
config.callbackPost(id, value, option);
$(config.agentSelect).append(option);
}
});
$(config.loading).hide();
$select.enable();
config.callbackAfter();
},
"json"
);
}
});
});
};
};
$.extend({
pandoraSelectGroupAgent: new _pandoraSelectGroupAgent(),
pandoraSelectGroupAgentDisabled: new _pandoraSelectGroupAgent(true)
});
$.extend({
pandoraSelectAgentModule: new (function() {
this.defaults = {
moduleSelect: "select#id_agent_module",
loading: "#module_loading",
callbackBefore: dummyFunc,
callbackPre: dummyFunc,
callbackPost: dummyFunc,
callbackAfter: dummyFunc,
moduleFilter: {
disabled: 0,
deletePending: 0
},
debug: false
};
/* public methods */
this.construct = function(settings) {
return this.each(function() {
this.config = {};
this.config = $.extend(
this.config,
$.pandoraSelectAgentModule.defaults,
settings
);
var config = this.config;
$(this).change(function() {
var $select = $(config.moduleSelect).disable();
$(config.loading).show();
$("option[value!=0]", $select).remove();
if (!config.callbackBefore(this)) return;
jQuery.post(
"ajax.php",
{
page: "operation/agentes/ver_agente",
get_agent_modules_json: 1,
id_agent: this.value,
disabled: config.moduleFilter.disabled,
delete_pending: config.moduleFilter.deletePending
},
function(data) {
jQuery.each(data, function(i, value) {
config.callbackPre();
// Get the selected item from hidden field
var selected = $(
"#hidden-" + config.moduleSelect.attr("id") + "_selected"
).val();
var option;
if (selected == i) {
option = $("<option></option>")
.prop("selected", true)
.attr("value", value["id_agente_modulo"])
.html(js_html_entity_decode(value["nombre"]));
} else {
option = $("<option></option>")
.attr("value", value["id_agente_modulo"])
.html(js_html_entity_decode(value["nombre"]));
}
config.callbackPost(i, value, option);
$(config.moduleSelect).append(option);
});
$(config.loading).hide();
$select.enable();
config.callbackAfter();
},
"json"
);
});
});
};
})()
});
$.extend({
pandoraSelectAgentAlert: new (function() {
this.defaults = {
alertSelect: "select#id_agent_module",
loading: "#alert_loading",
callbackBefore: dummyFunc,
callbackPre: dummyFunc,
callbackPost: dummyFunc,
callbackAfter: dummyFunc,
debug: false
};
/* public methods */
this.construct = function(settings) {
return this.each(function() {
this.config = {};
this.config = $.extend(
this.config,
$.pandoraSelectAgentAlert.defaults,
settings
);
var config = this.config;
$(this).change(function() {
var $select = $(config.alertSelect).disable();
$(config.loading).show();
$("option[value!=0]", $select).remove();
if (!config.callbackBefore(this)) return;
jQuery.post(
"ajax.php",
{
page: "include/ajax/alert_list.ajax",
get_agent_alerts_simple: 1,
id_agent: this.value
},
function(data) {
jQuery.each(data, function(i, value) {
config.callbackPre();
var option = $("<option></option>")
.attr("value", value["id"])
.html(js_html_entity_decode(value["template"]["name"]))
.append(
" (" + js_html_entity_decode(value["module_name"]) + ")"
);
config.callbackPost(i, value, option);
$(config.alertSelect).append(option);
});
$(config.loading).hide();
$select.enable();
config.callbackAfter();
},
"json"
);
});
});
};
})()
});
$.extend({
pandoraSelectOS: new (function() {
this.defaults = {
alertSelect: "select#id_os",
spanPreview: "#os_preview",
debug: false
};
/* public methods */
this.construct = function(settings) {
return this.each(function() {
this.config = {};
this.config = $.extend(
this.config,
$.pandoraSelectOS.defaults,
settings
);
var config = this.config;
$(this).change(function() {
var id_os = this.value;
var home_url;
if (typeof settings == "undefined") home_url = "./";
else home_url = settings.home_url;
$(config.spanPreview).fadeOut("fast", function() {
$("img", config.spanPreview).remove();
jQuery.post(
home_url + "ajax.php",
{ page: "godmode/setup/setup", get_os_icon: 1, id_os: id_os },
function(data) {
$(config.spanPreview)
.append(data)
.fadeIn("fast");
},
"html"
);
});
});
});
};
})()
});
$.extend({
pandoraSelectGroupIcon: new (function() {
this.defaults = {
alertSelect: "select#id_group",
spanPreview: "#group_preview",
debug: false
};
/* public methods */
this.construct = function(settings) {
return this.each(function() {
this.config = {};
this.config = $.extend(
this.config,
$.pandoraSelectGroupIcon.defaults,
settings
);
var config = this.config;
$(this).change(function() {
var id_group = this.value;
$(config.spanPreview).fadeOut("fast", function() {
$("img", config.spanPreview).remove();
jQuery.post(
"ajax.php",
{
page: "godmode/groups/group_list",
get_group_json: 1,
id_group: id_group
},
function(data) {
var img = $("<img />").attr(
"src",
"images/groups_small/" + data["icon"] + ".png"
);
$(config.spanPreview)
.append(img)
.fadeIn("fast");
},
"json"
);
});
});
});
};
})()
});
$.fn.extend({
pandoraSelectGroupAgent: $.pandoraSelectGroupAgent.construct,
pandoraSelectGroupAgentDisabled:
$.pandoraSelectGroupAgentDisabled.construct,
pandoraSelectAgentModule: $.pandoraSelectAgentModule.construct,
pandoraSelectAgentAlert: $.pandoraSelectAgentAlert.construct,
pandoraSelectOS: $.pandoraSelectOS.construct,
pandoraSelectGroupIcon: $.pandoraSelectGroupIcon.construct
});
})(jQuery);