2014-07-28 Ramon Novoa <rnovoa@artica.es>

* conf/pandora_server.conf.new,
	  lib/PandoraFMS/Config.pm: Added configuration tokens to disable the
	  translation of enterprise strings and variable bindings (SNMP console).

	* lib/PandoraFMS/SNMPServer.pm: Added thread support to the SNMP console.

	* lib/PandoraFMS/DB.pm: Fixed a warning.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@10359 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Ramon Novoa 2014-07-28 16:24:40 +00:00
parent a271695ba8
commit d91a102537
5 changed files with 293 additions and 242 deletions

View File

@ -1,3 +1,13 @@
2014-07-28 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf.new,
lib/PandoraFMS/Config.pm: Added configuration tokens to disable the
translation of enterprise strings and variable bindings (SNMP console).
* lib/PandoraFMS/SNMPServer.pm: Added thread support to the SNMP console.
* lib/PandoraFMS/DB.pm: Fixed a warning.
2014-07-25 Miguel de Dios <miguel.dedios@artica.es>
* lib/PandoraFMS/GIS.pm, lib/PandoraFMS/DB.pm,

View File

@ -86,6 +86,18 @@ master 1
snmpconsole 0
# snmpconsole_threads: number of SNMP console threads for processing SNMP traps.
snmpconsole_threads 1
# Attempt to translate variable bindings when processing SNMP traps. 1 enabled, 0 disabled. 0 by default. (ENTERPRISE ONLY).
translate_variable_bindings 0
# Attempt to translate enterprise strings when processing SNMP traps. 1 enabled, 0 disabled. 1 by default. (ENTERPRISE ONLY).
translate_enterprise_strings 0
# snmptrapd will ignore authenticationFailure traps if set to 1.
snmp_ignore_authfailure 1

View File

@ -248,6 +248,9 @@ sub pandora_load_config {
$pa_config->{"snmp_pdu_address"} = 0; # 5.0
$pa_config->{"snmp_storm_protection"} = 0; # 5.0
$pa_config->{"snmp_storm_timeout"} = 600; # 5.0
$pa_config->{"snmpconsole_threads"} = 1; # 5.1
$pa_config->{"translate_variable_bindings"} = 0; # 5.1
$pa_config->{"translate_enterprise_strings"} = 1; # 5.1
# Internal MTA for alerts, each server need its own config.
$pa_config->{"mta_address"} = '127.0.0.1'; # Introduced on 2.0
@ -465,6 +468,15 @@ sub pandora_load_config {
elsif ($parametro =~ m/^snmp_storm_timeout\s+(\d+)/i) {
$pa_config->{'snmp_storm_timeout'}= clean_blank($1);
}
elsif ($parametro =~ m/^snmpconsole_threads\s+(\d+)/i) {
$pa_config->{'snmpconsole_threads'}= clean_blank($1);
}
elsif ($parametro =~ m/^translate_variable_bindings\s+([0-1])/i) {
$pa_config->{'translate_variable_bindings'}= clean_blank($1);
}
elsif ($parametro =~ m/^translate_enterprise_strings\s+([0-1])/i) {
$pa_config->{'translate_enterprise_strings'}= clean_blank($1);
}
elsif ($parametro =~ m/^dbengine\s(.*)/i) {
$pa_config->{'dbengine'}= clean_blank($1);
}

View File

@ -884,9 +884,8 @@ sub get_alert_template_name ($$) {
sub db_concat ($$) {
my ($element1, $element2) = @_;
return " concat(" . $element1 . ", ' '," . $element2 . ") " if ($RDBMS eq 'mysql');
return " " . $element1 . " || ' ' || " . $element2 . " " if ($RDBMS eq 'oracle' or $RDBMS eq 'postgresql');
return " concat(" . $element1 . ", ' '," . $element2 . ") ";
}
########################################################################

View File

@ -25,6 +25,7 @@ use threads::shared;
use Thread::Semaphore;
use Time::Local;
use Time::HiRes qw(usleep);
use XML::Simple;
# Default lib dir for RPM and DEB packages
@ -33,21 +34,27 @@ use lib '/usr/lib/perl5';
use PandoraFMS::Tools;
use PandoraFMS::DB;
use PandoraFMS::Core;
use PandoraFMS::Server;
use PandoraFMS::ProducerConsumerServer;
# Inherits from PandoraFMS::Server
our @ISA = qw(PandoraFMS::Server);
# Inherits from PandoraFMS::ProducerConsumerServer
our @ISA = qw(PandoraFMS::ProducerConsumerServer);
# Tells the server to keep running
my $RUN :shared;
# Global variables
my @TaskQueue :shared;
my %PendingTasks :shared;
my $Sem :shared;
my $TaskSem :shared;
# Trap statistics by agent
my %AGENTS = ();
# Index file management
my ($IDX_FILE, $LAST_LINE, $LAST_SIZE) = ();
########################################################################################
# SNMP Server class constructor.
########################################################################################
sub new ($$;$) {
sub new ($$$) {
my ($class, $config, $dbh) = @_;
return undef unless $config->{'snmpconsole'} == 1;
@ -57,15 +64,46 @@ sub new ($$;$) {
return undef;
}
# Wait for the SNMP log file to be available
my $log_file = $config->{'snmp_logfile'};
sleep ($config->{'server_threshold'}) if (! -e $log_file);
if (!open (SNMPLOGFILE, $log_file)) {
logger ($config, ' [E] Could not open the SNMP log file ' . $config->{'snmp_logfile'} . ".", 1);
print_message ($config, ' [E] Could not open the SNMP log file ' . $config->{'snmp_logfile'} . ".", 1);
return 1;
}
# Process index file, if available
($IDX_FILE, $LAST_LINE, $LAST_SIZE) = ($log_file . '.index', 0, 0);
if (-e $IDX_FILE) {
open (INDEXFILE, $IDX_FILE) or return;
my $idx_data = <INDEXFILE>;
close INDEXFILE;
($LAST_LINE, $LAST_SIZE) = split(/\s+/, $idx_data);
}
my $log_size = (stat ($log_file))[7];
# New SNMP log file found
if ($log_size < $LAST_SIZE) {
unlink ($IDX_FILE);
($LAST_LINE, $LAST_SIZE) = (0, 0);
}
# Skip already processed lines
readline SNMPLOGFILE for (1..$LAST_LINE);
# Initialize semaphores and queues
@TaskQueue = ();
%PendingTasks = ();
$Sem = Thread::Semaphore->new;
$TaskSem = Thread::Semaphore->new (0);
# Call the constructor of the parent class
my $self = $class->SUPER::new($config, 2, $dbh);
my $self = $class->SUPER::new($config, 2, \&PandoraFMS::SNMPServer::data_producer, \&PandoraFMS::SNMPServer::data_consumer, $dbh);
# Save the path of snmptrapd
$self->{'snmp_trapd'} = $config->{'snmp_trapd'};
# Run!
$RUN = 1;
bless $self, $class;
return $self;
}
@ -77,72 +115,89 @@ sub run ($) {
my $self = shift;
my $pa_config = $self->getConfig ();
print_message ($pa_config, " [*] Starting Pandora FMS SNMP Console.", 1);
$self->SUPER::run (\&PandoraFMS::SNMPServer::pandora_snmptrapd);
print_message ($pa_config, " [*] Starting Pandora FMS SNMP Console.", 2);
# Set the initial date for storm protection.
$pa_config->{"__storm_ref__"} = time();
# This is the only server that reads from disk instead of from the DB. No need for a higher server threshold.
$pa_config->{'server_threshold'} = 1;
$self->setNumThreads ($pa_config->{'snmpconsole_threads'});
$self->SUPER::run (\@TaskQueue, \%PendingTasks, $Sem, $TaskSem);
}
###############################################################################
# Data producer.
###############################################################################
sub data_producer ($) {
my $self = shift;
my ($pa_config, $dbh) = ($self->getConfig (), $self->getDBH ());
my @tasks;
# Slave server
if ($pa_config->{'pandora_master'} == 0 && get_db_value ($dbh, 'SELECT name FROM tserver WHERE name = ANY(SELECT name FROM tserver WHERE status = 0)') == undef) {
return @tasks;
}
# Reset storm protection counters
my $curr_time = time ();
if ($pa_config->{"__storm_ref__"} + $pa_config->{"snmp_storm_timeout"} < $curr_time) {
$pa_config->{"__storm_ref__"} = $curr_time;
%AGENTS = ();
}
while (my $line = <SNMPLOGFILE>) {
$LAST_LINE++;
$LAST_SIZE = (stat ($pa_config->{'snmp_logfile'}))[7];
chomp ($line);
# Update index file
open INDEXFILE, '>' . $IDX_FILE;
print INDEXFILE $LAST_LINE . ' ' . $LAST_SIZE;
close INDEXFILE;
# Skip lines other than SNMP Trap logs
next unless ($line =~ m/^SNMPv[12]\[\*\*\]/);
# Storm protection.
my ($ver, $date, $time, $source, $null) = split(/\[\*\*\]/, $line, 5);
next unless defined ($source);
if (! defined ($AGENTS{$source})) {
$AGENTS{$source}{'count'} = 1;
$AGENTS{$source}{'event'} = 0;
} else {
$AGENTS{$source}{'count'} += 1;
}
if ($pa_config->{'snmp_storm_protection'} > 0 && $AGENTS{$source}{'count'} > $pa_config->{'snmp_storm_protection'}) {
if ($AGENTS{$source}{'event'} == 0) {
pandora_event ($pa_config, "Too many traps coming from $source. Silenced for " . int ($pa_config->{"snmp_storm_timeout"} / 60) . " minutes.", 0, 0, 4, 0, 0, 'system', 0, $dbh);
}
$AGENTS{$source}{'event'} = 1;
next;
}
push (@tasks, $line);
}
return @tasks;
}
###############################################################################
# Data consumer.
###############################################################################
sub data_consumer ($$) {
my ($self, $task) = @_;
pandora_snmptrapd ($self->getConfig (), $task, $self->getServerID (), $self->getDBH ());
}
##########################################################################
# Process SNMP log file.
##########################################################################
sub pandora_snmptrapd {
my $self = shift;
my $pa_config = $self->getConfig ();
my $dbh;
eval {
# Connect to the DB
$dbh = db_connect ($pa_config->{'dbengine'}, $pa_config->{'dbname'}, $pa_config->{'dbhost'},
$pa_config->{'dbport'}, $pa_config->{'dbuser'}, $pa_config->{'dbpass'});
$self->setDBH ($dbh);
# Wait for the SNMP log file to be available
my $log_file = $pa_config->{'snmp_logfile'};
sleep ($pa_config->{'server_threshold'}) while (! -e $log_file);
open (SNMPLOGFILE, $log_file) or return;
# Process index file, if available
my ($idx_file, $last_line, $last_size) = ($log_file . '.index', 0, 0);
if (-e $idx_file) {
open (INDEXFILE, $idx_file) or return;
my $idx_data = <INDEXFILE>;
close INDEXFILE;
($last_line, $last_size) = split(/\s+/, $idx_data);
}
my $log_size = (stat ($log_file))[7];
# New SNMP log file found
if ($log_size < $last_size) {
unlink ($idx_file);
($last_line, $last_size) = (0, 0);
}
# Skip already processed lines
readline SNMPLOGFILE for (1..$last_line);
# Main loop
my $storm_ref = time ();
while ($RUN == 1) {
# Reset storm protection counters
my $curr_time = time ();
if ($storm_ref + $pa_config->{"snmp_storm_timeout"} < $curr_time) {
$storm_ref = $curr_time;
%AGENTS = ();
}
while (my $line = <SNMPLOGFILE>) {
$last_line++;
$last_size = (stat ($log_file))[7];
chomp ($line);
# Update index file
open INDEXFILE, '>' . $idx_file;
print INDEXFILE $last_line . ' ' . $last_size;
close INDEXFILE;
# Skip lines other than SNMP Trap logs
next unless ($line =~ m/^SNMPv[12]\[\*\*\]/);
my ($pa_config, $line, $server_id, $dbh) = @_;
(my $trap_ver, $line) = split(/\[\*\*\]/, $line, 2);
@ -190,21 +245,6 @@ sub pandora_snmptrapd {
# custom_type, custom_value is not used since 4.0 version, all custom data goes on custom_oid
$custom_oid = $data;
# Storm protection
if (! defined ($AGENTS{$source})) {
$AGENTS{$source}{'count'} = 1;
$AGENTS{$source}{'event'} = 0;
} else {
$AGENTS{$source}{'count'} += 1;
}
if ($pa_config->{'snmp_storm_protection'} > 0 && $AGENTS{$source}{'count'} > $pa_config->{'snmp_storm_protection'}) {
if ($AGENTS{$source}{'event'} == 0) {
pandora_event ($pa_config, "Too many traps coming from $source. Silenced for " . int ($pa_config->{"snmp_storm_timeout"} / 60) . " minutes.", 0, 0, 4, 0, 0, 'system', 0, $dbh);
}
$AGENTS{$source}{'event'} = 1;
next;
}
#Trap forwarding
if ($pa_config->{'snmp_forward_trap'}==1) {
my $trap_data_string = "";
@ -280,7 +320,7 @@ sub pandora_snmptrapd {
}
# Insert the trap into the DB
if (! defined(enterprise_hook ('snmp_insert_trap', [$pa_config, $source, $oid, $type, $value, $custom_oid, $custom_value, $custom_type, $timestamp, $self->getServerID (), $dbh]))) {
if (! defined(enterprise_hook ('snmp_insert_trap', [$pa_config, $source, $oid, $type, $value, $custom_oid, $custom_value, $custom_type, $timestamp, $server_id, $dbh]))) {
my $trap_id = db_insert ($dbh, 'id_trap', 'INSERT INTO ttrap (timestamp, source, oid, type, value, oid_custom, value_custom, type_custom) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
$timestamp, $source, $oid, $type, $value, $custom_oid, $custom_value, $custom_type);
logger ($pa_config, "Received SNMP Trap from $source", 4);
@ -288,31 +328,6 @@ sub pandora_snmptrapd {
# Evaluate alerts for this trap
pandora_evaluate_snmp_alerts ($pa_config, $trap_id, $source, $oid, $type, $oid, $value, $custom_oid, $dbh);
}
}
sleep ($pa_config->{'server_threshold'});
}
};
if ($@) {
$self->setErrStr ($@);
}
db_disconnect ($dbh);
}
########################################################################################
# Stop the server, killing snmptrapd before.
########################################################################################
sub stop () {
my $self = shift;
if ($self->{'snmp_trapd'} ne 'manual') {
system ("kill -9 `cat /var/run/pandora_snmptrapd.pid 2>$DEVNULL`");
unlink ('/var/run/pandora_snmptrapd.pid');
}
$self->SUPER::stop ();
}
########################################################################################
@ -403,7 +418,10 @@ sub start_snmptrapd ($) {
sub DESTROY {
my $self = shift;
$RUN = 0;
if ($self->{'snmp_trapd'} ne 'manual') {
system ("kill -9 `cat /var/run/pandora_snmptrapd.pid 2>$DEVNULL`");
unlink ('/var/run/pandora_snmptrapd.pid');
}
}
1;