Added new functionality for CLI to delete policy agents.

Former-commit-id: 753f9c47d2a4ffe8e5eb599dbd41d5b851500164
This commit is contained in:
samucarc 2018-12-17 17:51:30 +01:00
parent b25686713d
commit 58289e73bc
2 changed files with 67 additions and 0 deletions

View File

@ -32,6 +32,7 @@ include_once($config['homedir'] . "/include/functions_servers.php");
include_once($config['homedir'] . "/include/functions_planned_downtimes.php");
include_once($config['homedir'] . "/include/functions_db.php");
include_once($config['homedir'] . "/include/functions_event_responses.php");
include_once($config['homedir'] . "/include/functions_policies.php");
enterprise_include_once ('include/functions_local_components.php');
enterprise_include_once ('include/functions_events.php');
enterprise_include_once ('include/functions_agents.php');
@ -6448,6 +6449,54 @@ function api_set_update_snmp_module_policy($id, $thrash1, $other, $thrash3) {
array('type' => 'string', 'data' => __('SNMP policy module updated.')));
}
function api_set_remove_agent_from_policy ($id, $id2, $thrash2, $thrash3) {
global $config;
if (!check_acl($config['id_user'], 0, "AW")){
returnError('forbidden', 'string');
return;
}
if ($id == '' || !$id) {
returnError('error_parameter', __('Error deleting agent from policy. Policy cannot be left blank.'));
return;
}
if ($id2 == '' || !$id2) {
returnError('error_parameter', __('Error deleting agent from policy. Agent cannot be left blank.'));
return;
}
$policy = policies_get_policy ($id, false, false);
$agent = db_get_row_filter('tagente', array('id_agente' => $id2));
$policy_agent = db_get_row_filter('tpolicy_agents', array('id_policy' => $id ,'id_agent' => $id2));
if (empty ($policy)){
returnError('error_policy', __('This policy does not exist.'));
return;
}
if (empty ($agent)){
returnError('error_agent', __('This agent does not exist.'));
return;
}
if (empty ($policy_agent)){
returnError('error_policy_agent', __('This agent does not exist in this policy.'));
return;
}
$return = policies_change_delete_pending_agent($policy_agent['id']);
$data = __('Successfully added to delete pending id agent %d to id policy %d.', $id2, $id);
if ($return === false)
returnError('error_delete_policy_agent', 'Could not be deleted id agent %d from id policy %d', $id2, $id);
else
returnData('string', array('type' => 'string', 'data' => $data));
}
/**
* Create a new group. And return the id_group of the new group.
*

View File

@ -195,6 +195,7 @@ sub help_screen{
help_screen_line('--apply_policy', '<policy_name>', 'Force apply a policy');
help_screen_line('--apply_all_policies', '', 'Force apply to all the policies');
help_screen_line('--add_agent_to_policy', '<agent_name> <policy_name>', 'Add an agent to a policy');
help_screen_line('--remove_agent_from_policy', '<policy_id> <agent_id>', 'Delete an agent to a policy');
help_screen_line('--delete_not_policy_modules', '', 'Delete all modules without policy from configuration file');
help_screen_line('--disable_policy_alerts', '<policy_name>', 'Disable all the alerts of a policy');
help_screen_line('--create_policy', '<policy_name> <group_name> <description>');
@ -4101,6 +4102,19 @@ sub cli_policy_add_agent() {
}
}
##############################################################################
# delete an agent to a policy
# Related option: --remove_agent_from_policy
##############################################################################
sub cli_policy_delete_agent() {
my ($policy_id, $agent_id) = @ARGV[2..3];
my $result = api_call(\%conf,'set', 'remove_agent_from_policy', $policy_id, $agent_id);
print "$result \n\n ";
}
sub cli_create_planned_downtime() {
my $name = @ARGV[2];
my @todo = @ARGV[3..21];
@ -5836,6 +5850,10 @@ sub pandora_manage_main ($$$) {
param_check($ltotal, 2);
cli_policy_add_agent();
}
elsif ($param eq '--remove_agent_from_policy') {
param_check($ltotal, 2);
cli_policy_delete_agent();
}
elsif ($param eq '--enable_user') {
param_check($ltotal, 1);
cli_user_enable();