From 0c02bda5ec2a024726bd4d14e0e0e22a5e5320f9 Mon Sep 17 00:00:00 2001 From: slerena Date: Thu, 15 Oct 2009 11:23:40 +0000 Subject: [PATCH] 2009-10-15 Sancho lerena * lib/PandoraFMS/SNMPServer.pm: Fixed small problem in snmp2agent forwarding and better implementation of server error control. Also solved problem with duplicated traps in SNMP console. * lib/PandoraFMS/Server.pm: better implementation of server error control. * lib/PandoraFMS/Core.pm: SNMP alerts now show the IP in _agent_ macro * lib/PandoraFMS/Tools.pm: Better management of enterprise_hook on opensource version. * util/alert_multicast.pl: Added sample of alert script for render alerts as multicast XML. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2023 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 16 +++ pandora_server/lib/PandoraFMS/Core.pm | 10 +- pandora_server/lib/PandoraFMS/SNMPServer.pm | 141 ++++++++++---------- pandora_server/lib/PandoraFMS/Server.pm | 8 +- pandora_server/lib/PandoraFMS/Tools.pm | 5 +- pandora_server/util/alert_multicast.pl | 33 +++++ 6 files changed, 139 insertions(+), 74 deletions(-) create mode 100644 pandora_server/util/alert_multicast.pl diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 2b2964d78c..e4675ccd49 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,19 @@ +2009-10-15 Sancho lerena + + * lib/PandoraFMS/SNMPServer.pm: Fixed small problem in snmp2agent + forwarding and better implementation of server error control. + Also solved problem with duplicated traps in SNMP console. + + * lib/PandoraFMS/Server.pm: better implementation of server error control. + + * lib/PandoraFMS/Core.pm: SNMP alerts now show the IP in _agent_ macro + + * lib/PandoraFMS/Tools.pm: Better management of enterprise_hook on + opensource version. + + * util/alert_multicast.pl: Added sample of alert script for render + alerts as multicast XML. + 2009-10-13 Sancho Lerena * util/pandora_server: Incremented shutting down grace time to 60 secs diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 8350f6aef8..96da669400 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -443,7 +443,7 @@ sub pandora_execute_action ($$$$$$$$) { _alert_times_fired_ => $alert->{'times_fired'}, _alert_priority_ => $alert->{'priority'}, _module_ => (defined ($module)) ? $module->{'nombre'} : '', - _id_agent_ => (defined ($agent)) ? $module->{'id_agente'} : '', + _id_agent_ => (defined ($module)) ? $module->{'id_agente'} : '', ); @@ -905,13 +905,19 @@ sub pandora_evaluate_snmp_alerts ($$$$$$$$) { 'priority' => $alert->{'priority'}, ); + my %agent = ( + 'nombre' => $trap_agent, + 'direccion' => $trap_agent, + ); + # Execute alert my $action = get_db_single_row ($dbh, 'SELECT * FROM talert_actions, talert_commands WHERE talert_actions.id_alert_command = talert_commands.id AND talert_actions.id = ?', $alert->{'id_alert'}); - pandora_execute_action ($pa_config, '', undef, \%alert, 1, $action, undef, $dbh) if (defined ($action)); + my $trap_rcv_full = $trap_oid . " " . $trap_custom_oid . " " . $trap_custom_value; + pandora_execute_action ($pa_config, $trap_rcv_full, \%agent, \%alert, 1, $action, undef, $dbh) if (defined ($action)); # Generate an event pandora_event ($pa_config, "SNMP alert fired (" . $alert->{'description'} . ")", diff --git a/pandora_server/lib/PandoraFMS/SNMPServer.pm b/pandora_server/lib/PandoraFMS/SNMPServer.pm index 61192d6ba4..b6c51a4124 100644 --- a/pandora_server/lib/PandoraFMS/SNMPServer.pm +++ b/pandora_server/lib/PandoraFMS/SNMPServer.pm @@ -74,90 +74,95 @@ sub pandora_snmptrapd { my $self = shift; my $pa_config = $self->getConfig (); - # Connect to the DB - my $dbh = db_connect ('mysql', $pa_config->{'dbname'}, $pa_config->{'dbhost'}, - 3306, $pa_config->{'dbuser'}, $pa_config->{'dbpass'}); - $self->setDBH ($dbh); + eval { + # Connect to the DB + my $dbh = db_connect ('mysql', $pa_config->{'dbname'}, $pa_config->{'dbhost'}, + 3306, $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; + # 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 = ; - 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); - - my $trap2agent = enterprise_hook('snmp_get_trap2agent', [$dbh]); - - # Main loop - while (1) { - while (my $line = ) { - $last_line++; - $last_size = (stat ($log_file))[7]; - - # Update index file - open INDEXFILE, '>' . $idx_file; - print INDEXFILE $last_line . ' ' . $last_size; + # 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 = ; close INDEXFILE; + ($last_line, $last_size) = split(/\s+/, $idx_data); + } - # Skip Headers - next if ($line =~ m/NET-SNMP/); + my $log_size = (stat ($log_file))[7]; - # Unknown data - next if ($line !~ m/\[\*\*\]/); + # New SNMP log file found + if ($log_size < $last_size) { + unlink ($idx_file); + ($last_line, $last_size) = (0, 0); + } - my ($date, $time, $source, $oid, - $type, $type_desc, $value, $data) = split(/\[\*\*\]/, $line); + # Skip already processed lines + readline SNMPLOGFILE for (1..$last_line); - my $timestamp = $date . ' ' . $time; - $value = limpia_cadena ($value); + my $trap2agent = enterprise_hook('snmp_get_trap2agent', [$dbh]); - my ($custom_oid, $custom_type, $custom_value) = ('', '', $type_desc); + # Main loop + while (1) { + while (my $line = ) { + $last_line++; + $last_size = (stat ($log_file))[7]; + + # Update index file + open INDEXFILE, '>' . $idx_file; + print INDEXFILE $last_line . ' ' . $last_size; + close INDEXFILE; - # Custom OID - if ($type eq '6') { - - #String data - next if ($data =~ m/STRING/) && ($data !~ m/([0-9\.]*)\s\=\s([A-Za-z0-9]*)\:\s\"(.+)\"/); + # Skip Headers + next if ($line =~ m/NET-SNMP/); - next if ($data !~ m/([0-9\.]*)\s\=\s([A-Za-z0-9]*)\:\s(.+)/); + # Unknown data + next if ($line !~ m/\[\*\*\]/); - ($custom_oid, $custom_type, $custom_value) = ($1, $2, $3); - $custom_value = limpia_cadena ($custom_value); - } + my ($date, $time, $source, $oid, + $type, $type_desc, $value, $data) = split(/\[\*\*\]/, $line); - # 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, $dbh]))) { - my $trap_id = db_insert ($dbh, '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); + my $timestamp = $date . ' ' . $time; + $value = limpia_cadena ($value); + + my ($custom_oid, $custom_type, $custom_value) = ('', '', $type_desc); + + # Custom OID + if ($type eq '6') { + + #String data + next if ($data =~ m/STRING/) && ($data !~ m/([0-9\.]*)\s\=\s([A-Za-z0-9]*)\:\s\"(.+)\"/); + + next if ($data !~ m/([0-9\.]*)\s\=\s([A-Za-z0-9]*)\:\s(.+)/); + + ($custom_oid, $custom_type, $custom_value) = ($1, $2, $3); + $custom_value = limpia_cadena ($custom_value); + } + + # 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, $dbh]))) { + my $trap_id = db_insert ($dbh, '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); + + # Evaluate alerts for this trap + pandora_evaluate_snmp_alerts ($pa_config, $trap_id, $source, $oid, $oid, $custom_oid, $custom_value, $dbh); + } - # Evaluate alerts for this trap - pandora_evaluate_snmp_alerts ($pa_config, $trap_id, $source, $oid, $oid, $custom_oid, $custom_value, $dbh); - } - if ($pa_config->{enterprise} == 1){ enterprise_hook ('snmp_trap2agent', [$trap2agent, $pa_config, $source, $oid, $value, $custom_oid, $custom_value, $timestamp, $self->getServerID (), $dbh]); } + + sleep ($pa_config->{'server_threshold'}); } - - sleep ($pa_config->{'server_threshold'}); + }; + + if ($@) { + $self->setErrStr ($@); } } diff --git a/pandora_server/lib/PandoraFMS/Server.pm b/pandora_server/lib/PandoraFMS/Server.pm index c1ef8a5c47..95d44fe15e 100644 --- a/pandora_server/lib/PandoraFMS/Server.pm +++ b/pandora_server/lib/PandoraFMS/Server.pm @@ -241,9 +241,11 @@ sub restartEvent ($$) { my ($self, $msg) = @_; return unless defined ($self->{'_dbh'}); - pandora_event ($self->{'_pa_config'}, $self->{'_pa_config'}->{'servername'} . - $ServerTypes[$self->{'_server_type'}] . " RESTARTING" . (defined ($msg) ? " ($msg)" : ''), - 0, 0, 4, 0, 0, 'system', $self->{'_dbh'}); + eval { + pandora_event ($self->{'_pa_config'}, $self->{'_pa_config'}->{'servername'} . + $ServerTypes[$self->{'_server_type'}] . " RESTARTING" . (defined ($msg) ? " ($msg)" : ''), + 0, 0, 4, 0, 0, 'system', $self->{'_dbh'}); + }; } ######################################################################################## diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 4c19bfdd6c..378647edcd 100644 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -365,7 +365,10 @@ sub enterprise_hook ($$) { my $output = eval { &$func (@args); }; # Check for errors - return undef if ($@); + #return undef if ($@); + + # undef is returned only if the enterprise function was not found + return '' unless defined ($output); return $output; } diff --git a/pandora_server/util/alert_multicast.pl b/pandora_server/util/alert_multicast.pl new file mode 100644 index 0000000000..d4edc354d9 --- /dev/null +++ b/pandora_server/util/alert_multicast.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +# Multicast client +# Copyright (c) 2007 Artica Soluciones Tecnologicas S.L. + +use strict; +use warnings; +use POSIX qw(strftime); +use IO::Socket::Multicast; + +if ($#ARGV != 3) { + print "Usage: $0 \n"; + exit 1; +} + +my $group = $ARGV[0]; +my $port = $ARGV[1]; +my $agent_name = $ARGV[2]; +my $alert_name = $ARGV[3]; + +my $status_report = "\n"; + +$status_report .= "\n"; + +$status_report .= "\n"; + +my $socket = IO::Socket::Multicast->new(Proto => 'udp', + PeerAddr => $group . ':' . $port); +return unless defined ($socket); +$socket->send($status_report); + +# print $status_report; + +