diff --git a/pandora_server/conf/pandora_server.conf.new b/pandora_server/conf/pandora_server.conf.new index 36540cfad9..90aaad6ee7 100644 --- a/pandora_server/conf/pandora_server.conf.new +++ b/pandora_server/conf/pandora_server.conf.new @@ -304,6 +304,11 @@ fsnmp /usr/bin/pandorafsnmp autocreate_group 10 +# If set to 1, new agents will be added to the group specified by autocreate_group (the group specified by the agent will be used as fallback). +# If set to 0, new agents will be added to the group specified by the agent (the group specified by autocreate_group will be used as fallback). + +autocreate_group_force 1 + # Set to 1 if want to autocreate agents with Pandora FMS Data Server, # set to 0 to disable (for security purposes, for example). diff --git a/pandora_server/conf/pandora_server.conf.windows b/pandora_server/conf/pandora_server.conf.windows index c78fbc86c7..4c92108dd2 100644 --- a/pandora_server/conf/pandora_server.conf.windows +++ b/pandora_server/conf/pandora_server.conf.windows @@ -261,6 +261,11 @@ dataserver_threads 2 autocreate_group 10 +# If set to 1, new agents will be added to the group specified by autocreate_group (the group specified by the agent will be used as fallback). +# If set to 0, new agents will be added to the group specified by the agent (the group specified by autocreate_group will be used as fallback). + +autocreate_group_force 1 + # Set to 1 if want to autocreate agents with Pandora FMS Data Server, # set to 0 to disable diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index 1cc667b781..5d1c4b18a7 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -333,6 +333,7 @@ sub pandora_load_config { $pa_config->{"snmpget"} = "/usr/bin/snmpget"; $pa_config->{'autocreate_group'} = -1; + $pa_config->{'autocreate_group_force'} = 1; $pa_config->{'autocreate'} = 1; # max log size (bytes) @@ -811,6 +812,9 @@ sub pandora_load_config { elsif ($parametro =~ m/^autocreate_group\s+([0-9*]*)/i) { $pa_config->{'autocreate_group'}= clean_blank($1); } + elsif ($parametro =~ m/^autocreate_group_force\s+([0-1])/i) { + $pa_config->{'autocreate_group_force'}= clean_blank($1); + } elsif ($parametro =~ m/^discovery_threads\s+([0-9]*)/i) { $pa_config->{'discovery_threads'}= clean_blank($1); } diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 7f9e50acea..db5dd96bf0 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -184,6 +184,7 @@ our @EXPORT = qw( pandora_execute_action pandora_exec_forced_alerts pandora_generate_alerts + pandora_get_agent_group pandora_get_config_value pandora_get_credential pandora_get_module_tags @@ -3190,9 +3191,9 @@ sub pandora_create_agent ($$$$$$$$$$;$$$$$$$$$$) { logger ($pa_config, "Server '$server_name' creating agent '$agent_name' address '$address'.", 10); if (!defined($group_id)) { - $group_id = $pa_config->{'autocreate_group'}; - if (! defined (get_group_name ($dbh, $group_id))) { - logger($pa_config, "Group id $group_id does not exist (check autocreate_group config token)", 3); + $group_id = pandora_get_agent_group($pa_config, $dbh, $agent_name); + if ($group_id <= 0) { + logger($pa_config, "Unable to create agent '" . safe_output($agent_name) . "': No valid group found.", 3); return; } } @@ -3442,6 +3443,47 @@ sub pandora_extended_event($$$$) { ); } +########################################################################## +# Returns a valid group ID to place an agent on success, -1 on error. +########################################################################## +sub pandora_get_agent_group { + my ($pa_config, $dbh, $agent_name, $agent_group, $agent_group_password) = @_; + + my $group_id; + my @groups = $pa_config->{'autocreate_group_force'} == 1 ? ($pa_config->{'autocreate_group'}, $agent_group) : ($agent_group, $pa_config->{'autocreate_group'}); + foreach my $group (@groups) { + next unless defined($group); + + # Does the group exist? + if ($group eq $pa_config->{'autocreate_group'}) { + next if ($group <= 0); + $group_id = $group; + if (!defined(get_group_name ($dbh, $group_id))) { + logger($pa_config, "Group ID " . $group_id . " does not exist.", 10); + next; + } + } else { + next if ($group eq ''); + $group_id = get_group_id ($dbh, $group); + if ($group_id <= 0) { + logger($pa_config, "Group " . $group . " does not exist.", 10); + next; + } + } + + # Check the group password. + my $rc = enterprise_hook('check_group_password', [$dbh, $group_id, $agent_group_password]); + if (defined($rc) && $rc != 1) { + logger($pa_config, "Agent " . safe_output($agent_name) . " did not send a valid password for group ID $group_id.", 10); + next; + } + + return $group_id; + } + + return -1; +} + ########################################################################## =head2 C<< pandora_update_module_on_error (I<$pa_config>, I<$id_agent_module>, I<$dbh>) >> diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm index 2372bae0f2..b0c7e2dcc8 100644 --- a/pandora_server/lib/PandoraFMS/DataServer.pm +++ b/pandora_server/lib/PandoraFMS/DataServer.pm @@ -83,6 +83,13 @@ sub new ($$;$) { push(@XML::Parser::Expat::Encoding_Path, $config->{'enc_dir'}); } + if ($config->{'autocreate_group'} > 0 && !defined(get_group_name ($dbh, $config->{'autocreate_group'}))) { + my $msg = "Group id " . $config->{'autocreate_group'} . " does not exist (check autocreate_group config token)."; + logger($config, $msg, 3); + print_message($config, $msg, 1); + pandora_event ($config, $msg, 0, 0, 0, 0, 0, 'error', 0, $dbh); + } + bless $self, $class; return $self; } @@ -327,33 +334,10 @@ sub process_xml_data ($$$$$) { # Get OS, group and description my $os = pandora_get_os ($dbh, $data->{'os_name'}); - $group_id = $pa_config->{'autocreate_group'}; - if (! defined (get_group_name ($dbh, $group_id))) { - if (defined ($data->{'group_id'}) && $data->{'group_id'} ne '') { - $group_id = $data->{'group_id'}; - if (! defined (get_group_name ($dbh, $group_id))) { - pandora_event ($pa_config, "Unable to create agent '" . safe_output($agent_name) . "': group ID '" . $group_id . "' does not exist.", 0, 0, 0, 0, 0, 'error', 0, $dbh); - logger($pa_config, "Group ID " . $group_id . " does not exist.", 3); - return; - } - } elsif (defined ($data->{'group'}) && $data->{'group'} ne '') { - $group_id = get_group_id ($dbh, $data->{'group'}); - if (! defined (get_group_name ($dbh, $group_id))) { - pandora_event ($pa_config, "Unable to create agent '" . safe_output($agent_name) . "': group '" . safe_output($data->{'group'}) . "' does not exist.", 0, 0, 0, 0, 0, 'error', 0, $dbh); - logger($pa_config, "Group " . $data->{'group'} . " does not exist.", 3); - return; - } - } else { - pandora_event ($pa_config, "Unable to create agent '" . safe_output($agent_name) . "': autocreate_group $group_id does not exist. Edit the server configuration file and change it.", 0, 0, 0, 0, 0, 'error', 0, $dbh); - logger($pa_config, "Group id $group_id does not exist (check autocreate_group config token).", 3); - return; - } - } - - # Check the group password. - my $rc = enterprise_hook('check_group_password', [$dbh, $group_id, $data->{'group_password'}]); - if (defined($rc) && $rc != 1) { - logger($pa_config, "Agent $agent_name did not send a valid password for group id $group_id.", 10); + $group_id = pandora_get_agent_group($pa_config, $dbh, $agent_name, $data->{'group'}, $data->{'group_password'}); + if ($group_id <= 0) { + pandora_event ($pa_config, "Unable to create agent '" . safe_output($agent_name) . "': No valid group found.", 0, 0, 0, 0, 0, 'error', 0, $dbh); + logger($pa_config, "Unable to create agent '" . safe_output($agent_name) . "': No valid group found.", 3); return; }