Add functions in environment metaconsole. Tiquet: #3119

This commit is contained in:
m-lopez-f 2015-12-29 17:41:39 +01:00
parent 5594685341
commit 66b29cdeae
1 changed files with 211 additions and 12 deletions

View File

@ -115,6 +115,7 @@ sub help_screen{
help_screen_line('--get_planned_downtimes_items', '<name> [<id_group> <type_downtime> <type_execution> <type_periodicity>]', 'Get all items of planned downtimes');
help_screen_line('--set_planned_downtimes_deleted', '<name> ', 'Deleted a planned downtime');
help_screen_line('--get_agent_group', '<agent_name>', 'Get the group name of an agent');
help_screen_line('--get_agent_group_id', '<agent_name>', 'Get the group ID of an agent');
help_screen_line('--get_agent_modules', '<agent_name>', 'Get the modules of an agent');
help_screen_line('--get_agents', '[<group_name> <os_name> <status> <max_modules> <filter_substring> <policy_name>]', "Get \n\t list of agents with optative filter parameters");
help_screen_line('--delete_conf_file', '<agent_name>', 'Delete a local conf of a given agent');
@ -971,10 +972,31 @@ sub cli_delete_agent() {
$agent_name = decode_entities($agent_name);
print_log "[INFO] Deleting agent '$agent_name'\n\n";
my $id_agent = get_agent_id($dbh,$agent_name);
exist_check($id_agent,'agent',$agent_name);
if (is_metaconsole($conf) == 1) {
my $servers = enterprise_hook('get_metaconsole_setup_servers',[$dbh]);
my @servers_id = split(',',$servers);
my @list_servers;
my $list_names_servers;
foreach my $server (@servers_id) {
my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]);
my $id_agent = get_agent_id($dbh_metaconsole,$agent_name);
if ($id_agent == -1) {
next;
}
else {
pandora_delete_agent($dbh_metaconsole,$id_agent,$conf);
}
}
}
else {
pandora_delete_agent($dbh,$id_agent,$conf);
my $id_agent = get_agent_id($dbh,$agent_name);
exist_check($id_agent,'agent',$agent_name);
pandora_delete_agent($dbh,$id_agent,$conf);
}
}
@ -3134,14 +3156,96 @@ sub cli_validate_policy_alerts() {
sub cli_get_agent_group() {
my $agent_name = @ARGV[2];
my $id_agent = get_agent_id($dbh,$agent_name);
exist_check($id_agent,'agent',$agent_name);
my $id_group = get_agent_group ($dbh, $id_agent);
my $group_name = get_group_name ($dbh, $id_group);
if (is_metaconsole($conf) == 1) {
my $agents_groups = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]);
if (scalar(@{$agents_groups}) != 0) {
foreach my $agent (@{$agents_groups}) {
my @test = $agent;
print Dumper $test[0];
my $group_name = get_group_name ($dbh, $agent->{'id_grupo'});
print "Server: $agent->{'server_name'} Agent: $agent->{'nombre'} Name Group: $group_name \n\n";
}
}
else {
my $servers = enterprise_hook('get_metaconsole_setup_servers',[$dbh]);
my @servers_id = split(',',$servers);
my @list_servers;
my $list_names_servers;
foreach my $server (@servers_id) {
my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]);
my $id_agent = get_agent_id($dbh_metaconsole,$agent_name);
if ($id_agent == -1) {
next;
}
else {
my $id_group = get_agent_group ($dbh_metaconsole, $id_agent);
my $group_name = get_group_name ($dbh_metaconsole, $id_group);
my $metaconsole_name = enterprise_hook('get_metaconsole_setup_server_name',[$dbh, $server]);
print "[INFO] Server: $metaconsole_name Agent: $agent_name Name Group: $group_name\n\n";
}
}
}
}
else {
my $id_agent = get_agent_id($dbh,$agent_name);
exist_check($id_agent,'agent',$agent_name);
my $id_group = get_agent_group ($dbh, $id_agent);
my $group_name = get_group_name ($dbh, $id_group);
print $group_name;
}
}
print $group_name;
##############################################################################
# Show the group id where is a given agent
# Related option: --get_agent_group_id
##############################################################################
sub cli_get_agent_group_id() {
my $agent_name = @ARGV[2];
if (is_metaconsole($conf) == 1) {
my $agents_groups = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]);
if (scalar(@{$agents_groups}) != 0) {
foreach my $agent (@{$agents_groups}) {
print "Server: $agent->{'server_name'} Agent: $agent->{'nombre'} ID Group: $agent->{'id_grupo'}\n\n";
}
}
else {
my $servers = enterprise_hook('get_metaconsole_setup_servers',[$dbh]);
my @servers_id = split(',',$servers);
my @list_servers;
my $list_names_servers;
foreach my $server (@servers_id) {
my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]);
my $metaconsole_name = enterprise_hook('get_metaconsole_setup_server_name',[$dbh, $server]);
my $id_agent = get_agent_id($dbh_metaconsole,$agent_name);
if ($id_agent == -1) {
next;
}
else {
my $id_group = get_agent_group ($dbh_metaconsole, $id_agent);
print "Server: $metaconsole_name Agent: $agent_name ID Group: $id_group\n\n";
}
}
}
}
else {
my $id_agent = get_agent_id($dbh,$agent_name);
exist_check($id_agent,'agent',$agent_name);
my $id_group = get_agent_group ($dbh, $id_agent);
print $id_group;
}
}
##############################################################################
@ -3645,12 +3749,99 @@ sub cli_create_group() {
$description = '' unless defined($description);
$group_id = pandora_create_group ($group_name, $icon, $parent_group_id, 0, 0, '', 0, $description, $dbh);
if($group_id == -1) {
print_log "[ERROR] A problem has been ocurred creating group '$group_name'\n\n";
}
else {
if (is_metaconsole($conf) == 1) {
my $servers = enterprise_hook('get_metaconsole_setup_servers',[$dbh]);
my @servers_id = split(',',$servers);
my $count_error = 0;
my $count_success = 0;
foreach my $server (@servers_id) {
my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]);
my $group_id_nodo;
my $group_id = get_group_id($dbh_metaconsole,$group_name);
if ($group_id != -1) {
$count_error++;
next;
}
eval {
$group_id_nodo = db_insert ($dbh_metaconsole, 'id_grupo', 'INSERT INTO tgrupo (id_grupo, nombre, icon, parent, propagate, disabled,
custom_id, id_skin, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $group_name, safe_input($group_name), $icon,
$parent_group_id, 0, 0, '', 0, $description);
};
if ($@) {
print_log "[ERROR] Problems with IDS and doesnt created group\n\n";
$count_error++;
next;
}
if ($group_id_nodo == -1) {
$count_error++;
}
else {
$count_success++;
}
}
print_log "[INFO] Created group success: $count_success error: $count_error\n\n";
}
else {
print_log "[INFO] Created group '$group_name'\n\n";
}
}
}
###############################################################################
# Locate agent in any Nodes of metaconsole
# Related option: --locate_agent
###############################################################################
sub cli_locate_agent () {
my ($agent_name) = @ARGV[2];
if (is_metaconsole($conf) == 1) {
my $agents_server = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]);
if (scalar(@{$agents_server}) != 0) {
foreach my $agent (@{$agents_server}) {
my $server = enterprise_hook('get_metaconsole_setup_server_id',[$dbh, $agent->{'server_name'}]);
print " $server \n\n "
}
}
else {
my $servers = enterprise_hook('get_metaconsole_setup_servers',[$dbh]);
my @servers_id = split(',',$servers);
my @list_servers;
my $list_names_servers;
foreach my $server (@servers_id) {
my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]);
my $agent_id = get_agent_id($dbh_metaconsole,$agent_name);
if ($agent_id == -1) {
next;
}
else {
push @list_servers,$server;
}
}
if (defined(@list_servers)) {
$list_names_servers = join(',',@list_servers);
print_log "[INFO] The agent: $agent_name find in server with IDS: $list_names_servers\n\n";
}
else {
print_log "[ERROR] This agent: $agent_name not find in any node\n\n";
}
}
}
else {
print_log "[INFO] Created group '$group_name'\n\n";
print_log "[ERROR] This functions only working in metaconsole system\n\n";
}
}
@ -4310,6 +4501,10 @@ sub pandora_manage_main ($$$) {
param_check($ltotal, 1);
cli_get_agent_group();
}
elsif ($param eq '--get_agent_group_id') {
param_check($ltotal, 1);
cli_get_agent_group_id();
}
elsif ($param eq '--get_agents_module_current_data') {
param_check($ltotal, 1);
cli_get_agents_module_current_data();
@ -4442,6 +4637,10 @@ sub pandora_manage_main ($$$) {
param_check($ltotal, 1);
cli_set_delete_planned_downtime();
}
elsif ($param eq '--locate_agent') {
param_check($ltotal, 1);
cli_locate_agent();
}
else {
print_log "[ERROR] Invalid option '$param'.\n\n";
$param = '';