From 58289e73bcab4e2a98b7b4a28e8518eed099a127 Mon Sep 17 00:00:00 2001 From: samucarc Date: Mon, 17 Dec 2018 17:51:30 +0100 Subject: [PATCH 1/2] Added new functionality for CLI to delete policy agents. Former-commit-id: 753f9c47d2a4ffe8e5eb599dbd41d5b851500164 --- pandora_console/include/functions_api.php | 49 +++++++++++++++++++++++ pandora_server/util/pandora_manage.pl | 18 +++++++++ 2 files changed, 67 insertions(+) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 53479ae7ed..f406ddd275 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -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. * diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index ba3799b47f..2ec51b7519 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -195,6 +195,7 @@ sub help_screen{ help_screen_line('--apply_policy', '', 'Force apply a policy'); help_screen_line('--apply_all_policies', '', 'Force apply to all the policies'); help_screen_line('--add_agent_to_policy', ' ', 'Add an agent to a policy'); + help_screen_line('--remove_agent_from_policy', ' ', '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', '', 'Disable all the alerts of a policy'); help_screen_line('--create_policy', ' '); @@ -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(); From a9d3d81757a09a5e3bbbe90edf2beaa7efeca2fe Mon Sep 17 00:00:00 2001 From: samucarc Date: Mon, 17 Dec 2018 17:56:01 +0100 Subject: [PATCH 2/2] Fixed api function Former-commit-id: 9b376941609b04ac13da551e8253d5ed5a8085dc --- pandora_console/include/functions_api.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index f406ddd275..8c4bc6520c 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -6449,7 +6449,16 @@ function api_set_update_snmp_module_policy($id, $thrash1, $other, $thrash3) { array('type' => 'string', 'data' => __('SNMP policy module updated.'))); } - +/** + * Remove an agent from a policy. + * @param $id Id of the policy + * @param $id2 Id of the agent policy + * @param $trash1 + * @param $trash2 + * + * Example: + * api.php?op=set&op2=remove_agent_from_policy&apipass=1234&user=admin&pass=pandora&id=11&id2=2 + */ function api_set_remove_agent_from_policy ($id, $id2, $thrash2, $thrash3) { global $config;