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.
#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;
}