Forum Moderators: coopster & phranque

Message Too Old, No Replies

         

diogonunes

9:27 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



Hi There,
Iīm new around here... and Iīm having some problem to solve this... Well... I wanna create a script that can identify the trap-sender-IP. How can I do so? Does this information come with the trap? Can I get it just using BER or do I need a PERL function? Please, need help!
Thanks!

adni18

2:02 am on Nov 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$ENV{'REMOTE_ADDR'}

jatar_k

4:27 am on Nov 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld diogonunes,

you might have to clarify exactly what you mean by trap.

diogonunes

11:06 pm on Nov 26, 2004 (gmt 0)

10+ Year Member



Well... Iīll try to explain it better... Iīm developing a network management script. Itīs quite simple actually. Itīs a perl script that uses SNMP_Session library to receive traps. So, to test this Iīve done 2 scripts. Both using SNMP_Session library. One sends traps (event_report) to port 162 (duh! off course) every 10 seconds. The receiver script receives these traps and records the references in a file. Well... I have a third perl script that checks every 30 seconds for the state of the file that changes when receives traps. For example, for each trap received, the receiver script writes into the 'test.txt' file. The 3rd script tests the old state of the 'test.txt' file with the new state. If the file has changed it means that the trap sender is UP.
So far, so good.
Now comes the problem... The trap receiver server will be a manager of some trap senders, not just one. So, to check which one is up and which one is down I have to detect the Senderīs IP number and thatīs my problem.
Iīll receive traps in the same port (162 off course). So, I have to detect the senderīs IP. What do I do?

IanKelley

12:51 am on Nov 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The environment variable adni posted should work.

If you have problems with that have the script read a text file or database entry that contains the IP of the server it's located on (or depending on the server software this may already be assigned to an environment variable) and then send this IP to the trap receiver.

diogonunes

3:22 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



thank you guys, to clear it out for yīall, Iīm posting the scripts here!

#THE REMOTE MACHINE THAT SENDS TRAPS TO INFORM
#THAT ITīS UP!
require 'SNMP_Session.pm';
use BER;
while(1) {
my $trap_receiver = "192.168.1.219";
my $trap_community = "ead";
my $trap_session = $version eq '1'
? SNMP_Session->open ($trap_receiver, $trap_community, 162)
: SNMPv2c_Session->open ($trap_receiver, $trap_community, 162);
my $myIpAddress = "192.168.1.219";
my $start_time = time;
link_down_trap();
sub link_down_trap ($$) {
my ($if_index, $version) = @_;
my $genericTrap = 2; # linkDown
my $specificTrap = 0;
my @ifIndexOID = ( 1,3,6,1,2,1,2,2,1,1 );
my $upTime = int ((time - $start_time) * 100.0);
my @myOID = ( 1,3,6,1,4,1,2946,0,8,15 );

warn "Sending trap failed"
unless ($version eq '1')
? $trap_session->trap_request_send (encode_oid (@myOID),
encode_ip_address ($myIpAddress),
encode_int ($genericTrap),
encode_int ($specificTrap),
encode_timeticks ($upTime),
[encode_oid (@ifIndex_OID,$if_index),
encode_int ($if_index)],
[encode_oid (@ifDescr_OID,$if_index),
encode_string ("ead")])
: $trap_session->v2_trap_request_send (\@linkDown_OID, $upTime,
[encode_oid (@ifIndex_OID,$if_index),
encode_int ($if_index)],
[encode_oid (@ifDescr_OID,$if_index),
encode_string ("ead")]);
}
sleep 10;
}
#END!

#THE SCRIPT THAT RECEIVES TRAPS AND WRITES TO TEST
#FILE.
require 'SNMP_Session.pm';
use BER;
while(1) {
my $trap_session = SNMPv2c_Session->open_trap_session ()
or die "cannot open trap session";
my ($trap, $sender_addr, $sender_port) = $trap_session->receive_trap ()
or die "cannot receive trap";

my ($community, $enterprise, $agent, $generic, $specific, $sysUpTime, $bindings) = $trap_session->decode_trap_request ($trap);

my ($binding, $oid, $value);
while ($bindings ne '') {
($binding,$bindings) = &decode_sequence ($bindings);
($oid, $value) = &decode_by_template ($binding, "%O%@");
open (ARQUIVO, ">>test.txt");
print ARQUIVO BER::pretty_oid ($binding)," => ",pretty_print ($value),"\n";
close(ARQUIVO);

}
};

#THE SCRIPT IN THE TRAP RECEIVER SERVER THAT CHECKS
#IF THE FILE 'TEST.TXT' HAS CHANGED OR NOT.
#!/usr/bin/perl
use Mail::Sendmail;
$file="test.txt";
$lst=(stat($file))[9];
sleep 10;
while (1) {
$last2=(stat($file))[9];
if(!($last2>$last)) {
%mail = ( To => 'diogo.senai@sistemafieg.org.br',
From => 'root@jordaniel.feioso.br',
Message => "O server is down"
);

sendmail(%mail) or die $Mail::Sendmail::error;

print "OK. Log says:\n", $Mail::Sendmail::log;
open(HTML,">/home/www/status.html");
print HTML "<html><head><h1><center>Status</center></h1></head><body><br><h5>Status: </h5><img src=\"down.jpg\"></img></body></html>";
close(HTML);
}
else {
open(HTML,">/home/www/status.html");
print HTML "<html><head><h1><center>Status</center></h1></head><body><br><h5>Status: </h5><img src=\"up.jpg\"></img></body></html>";
close(HTML);
}

$last=$last2;
sleep 30;
}

diogonunes

3:27 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



Guys... them real problem is that Iīm doing this to test link (up or down), but not just over server to server, but also for server to ADSL modem. So, thereīs no way to put a script in the modem, but it supports SNMP. Thatīs why I have to find a way to detect the IP, not to receive it by common information, ok?