An Example link Failover Script ( MPLS to OpenVPN ) With Mail Alerts
Objective:-
To monitor the Link and Change over to alternate link if the link goes down. If it comes back up, Revert the link back.
This Document Assumes. 1) You have a working MPLS/VPN setup,
#!/usr/bin/perl -w
#
#############
########### Written By Manjunath #####
######## http://linuxmanju.com
use Net::SMTP;
my $IFCNF=”/sbin/ifconfig”;
my $OVPN=”/usr/local/sbin/openvpn”;
my $SWALL=”/sbin/shorewall”;
####### Change this to your gateway to monitor ##
my $GW=”10.1.28.105″;
###### Change my.mailserver.com to the Email Server IP ###
my $smtp = Net::SMTP->new(’my.mailserver.com’,Timeout => 30);
######## Change username and password to the SMTP AUth Username and Password
$smtp->auth ( ‘username’, ‘password’ ) or die “Could not authenticate $!”;
############# Change From Address and To address below.
$smtp->mail(’alert@mydomain.com’);
$smtp->to(’linux-report@mydomain.com’);
$smtp->data();
$smtp->datasend(’From: pondi-alert@mydomain.com’);
$smtp->datasend(”\n”);
$smtp->datasend(’To: manjunathkp@mydomain.com’);
$smtp->datasend(”\n”);
sub mplscheck {
if ( system(”ping -c4 $GW > /dev/null”) != 0) {
if ( ! -e “/var/lock/mpls.lck” ) {
system(’logger -p info “Mpls Link is down”‘);
system(”$IFCNF eth2 down”);
sleep 4;
system(”$IFCNF eth2 up”);
system(”$OVPN –config /etc/openvpn/openvpn.conf –daemon”);
system(”$SWALL restart /etc/shorewall.ovpn”);
$smtp->datasend(’Subject: MPLS link Down..’);
$smtp->datasend(”\n \n”);
$smtp->datasend(”\n \n MPLS link is Down in Pondichery.. Unable to reach the gateway..”);
$smtp->dataend();
open LCK, “>/var/lock/mpls.lck” ;
close LCK;
}
}
elsif ( -e “/var/lock/mpls.lck” ) {
system(’logger -p info “Mpls Came back Up”‘);
system(”/bin/kill -SIGTERM openvpn”);
system(’/etc/scripts/connections.sh’);
unlink(”/var/lock/mpls.lck”);
$smtp->datasend(’Subject: MPLS link Came Back up..’);
$smtp->datasend(”\n \n”);
$smtp->datasend(”\n \n MPLS link Came back up.. Able to reach the gateway..”);
}
}
mplscheck();
$smtp->quit;
Copy the script to /usr/bin and chmod +x /usr/bin/myscript.pl
Put the above script in the crontab for every 5 minutes ( Or less/more ) For eg..
*/5 * * * * /usr/bin/myscript.pl
