From cd199e233d56f5a377f2aff6992040c503b7f18e Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Mon, 19 Sep 2022 12:17:24 +0200 Subject: [PATCH] #9334 Added improve in satellite hosts --- .../godmode/servers/servers.build_table.php | 11 + .../include/class/SatelliteAgent.class.php | 525 +++++++++++++++--- 2 files changed, 471 insertions(+), 65 deletions(-) diff --git a/pandora_console/godmode/servers/servers.build_table.php b/pandora_console/godmode/servers/servers.build_table.php index 66e995fd2c..9629dccb22 100644 --- a/pandora_console/godmode/servers/servers.build_table.php +++ b/pandora_console/godmode/servers/servers.build_table.php @@ -238,6 +238,17 @@ foreach ($servers as $server) { $data[8] .= ''; if (($names_servers[$safe_server_name] === true) && ($server['type'] === 'data' || $server['type'] === 'enterprise satellite')) { + $data[8] .= ''; + $data[8] .= html_print_image( + 'images/agent.png', + true, + [ + 'title' => __('Manage satellite hosts'), + 'class' => 'invert_filter', + ] + ); + $data[8] .= ''; + $data[8] .= ''; $data[8] .= html_print_image( 'images/remote_configuration.png', diff --git a/pandora_console/include/class/SatelliteAgent.class.php b/pandora_console/include/class/SatelliteAgent.class.php index f1c187afdf..652be2c8e1 100644 --- a/pandora_console/include/class/SatelliteAgent.class.php +++ b/pandora_console/include/class/SatelliteAgent.class.php @@ -56,6 +56,7 @@ class SatelliteAgent extends HTML 'draw', 'addAgent', 'deleteAgent', + 'disableAgent', 'loadModal', ]; @@ -97,7 +98,6 @@ class SatelliteAgent extends HTML $this->satellite_name = servers_get_name($this->satellite_server); $this->satellite_config = (array) config_satellite_get_config_file($this->satellite_name); } - } @@ -108,13 +108,15 @@ class SatelliteAgent extends HTML */ public function run() { + global $config; // Javascript. ui_require_jquery_file('pandora'); // CSS. ui_require_css_file('wizard'); ui_require_css_file('discovery'); - global $config; + $this->createBlock(); + // Datatables list. try { $columns = [ @@ -129,6 +131,13 @@ class SatelliteAgent extends HTML __('Actions'), ]; + $show_agents = [ + 0 => __('Everyone'), + 1 => __('Only disabled'), + 2 => __('Only deleted'), + 3 => __('Only added'), + ]; + $this->tableId = 'satellite_agents'; if (is_metaconsole() === true) { @@ -167,7 +176,15 @@ class SatelliteAgent extends HTML 'name' => 'filter_search', 'size' => 12, ], - + [ + 'label' => __('Show agents'), + 'type' => 'select', + 'id' => 'filter_agents', + 'name' => 'filter_agents', + 'fields' => $show_agents, + 'return' => true, + 'selected' => 0, + ], ], ], ] @@ -200,7 +217,6 @@ class SatelliteAgent extends HTML echo ''; // Load own javascript file. echo $this->loadJS(); - } @@ -212,8 +228,7 @@ class SatelliteAgent extends HTML public function draw() { global $config; - // Initialice filter. - $filter = '1=1'; + // Init data. $data = []; // Count of total records. @@ -228,8 +243,23 @@ class SatelliteAgent extends HTML ob_start(); $data = []; + $agents_db = db_get_all_rows_sql( + sprintf( + 'SELECT id_agente, alias AS name, direccion AS address, + IF(disabled = 0, INSERT("add_host", 0 , 0, ""), INSERT("ignore_host", 0 , 0, "")) AS type + FROM tagente WHERE `satellite_server` = %d AND modo = 1', + $this->satellite_server + ) + ); + + if (empty($agents_db) === false) { + $data = $agents_db; + } + foreach ($this->satellite_config as $line) { - $re = '/^#*add_host \b(\S+) (\S*)$/m'; + $re = '/^#*add_host \b(\S+) (\S*)/m'; + $re_disable = '/^ignore_host \b(\S+) (\S*)/m'; + $re_delete = '/^delete_host \b(\S+) (\S*)/m'; if (preg_match($re, $line, $matches, PREG_OFFSET_CAPTURE, 0) > 0) { $agent['address'] = $matches[1][0]; @@ -239,17 +269,74 @@ class SatelliteAgent extends HTML $agent['name'] = $matches[2][0]; } - if (empty($filters['filter_search']) === false) { - if (empty(preg_grep('/'.$filters['filter_search'].'?/mi', array_values($agent))) === true) { - continue; - } + $agent['type'] = 'add_host'; + + array_push($data, $agent); + } + + if (preg_match($re_disable, $line, $matches, PREG_OFFSET_CAPTURE, 0) > 0) { + $agent['address'] = $matches[1][0]; + if (isset($matches[2][0]) === false || empty($matches[2][0]) === true) { + $agent['name'] = ''; + } else { + $agent['name'] = $matches[2][0]; } + $agent['type'] = 'ignore_host'; + + array_push($data, $agent); + } + + if (preg_match($re_delete, $line, $matches, PREG_OFFSET_CAPTURE, 0) > 0) { + $agent['address'] = $matches[1][0]; + if (isset($matches[2][0]) === false || empty($matches[2][0]) === true) { + $agent['name'] = ''; + } else { + $agent['name'] = $matches[2][0]; + } + + $agent['type'] = 'delete_host'; + array_push($data, $agent); } } if (empty($data) === false) { + $data = $this->uniqueMultidimArray($data, ['name', 'address']); + if (empty($filters['filter_agents']) === false || empty($filters['filter_search']) === false) { + foreach ($data as $key => $value) { + switch ($filters['filter_agents']) { + case 1: + if ($value['type'] !== 'ignore_host') { + unset($data[$key]); + } + break; + + case 2: + if ($value['type'] !== 'delete_host') { + unset($data[$key]); + } + break; + + case 3: + if ($value['type'] !== 'add_host') { + unset($data[$key]); + } + break; + + default: + // Everyone. + break; + } + + if (empty($filters['filter_search']) === false) { + if (empty(preg_grep('/'.$filters['filter_search'].'?/mi', array_values($value))) === true) { + unset($data[$key]); + } + } + } + } + $data = array_reduce( $data, function ($carry, $item) { @@ -258,15 +345,44 @@ class SatelliteAgent extends HTML // of objects, making a post-process of certain fields. $tmp = (object) $item; - $tmp->actions .= html_print_image( - 'images/cross.png', - true, - [ - 'border' => '0', - 'class' => 'action_button_img invert_filter', - 'onclick' => 'delete_agent(\''.$tmp->address.'\',\''.$tmp->name.'\')', - ] - ); + $disable = ($tmp->type === 'ignore_host'); + $delete = ($tmp->type === 'delete_host'); + + if ($disable === true) { + $tmp->name = ''.$tmp->name.''; + } + + if ($delete === true) { + $tmp->name = ''.$tmp->name.''; + } + + $id_agente = (isset($tmp->id_agente) === true) ? $tmp->id_agente : 0; + + $tmp->actions = ''; + + if ($delete === false) { + $tmp->actions .= html_print_image( + ($disable === true) ? 'images/lightbulb_off.png' : 'images/lightbulb.png', + true, + [ + 'border' => '0', + 'class' => 'action_button_img mrgn_lft_05em invert_filter', + 'onclick' => 'disable_agent(\''.$tmp->address.'\',\''.strip_tags($tmp->name).'\',\''.(int) $disable.'\',\''.$id_agente.'\')', + ] + ); + } + + if ($disable === false) { + $tmp->actions .= html_print_image( + ($delete === true) ? 'images/add.png' : 'images/cross.png', + true, + [ + 'border' => '0', + 'class' => 'action_button_img mrgn_lft_05em invert_filter', + 'onclick' => 'delete_agent(\''.$tmp->address.'\',\''.strip_tags($tmp->name).'\',\''.(int) $delete.'\',\''.$id_agente.'\')', + ] + ); + } $carry[] = $tmp; return $carry; @@ -274,8 +390,13 @@ class SatelliteAgent extends HTML ); } - $data = array_slice($data, $start, $length, true); - $total = count($data); + if (empty($data) === true) { + $total = 0; + $data = []; + } else { + $total = count($data); + $data = array_slice($data, $start, $length, false); + } echo json_encode( [ @@ -392,6 +513,16 @@ class SatelliteAgent extends HTML $values['address'] = get_parameter('address'); $values['name'] = get_parameter('name'); + if ($this->checkAddressExists($values['address']) === true) { + $this->ajaxMsg('error', __('Error saving agent. The address already exists')); + exit; + } + + if ($this->checkNameExists($values['name']) === true) { + $this->ajaxMsg('error', __('Error saving agent. The Name already exists')); + exit; + } + if ($this->parseSatelliteConf('save', $values) === false) { $this->ajaxMsg('error', __('Error saving agent')); } else { @@ -412,11 +543,70 @@ class SatelliteAgent extends HTML { $values['address'] = get_parameter('address', ''); $values['name'] = get_parameter('name', ''); + $values['delete'] = get_parameter('delete', ''); + $values['id'] = get_parameter('id', 0); + + if ((bool) $values['id'] === true) { + db_process_sql_update( + 'tagente', + [ + 'disabled' => ($values['delete'] === '0') ? 1 : 0, + 'modo' => ($values['delete'] === '0') ? 2 : 1, + ], + ['id_agente' => (int) $values['id']] + ); + } if ($this->parseSatelliteConf('delete', $values) === false) { - $this->ajaxMsg('error', __('Error saving agent')); + $this->ajaxMsg('error', ($values['delete'] === '0') ? __('Error delete agent') : __('Error add agent')); } else { - $this->ajaxMsg('result', _('Host '.$values['addres'].' added.')); + $this->ajaxMsg( + 'result', + ($values['delete'] === '0') + ? _('Host '.$values['address'].' deleted.') + : _('Host '.$values['address'].' added.'), + true + ); + } + + exit; + } + + + /** + * Disable agent from satellite conf. + * + * @return void + */ + public function disableAgent() + { + $values['address'] = get_parameter('address', ''); + $values['name'] = get_parameter('name', ''); + $values['disable'] = get_parameter('disable', ''); + $values['id'] = get_parameter('id', 0); + + if ((bool) $values['id'] === true) { + db_process_sql_update( + 'tagente', + ['disabled' => ($values['disable'] === '0') ? 1 : 0], + ['id_agente' => (int) $values['id']] + ); + } + + if ($this->parseSatelliteConf('disable', $values) === false) { + $this->ajaxMsg( + 'error', + ($values['disable'] === '0') ? __('Error disable agent') : __('Error enable agent') + ); + } else { + $this->ajaxMsg( + 'result', + ($values['disable'] === '0') + ? _('Host '.$values['address'].' disabled.') + : _('Host '.$values['address'].' enabled.'), + false, + true + ); } exit; @@ -435,29 +625,106 @@ class SatelliteAgent extends HTML switch ($action) { case 'save': if (isset($values['address']) === true && empty($values['address']) === false) { - $string_hosts = 'add_host '.$values['address'].' '.$values['name']."\n"; + $pos = preg_grep('/^\#INIT ignore_host/', $this->satellite_config); + if (empty($pos) === false) { + $string_hosts = 'add_host '.$values['address'].' '.$values['name']."\n"; - // Add host to conf - array_push($this->satellite_config, $string_hosts); + $array1 = array_slice($this->satellite_config, 0, array_key_first($pos)); + $array2 = array_slice($this->satellite_config, array_key_first($pos)); + // Add host to conf. + $array_merge = array_merge($array1, [$string_hosts], $array2); + $this->satellite_config = $array_merge; - // Check config. - if (empty($this->satellite_config)) { - return false; + // Check config. + if (empty($this->satellite_config)) { + return false; + } + + $conf = implode('', $this->satellite_config); } - - $conf = implode('', $this->satellite_config); } else { return false; } break; + case 'disable': + if ((bool) $values['disable'] === true) { + $pos = preg_grep('/^\#INIT ignore_host/', $this->satellite_config); + if (empty($pos) === false) { + $string_hosts = 'add_host '.$values['address'].' '.$values['name']."\n"; + + $array1 = array_slice($this->satellite_config, 0, array_key_first($pos)); + $array2 = array_slice($this->satellite_config, array_key_first($pos)); + // Add host to conf. + $array_merge = array_merge($array1, [$string_hosts], $array2); + $this->satellite_config = $array_merge; + + // Remove ignore_host. + $pattern = io_safe_expreg('ignore_host '.$values['address'].' '.$values['name']); + $pos = preg_grep('/'.$pattern.'/', $this->satellite_config); + if (empty($pos) === false) { + unset($this->satellite_config[array_key_first($pos)]); + } + + $conf = implode('', $this->satellite_config); + } + } else { + $pos = preg_grep('/^\#INIT delete_host/', $this->satellite_config); + if (empty($pos) === false) { + $string_hosts = 'ignore_host '.$values['address'].' '.$values['name']."\n"; + + $array1 = array_slice($this->satellite_config, 0, array_key_first($pos)); + $array2 = array_slice($this->satellite_config, array_key_first($pos)); + // Add host to conf. + $array_merge = array_merge($array1, [$string_hosts], $array2); + $this->satellite_config = $array_merge; + + // Remove add_host. + $pattern = io_safe_expreg('add_host '.$values['address'].' '.$values['name']); + $pos = preg_grep('/'.$pattern.'/', $this->satellite_config); + if (empty($pos) === false) { + unset($this->satellite_config[array_key_first($pos)]); + } + + $conf = implode('', $this->satellite_config); + } + } + break; + case 'delete': - $conf = implode('', $this->satellite_config); - // Find agent to mark for deletion. - $pattern = io_safe_expreg($values['address'].' '.$values['name']); - $re = "/add_host ($pattern)/m"; - $subst = 'delete_host $1'; - $conf = preg_replace($re, $subst, $conf); + if ((bool) $values['delete'] === true) { + $pos = preg_grep('/^\#INIT ignore_host/', $this->satellite_config); + if (empty($pos) === false) { + $string_hosts = 'add_host '.$values['address'].' '.$values['name']."\n"; + + $array1 = array_slice($this->satellite_config, 0, array_key_first($pos)); + $array2 = array_slice($this->satellite_config, array_key_first($pos)); + // Add host to conf. + $array_merge = array_merge($array1, [$string_hosts], $array2); + $this->satellite_config = $array_merge; + + // Remove delete_host. + $pattern = io_safe_expreg('delete_host '.$values['address'].' '.$values['name']); + $pos = preg_grep('/'.$pattern.'/', $this->satellite_config); + unset($this->satellite_config[array_key_first($pos)]); + + $conf = implode('', $this->satellite_config); + } + } else { + // Find agent to mark for deletion. + $pattern = io_safe_expreg('add_host '.$values['address'].' '.$values['name']); + $pos = preg_grep('/'.$pattern.'/', $this->satellite_config); + unset($this->satellite_config[array_key_first($pos)]); + + $string_hosts = 'delete_host '.$values['address'].' '.$values['name']."\n"; + $pos = preg_grep('/delete_host/', $this->satellite_config); + $array1 = array_slice($this->satellite_config, 0, array_key_last($pos)); + $array2 = array_slice($this->satellite_config, (array_key_last($pos) + 1)); + $array_merge = array_merge($array1, [$string_hosts], $array2); + $this->satellite_config = $array_merge; + + $conf = implode('', $this->satellite_config); + } break; default: @@ -469,11 +736,37 @@ class SatelliteAgent extends HTML } + public function checkAddressExists($address) + { + $pos_address = preg_grep('/.*_host\s('.$address.').*/', $this->satellite_config); + + if (empty($pos_address) === false) { + return true; + } + + return false; + + } + + + public function checkNameExists($name) + { + $pos_name = preg_grep('/.*_host.*('.$name.')$/', $this->satellite_config); + + if (empty($pos_name) === false) { + return true; + } + + return false; + } + + /** * Saves agent to satellite cofiguration file. * - * @param array $values - * @return void + * @param string $new_conf Config file. + * + * @return boolean|void */ private function saveAgent(string $new_conf) { @@ -483,13 +776,13 @@ class SatelliteAgent extends HTML return false; } - db_pandora_audit( - AUDIT_LOG_SYSTEM, - 'Update remote config for server '.$this->satellite_name - ); + db_pandora_audit( + AUDIT_LOG_SYSTEM, + 'Update remote config for server '.$this->satellite_name + ); - // Convert to config file encoding. - $encoding = config_satellite_get_encoding($new_conf); + // Convert to config file encoding. + $encoding = config_satellite_get_encoding($new_conf); if ($encoding !== false) { $converted_server_config = mb_convert_encoding($new_conf, $encoding, 'UTF-8'); if ($converted_server_config !== false) { @@ -497,7 +790,7 @@ class SatelliteAgent extends HTML } } - // Get filenames. + // Get filenames. if ($this->satellite_server !== false) { $files = config_satellite_get_satellite_config_filenames($this->satellite_name); } else { @@ -506,16 +799,40 @@ class SatelliteAgent extends HTML $files['md5'] = $config['remote_config'].'/md5/'.md5($this->satellite_name).'.srv.md5'; } - // Save configuration - $result = file_put_contents($files['conf'], $new_conf); + // Save configuration. + $result = file_put_contents($files['conf'], $new_conf); if ($result === false) { return false; } - // Save configuration md5 - $result = file_put_contents($files['md5'], md5($new_conf)); + // Save configuration md5. + $result = file_put_contents($files['md5'], md5($new_conf)); + } + + /** + * Creates add_host, ignore_host and delete_host blocks + * + * @return void + */ + public function createBlock() + { + $init = preg_grep('/^\#INIT/', $this->satellite_config); + + if (empty($init) === true) { + $add_host = "#INIT add_host\n"; + $ignore_host = "#INIT ignore_host\n"; + $delete_host = "#INIT delete_host\n"; + + array_push($this->satellite_config, "\n"); + array_push($this->satellite_config, $add_host); + array_push($this->satellite_config, $ignore_host); + array_push($this->satellite_config, $delete_host); + + $conf = implode('', $this->satellite_config); + $this->saveAgent($conf); + } } @@ -541,24 +858,26 @@ class SatelliteAgent extends HTML * * @return void */ - private function ajaxMsg($type, $msg, $delete=false) + private function ajaxMsg($type, $msg, $delete=false, $disable=false) { $msg_err = 'Failed while saving: %s'; $msg_ok = 'Successfully saved agent '; - if ($delete) { + if ($delete === true) { $msg_err = 'Failed while removing: %s'; $msg_ok = 'Successfully deleted '; } + if ($disable === true) { + $msg_err = 'Failed while disabling: %s'; + $msg_ok = 'Successfully disabled'; + } + if ($type == 'error') { echo json_encode( [ $type => ui_print_error_message( - __( - $msg_err, - $msg - ), + __($msg), '', true ), @@ -568,10 +887,7 @@ class SatelliteAgent extends HTML echo json_encode( [ $type => ui_print_success_message( - __( - $msg_ok, - $msg - ), + __($msg), '', true ), @@ -583,6 +899,35 @@ class SatelliteAgent extends HTML } + /** + * Removes duplicate values from a multidimensional array + * + * @param array $array Input array. + * @param array $key Keys. + * + * @return array + */ + public function uniqueMultidimArray($array, $key) + { + $temp_array = []; + $i = 0; + $key_array_name = []; + $key_array_address = []; + + foreach ($array as $val) { + if (!in_array($val[$key[0]], $key_array_name) && !in_array($val[$key[1]], $key_array_address)) { + $key_array_name[$i] = $val[$key[0]]; + $key_array_address[$i] = $val[$key[1]]; + $temp_array[$i] = $val; + } + + $i++; + } + + return $temp_array; + } + + /** * Load Javascript code. * @@ -707,11 +1052,11 @@ class SatelliteAgent extends HTML /** * Delete selected agent */ - function delete_agent(address, name) { + function delete_agent(address, name, deleted, id_agente) { $('#aux').empty(); $('#aux').text(''); $('#aux').dialog({ - title: ' ' + address, + title: (deleted == 0) ? ' '+address : ''+address, buttons: [ { class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub upd submit-cancel', @@ -723,7 +1068,7 @@ class SatelliteAgent extends HTML } }, { - text: 'Delete', + text: (deleted == 0) ? 'Delete' : 'Add', class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub ok submit-next', click: function(e) { $.ajax({ @@ -734,6 +1079,56 @@ class SatelliteAgent extends HTML method: 'deleteAgent', address: address, name: name, + id: id_agente, + delete: deleted, + server_remote: satellite_server; ?>, + }, + datatype: "json", + success: function (data) { + showMsg(data); + }, + error: function(e) { + showMsg(e); + } + }); + } + } + ] + }); + } + + /** + * Disable selected agent + */ + function disable_agent(address, name, disabled, id_agente) { + $('#aux').empty(); + $('#aux').text(''); + $('#aux').dialog({ + title: (disabled == 0) ? ''+address : ''+address, + buttons: [ + { + class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub upd submit-cancel', + text: '', + click: function(e) { + $(this).dialog('close'); + cleanupDOM(); + + } + }, + { + text: (disabled == 0) ? 'Disable' : 'Enable', + class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub ok submit-next', + click: function(e) { + $.ajax({ + method: 'post', + url: '', + data: { + page: 'enterprise/godmode/servers/agents_satellite', + method: 'disableAgent', + address: address, + disable: disabled, + id: id_agente, + name: name, server_remote: satellite_server; ?>, }, datatype: "json", @@ -757,7 +1152,7 @@ class SatelliteAgent extends HTML }); }); - +