2009-10-15 Sancho lerena <slernea@artica.es>

* 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
This commit is contained in:
slerena 2009-10-15 11:23:40 +00:00
parent 2a2c5b9a52
commit b56c3d8ef7
6 changed files with 139 additions and 74 deletions

View File

@ -1,3 +1,19 @@
2009-10-15 Sancho lerena <slernea@artica.es>
* 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 <slerena@artica.es>
* util/pandora_server: Incremented shutting down grace time to 60 secs

View File

@ -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'} . ")",

View File

@ -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 = <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);
my $trap2agent = enterprise_hook('snmp_get_trap2agent', [$dbh]);
# Main loop
while (1) {
while (my $line = <SNMPLOGFILE>) {
$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 = <INDEXFILE>;
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 = <SNMPLOGFILE>) {
$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 ($@);
}
}

View File

@ -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'});
};
}
########################################################################################

View File

@ -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;
}

View File

@ -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 <group> <port> <agent_name> <alert_name>\n";
exit 1;
}
my $group = $ARGV[0];
my $port = $ARGV[1];
my $agent_name = $ARGV[2];
my $alert_name = $ARGV[3];
my $status_report = "<status_report>\n";
$status_report .= "<element id='$agent_name' name='$alert_name' status='ALRM' timestamp='" . strftime ("%Y/%m/%d %H:%M:%S", localtime()) . "'></element>\n";
$status_report .= "</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;