Remove service elements when removing items itselves and multiple fixes

This commit is contained in:
fbsanchez 2021-06-01 12:12:07 +02:00
parent a98a72354b
commit 70b8eb77a3
8 changed files with 209 additions and 115 deletions

View File

@ -500,7 +500,7 @@ if (enterprise_installed()) {
false,
// Delete_groups.
// Do not show the primary group in this selection.
array_merge($secondary_groups_selected['plain'], [$agent['id_grupo']])
array_merge(($secondary_groups_selected['plain'] ?? []), [$agent['id_grupo']])
// Include_groups.
// Size.
// Simple_multiple_options.

View File

@ -2095,106 +2095,8 @@ if ($delete_module) {
exit;
}
enterprise_include_once('include/functions_config_agents.php');
enterprise_hook('config_agents_delete_module_in_conf', [modules_get_agentmodule_agent($id_borrar_modulo), modules_get_agentmodule_name($id_borrar_modulo)]);
// Init transaction.
$error = 0;
// First delete from tagente_modulo -> if not successful, increment
// error. NOTICE that we don't delete all data here, just marking for deletion
// and delete some simple data.
$values = [
'nombre' => 'pendingdelete',
'disabled' => 1,
'delete_pending' => 1,
];
$result = db_process_sql_update(
'tagente_modulo',
$values,
['id_agente_modulo' => $id_borrar_modulo]
);
if ($result === false) {
$error++;
} else {
// Set flag to update module status count.
db_process_sql(
'UPDATE tagente
SET update_module_count = 1, update_alert_count = 1
WHERE id_agente = '.$module_data['id_agente']
);
}
$result = db_process_sql_delete(
'tagente_estado',
['id_agente_modulo' => $id_borrar_modulo]
);
if ($result === false) {
$error++;
}
$result = db_process_sql_delete(
'tagente_datos_inc',
['id_agente_modulo' => $id_borrar_modulo]
);
if ($result === false) {
$error++;
}
if (alerts_delete_alert_agent_module(
false,
['id_agent_module' => $id_borrar_modulo]
) === false
) {
$error++;
}
$result = db_process_delete_temp(
'ttag_module',
'id_agente_modulo',
$id_borrar_modulo
);
if ($result === false) {
$error++;
}
// Trick to detect if we are deleting a synthetic module (avg or arithmetic)
// If result is empty then module doesn't have this type of submodules.
$ops_json = enterprise_hook('modules_get_synthetic_operations', [$id_borrar_modulo]);
$result_ops_synthetic = json_decode($ops_json);
if (!empty($result_ops_synthetic)) {
$result = enterprise_hook('modules_delete_synthetic_operations', [$id_borrar_modulo]);
if ($result === false) {
$error++;
}
} else {
$result_components = enterprise_hook('modules_get_synthetic_components', [$id_borrar_modulo]);
$count_components = 1;
if (!empty($result_components)) {
// Get number of components pending to delete to know when it's needed to update orders.
$num_components = count($result_components);
$last_target_module = 0;
foreach ($result_components as $id_target_module) {
// Detects change of component or last component to update orders.
if (($count_components == $num_components)
|| ($last_target_module != $id_target_module)
) {
$update_orders = true;
} else {
$update_orders = false;
}
$result = enterprise_hook('modules_delete_synthetic_operations', [$id_target_module, $id_borrar_modulo, $update_orders]);
if ($result === false) {
$error++;
}
$count_components++;
$last_target_module = $id_target_module;
}
}
}
// Also call base function to delete modules madafakas de los cojones.
modules_delete_agent_module($id_borrar_modulo);
// Check for errors.
if ($error != 0) {

View File

@ -36,6 +36,7 @@ include_javascript_d3();
global $config;
function prepend_table_simple($row, $id=false)
{
global $table_simple;
@ -598,8 +599,12 @@ if ($moduletype == MODULE_DATA) {
$table_advanced->colspan[1][1] = 2;
$interval_factor = 1;
if (isset($id_agente)) {
$agent_interval = agents_get_interval($id_agente);
$interval_factor = ($interval / $agent_interval);
$agent_interval = (float) agents_get_interval($id_agente);
if ($agent_interval > 0) {
$interval = (float) $interval;
$interval_factor = ($interval / $agent_interval);
}
$table_advanced->data[1][1] = human_time_description_raw($interval).' ('.sprintf(__('Agent interval x %s'), $interval_factor).') ';
} else {
$table_advanced->data[1][1] = sprintf(__('Agent interval x %s'), $interval_factor);

View File

@ -2417,6 +2417,30 @@ function agents_delete_agent($id_agents, $disableACL=false)
$id_agent
);
// Process a controlled module ellimination, keeping the old behaviour
// a couple of lines below this section.
try {
$filter = ['id_agente' => $id_agent];
$modules = [];
$rows = \db_get_all_rows_filter(
'tagente_modulo',
$filter
);
if (is_array($rows) === true) {
foreach ($rows as $row) {
$modules[] = PandoraFMS\Module::build($row);
}
}
foreach ($modules as $module) {
$module->delete();
}
} catch (Exception $e) {
// Ignore.
error_log($e->getMessage().' in '.$e->getFile().':'.$e->getLine());
}
// The status of the module
db_process_delete_temp('tagente_estado', 'id_agente', $id_agent);
@ -2456,15 +2480,27 @@ function agents_delete_agent($id_agents, $disableACL=false)
$target_filter
);
foreach ($commands as $command) {
$rcmd_id = $command['rcmd_id'];
$rcmd = new RCMDFile($rcmd_id);
if (is_array($commands) === true) {
foreach ($commands as $command) {
$rcmd_id = $command['rcmd_id'];
$rcmd = new RCMDFile($rcmd_id);
$command_targets = [];
$command_targets = [];
$command_targets = $rcmd->getTargets(false, $target_filter);
$rcmd->deleteTargets(array_keys($command_targets));
$command_targets = $rcmd->getTargets(false, $target_filter);
$rcmd->deleteTargets(array_keys($command_targets));
}
}
// Remove agents from service child list.
enterprise_include_once('include/functions_services.php');
\enterprise_hook(
'service_elements_removal_tool',
[
$id_agent,
SERVICE_ELEMENT_AGENT,
]
);
}
// tagente_datos_inc

View File

@ -524,10 +524,13 @@ function io_json_mb_encode($string, $encode_options=0)
$v = json_encode($string, $encode_options);
$v = preg_replace_callback(
"/\\\\u([0-9a-zA-Z]{4})/",
create_function(
'$matches',
'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UTF-16");'
),
function ($matches) {
return mb_convert_encoding(
pack('H*', $matches[1]),
'UTF-8',
'UTF-16'
);
},
$v
);
$v = preg_replace('/\\\\\//', '/', $v);

View File

@ -387,7 +387,7 @@ function modules_change_disabled($id_agent_module, $new_value=1)
*
* @param mixed Agent module id to be deleted. Accepts an array with ids.
*
* @return True if the module was deleted. False if not.
* @return boolean True if the module was deleted. False if not.
*/
function modules_delete_agent_module($id_agent_module)
{
@ -455,6 +455,16 @@ function modules_delete_agent_module($id_agent_module)
}
}
// Remove module from service child list.
enterprise_include_once('include/functions_services.php');
\enterprise_hook(
'service_elements_removal_tool',
[
$id_agent_module,
SERVICE_ELEMENT_MODULE,
]
);
alerts_delete_alert_agent_module(0, $where);
db_process_sql_delete('tgraph_source', $where);
@ -477,6 +487,130 @@ function modules_delete_agent_module($id_agent_module)
);
db_process_sql_delete('ttag_module', $where);
$id_borrar_modulo = $id_agent_module;
enterprise_include_once('include/functions_config_agents.php');
enterprise_hook(
'config_agents_delete_module_in_conf',
[
modules_get_agentmodule_agent($id_borrar_modulo),
modules_get_agentmodule_name($id_borrar_modulo),
]
);
// Init transaction.
$error = 0;
// First delete from tagente_modulo -> if not successful, increment
// error. NOTICE that we don't delete all data here, just marking for deletion
// and delete some simple data.
$values = [
'nombre' => 'pendingdelete',
'disabled' => 1,
'delete_pending' => 1,
];
$result = db_process_sql_update(
'tagente_modulo',
$values,
['id_agente_modulo' => $id_borrar_modulo]
);
if ($result === false) {
$error++;
} else {
// Set flag to update module status count.
db_process_sql(
'UPDATE tagente
SET update_module_count = 1, update_alert_count = 1
WHERE id_agente = '.$id_agent
);
}
$result = db_process_sql_delete(
'tagente_estado',
['id_agente_modulo' => $id_borrar_modulo]
);
if ($result === false) {
$error++;
}
$result = db_process_sql_delete(
'tagente_datos_inc',
['id_agente_modulo' => $id_borrar_modulo]
);
if ($result === false) {
$error++;
}
if (alerts_delete_alert_agent_module(
false,
['id_agent_module' => $id_borrar_modulo]
) === false
) {
$error++;
}
$result = db_process_delete_temp(
'ttag_module',
'id_agente_modulo',
$id_borrar_modulo
);
if ($result === false) {
$error++;
}
// Trick to detect if we are deleting a synthetic module (avg or arithmetic)
// If result is empty then module doesn't have this type of submodules.
$ops_json = enterprise_hook(
'modules_get_synthetic_operations',
[$id_borrar_modulo]
);
$result_ops_synthetic = json_decode($ops_json);
if (!empty($result_ops_synthetic)) {
$result = enterprise_hook(
'modules_delete_synthetic_operations',
[$id_borrar_modulo]
);
if ($result === false) {
$error++;
}
} else {
$result_components = enterprise_hook(
'modules_get_synthetic_components',
[$id_borrar_modulo]
);
$count_components = 1;
if (!empty($result_components)) {
// Get number of components pending to delete to know when it's needed to update orders.
$num_components = count($result_components);
$last_target_module = 0;
foreach ($result_components as $id_target_module) {
$update_orders = false;
// Detects change of component or last component to update orders.
if (($count_components == $num_components)
|| ($last_target_module != $id_target_module)
) {
$update_orders = true;
}
$result = enterprise_hook(
'modules_delete_synthetic_operations',
[
$id_target_module,
$id_borrar_modulo,
$update_orders,
]
);
if ($result === false) {
$error++;
}
$count_components++;
$last_target_module = $id_target_module;
}
}
}
return true;
}

View File

@ -588,6 +588,13 @@ class Agent extends Entity
$this->fields['id_agente']
);
// Delete modules.
if ($this->modules !== null) {
foreach ($this->modules as $module) {
$module->delete();
}
}
unset($this->fields);
unset($this->modules);
}

View File

@ -1325,7 +1325,14 @@ $agent_interfaces = agents_get_network_interfaces(
['id_agente' => $id_agente]
);
$agent_interfaces_count = count($agent_interfaces[$id_agente]['interfaces']);
if (is_array($agent_interfaces[$id_agente]['interfaces']) !== true
|| is_object($agent_interfaces[$id_agente]['interfaces']) !== true
) {
$agent_interfaces_count = 0;
} else {
$agent_interfaces_count = count($agent_interfaces[$id_agente]['interfaces']);
}
if ($agent_interfaces_count > 0) {
$interfacetab['text'] = '<a href="index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$id_agente.'&tab=interface">'.html_print_image(