From e58bc4f96ae700c1f1625836671df250d124bfb9 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Thu, 14 Jul 2022 16:06:51 +0200 Subject: [PATCH 1/2] move massive aperation edit agent pandora_enterprise#8564 --- .../godmode/massive/massive_delete_agents.php | 207 +--- .../godmode/massive/massive_edit_agents.php | 965 ++++++++++-------- .../godmode/massive/massive_operations.php | 5 - .../include/functions_massive_operations.php | 165 +++ .../include/javascript/massive_operations.js | 52 + 5 files changed, 769 insertions(+), 625 deletions(-) diff --git a/pandora_console/godmode/massive/massive_delete_agents.php b/pandora_console/godmode/massive/massive_delete_agents.php index cc87efebed..7a42dc721d 100755 --- a/pandora_console/godmode/massive/massive_delete_agents.php +++ b/pandora_console/godmode/massive/massive_delete_agents.php @@ -169,154 +169,6 @@ if ($delete === true) { } } -$table = new stdClass; -$table->id = 'delete_table'; -$table->class = 'databox filters'; -$table->width = '100%'; -$table->data = []; -$table->style = []; -$table->style[0] = 'font-weight: bold;'; -$table->style[2] = 'font-weight: bold'; -$table->size = []; -$table->size[0] = '15%'; -$table->size[1] = '35%'; -$table->size[2] = '15%'; -$table->size[3] = '35%'; - -$table->data = []; -$table->data[0][0] = __('Group'); -$table->data[0][1] = html_print_select_groups( - false, - 'AW', - true, - 'id_group', - $id_group, - false, - '', - '', - true -); -$table->data[0][2] = __('Group recursion'); -$table->data[0][3] = html_print_checkbox( - 'recursion', - 1, - $recursion, - true, - false -); - -$status_list = []; -$status_list[AGENT_STATUS_NORMAL] = __('Normal'); -$status_list[AGENT_STATUS_WARNING] = __('Warning'); -$status_list[AGENT_STATUS_CRITICAL] = __('Critical'); -$status_list[AGENT_STATUS_UNKNOWN] = __('Unknown'); -$status_list[AGENT_STATUS_NOT_NORMAL] = __('Not normal'); -$status_list[AGENT_STATUS_NOT_INIT] = __('Not init'); -$table->data[1][0] = __('Status'); -$table->data[1][1] = html_print_select( - $status_list, - 'status_agents', - 'selected', - '', - __('All'), - AGENT_STATUS_ALL, - true -); - -$table->data[1][2] = __('Show agents'); -$table->data[1][3] = html_print_select( - [ - 0 => 'Only enabled', - 1 => 'Only disabled', - ], - 'disabled', - 2, - '', - __('All'), - 2, - true, - false, - true, - '', - false, - 'width:30%;' -); - -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, - 'nodes[]', - 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 - ); -} - -$table->data[3][0] = __('Agents'); -$table->data[3][0] .= ''; - -$agents = []; -if (is_metaconsole() === false) { - $agents = agents_get_group_agents( - array_keys(users_get_groups($config['id_user'], 'AW', false)), - ['disabled' => 2], - 'none' - ); -} - - -$table->data[3][1] = html_print_select( - $agents, - '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 -); $url = 'index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=delete_agents'; if (is_metaconsole() === true) { @@ -324,10 +176,15 @@ if (is_metaconsole() === true) { } echo '
'; -html_print_table($table); + +$params = [ + 'id_group' => $id_group, + 'recursion' => $recursion, +]; +echo get_table_inputs_masive_agents($params); if (is_metaconsole() === true || is_management_allowed() === true) { - attachActionButton('delete', 'delete', $table->width); + attachActionButton('delete', 'delete', '100%'); } echo '
'; @@ -342,54 +199,6 @@ ui_require_jquery_file('pandora.controls'); $(document).ready (function () { // Check Metaconsole. var metaconsole = ''; - - // Listeners. - var recursion; - $("#checkbox-recursion").click(function () { - recursion = this.checked ? 1 : 0; - $("#id_group").trigger("change"); - }); - - var disabled; - $("#disabled").change(function () { - disabled = this.value; - $("#id_group").trigger("change"); - }); - - var nodes; - $("#nodes").change(function () { - nodes = $("#nodes").val(); - $("#id_group").trigger("change"); - }); - - $("#status_agents").change(function() { - $("#id_group").trigger("change"); - }); - - // Build data. - var data = { - status_agents: function () { - return $("#status_agents").val(); - }, - agentSelect: "select#id_agents", - privilege: "AW", - recursion: function() { - return recursion; - }, - disabled: function() { - return disabled; - }, - } - - if (metaconsole == 1) { - data.serialized = true; - data.serialized_separator = '|'; - data.nodes = function() { - return nodes; - }; - } - - // Change agents. - $("#id_group").pandoraSelectGroupAgent(data); + form_controls_massive_operations_agents(metaconsole); }); diff --git a/pandora_console/godmode/massive/massive_edit_agents.php b/pandora_console/godmode/massive/massive_edit_agents.php index a68104f4e4..940b16c9a8 100755 --- a/pandora_console/godmode/massive/massive_edit_agents.php +++ b/pandora_console/godmode/massive/massive_edit_agents.php @@ -26,6 +26,9 @@ * ============================================================================ */ +use PandoraFMS\Agent; +use PandoraFMS\Enterprise\Metaconsole\Node; + // Begin. check_login(); @@ -48,17 +51,47 @@ require_once $config['homedir'].'/include/functions_users.php'; enterprise_include_once('include/functions_config_agents.php'); if (is_ajax() === true) { - $get_n_conf_files = (bool) get_parameter('get_n_conf_files', false); - $groups_secondary_selected = (bool) get_parameter('groups_secondary_selected', false); + $get_n_conf_files = (bool) get_parameter( + 'get_n_conf_files', + false + ); + + $groups_secondary_selected = (bool) get_parameter( + 'groups_secondary_selected', + false + ); if ($get_n_conf_files === true) { $id_agents = get_parameter('id_agents'); $cont = 0; foreach ($id_agents as $id_agent) { - $name = agents_get_name($id_agent); - $agent_md5 = md5($name); - if (file_exists($config['remote_config'].'/md5/'.$agent_md5.'.md5') === true) { - $cont++; + if (is_metaconsole() === true) { + $array_id = explode('|', $id_agent); + try { + $node = new Node((int) $array_id[0]); + $node->connect(); + + $agent = new Agent((int) $array_id[1]); + if ($agent->hasRemoteConf() === true) { + $cont++; + } + + $node->disconnect(); + } catch (\Exception $e) { + // Unexistent agent. + $cont = 0; + $node->disconnect(); + } + } else { + try { + $agent = new Agent((int) $array_id[1]); + if ($agent->hasRemoteConf() === true) { + $cont++; + } + } catch (\Exception $e) { + // Unexistent agent. + $cont = 0; + } } } @@ -88,12 +121,15 @@ $recursion = get_parameter('recursion'); if ($update_agents) { $values = []; - if (get_parameter('group', '') != -1) { + + if ((int) get_parameter('group', '') !== -1) { $values['id_grupo'] = get_parameter('group'); } - if (!(get_parameter('interval_select') == -1 && empty(get_parameter('interval_text')))) { - if (get_parameter('interval', 0) != 0) { + if (!(get_parameter('interval_select') === -1 + && empty(get_parameter('interval_text'))) + ) { + if (get_parameter('interval') != -2) { $values['intervalo'] = get_parameter('interval'); } } @@ -158,8 +194,14 @@ if ($update_agents) { $values['safe_mode_module'] = '0'; } - $secondary_groups_added = (array) get_parameter('secondary_groups_added', []); - $secondary_groups_removed = (array) get_parameter('secondary_groups_removed', []); + $secondary_groups_added = (array) get_parameter( + 'secondary_groups_added', + [] + ); + $secondary_groups_removed = (array) get_parameter( + 'secondary_groups_removed', + [] + ); $fields = db_get_all_fields_in_table('tagent_custom_fields'); @@ -178,20 +220,26 @@ if ($update_agents) { } } - // Get the id_agente_modulo to update the 'safe_operation_mode' field. - if (isset($values['safe_mode_module']) && ($values['safe_mode_module'] != '0')) { - foreach ($id_agents as $id_agent) { - $id_module_safe[$id_agent] = db_get_value_filter( - 'id_agente_modulo', - 'tagente_modulo', - [ - 'id_agente' => $id_agent, - 'nombre' => $values['safe_mode_module'], - ] - ); + $id_module_safe = []; + if (is_metaconsole() === false) { + // Get the id_agente_modulo to update the 'safe_operation_mode' field. + if (isset($values['safe_mode_module']) === true + && ($values['safe_mode_module'] != '0') + ) { + foreach ($id_agents as $id_agent) { + $id_module_safe[$id_agent] = db_get_value_filter( + 'id_agente_modulo', + 'tagente_modulo', + [ + 'id_agente' => $id_agent, + 'nombre' => $values['safe_mode_module'], + ] + ); + } } } + // TODO:XXX // CONF FILE DELETION. if (isset($values['delete_conf'])) { unset($values['delete_conf']); @@ -229,141 +277,58 @@ if ($update_agents) { ); } - if (empty($values) && empty($fields)) { + if (empty($values) === true + && empty($fields) === true + ) { $id_agents = []; } - $n_edited = 0; + hd($values); + $result = []; foreach ($id_agents as $id_agent) { - $old_interval_value = db_get_value_filter('intervalo', 'tagente', ['id_agente' => $id_agent]); - - if (!empty($values)) { - $disabled_old = false; - if ($values['id_grupo'] || isset($values['disabled'])) { - $values_old = db_get_row_filter('tagente', ['id_agente' => $id_agent], ['id_grupo', 'disabled']); - if (isset($values['disabled'])) { - $disabled_old = $values_old['disabled']; - } - } - - // Get the id_agent_module for this agent to update the 'safe_operation_mode' field. - if (isset($values['safe_mode_module']) && ($values['safe_mode_module'] != '0')) { - $values['safe_mode_module'] = $id_module_safe[$id_agent]; - } - - $result[$id_agent]['db'] = db_process_sql_update( - 'tagente', - $values, - ['id_agente' => $id_agent] - ); - - if ($result[$id_agent]['db'] && $config['metaconsole_agent_cache'] == 1) { - $server_name['server_name'] = db_get_sql('SELECT server_name FROM tagente WHERE id_agente ='.$id_agent); - // Force an update of the agent cache. - $result_metaconsole = agent_update_from_cache($id_agent, $values, $server_name); - } - - // Update the configuration files. - if ($result[$id_agent]['db'] && ($old_interval_value != $values['intervalo']) && !empty($values['intervalo'])) { - enterprise_hook( - 'config_agents_update_config_token', - [ - $id_agent, - 'interval', - $values['intervalo'], - ] - ); - } - - if ($disabled_old !== false && $disabled_old != $values['disabled']) { - enterprise_hook( - 'config_agents_update_config_token', - [ - $id_agent, - 'standby', - $values['disabled'], - ] - ); - // Validate alerts for disabled agents. - if ($values['disabled'] == 1) { - alerts_validate_alert_agent($id_agent); - } - } - } - - $info = []; - // Update Custom Fields. - foreach ($fields as $field) { - $info[$field['id_field']] = $field['name']; - $value = get_parameter('customvalue_'.$field['id_field']); - if (empty($value) === false) { - $key = $field['id_field']; - $old_value = db_get_all_rows_filter( - 'tagent_custom_data', - [ - 'id_agent' => $id_agent, - 'id_field' => $key, - ] - ); - - if ($old_value === false) { - // Create custom field if not exist. - $result[$id_agent]['fields'][$field['id_field']] = db_process_sql_insert( - 'tagent_custom_data', - [ - 'id_field' => $key, - 'id_agent' => $id_agent, - 'description' => $value, - ] - ); - } else { - if ($old_value[0]['description'] !== $value) { - $result[$id_agent]['fields'][$field['id_field']] = db_process_sql_update( - 'tagent_custom_data', - ['description' => $value], - [ - 'id_field' => $key, - 'id_agent' => $id_agent, - ] - ); - } - } - } - } - - // Create or Remove the secondary groups. - if (empty($secondary_groups_added) === false - || empty($secondary_groups_removed) === false - ) { - $result[$id_agent]['secondary'] = enterprise_hook( - 'agents_update_secondary_groups', - [ - $id_agent, + if (is_metaconsole() === true) { + $array_id = explode('|', $id_agent); + try { + $node = new Node((int) $array_id[0]); + $node->connect(); + $result[$id_agent] = edit_massive_agent( + (int) $array_id[1], + $values, + $id_module_safe, + $fields, $secondary_groups_added, - $secondary_groups_removed, - true, - ] - ); - } - - if ($result['db'] !== false) { - db_pandora_audit( - AUDIT_LOG_MASSIVE_MANAGEMENT, - 'Update agent '.$id_agent, - false, - false, - json_encode($info) - ); - } else { - if (isset($id_agent) === true) { - db_pandora_audit( - AUDIT_LOG_MASSIVE_MANAGEMENT, - 'Try to update agent '.$id_agent, - false, - false, - json_encode($info) + $secondary_groups_removed ); + + $agents_values = agents_get_agent((int) $array_id[1]); + $node->disconnect(); + + if (empty($values) === false) { + update_agents_in_metaconsole( + (int) $array_id[1], + $values, + $agents_values + ); + } + } catch (\Exception $e) { + // Unexistent agent. + $result = []; + $node->disconnect(); + } + } else { + try { + $result[$id_agent] = edit_massive_agent( + $id_agent, + $values, + $id_module_safe, + $fields, + $secondary_groups_added, + $secondary_groups_removed + ); + } catch (\Exception $e) { + // Unexistent agent. + $result = []; } } } @@ -488,121 +453,160 @@ if ($update_agents) { } } -$id_group = 0; -$table = new StdClass(); -$table->id = 'delete_table'; -$table->class = 'databox filters'; -$table->width = '100%'; -$table->data = []; -$table->style = []; -$table->style[0] = 'font-weight: bold;'; -$table->style[2] = 'font-weight: bold'; -$table->size = []; -$table->size[0] = '15%'; -$table->size[1] = '35%'; -$table->size[2] = '15%'; -$table->size[3] = '35%'; +/** + * Edit massive agent. + * + * @param integer $id_agent + * @param array $values + * @param array $id_module_safe + * @param array $fields + * @param array $secondary_groups_added + * @param array $secondary_groups_removed + * @return void + */ +function edit_massive_agent( + int $id_agent, + array $values, + array $id_module_safe, + array $fields, + array $secondary_groups_added, + array $secondary_groups_removed +) { + global $config; + $result = false; -$table->data = []; -$table->data[0][0] = __('Group'); -$table->data[0][1] = html_print_select_groups( - false, - 'AW', - true, - 'id_group', - $id_group, - false, - '', - '', - true -); -$table->data[0][2] = __('Group recursion'); -$table->data[0][3] = html_print_checkbox( - 'recursion2', - 1, - $recursion, - true, - false -); + if (empty($values) === false) { + $agent = new Agent($id_agent); + $disabled_old = $agent->disabled(); + + foreach ($values as $key => $value) { + $agent->{$key}($value); + } + + if (is_metaconsole() === false) { + // Get the id_agent_module for this agent to update the 'safe_operation_mode' field. + if (isset($values['safe_mode_module']) === true + && ($values['safe_mode_module'] != '0') + ) { + $values['safe_mode_module'] = $id_module_safe[$id_agent]; + } + } + + $result['db'] = $agent->save(); + + if (is_metaconsole() === false) { + if ($result['db'] !== false + && (bool) $config['metaconsole_agent_cache'] === true + ) { + // Force an update of the agent cache. + $agent->updateFromCache(); + } + } + + if ($disabled_old !== $values['disabled']) { + // Validate alerts for disabled agents. + if ($values['disabled'] == 1) { + alerts_validate_alert_agent($id_agent); + } + } + } + + $info = []; + // Update Custom Fields. + if (isset($fields) === true + && empty($fields) === false + ) { + foreach ($fields as $field) { + $info[$field['id_field']] = $field['name']; + $value = get_parameter('customvalue_'.$field['id_field']); + if (empty($value) === false) { + $key = $field['id_field']; + $old_value = db_get_all_rows_filter( + 'tagent_custom_data', + [ + 'id_agent' => $id_agent, + 'id_field' => $key, + ] + ); + + if ($old_value === false) { + // Create custom field if not exist. + $result['fields'][$field['id_field']] = db_process_sql_insert( + 'tagent_custom_data', + [ + 'id_field' => $key, + 'id_agent' => $id_agent, + 'description' => $value, + ] + ); + } else { + if ($old_value[0]['description'] !== $value) { + $result['fields'][$field['id_field']] = db_process_sql_update( + 'tagent_custom_data', + ['description' => $value], + [ + 'id_field' => $key, + 'id_agent' => $id_agent, + ] + ); + } + } + } + } + } + + // Create or Remove the secondary groups. + if (empty($secondary_groups_added) === false + || empty($secondary_groups_removed) === false + ) { + $result['secondary'] = enterprise_hook( + 'agents_update_secondary_groups', + [ + $id_agent, + $secondary_groups_added, + $secondary_groups_removed, + true, + ] + ); + } + + if ($result['db'] !== false) { + db_pandora_audit( + AUDIT_LOG_MASSIVE_MANAGEMENT, + 'Update agent '.$id_agent, + false, + false, + json_encode($info) + ); + } else { + if (isset($id_agent) === true) { + db_pandora_audit( + AUDIT_LOG_MASSIVE_MANAGEMENT, + 'Try to update agent '.$id_agent, + false, + false, + json_encode($info) + ); + } + } + + return $result; +} -$status_list = []; -$status_list[AGENT_STATUS_NORMAL] = __('Normal'); -$status_list[AGENT_STATUS_WARNING] = __('Warning'); -$status_list[AGENT_STATUS_CRITICAL] = __('Critical'); -$status_list[AGENT_STATUS_UNKNOWN] = __('Unknown'); -$status_list[AGENT_STATUS_NOT_NORMAL] = __('Not normal'); -$status_list[AGENT_STATUS_NOT_INIT] = __('Not init'); -$table->data[1][0] = __('Status'); -$table->data[1][1] = html_print_select( - $status_list, - 'status_agents', - 'selected', - '', - __('All'), - AGENT_STATUS_ALL, - true -); -$table->data[1][2] = __('Show agents'); -$table->data[1][3] = html_print_select( - [ - 0 => 'Only enabled', - 1 => 'Only disabled', - ], - 'disabled', - 2, - '', - __('All'), - 2, - true, - false, - true, - '', - false, - 'width:30%;' -); +$url = 'index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=edit_agents'; +if (is_metaconsole() === true) { + $url = 'index.php?sec=advanced&sec2=advanced/massive_operations&tab=massive_agents&pure=0&option=edit_agents'; +} -$table->data[2][0] = __('Agents'); -$table->data[2][0] .= ''; -$all_agents = agents_get_group_agents( - array_keys(users_get_groups($config['id_user'], 'AW', false)), - ['disabled' => 2], - 'none' -); - -$table->data[2][1] = html_print_select( - $all_agents, - '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 '
'; +echo ''; echo html_print_avoid_autocomplete(); - -html_print_table($table); +$params = [ + 'id_group' => $id_group, + 'recursion' => $recursion, +]; +echo get_table_inputs_masive_agents($params); $nombre_agente = ''; $direccion_agente = ''; @@ -620,7 +624,6 @@ echo '