From acf9b4e6f32bb7a392dbb5de19156f6e13189635 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Tue, 18 Jan 2022 16:01:12 +0100 Subject: [PATCH 01/14] add new macros #3763 --- pandora_server/lib/PandoraFMS/Core.pm | 177 +++++++++++++------------ pandora_server/lib/PandoraFMS/Tools.pm | 26 ++++ 2 files changed, 121 insertions(+), 82 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 9d1736266f..cff0716ec3 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -1093,12 +1093,12 @@ sub pandora_execute_action ($$$$$$$$$;$) { my ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10); my ($field11, $field12, $field13, $field14, $field15, $field16, $field17, $field18, $field19, $field20); - # Check for empty alert fields and assign command field. - my $index = 1; - my @command_fields = split(/,|\[|\]/, $action->{'fields_values'}); - foreach my $field (@command_fields) { + # Check for empty alert fields and assign command field. + my $index = 1; + my @command_fields = split(/,|\[|\]/, $action->{'fields_values'}); + foreach my $field (@command_fields) { unless (defined($action->{'field'.$index}) && $action->{'field'.$index} ne "") { - $action->{'field'.$index} = defined($field) ? $field : "" ; + $action->{'field'.$index} = defined($field) ? $field : "" ; } } @@ -1259,6 +1259,13 @@ sub pandora_execute_action ($$$$$$$$$;$) { $group = get_db_single_row ($dbh, 'SELECT * FROM tgrupo WHERE id_grupo = ?', $agent->{'id_grupo'}); } + my $agent_status; + if(ref ($module) eq "HASH") { + $agent_status = get_db_single_row ($dbh, 'SELECT * FROM tagente_estado WHERE id_agente_modulo = ?', $module->{'id_agente_modulo'}); + } + + my $time_down = (defined ($agent_status)) ? (time() - $agent_status->{'last_status_change'}) : undef; + if (is_numeric($data)) { my $data_precision = $pa_config->{'graph_precision'}; $data = sprintf("%.$data_precision" . "f", $data); @@ -1266,94 +1273,100 @@ sub pandora_execute_action ($$$$$$$$$;$) { $data =~ s/\.+$//; } - # Thanks to people of Cordoba univ. for the patch for adding module and + # Thanks to people of Cordoba univ. for the patch for adding module and # id_agent macros to the alert. - + # TODO: Reuse queries. For example, tag data can be extracted with a single query. # Alert macros - my %macros = (_field1_ => $field1, - _field2_ => $field2, - _field3_ => $field3, - _field4_ => $field4, - _field5_ => $field5, - _field6_ => $field6, - _field7_ => $field7, - _field8_ => $field8, - _field9_ => $field9, - _field10_ => $field10, - _field11_ => $field11, - _field12_ => $field12, - _field13_ => $field13, - _field14_ => $field14, - _field15_ => $field15, - _field16_ => $field16, - _field17_ => $field17, - _field18_ => $field18, - _field19_ => $field19, - _field20_ => $field20, - _agentname_ => (defined ($agent)) ? $agent->{'nombre'} : '', - _agentalias_ => (defined ($agent)) ? $agent->{'alias'} : '', - _agent_ => (defined ($agent)) ? ($agent->{'alias'} ? $agent->{'alias'} : $agent->{'nombre'}) : '', - _agentcustomid_ => (defined ($agent)) ? $agent->{'custom_id'} : '', - '_agentcustomfield_\d+_' => undef, - _agentdescription_ => (defined ($agent)) ? $agent->{'comentarios'} : '', - _agentgroup_ => (defined ($group)) ? $group->{'nombre'} : '', - _agentstatus_ => undef, - _agentos_ => (defined ($agent)) ? get_os_name($dbh, $agent->{'id_os'}) : '', - _address_ => (defined ($agent)) ? $agent->{'direccion'} : '', - _timestamp_ => (defined($timestamp)) ? $timestamp : strftime ("%Y-%m-%d %H:%M:%S", localtime()), - _timezone_ => strftime ("%Z", localtime()), - _data_ => $data, - _prevdata_ => undef, - _homeurl_ => $pa_config->{'public_url'}, - _alert_name_ => $alert->{'name'}, - _alert_description_ => $alert->{'description'}, - _alert_threshold_ => $alert->{'time_threshold'}, - _alert_times_fired_ => $alert->{'times_fired'}, - _alert_priority_ => $alert->{'priority'}, - _alert_text_severity_ => get_priority_name($alert->{'priority'}), - _alert_critical_instructions_ => $alert->{'critical_instructions'}, - _alert_warning_instructions_ => $alert->{'warning_instructions'}, - _alert_unknown_instructions_ => $alert->{'unknown_instructions'}, - _groupcontact_ => (defined ($group)) ? $group->{'contact'} : '', - _groupcustomid_ => (defined ($group)) ? $group->{'custom_id'} : '', - _groupother_ => (defined ($group)) ? $group->{'other'} : '', - _module_ => (defined ($module)) ? $module->{'nombre'} : '', - _modulecustomid_ => (defined ($module)) ? $module->{'custom_id'} : '', - _modulegroup_ => undef, - _moduledescription_ => (defined ($module)) ? $module->{'descripcion'} : '', - _modulestatus_ => undef, - _moduletags_ => undef, - '_moduledata_\S+_' => undef, - _id_agent_ => (defined ($module)) ? $module->{'id_agente'} : '', - _id_module_ => (defined ($module)) ? $module->{'id_agente_modulo'} : '', - _id_group_ => (defined ($group)) ? $group->{'id_grupo'} : '', - _id_alert_ => (defined ($alert->{'id_template_module'})) ? $alert->{'id_template_module'} : '', - _interval_ => (defined ($module) && $module->{'module_interval'} != 0) ? $module->{'module_interval'} : (defined ($agent)) ? $agent->{'intervalo'} : '', - _server_ip_ => (defined ($agent)) ? get_db_value($dbh, "SELECT ip_address FROM tserver WHERE name = ?", $agent->{'server_name'}) : '', - _server_name_ => (defined ($agent)) ? $agent->{'server_name'} : '', - _target_ip_ => (defined ($module)) ? $module->{'ip_target'} : '', - _target_port_ => (defined ($module)) ? $module->{'tcp_port'} : '', - _policy_ => (defined ($module)) ? get_db_value ($dbh, "SELECT name FROM tpolicies WHERE id = ?", get_db_value ($dbh, "SELECT id_policy FROM tpolicy_modules WHERE id = ?", $module->{'id_policy_module'})) : '', - _plugin_parameters_ => (defined ($module)) ? $module->{'plugin_parameter'} : '', - _email_tag_ => undef, - _phone_tag_ => undef, - _name_tag_ => undef, - _all_address_ => undef, - '_addressn_\d+_' => undef, - _secondarygroups_ => undef, - ); - + my %macros = ( + _field1_ => $field1, + _field2_ => $field2, + _field3_ => $field3, + _field4_ => $field4, + _field5_ => $field5, + _field6_ => $field6, + _field7_ => $field7, + _field8_ => $field8, + _field9_ => $field9, + _field10_ => $field10, + _field11_ => $field11, + _field12_ => $field12, + _field13_ => $field13, + _field14_ => $field14, + _field15_ => $field15, + _field16_ => $field16, + _field17_ => $field17, + _field18_ => $field18, + _field19_ => $field19, + _field20_ => $field20, + _agentname_ => (defined ($agent)) ? $agent->{'nombre'} : '', + _agentalias_ => (defined ($agent)) ? $agent->{'alias'} : '', + _agent_ => (defined ($agent)) ? ($agent->{'alias'} ? $agent->{'alias'} : $agent->{'nombre'}) : '', + _agentcustomid_ => (defined ($agent)) ? $agent->{'custom_id'} : '', + '_agentcustomfield_\d+_' => undef, + _agentdescription_ => (defined ($agent)) ? $agent->{'comentarios'} : '', + _agentgroup_ => (defined ($group)) ? $group->{'nombre'} : '', + _agentstatus_ => undef, + _agentos_ => (defined ($agent)) ? get_os_name($dbh, $agent->{'id_os'}) : '', + _address_ => (defined ($agent)) ? $agent->{'direccion'} : '', + _timestamp_ => (defined($timestamp)) ? $timestamp : strftime ("%Y-%m-%d %H:%M:%S", localtime()), + _timezone_ => strftime ("%Z", localtime()), + _data_ => $data, + _prevdata_ => undef, + _homeurl_ => $pa_config->{'public_url'}, + _alert_name_ => $alert->{'name'}, + _alert_description_ => $alert->{'description'}, + _alert_threshold_ => $alert->{'time_threshold'}, + _alert_times_fired_ => $alert->{'times_fired'}, + _alert_priority_ => $alert->{'priority'}, + _alert_text_severity_ => get_priority_name($alert->{'priority'}), + _alert_critical_instructions_ => $alert->{'critical_instructions'}, + _alert_warning_instructions_ => $alert->{'warning_instructions'}, + _alert_unknown_instructions_ => $alert->{'unknown_instructions'}, + _groupcontact_ => (defined ($group)) ? $group->{'contact'} : '', + _groupcustomid_ => (defined ($group)) ? $group->{'custom_id'} : '', + _groupother_ => (defined ($group)) ? $group->{'other'} : '', + _module_ => (defined ($module)) ? $module->{'nombre'} : '', + _modulecustomid_ => (defined ($module)) ? $module->{'custom_id'} : '', + _modulegroup_ => undef, + _moduledescription_ => (defined ($module)) ? $module->{'descripcion'} : '', + _modulestatus_ => undef, + _moduletags_ => undef, + '_moduledata_\S+_' => undef, + _id_agent_ => (defined ($module)) ? $module->{'id_agente'} : '', + _id_module_ => (defined ($module)) ? $module->{'id_agente_modulo'} : '', + _id_group_ => (defined ($group)) ? $group->{'id_grupo'} : '', + _id_alert_ => (defined ($alert->{'id_template_module'})) ? $alert->{'id_template_module'} : '', + _interval_ => (defined ($module) && $module->{'module_interval'} != 0) ? $module->{'module_interval'} : (defined ($agent)) ? $agent->{'intervalo'} : '', + _server_ip_ => (defined ($agent)) ? get_db_value($dbh, "SELECT ip_address FROM tserver WHERE name = ?", $agent->{'server_name'}) : '', + _server_name_ => (defined ($agent)) ? $agent->{'server_name'} : '', + _target_ip_ => (defined ($module)) ? $module->{'ip_target'} : '', + _target_port_ => (defined ($module)) ? $module->{'tcp_port'} : '', + _policy_ => (defined ($module)) ? get_db_value ($dbh, "SELECT name FROM tpolicies WHERE id = ?", get_db_value ($dbh, "SELECT id_policy FROM tpolicy_modules WHERE id = ?", $module->{'id_policy_module'})) : '', + _plugin_parameters_ => (defined ($module)) ? $module->{'plugin_parameter'} : '', + _email_tag_ => undef, + _phone_tag_ => undef, + _name_tag_ => undef, + _all_address_ => undef, + '_addressn_\d+_' => undef, + _secondarygroups_ => undef, + _time_down_seconds_ => (defined ($time_down)) ? int($time_down) : '', + _time_down_human_ => seconds_totime($time_down), + _warning_threshold_min_ => (defined ($module->{'min_warning'})) ? $module->{'min_warning'} : '', + _warning_threshold_max_ => (defined ($module->{'max_warning'})) ? $module->{'max_warning'} : '', + _critical_threshold_min_ => (defined ($module->{'min_critical'})) ? $module->{'min_critical'} : '', + _critical_threshold_max_ => (defined ($module->{'max_critical'})) ? $module->{'max_critical'} : '', + ); + if ((defined ($extra_macros)) && (ref($extra_macros) eq "HASH")) { while ((my $macro, my $value) = each (%{$extra_macros})) { $macros{$macro} = $value; } } - + if (defined ($module)) { load_module_macros ($module->{'module_macros'}, \%macros); } - #logger($pa_config, "Clean name ".$clean_name, 10); # User defined alert diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 2d17d53b80..bcffd78034 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -147,6 +147,7 @@ our @EXPORT = qw( ping resolve_hostname ticks_totime + seconds_totime safe_input safe_output month_have_days @@ -1409,6 +1410,31 @@ sub ticks_totime ($){ return "$days days, $hours hours, $minutes minutes, $seconds seconds"; } + + +################################################################################ +## SUB human_time_readable +# Transform a seconds count in a human readable date +################################################################################ +sub seconds_totime ($){ + my $SECONDS_PER_MINUTE = 60; + my $SECONDS_PER_HOUR = $SECONDS_PER_MINUTE * 60; + my $SECONDS_PER_DAY = $SECONDS_PER_HOUR * 24; + + my $orig_seconds = shift; + + if (!defined($orig_seconds)){ + return ""; + } + + my $seconds = int($orig_seconds) % 60; + my $minutes = int($orig_seconds / $SECONDS_PER_MINUTE) % 60; + my $hours = int($orig_seconds / $SECONDS_PER_HOUR) % 24; + my $days = int($orig_seconds / $SECONDS_PER_DAY); + + return "$days days, $hours hours, $minutes minutes, $seconds seconds"; +} + ################################################################################ =head2 C<< pandora_ping (I<$pa_config>, I<$host>) >> From 87bfd08bb97c3a8652a42a93d50cf8b16e052109 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Thu, 20 Jan 2022 16:52:14 +0100 Subject: [PATCH 02/14] bulk operations agents in meta #8290 --- .../godmode/massive/massive_delete_agents.php | 141 +++- .../javascript/jquery.pandora.controls.js | 759 +++++++++--------- 2 files changed, 497 insertions(+), 403 deletions(-) diff --git a/pandora_console/godmode/massive/massive_delete_agents.php b/pandora_console/godmode/massive/massive_delete_agents.php index 46d839aea9..8244da77e2 100755 --- a/pandora_console/godmode/massive/massive_delete_agents.php +++ b/pandora_console/godmode/massive/massive_delete_agents.php @@ -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 = ''; + $url_link .= __('metaconsole'); + $url_link .= ''; + } 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] .= ''; -$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] .= ''; +$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 '
'; +$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 ''; 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'); diff --git a/pandora_console/include/javascript/jquery.pandora.controls.js b/pandora_console/include/javascript/jquery.pandora.controls.js index 0158879c6b..135e006798 100644 --- a/pandora_console/include/javascript/jquery.pandora.controls.js +++ b/pandora_console/include/javascript/jquery.pandora.controls.js @@ -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 = $("") - .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 = $("") - .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 = $("") - .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 = $("") - .prop ("selected", true) - .attr ("value", value['id_agente_modulo']) - .html (js_html_entity_decode (value['nombre'])); - } - else { - 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 = $("") - .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 = $("").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 = $("") + .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 = $("") + .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 = $("") + .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 = $("") + .prop("selected", true) + .attr("value", value["id_agente_modulo"]) + .html(js_html_entity_decode(value["nombre"])); + } else { + 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 = $("") + .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 = $("").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); From 7c2000c34d564f0db40ad2b23a4cd046400da23d Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Fri, 21 Jan 2022 11:49:30 +0100 Subject: [PATCH 03/14] bulk operations agents in meta #8290 --- pandora_console/godmode/groups/group_list.php | 7 ++ .../godmode/massive/massive_delete_agents.php | 94 ++++++++++----- .../godmode/massive/massive_edit_agents.php | 22 ++-- pandora_console/include/functions_agents.php | 26 ++++- .../javascript/jquery.pandora.controls.js | 107 ++++++------------ 5 files changed, 138 insertions(+), 118 deletions(-) diff --git a/pandora_console/godmode/groups/group_list.php b/pandora_console/godmode/groups/group_list.php index 862b923f59..e4d384c129 100644 --- a/pandora_console/godmode/groups/group_list.php +++ b/pandora_console/godmode/groups/group_list.php @@ -113,6 +113,9 @@ if (is_ajax() === true) { '|' ); $force_serialized = (bool) get_parameter('force_serialized', false); + $nodes = (array) get_parameter('nodes', []); + + hd($nodes, true); if ((bool) check_acl($config['id_user'], $id_group, 'AR') === false) { db_pandora_audit( @@ -154,6 +157,10 @@ if (is_ajax() === true) { $filter[$_sql_post] = '1'; } + if (is_metaconsole() === true && empty($nodes) === false) { + $filter['id_server'] = $nodes; + } + $id_groups_get_agents = $id_group; if ($id_group == 0 && $privilege != '') { $groups = users_get_groups($config['id_user'], $privilege, false); diff --git a/pandora_console/godmode/massive/massive_delete_agents.php b/pandora_console/godmode/massive/massive_delete_agents.php index 8244da77e2..c164fb1ced 100755 --- a/pandora_console/godmode/massive/massive_delete_agents.php +++ b/pandora_console/godmode/massive/massive_delete_agents.php @@ -45,24 +45,27 @@ require_once $config['homedir'].'/include/functions_users.php'; require_once $config['homedir'].'/include/functions_massive_operations.php'; +/** + * Bulk operations Delete. + * + * @param array $id_agents Agents to delete. + * + * @return boolean + */ function process_manage_delete($id_agents) { - if (empty($id_agents)) { + if (empty($id_agents) === true) { ui_print_error_message(__('No agents selected')); return false; } $id_agents = (array) $id_agents; - $copy_modules = (bool) get_parameter('copy_modules'); - $copy_alerts = (bool) get_parameter('copy_alerts'); - - $error = false; $count_deleted = 0; $agent_id_restore = 0; foreach ($id_agents as $id_agent) { $success = agents_delete_agent($id_agent); - if (! $success) { + if (!$success) { $agent_id_restore = $id_agent; break; } @@ -70,7 +73,7 @@ function process_manage_delete($id_agents) $count_deleted++; } - if (! $success) { + if (!$success) { ui_print_error_message( sprintf( __('There was an error deleting the agent, the operation has been cancelled Could not delete agent %s'), @@ -97,7 +100,6 @@ function process_manage_delete($id_agents) $id_group = (int) get_parameter('id_group'); $id_agents = get_parameter('id_agents'); $recursion = get_parameter('recursion'); - $delete = (bool) get_parameter_post('delete'); if ($delete === true) { @@ -135,14 +137,12 @@ if (is_metaconsole() === false && is_management_allowed() === false) { \ui_print_warning_message( __( - 'This node is configured with centralized mode. All alert calendar information is read only. Go to %s to manage it.', + 'This node is configured with centralized mode. Bulk operations 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'; @@ -226,24 +226,44 @@ if (is_metaconsole() === true) { $table->data[2][2] = __('Node'); $table->data[2][3] = html_print_select( $server_fields, - 'node', + 'nodes[]', 0, + false, '', - __('All'), - 0, + '', + true, + true, + true, + '', + false, + 'min-width: 500px; max-width: 500px; max-height: 100px', + false, + false, + false, + '', + false, + false, + false, + false, + true, + true, true ); } - - $table->data[3][0] = __('Agents'); $table->data[3][0] .= ''; $table->data[3][1] = html_print_select( - agents_get_agents_selected( - array_keys(users_get_groups($config['id_user'], 'AW', false)) + agents_get_group_agents( + array_keys(users_get_groups($config['id_user'], 'AW', false)), + false, + 'none', + false, + false, + is_metaconsole(), + '|' ), 'id_agents[]', 0, @@ -291,6 +311,10 @@ ui_require_jquery_file('pandora.controls'); diff --git a/pandora_console/godmode/massive/massive_edit_agents.php b/pandora_console/godmode/massive/massive_edit_agents.php index 7b80959f70..ee2952fa24 100755 --- a/pandora_console/godmode/massive/massive_edit_agents.php +++ b/pandora_console/godmode/massive/massive_edit_agents.php @@ -56,7 +56,7 @@ if (is_ajax()) { $name = agents_get_name($id_agent); $agent_md5 = md5($name); if (file_exists($config['remote_config'].'/md5/'.$agent_md5.'.md5')) { - $cont ++; + $cont++; } } @@ -468,12 +468,13 @@ $table->data[1][3] = html_print_select( __('All'), 2, true, + false, + true, '', - '', - '', - '', + false, 'width:30%;' ); + $table->data[2][0] = __('Agents'); $table->data[2][0] .= '