"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/PandoraFMS/SNMPServer.pm" between
pandorafms_server-7.0NG.755.tar.gz and pandorafms_server-7.0NG.756.tar.gz

About: Pandora FMS (Flexible Monitoring System) offers enterprise IT monitoring for networks, applications, servers and virtual infrastructure. Core server.

SNMPServer.pm  (pandorafms_server-7.0NG.755):SNMPServer.pm  (pandorafms_server-7.0NG.756)
skipping to change at line 48 skipping to change at line 48
use PandoraFMS::Core; use PandoraFMS::Core;
use PandoraFMS::ProducerConsumerServer; use PandoraFMS::ProducerConsumerServer;
# Inherits from PandoraFMS::ProducerConsumerServer # Inherits from PandoraFMS::ProducerConsumerServer
our @ISA = qw(PandoraFMS::ProducerConsumerServer); our @ISA = qw(PandoraFMS::ProducerConsumerServer);
# Global variables # Global variables
my @TaskQueue :shared; my @TaskQueue :shared;
my %PendingTasks :shared; my %PendingTasks :shared;
my $Sem :shared; my $Sem :shared;
my %Sources :shared;
my $SourceSem :shared;
my $TaskSem :shared; my $TaskSem :shared;
# Trap statistics by agent # Trap statistics by agent
my %AGENTS = (); my %AGENTS = ();
# Sources silenced by storm protection. # Sources silenced by storm protection.
my %SILENCEDSOURCES = (); my %SILENCEDSOURCES = ();
# Index and buffer management for trap log files # Index and buffer management for trap log files
my $SNMPTRAPD = { 'log_file' => '', 'fd' => undef, 'idx_file' => '', 'last_lin my $SNMPTRAPD = { 'log_file' => '', 'fd' => undef, 'idx_file' => '', 'last_line
e' => 0, 'last_size' => 0, 'read_ahead_line' => '', 'read_ahead_pos' => 0 }; ' => 0, 'last_size' => 0, 'read_ahead_line' => '', 'read_ahead_pos' => 0 };
my $DATASERVER = { 'log_file' => '', 'fd' => undef, 'idx_file' => '', 'last_lin my $DATASERVER = { 'log_file' => '', 'fd' => undef, 'idx_file' => '', 'last_line
e' => 0, 'last_size' => 0, 'read_ahead_line' => '', 'read_ahead_pos' => 0 }; ' => 0, 'last_size' => 0, 'read_ahead_line' => '', 'read_ahead_pos' => 0 };
my $BUFFER = { 'log_file' => undef, 'fd' => [], 'idx_file' => undef, 'last_l
ine' => 0, 'last_size' => 0, 'read_ahead_line' => undef, 'read_ahead_pos' => 0 }
;
################################################################################ ######## ################################################################################ ########
# SNMP Server class constructor. # SNMP Server class constructor.
################################################################################ ######## ################################################################################ ########
sub new ($$$) { sub new ($$$) {
my ($class, $config, $dbh) = @_; my ($class, $config, $dbh) = @_;
return undef unless $config->{'snmpconsole'} == 1; return undef unless $config->{'snmpconsole'} == 1;
# Start snmptrapd # Start snmptrapd
skipping to change at line 99 skipping to change at line 103
return 1; return 1;
} }
init_log_file($config, $DATASERVER); init_log_file($config, $DATASERVER);
} }
# Initialize semaphores and queues # Initialize semaphores and queues
@TaskQueue = (); @TaskQueue = ();
%PendingTasks = (); %PendingTasks = ();
$Sem = Thread::Semaphore->new; $Sem = Thread::Semaphore->new;
$TaskSem = Thread::Semaphore->new (0); $TaskSem = Thread::Semaphore->new (0);
$SourceSem = Thread::Semaphore->new (1);
# Call the constructor of the parent class # Call the constructor of the parent class
my $self = $class->SUPER::new($config, SNMPCONSOLE, \&PandoraFMS::SNMPSer ver::data_producer, \&PandoraFMS::SNMPServer::data_consumer, $dbh); my $self = $class->SUPER::new($config, SNMPCONSOLE, \&PandoraFMS::SNMPSer ver::data_producer, \&PandoraFMS::SNMPServer::data_consumer, $dbh);
# Save the path of snmptrapd # Save the path of snmptrapd
$self->{'snmp_trapd'} = $config->{'snmp_trapd'}; $self->{'snmp_trapd'} = $config->{'snmp_trapd'};
bless $self, $class; bless $self, $class;
return $self; return $self;
} }
skipping to change at line 122 skipping to change at line 127
############################################################################### ###############################################################################
sub run ($) { sub run ($) {
my $self = shift; my $self = shift;
my $pa_config = $self->getConfig (); my $pa_config = $self->getConfig ();
print_message ($pa_config, " [*] Starting " . $pa_config->{'rb_product_na me'} . " SNMP Console.", 2); print_message ($pa_config, " [*] Starting " . $pa_config->{'rb_product_na me'} . " SNMP Console.", 2);
# Set the initial date for storm protection. # Set the initial date for storm protection.
$pa_config->{"__storm_ref__"} = time(); $pa_config->{"__storm_ref__"} = time();
# Set a server-specific period.
if ($pa_config->{'snmpconsole_threshold'} > 0) {
$self->setPeriod($pa_config->{'snmpconsole_threshold'});
}
$self->setNumThreads ($pa_config->{'snmpconsole_threads'}); $self->setNumThreads ($pa_config->{'snmpconsole_threads'});
$self->SUPER::run (\@TaskQueue, \%PendingTasks, $Sem, $TaskSem); $self->SUPER::run (\@TaskQueue, \%PendingTasks, $Sem, $TaskSem);
} }
############################################################################### ###############################################################################
# Data producer. # Data producer.
############################################################################### ###############################################################################
sub data_producer ($) { sub data_producer ($) {
my $self = shift; my $self = shift;
my ($pa_config, $dbh) = ($self->getConfig (), $self->getDBH ()); my ($pa_config, $dbh) = ($self->getConfig (), $self->getDBH ());
my %tasks_by_source;
my @tasks; my @tasks;
my @buffer;
# Reset storm protection counters # Reset storm protection counters
my $curr_time = time (); my $curr_time = time ();
if ($pa_config->{"__storm_ref__"} + $pa_config->{"snmp_storm_timeout"} < $curr_time) { if ($pa_config->{"__storm_ref__"} + $pa_config->{"snmp_storm_timeout"} < $curr_time) {
$pa_config->{"__storm_ref__"} = $curr_time; $pa_config->{"__storm_ref__"} = $curr_time;
%AGENTS = (); %AGENTS = ();
} }
for my $fs (($SNMPTRAPD, $DATASERVER)) { # Make a local copy of locked sources.
$SourceSem->down ();
my $local_sources = {%Sources};
$SourceSem->up ();
for my $fs (($BUFFER, $SNMPTRAPD, $DATASERVER)) {
next unless defined($fs->{'fd'}); next unless defined($fs->{'fd'});
reset_if_truncated($pa_config, $fs); reset_if_truncated($pa_config, $fs);
while (my $line_with_pos = read_snmplogfile($fs)) { while (my $line_with_pos = read_snmplogfile($fs)) {
my $line; my $line;
$fs->{'last_line'}++; $fs->{'last_line'}++;
($fs->{'last_size'}, $line) = @$line_with_pos; ($fs->{'last_size'}, $line) = @$line_with_pos;
chomp ($line); chomp ($line);
# Update index file # Update index file
open(my $idxfd, '>' . $fs->{'idx_file'}); if (defined($fs->{'idx_file'})) {
print $idxfd $fs->{'last_line'} . ' ' . $fs->{'last_size' open(my $idxfd, '>' . $fs->{'idx_file'});
}; print $idxfd $fs->{'last_line'} . ' ' . $fs->{'la
close $idxfd; st_size'};
set_file_permissions($pa_config, $fs->{'idx_file'}, "0666 close $idxfd;
"); }
# Skip lines other than SNMP Trap logs # Skip lines other than SNMP Trap logs
next unless ($line =~ m/^SNMPv[12]\[\*\*\]/); next unless ($line =~ m/^SNMPv[12]\[\*\*\]/);
# Storm protection. # Storm protection.
my ($ver, $date, $time, $source, $null) = split(/\[\*\*\] /, $line, 5); my ($ver, $date, $time, $source, $null) = split(/\[\*\*\] /, $line, 5);
if ($ver eq "SNMPv2" || $pa_config->{'snmp_pdu_address'} eq '1' ) { if ($ver eq "SNMPv2" || $pa_config->{'snmp_pdu_address'} eq '1' ) {
$source =~ s/(?:(?:TCP|UDP):\s*)?\[?([^] ]+)\]?(? ::-?\d+)?(?:\s*->.*)?$/$1/; $source =~ s/(?:(?:TCP|UDP):\s*)?\[?([^] ]+)\]?(? ::-?\d+)?(?:\s*->.*)?$/$1/;
} }
skipping to change at line 192 skipping to change at line 210
if ($pa_config->{'snmp_storm_protection'} > 0 && $AGENTS{ $source}{'count'} > $pa_config->{'snmp_storm_protection'}) { if ($pa_config->{'snmp_storm_protection'} > 0 && $AGENTS{ $source}{'count'} > $pa_config->{'snmp_storm_protection'}) {
if ($AGENTS{$source}{'event'} == 0) { if ($AGENTS{$source}{'event'} == 0) {
$SILENCEDSOURCES{$source} = $curr_time + $pa_config->{'snmp_storm_silence_period'}; $SILENCEDSOURCES{$source} = $curr_time + $pa_config->{'snmp_storm_silence_period'};
my $silenced_time = ($pa_config->{'snmp_s torm_silence_period'} eq 0 ? $pa_config->{"snmp_storm_timeout"} : $pa_config->{' snmp_storm_silence_period'}); my $silenced_time = ($pa_config->{'snmp_s torm_silence_period'} eq 0 ? $pa_config->{"snmp_storm_timeout"} : $pa_config->{' snmp_storm_silence_period'});
pandora_event ($pa_config, "Too many trap s coming from $source. Silenced for " . $silenced_time . " seconds.", 0, 0, 4, 0 , 0, 'system', 0, $dbh); pandora_event ($pa_config, "Too many trap s coming from $source. Silenced for " . $silenced_time . " seconds.", 0, 0, 4, 0 , 0, 'system', 0, $dbh);
} }
$AGENTS{$source}{'event'} = 1; $AGENTS{$source}{'event'} = 1;
next; next;
} }
push (@tasks, $line); # Either buffer or process the trap.
if (source_lock($pa_config, $source, $local_sources) == 0
) {
push(@buffer, $line);
} else {
push (@tasks, $line);
}
} }
} }
# Save the buffer for the next run.
$BUFFER->{'fd'} = \@buffer;
return @tasks; return @tasks;
} }
############################################################################### ###############################################################################
# Data consumer. # Data consumer.
############################################################################### ###############################################################################
sub data_consumer ($$) { sub data_consumer ($$) {
my ($self, $task) = @_; my ($self, $task) = @_;
my ($pa_config, $server_id, $dbh) = ($self->getConfig(), $self->getServer
ID(), $self->getDBH());
pandora_snmptrapd ($pa_config, $task, $server_id, $dbh);
pandora_snmptrapd ($self->getConfig (), $task, $self->getServerID (), $se # Unlock.
lf->getDBH ()); if ($pa_config->{'snmpconsole_lock'} == 1) {
my ($ver, $date, $time, $source, $null) = split(/\[\*\*\]/, $task
, 5);
if ($ver eq "SNMPv2" || $pa_config->{'snmp_pdu_address'} eq '1' )
{
$source =~ s/(?:(?:TCP|UDP):\s*)?\[?([^] ]+)\]?(?::-?\d+)
?(?:\s*->.*)?$/$1/;
}
source_unlock($pa_config, $source);
}
} }
########################################################################## ##########################################################################
# Process SNMP log file. # Process SNMP log file.
########################################################################## ##########################################################################
sub pandora_snmptrapd { sub pandora_snmptrapd {
my ($pa_config, $line, $server_id, $dbh) = @_; my ($pa_config, $line, $server_id, $dbh) = @_;
(my $trap_ver, $line) = split(/\[\*\*\]/, $line, 2); (my $trap_ver, $line) = split(/\[\*\*\]/, $line, 2);
skipping to change at line 461 skipping to change at line 497
############################################################################### ###############################################################################
# Read SNMP Log file with buffering (to handle multi-line Traps). # Read SNMP Log file with buffering (to handle multi-line Traps).
# Return reference of array (file-pos, line-data) if successful, undef othersise . # Return reference of array (file-pos, line-data) if successful, undef othersise .
############################################################################### ###############################################################################
sub read_snmplogfile($) { sub read_snmplogfile($) {
my ($fs) = @_; my ($fs) = @_;
my $line; my $line;
my $pos; my $pos;
# Reading from a temporary buffer.
if (ref($fs->{'fd'}) eq 'ARRAY') {
if ($#{$fs->{'fd'}} < 0) {
return undef;
}
return [0, shift(@{$fs->{'fd'}})];
}
if(defined($fs->{'read_ahead_line'})) { if(defined($fs->{'read_ahead_line'})) {
# Restore saved line # Restore saved line
$line = $fs->{'read_ahead_line'}; $line = $fs->{'read_ahead_line'};
$pos = $fs->{'read_ahead_pos'}; $pos = $fs->{'read_ahead_pos'};
} }
else { else {
# No saved line # No saved line
my $fd = $fs->{'fd'}; my $fd = $fs->{'fd'};
$line = <$fd>; $line = <$fd>;
$pos = tell($fs->{'fd'}); $pos = tell($fs->{'fd'});
skipping to change at line 540 skipping to change at line 585
# Skip already processed lines # Skip already processed lines
read_snmplogfile($fs) for (1..$fs->{'last_line'}); read_snmplogfile($fs) for (1..$fs->{'last_line'});
} }
############################################################################### ###############################################################################
# Reset the index if the file has been truncated. # Reset the index if the file has been truncated.
############################################################################### ###############################################################################
sub reset_if_truncated($$) { sub reset_if_truncated($$) {
my ($pa_config, $fs) = @_; my ($pa_config, $fs) = @_;
if (!defined($fs->{'log_file'})) {
return;
}
my $log_size = (stat ($fs->{'log_file'}))[7]; my $log_size = (stat ($fs->{'log_file'}))[7];
# New SNMP log file found # New SNMP log file found
if ($log_size < $fs->{'last_size'}) { if ($log_size < $fs->{'last_size'}) {
logger ($pa_config, 'File ' . $fs->{'log_file'} . ' was truncated .', 10); logger ($pa_config, 'File ' . $fs->{'log_file'} . ' was truncated .', 10);
unlink ($fs->{'idx_file'}); unlink ($fs->{'idx_file'});
($fs->{'last_line'}, $fs->{'last_size'}) = (0, 0); ($fs->{'last_line'}, $fs->{'last_size'}) = (0, 0);
seek($fs->{'fd'}, 0, 0); seek($fs->{'fd'}, 0, 0);
} }
} }
##########################################################################
# Get a lock on the given source. Return 1 on success, 0 otherwise.
##########################################################################
sub source_lock($$$) {
my ($pa_config, $source, $local_sources) = @_;
# Locking is disabled.
if ($pa_config->{'snmpconsole_lock'} == 0) {
return 1;
}
if (defined($local_sources->{$source})) {
return 0;
}
$local_sources->{$source} = 1;
$SourceSem->down ();
$Sources{$source} = 1;
$SourceSem->up ();
return 1;
}
##########################################################################
# Remove the lock on the given source.
##########################################################################
sub source_unlock {
my ($pa_config, $source) = @_;
# Locking is disabled.
if ($pa_config->{'snmpconsole_lock'} == 0) {
return;
}
$SourceSem->down ();
delete ($Sources{$source});
$SourceSem->up ();
}
############################################################################### ###############################################################################
# Clean-up when the server is destroyed. # Clean-up when the server is destroyed.
############################################################################### ###############################################################################
sub DESTROY { sub DESTROY {
my $self = shift; my $self = shift;
if ($self->{'snmp_trapd'} ne 'manual') { if ($self->{'snmp_trapd'} ne 'manual') {
my $pid_file = '/var/run/pandora_snmptrapd.pid'; my $pid_file = '/var/run/pandora_snmptrapd.pid';
if (-e $pid_file) { if (-e $pid_file) {
my $pid = `cat $pid_file 2>$DEVNULL`; my $pid = `cat $pid_file 2>$DEVNULL`;
 End of changes. 16 change blocks. 
14 lines changed or deleted 107 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)