2011-11-15 Ramon Novoa <rnovoa@artica.es>

* pandora_agent: Re-wrote broker agent code (was not working properly).



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5136 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Ramon Novoa 2011-11-15 13:33:16 +00:00
parent 6fd5410ebe
commit 73ac42e4c7
2 changed files with 136 additions and 76 deletions

View File

@ -1,3 +1,7 @@
2011-11-15 Ramon Novoa <rnovoa@artica.es>
* pandora_agent: Re-wrote broker agent code (was not working properly).
2011-09-29 Koichiro Kikuchi <koichiro@rworks.jp> 2011-09-29 Koichiro Kikuchi <koichiro@rworks.jp>
* pandora_agent.redhat.spec: Fixed wrong command paths in %prereq. * pandora_agent.redhat.spec: Fixed wrong command paths in %prereq.

View File

@ -53,7 +53,7 @@ if (!$@) {
} }
use constant AGENT_VERSION => '4.0'; use constant AGENT_VERSION => '4.0';
use constant AGENT_BUILD => '110923'; use constant AGENT_BUILD => '111104';
# Commands to retrieve total memory information in kB # Commands to retrieve total memory information in kB
use constant TOTALMEMORY_CMDS => { use constant TOTALMEMORY_CMDS => {
@ -112,7 +112,9 @@ my $ConfDir = '';
# Pandora FMS agent configuration file # Pandora FMS agent configuration file
my $ConfFile = 'pandora_agent.conf'; my $ConfFile = 'pandora_agent.conf';
my @all_conf;
# Broker agent configuration files
my @BrokerConfFiles;
# Configuration tokens # Configuration tokens
my %Conf = ( my %Conf = (
@ -255,7 +257,8 @@ sub chmodr {
################################################################################ ################################################################################
# Open the agent logfile and start logging. # Open the agent logfile and start logging.
################################################################################ ################################################################################
sub start_log () { sub start_log (;$) {
my $quiet = shift;
# Get the logfile # Get the logfile
my $log_file_name = read_config ('logfile'); my $log_file_name = read_config ('logfile');
@ -263,7 +266,9 @@ sub start_log () {
# Open it # Open it
open ($LogFileFH, "> $log_file_name") or error ("Could not open log file '$log_file_name' for writing: $!."); open ($LogFileFH, "> $log_file_name") or error ("Could not open log file '$log_file_name' for writing: $!.");
print "Logging to $log_file_name\n"; if (! defined ($quiet)) {
print "Logging to $log_file_name\n";
}
} }
################################################################################ ################################################################################
@ -449,23 +454,31 @@ sub parse_conf_modules($) {
} }
################################################################################ ################################################################################
# Create configuration file for drone agents. # Create configuration file for broker agents.
################################################################################ ################################################################################
sub write_broker_conf($$$){ sub write_broker_conf($){
my ($conf_file, $broker_file, $name_agent) = @_; my ($broker_agent) = @_;
my $content = ''; my $content = '';
open (CONF_FILE, "$ConfDir/$ConfFile") or error ("Could not open file '$ConfDir/$ConfFile': $!."); open (CONF_FILE, "$ConfDir/$ConfFile") or error ("Could not open file '$ConfDir/$ConfFile': $!.");
open (BROKER_FILE, ">$broker_file") or error ("Could not write configuration file: $!"); open (BROKER_FILE, ">$ConfDir/${broker_agent}.conf") or error ("Could not write configuration file: $!");
while (my $line = <CONF_FILE>){ while (my $line = <CONF_FILE>){
if ($line =~ /^broker_agent\s+(\w*)\s*/){
# Skip broker definitions
if ($line =~ m/^\s*broker_agent/) {
next; next;
} }
#Change agent_name
if ($line =~ m/^\s*#*\s*agent_name\s+(\w*)\s*/) { # Change the agent name
$line = "agent_name $name_agent"; if ($line =~ m/^\s*#*\s*agent_name\s+/) {
$line = "agent_name $broker_agent\n";
} }
# Change the logfile
elsif ($line =~ m/^\s*logfile\s+(.*)/) {
$line = 'logfile ' . dirname ($1) . "/$broker_agent.log\n";
}
print BROKER_FILE $line; print BROKER_FILE $line;
} }
close (BROKER_FILE); close (BROKER_FILE);
@ -477,6 +490,7 @@ sub write_broker_conf($$$){
################################################################################ ################################################################################
sub read_config (;$) { sub read_config (;$) {
my $token = shift; my $token = shift;
my @found_tokens;
my $module; my $module;
error ("File '$ConfDir/$ConfFile' not found.") unless (-e "$ConfDir/$ConfFile"); error ("File '$ConfDir/$ConfFile' not found.") unless (-e "$ConfDir/$ConfFile");
@ -488,50 +502,57 @@ sub read_config (;$) {
foreach my $line (@file){ foreach my $line (@file){
# Skip comments and empty lines # Skip comments and empty lines
next if ($line =~ m/^\s*#/) or ($line =~ m/^\s*$/); next if ($line =~ m/^\s*#/) or ($line =~ m/^\s*$/);
# Single token search
# Token search
if (defined ($token)) { if (defined ($token)) {
return $2 if ($line =~ /^\s*(\S+)\s+(.*)$/ && $1 eq $token); if ($line =~ /^\s*(\S+)\s+(.*)$/ && $1 eq $token) {
# Multiple value token
if (wantarray ()) {
push (@found_tokens, $2);
}
# Single value token
else {
return $2;
}
}
next; next;
} }
if ($line =~ /^module\s*\w*/){ next if ($line =~ /^module\s*\w*/);
next;
} # Additional configuration file
#Additional configuration file
if ($line =~ /^include\s+(.*)\s*/){ if ($line =~ /^include\s+(.*)\s*/){
open (FILE, "$1") or next; open (FILE, "$1") or next;
my @file_conf = <FILE>; my @file_conf = <FILE>;
parse_conf_modules(\@file_conf); parse_conf_modules(\@file_conf);
close (FILE); close (FILE);
} }
#Broker agent
if ($line =~ /^broker_agent\s+(\w*)\s*/){ #Configuration token
my $path_conf = "$ConfDir/$ConfFile"; if ($line =~ /^\s*(\S+)\s+(.*)$/) {
my $path_broker = "$ConfDir/$1.conf";
my $name_agent = $1;
push (@all_conf, "$1.conf");
unless (-e $path_broker){
write_broker_conf($path_conf, $path_broker, $name_agent);
}
}
#Configuration token
if ($line =~ /^\s*(\S+)\s+(.*)$/) {
log_message ('setup', "$1 is $2"); log_message ('setup', "$1 is $2");
$Conf{$1} = $2; $Conf{$1} = $2;
# Remove trailing spaces # Remove trailing spaces
$Conf{$1} =~ s/\s*$//; $Conf{$1} =~ s/\s*$//;
} }
} }
# Module, plugin and collection definition # Token search
parse_conf_modules(\@file);
# Token not found
if (defined ($token)) { if (defined ($token)) {
# Multiple value token
if (wantarray ()) {
return @found_tokens;
}
# Single value token not found.
return undef; return undef;
} }
# Module, plugin and collection definitions
parse_conf_modules(\@file);
# Update the agent MD5 since agent_name may have changed # Update the agent MD5 since agent_name may have changed
$AgentMD5 = md5 ($Conf{'agent_name'}); $AgentMD5 = md5 ($Conf{'agent_name'});
$RemoteConfFile = "$AgentMD5.conf"; $RemoteConfFile = "$AgentMD5.conf";
@ -710,7 +731,7 @@ sub check_remote_config () {
# Log file may have changed # Log file may have changed
stop_log (); stop_log ();
start_log (); start_log ('quiet');
#Set nice of the pandora_agent #Set nice of the pandora_agent
my $PID = $$; my $PID = $$;
@ -737,7 +758,7 @@ sub launch_tentacle_proxy () {
exec ($new_process); exec ($new_process);
} }
} else { } else {
log_message ('error', 'You can not proxy to localhost'); error ('You can not proxy to localhost');
} }
} }
@ -1574,42 +1595,35 @@ error ("Directory '$ConfDir' does not exist.") unless (-d "$ConfDir");
#Pandora home path #Pandora home path
$ENV{'PANDORA_HOME'}=$ConfDir; $ENV{'PANDORA_HOME'}=$ConfDir;
push (@all_conf, $ConfFile); # Get user to run as
my $pandora_user = read_config ('pandora_user');
foreach my $conf_agent (@all_conf){ if (defined ($pandora_user)) {
# Change the EUID
$ConfFile = $conf_agent; my $pandora_user_uid = getpwnam ($pandora_user);
if (!defined ($pandora_user_uid)) {
# Get user to run as error ("Cannot get uid for user $pandora_user. Does the user exist and can we read /etc/passwd?");
my $pandora_user = read_config ('pandora_user'); }
if (defined ($pandora_user)) { $> = $pandora_user_uid;
# Change the EUID if ($> != $pandora_user_uid) {
my $pandora_user_uid = getpwnam ($pandora_user); error ("Cannot run as $pandora_user: Insufficient permissions.");
if (!defined ($pandora_user_uid)) {
error ("Cannot get uid for user $pandora_user. Does the user exist and can we read /etc/passwd?");
}
$> = $pandora_user_uid;
if ($> != $pandora_user_uid) {
error ("Cannot run as $pandora_user: Insufficient permissions.");
}
} }
# Guess the OS version
$OS_VERSION = guess_os_version ($OS);
# Initialize MD5 variables
md5_init ();
# Start logging
start_log ();
log_message ('log', 'Running as user ' . getpwuid ($>));
# Read configuration file
read_config ();
$ENV{'PANDORA_AGENT'}=$Conf{'agent_name'};
} }
# Guess the OS version
$OS_VERSION = guess_os_version ($OS);
# Initialize MD5 variables
md5_init ();
# Start logging
start_log ();
log_message ('log', 'Running as user ' . getpwuid ($>));
# Read configuration file
read_config ();
$ENV{'PANDORA_AGENT'}=$Conf{'agent_name'};
# Fix directory names # Fix directory names
$Conf{'temporal'} = fix_directory ($Conf{'temporal'}); $Conf{'temporal'} = fix_directory ($Conf{'temporal'});
error ("Temporal directory '" . $Conf{'temporal'} . "' does not exist.") unless (-d "$Conf{'temporal'}"); error ("Temporal directory '" . $Conf{'temporal'} . "' does not exist.") unless (-d "$Conf{'temporal'}");
@ -1631,7 +1645,7 @@ if ($Conf{'proxy_mode'}) {
if ($< != 0) { if ($< != 0) {
launch_tentacle_proxy(); launch_tentacle_proxy();
} else { } else {
log_message ('error', 'Proxy mode can not be launched as root'); error ('Proxy mode can not be launched as root');
} }
} }
@ -1648,6 +1662,9 @@ if ($Conf{'udp_server'} == 1){
} }
} }
# Must be set to 0 if the agent is a broker agent
my $main_agent = -1;
# Loop # Loop
while (1) { while (1) {
@ -1662,6 +1679,41 @@ while (1) {
# Check file collections # Check file collections
check_collections () unless ($Conf{'debug'} eq '1'); check_collections () unless ($Conf{'debug'} eq '1');
# Launch broker agents
my @broker_agents = read_config ('broker_agent');
foreach my $broker_agent (@broker_agents) {
# Create broker conf file if it does not exist
if (! -e "$ConfDir/${broker_agent}.conf") {
write_broker_conf($broker_agent);
}
$main_agent = fork ();
# Broker agent
if ($main_agent == 0) {
# Set the configuration file
$ConfFile = "${broker_agent}.conf";
# Log to a new file
stop_log ();
start_log ('quiet');
# Read configuration file
read_config ();
# Check for a new configuration
check_remote_config () unless ($Conf{'debug'} eq '1');
# Check file collections
check_collections () unless ($Conf{'debug'} eq '1');
# Execute
last;
}
}
my $address; my $address;
if(defined($Conf{'address'})) { if(defined($Conf{'address'})) {
@ -1789,10 +1841,14 @@ while (1) {
$SIG{'INT'} = \&udp_server_signal; $SIG{'INT'} = \&udp_server_signal;
} }
# Go to sleep # Sleep if main agent
# if ($main_agent != 0) {
sleep ($Conf{'interval'});
sleep ($Conf{'interval'}); }
# Finish if broker agent
else {
last;
}
} }