From 2ab45d28b8472e4da69f1593c07d9eb4166885eb Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 21 May 2018 14:42:27 +0200 Subject: [PATCH] Added api_get_agent_id (API) updated plugintools --- pandora_console/include/functions_api.php | 34 +++++++++ pandora_server/lib/PandoraFMS/PluginTools.pm | 74 ++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index a4a737b280..00aaf421f5 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -8821,6 +8821,40 @@ function api_get_agent_name($id_agent, $trash1, $trash2, $returnType) { returnData($returnType, $data); } +/** + * Return the ID or an hash of IDs of the detected given agents + * + * @param array or value $data + * + * +**/ +function api_get_agent_id($trash1, $trash2, $data, $returnType) { + $response; + + if (is_metaconsole()) { + return; + } + if (empty($returnType)) { + $returnType = "json"; + } + + $response = array(); + + if ($data["type"] == "array") { + $response["type"] = "array"; + $response["data"] = array(); + + foreach ($data["data"] as $name) { + $response["data"][$name] = agents_get_agent_id($name, 1); + } + } else { + $response["type"] = "string"; + $response["data"] = agents_get_agent_id($data["data"], 1); + } + + returnData($returnType, $response); +} + /** * Agent alias for a given id * diff --git a/pandora_server/lib/PandoraFMS/PluginTools.pm b/pandora_server/lib/PandoraFMS/PluginTools.pm index 7dd997484e..fe175e8db4 100644 --- a/pandora_server/lib/PandoraFMS/PluginTools.pm +++ b/pandora_server/lib/PandoraFMS/PluginTools.pm @@ -40,6 +40,7 @@ our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( api_available + api_call api_create_custom_field api_create_tag api_create_group @@ -1460,7 +1461,80 @@ sub api_available { id => (empty($rs)?undef:trim($rs)) } } +} +######################################################################################### +# Pandora API call +# apidata->{other} = [field1,field2,...,fieldi,...,fieldn] +######################################################################################### +sub api_call { + my ($conf, $apidata) = @_; + my ($api_url, $api_pass, $api_user, $api_user_pass, + $op, $op2, $other_mode, $other, $return_type); + my $separator; + + if (ref $apidata eq "ARRAY") { + ($api_url, $api_pass, $api_user, $api_user_pass, + $op, $op2, $return_type, $other_mode, $other) = @{$apidata}; + } + if (ref $apidata eq "HASH") { + $api_url = $apidata->{'api_url'}; + $api_pass = $apidata->{'api_pass'}; + $api_user = $apidata->{'api_user'}; + $api_user_pass = $apidata->{'api_user_pass'}; + $op = $apidata->{'op'}; + $op2 = $apidata->{'op2'}; + $return_type = $apidata->{'return_type'}; + $other_mode = "url_encode_separator_" . $apidata->{'url_encode_separator'} unless empty($apidata->{'url_encode_separator'}); + $other_mode = "url_encode_separator_|" if empty($other_mode); + ($separator) = $other_mode =~ /url_encode_separator_(.*)/; + } + + $api_url = $conf->{'api_url'} if empty($api_url); + $api_pass = $conf->{'api_pass'} if empty($api_pass); + $api_user = $conf->{'api_user'} if empty($api_user); + $api_user_pass = $conf->{'api_user_pass'} if empty($api_user_pass); + $op = $conf->{'op'} if empty($op); + $op2 = $conf->{'op2'} if empty($op2); + $return_type = $conf->{'return_type'} if empty($return_type); + $return_type = 'json' if empty($return_type); + if (ref ($apidata->{'other'}) eq "ARRAY") { + $other_mode = "url_encode_separator_|" if empty($other_mode); + ($separator) = $other_mode =~ /url_encode_separator_(.*)/; + + if (empty($separator)) { + $separator = "|"; + $other_mode = "url_encode_separator_|"; + } + + $other = join $separator, @{$apidata->{'other'}}; + } + + my $call; + + $call = $api_url . '?'; + $call .= 'op=' . $op . '&op2=' . $op2; + $call .= '&other_mode=url_encode_separator_' . $separator; + $call .= '&other=' . $other; + $call .= '&apipass=' . $api_pass . '&user=' . $api_user . '&pass=' . $api_user_pass; + $call .= '&return_type=' . $return_type; + + my $rs = call_url($conf, "$call"); + + if (ref($rs) ne "HASH") { + return { + rs => (empty($rs)?1:0), + error => (empty($rs)?"Empty response.":undef), + id => (empty($rs)?undef:trim($rs)), + response => (empty($rs)?undef:$rs), + } + } + else { + return { + rs => 1, + error => $rs->{'error'}, + } + } } #########################################################################################