Merge branch 'ent-6375-proyectos-auto-aplicacion-de-politicas' into 'develop'

some tools for policy-auto-application

See merge request artica/pandorafms!4526
This commit is contained in:
Daniel Rodriguez 2022-03-01 12:53:36 +00:00
commit 953aa2887c
11 changed files with 138 additions and 361 deletions

View File

@ -16,4 +16,17 @@ ALTER TABLE `tevent_alert` ADD COLUMN `schedule` TEXT;
ALTER TABLE `tautoconfig` ADD COLUMN `disabled` TINYINT DEFAULT 0;
UPDATE `tpolicy_groups` SET `policy_applied`=0;
CREATE TABLE IF NOT EXISTS `tpolicy_group_agents` (
`id` SERIAL,
`id_policy` INT UNSIGNED,
`id_agent` INT UNSIGNED,
`direct` TINYINT UNSIGNED DEFAULT 0,
FOREIGN KEY (`id_policy`) REFERENCES `tpolicies`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
COMMIT;

View File

@ -213,6 +213,20 @@ CREATE TABLE IF NOT EXISTS `tpolicy_groups` (
UNIQUE (`id_policy`, `id_group`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- -----------------------------------------------------
-- Table `tpolicy_group_agents`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tpolicy_group_agents` (
`id` SERIAL,
`id_policy` INT UNSIGNED,
`id_agent` INT UNSIGNED,
`direct` TINYINT UNSIGNED DEFAULT 0,
FOREIGN KEY (`id_policy`) REFERENCES `tpolicies`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ---------------------------------------------------------------------
-- Table `tdashboard`
-- ---------------------------------------------------------------------

View File

@ -30,7 +30,6 @@
global $config;
enterprise_include('godmode/agentes/configurar_agente.php');
enterprise_include('include/functions_policies.php');
enterprise_include_once('include/functions_modules.php');
require_once $config['homedir'].'/include/functions_agents.php';
require_once $config['homedir'].'/include/functions_cron.php';
@ -307,23 +306,6 @@ if ($create_agent) {
$agent_created_ok = true;
$tpolicy_group_old = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$grupo
);
if ($tpolicy_group_old) {
foreach ($tpolicy_group_old as $key => $old_group) {
db_process_sql_insert(
'tpolicy_agents',
[
'id_policy' => $old_group['id_policy'],
'id_agent' => $id_agente,
]
);
}
}
$info = '{"Name":"'.$nombre_agente.'",
"IP":"'.$direccion_agente.'",
"Group":"'.$grupo.'",
@ -1117,12 +1099,6 @@ if ($update_agent) {
// Force an update of the agent cache.
}
$group_old = db_get_sql('SELECT id_grupo FROM tagente WHERE id_agente ='.$id_agente);
$tpolicy_group_old = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$group_old
);
$result = db_process_sql_update('tagente', $values, ['id_agente' => $id_agente]);
if ($result === false && $update_custom_result == false) {
@ -1161,59 +1137,6 @@ if ($update_agent) {
}
}
if ($tpolicy_group_old) {
foreach ($tpolicy_group_old as $key => $value) {
$tpolicy_agents_old = db_get_sql(
'SELECT * FROM tpolicy_agents
WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$id_agente
);
if ($tpolicy_agents_old) {
$result2 = db_process_sql_update(
'tpolicy_agents',
['pending_delete' => 1],
[
'id_agent' => $id_agente,
'id_policy' => $value['id_policy'],
]
);
}
}
}
$tpolicy_group = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$grupo
);
if ($tpolicy_group) {
foreach ($tpolicy_group as $key => $value) {
$tpolicy_agents = db_get_sql(
'SELECT * FROM tpolicy_agents
WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$id_agente
);
if (!$tpolicy_agents) {
db_process_sql_insert(
'tpolicy_agents',
[
'id_policy' => $value['id_policy'],
'id_agent' => $id_agente,
]
);
} else {
$result3 = db_process_sql_update(
'tpolicy_agents',
['pending_delete' => 0],
[
'id_agent' => $id_agente,
'id_policy' => $value['id_policy'],
]
);
}
}
}
$info = '{
"id_agente":"'.$id_agente.'",
"alias":"'.$alias.'",

View File

@ -639,20 +639,20 @@ $table->data[5][1] = "
<tr><td>".ui_get_using_system_timezone_warning().'</td></tr>
<tr>
<td>'.__('Type Periodicity:').'&nbsp;'.html_print_select(
[
'weekly' => __('Weekly'),
'monthly' => __('Monthly'),
],
'type_periodicity',
$type_periodicity,
'change_type_periodicity();',
'',
0,
true,
false,
true,
'',
$disabled_in_execution
[
'weekly' => __('Weekly'),
'monthly' => __('Monthly'),
],
'type_periodicity',
$type_periodicity,
'change_type_periodicity();',
'',
0,
true,
false,
true,
'',
$disabled_in_execution
)."</td>
</tr>
<tr>
@ -672,31 +672,31 @@ $table->data[5][1] = "
<tr>
<td>".__('From day:').'</td>
<td>'.html_print_select(
$days,
'periodically_day_from',
$periodically_day_from,
'',
'',
0,
true,
false,
true,
'',
$disabled_in_execution
$days,
'periodically_day_from',
$periodically_day_from,
'',
'',
0,
true,
false,
true,
'',
$disabled_in_execution
).'</td>
<td>'.__('To day:').'</td>
<td>'.html_print_select(
$days,
'periodically_day_to',
$periodically_day_to,
'',
'',
0,
true,
false,
true,
'',
$disabled_in_execution
$days,
'periodically_day_to',
$periodically_day_to,
'',
'',
0,
true,
false,
true,
'',
$disabled_in_execution
).'</td>
<td>'.ui_print_help_tip(__('The end day must be higher than the start day'), true).'</td>
</tr>
@ -705,26 +705,26 @@ $table->data[5][1] = "
<tr>
<td>'.__('From hour:').'</td>
<td>'.html_print_input_text(
'periodically_time_from',
$periodically_time_from,
'',
7,
7,
true,
$disabled_in_execution
'periodically_time_from',
$periodically_time_from,
'',
7,
7,
true,
$disabled_in_execution
).ui_print_help_tip(
__('The end time must be higher than the start time'),
true
).'</td>
<td>'.__('To hour:').'</td>
<td>'.html_print_input_text(
'periodically_time_to',
$periodically_time_to,
'',
7,
7,
true,
$disabled_in_execution
'periodically_time_to',
$periodically_time_to,
'',
7,
7,
true,
$disabled_in_execution
).ui_print_help_tip(
__('The end time must be higher than the start time'),
true
@ -1008,13 +1008,13 @@ $table->data['module'][1] = "
<td class='cell_delete_button' style='text-align: right; width:10%;' id=''>".'<a class="link_delete"
onclick="if(!confirm(\''.__('Are you sure?').'\')) return false;"
href="">'.html_print_image(
'images/cross.png',
true,
[
'border' => '0',
'alt' => __('Delete'),
'class' => 'invert_filter',
]
'images/cross.png',
true,
[
'border' => '0',
'alt' => __('Delete'),
'class' => 'invert_filter',
]
).'</a>'."</td>
</tr>
<tr class='datos2' id='add_modules_row'>

View File

@ -218,14 +218,9 @@ if ($update_agents) {
$old_interval_value = db_get_value_filter('intervalo', 'tagente', ['id_agente' => $id_agent]);
if (!empty($values)) {
$group_old = false;
$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 ($values_old['id_grupo']) {
$group_old = $values_old['id_grupo'];
}
if (isset($values['disabled'])) {
$disabled_old = $values_old['disabled'];
}
@ -274,74 +269,6 @@ if ($update_agents) {
alerts_validate_alert_agent($id_agent);
}
}
if ($group_old || $result) {
if ($group_old && $group_old != null) {
$tpolicy_group_old = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$group_old
);
} else {
$tpolicy_group_old = db_get_all_rows_sql('SELECT id_policy FROM tpolicy_groups');
}
if ($tpolicy_group_old) {
foreach ($tpolicy_group_old as $key => $value) {
$tpolicy_agents_old = db_get_sql(
'SELECT * FROM tpolicy_agents
WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$id_agent
);
if ($tpolicy_agents_old) {
$result2 = db_process_sql_update(
'tpolicy_agents',
['pending_delete' => 1],
[
'id_agent' => $id_agent,
'id_policy' => $value['id_policy'],
]
);
}
}
}
if ($values['id_grupo'] && $values['id_grupo'] != null) {
$tpolicy_group_new = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$values['id_grupo']
);
} else {
$tpolicy_group_new = db_get_all_rows_sql('SELECT id_policy FROM tpolicy_groups');
}
if ($tpolicy_group_new) {
foreach ($tpolicy_group_new as $key => $value) {
$tpolicy_agents_new = db_get_sql(
'SELECT * FROM tpolicy_agents
WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$id_agent
);
if (!$tpolicy_agents_new) {
db_process_sql_insert(
'tpolicy_agents',
[
'id_policy' => $value['id_policy'],
'id_agent' => $id_agent,
]
);
} else {
$result3 = db_process_sql_update(
'tpolicy_agents',
['pending_delete' => 0],
[
'id_agent' => $id_agent,
'id_policy' => $value['id_policy'],
]
);
}
}
}
}
}
$info = [];

View File

@ -4004,23 +4004,23 @@ function print_SLA_list($width, $action, $idItem=null)
],
]
);
if (!empty($services_tmp)
&& $services_tmp != ENTERPRISE_NOT_HOOK
) {
foreach ($services_tmp as $service) {
$check_module_sla = modules_check_agentmodule_exists(
$service['sla_id_module']
);
$check_module_sla_value = modules_check_agentmodule_exists(
$service['sla_value_id_module']
);
if ($check_module_sla
&& $check_module_sla_value
if (!empty($services_tmp)
&& $services_tmp != ENTERPRISE_NOT_HOOK
) {
$services[$service['id']] = $service['name'];
foreach ($services_tmp as $service) {
$check_module_sla = modules_check_agentmodule_exists(
$service['sla_id_module']
);
$check_module_sla_value = modules_check_agentmodule_exists(
$service['sla_value_id_module']
);
if ($check_module_sla
&& $check_module_sla_value
) {
$services[$service['id']] = $service['name'];
}
}
}
}
}
echo '<td class="sla_list_service_col">';
echo html_print_select(

View File

@ -1535,10 +1535,6 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3)
'disabled',
]
);
$tpolicy_group_old = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$values_old['id_grupo']
);
$return = db_process_sql_update(
'tagente',
@ -1577,59 +1573,6 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3)
]
);
}
if ($tpolicy_group_old) {
foreach ($tpolicy_group_old as $key => $value) {
$tpolicy_agents_old = db_get_sql(
'SELECT * FROM tpolicy_agents
WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$id_agent
);
if ($tpolicy_agents_old) {
$result2 = db_process_sql_update(
'tpolicy_agents',
['pending_delete' => 1],
[
'id_agent' => $id_agent,
'id_policy' => $value['id_policy'],
]
);
}
}
}
$tpolicy_group = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$idGroup
);
if ($tpolicy_group) {
foreach ($tpolicy_group as $key => $value) {
$tpolicy_agents = db_get_sql(
'SELECT * FROM tpolicy_agents
WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$id_agent
);
if (!$tpolicy_agents) {
db_process_sql_insert(
'tpolicy_agents',
[
'id_policy' => $value['id_policy'],
'id_agent' => $id_agent,
]
);
} else {
$result3 = db_process_sql_update(
'tpolicy_agents',
['pending_delete' => 0],
[
'id_agent' => $id_agent,
'id_policy' => $value['id_policy'],
]
);
}
}
}
}
returnData(
@ -1829,11 +1772,6 @@ function api_set_update_agent_field($id_agent, $use_agent_alias, $params)
]
);
$tpolicy_group_old = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$values_old['id_grupo']
);
$return = db_process_sql_update(
'tagente',
$fields,
@ -1861,59 +1799,6 @@ function api_set_update_agent_field($id_agent, $use_agent_alias, $params)
]
);
}
if ($tpolicy_group_old) {
foreach ($tpolicy_group_old as $key => $value) {
$tpolicy_agents_old = db_get_sql(
'SELECT * FROM tpolicy_agents
WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$agent
);
if ($tpolicy_agents_old) {
$result2 = db_process_sql_update(
'tpolicy_agents',
['pending_delete' => 1],
[
'id_agent' => $agent,
'id_policy' => $value['id_policy'],
]
);
}
}
}
$tpolicy_group = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$field['id_grupo']
);
if ($tpolicy_group) {
foreach ($tpolicy_group as $key => $value) {
$tpolicy_agents = db_get_sql(
'SELECT * FROM tpolicy_agents
WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$agent
);
if (!$tpolicy_agents) {
db_process_sql_insert(
'tpolicy_agents',
[
'id_policy' => $value['id_policy'],
'id_agent' => $agent,
]
);
} else {
$result3 = db_process_sql_update(
'tpolicy_agents',
['pending_delete' => 0],
[
'id_agent' => $agent,
'id_policy' => $value['id_policy'],
]
);
}
}
}
}
}
@ -2066,23 +1951,6 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3)
agents_add_address($id_agente, $direccion_agente);
}
$tpolicy_group_old = db_get_all_rows_sql(
'SELECT id_policy FROM tpolicy_groups
WHERE id_group = '.$grupo
);
if ($tpolicy_group_old) {
foreach ($tpolicy_group_old as $key => $old_group) {
db_process_sql_insert(
'tpolicy_agents',
[
'id_policy' => $old_group['id_policy'],
'id_agent' => $id_agente,
]
);
}
}
$info = '{"Name":"'.$nombre_agente.'",
"IP":"'.$direccion_agente.'",
"Group":"'.$grupo.'",

View File

@ -557,13 +557,13 @@ $table_ichanges = '<div class="autorefresh_select">
</div>
<div class="autorefresh_select_arrows" style="display:grid">
<a href="javascript:">'.html_print_image(
'images/darrowright_green.png',
true,
[
'id' => 'right_autorefreshlist',
'alt' => __('Push selected pages into autorefresh list'),
'title' => __('Push selected pages into autorefresh list'),
]
'images/darrowright_green.png',
true,
[
'id' => 'right_autorefreshlist',
'alt' => __('Push selected pages into autorefresh list'),
'title' => __('Push selected pages into autorefresh list'),
]
).'</a>
<a href="javascript:">'.html_print_image(
'images/darrowleft_green.png',

View File

@ -2580,6 +2580,21 @@ CREATE TABLE IF NOT EXISTS `tpolicy_groups` (
UNIQUE (`id_policy`, `id_group`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;
-- -----------------------------------------------------
-- Table `tpolicy_group_agents`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tpolicy_group_agents` (
`id` SERIAL,
`id_policy` INT UNSIGNED,
`id_agent` INT UNSIGNED,
`direct` TINYINT UNSIGNED DEFAULT 0,
FOREIGN KEY (`id_policy`) REFERENCES `tpolicies`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ---------------------------------------------------------------------
-- Table `tdashboard`
-- ---------------------------------------------------------------------

View File

@ -5540,6 +5540,9 @@ sub pandora_process_policy_queue ($) {
next;
}
# Refresh policy agents.
enterprise_hook('pandora_apply_policy_groups', [$pa_config, $dbh]);
my $operation = enterprise_hook('get_first_policy_queue', [$dbh]);
next unless (defined ($operation) && $operation ne '');

View File

@ -126,6 +126,7 @@ our @EXPORT = qw(
is_offline
is_empty
is_in_array
array_diff
add_hashes
to_number
clean_blank
@ -868,6 +869,19 @@ sub is_in_array {
return 0;
}
################################################################################
# Check if a value is in an array
################################################################################
sub array_diff($$) {
my ($a, $b) = @_;
my %diff;
@diff{ @{$a} } = @{$a};
delete @diff{ @{$b} };
return keys %diff;
}
################################################################################
# Mix hashses
################################################################################