Added API and cli function get_agents_id_by_alias

This commit is contained in:
Luis Calvo 2019-10-10 14:25:17 +02:00
parent be2148ff26
commit bf19fbffd3
2 changed files with 69 additions and 0 deletions

View File

@ -13450,6 +13450,40 @@ function api_get_agents_id_name_by_cluster_name($cluster_name, $trash1, $trash2,
}
/**
* Get agents alias, id and server id (if Metaconsole) given agent alias
* matching part of it.
*
* @param string $alias
* @param $trash1
* @param $trash2
* @param string $returnType
* Example:
* api.php?op=get&op2=agents_id_name_by_alias&return_type=json&apipass=1234&user=admin&pass=pandora
*/
function api_get_agents_id_name_by_alias($alias, $trash1, $trash2, $returnType)
{
global $config;
if (is_metaconsole()) {
$all_agents = db_get_all_rows_sql("SELECT alias, id_agente, id_tagente,id_tmetaconsole_setup FROM tmetaconsole_agent WHERE upper(alias) LIKE upper('%$alias%')");
} else {
$all_agents = db_get_all_rows_sql("SELECT alias, id_agente from tagente WHERE upper(alias) LIKE upper('%$alias%')");
}
if ($all_agents !== false) {
$data = [
'type' => 'json',
'data' => $all_agents,
];
returnData('json', $data, JSON_FORCE_OBJECT);
} else {
returnError('error_agents', 'No agents retrieved.');
}
}
function api_get_modules_id_name_by_cluster_id($cluster_id)
{
global $config;

View File

@ -4003,6 +4003,37 @@ sub cli_get_agent_modules() {
}
}
##############################################################################
# Show id, name and id_server of an agent given alias
# Related option: --get_agents_id_name_by_alias
##############################################################################
sub cli_get_agents_id_name_by_alias() {
my $agent_alias = @ARGV[2];
my @agents;
if(is_metaconsole($conf) == 1) {
@agents = get_db_rows($dbh,"SELECT alias, id_agente, id_tagente,id_tmetaconsole_setup FROM tmetaconsole_agent WHERE UPPER(alias) LIKE UPPER(?)","%".$agent_alias."%");
} else {
@agents = get_db_rows($dbh,"SELECT alias, id_agente, id_agente FROM tagente WHERE UPPER(alias) LIKE UPPER(?)", "%".$agent_alias."%");
}
if(scalar(@agents) == 0) {
print "[ERROR] No agents retrieved.\n\n";
}
if(is_metaconsole($conf) == 1) {
print "id_module, alias, id_server\n";
} else {
print "id_module, alias\n";
}
foreach my $agent (@agents) {
print $agent->{'id_agente'}.",".safe_output($agent->{'alias'}).", ".$agent->{'id_tmetaconsole_setup'}."\n";
}
}
sub cli_create_synthetic() {
my $name_module = @ARGV[2];
my $synthetic_type = @ARGV[3];
@ -6285,6 +6316,10 @@ sub pandora_manage_main ($$$) {
param_check($ltotal, 1);
cli_get_agent_modules();
}
elsif ($param eq '--get_agents_id_name_by_alias') {
param_check($ltotal, 1);
cli_get_agents_id_name_by_alias();
}
elsif ($param eq '--get_policy_modules') {
param_check($ltotal, 1);
cli_get_policy_modules();