#!/usr/bin/perl
use Socket;

# The old getb.pl was not functioning on this box so I just started from
# scratch and adopted some of the old code from getb.pl.
#
# Its a little messy but it works.
#
# miyake at darkwing.uoregon.edu

$path_ip  = "/mnt/storage/oix-route-views/";
$path_dam = "/mnt/storage/oix-route-views-damp/";


# suspecting arp cache timeout and subsequent query failure as cause for
# this failure in collections:
# spawn telnet route-views.routeviews.org
# Trying 128.223.60.103...
# telnet: connect to address 128.223.60.103: No route to host
#
# hack it by massaging the arp into archive's table with ping.  4 pkts should
# be plenty.
$tmp = "/tmp/getbgp.$$";
system(("ping -c 5 route-views.routeviews.org > $tmp 2>1"));
if ($? == -1) {
    print "ping failed to exec: $!\n";
} elsif ($? >> 8) {
    printf("ping exited with value %d\n", $? >> 8);
    system(("/bin/cat $tmp"));
}
unlink($tmp);


&getdate;

$dir_dam=$path_dam.$year.".".$mon;
$filename_dam1=$dir_dam."/oix-damp-snapshot-".$year."-".$mon."-".$mday."-".$hrmin.".dat";
$filename_dam2="oix-damp-snapshot-".$year."-".$mon."-".$mday."-".$hrmin.".dat";

$runexpect_dam="/usr/local/bin/clogin -f /usr/local/etc/.cloginrc ".
	"-c 'show ip bgp summary;show ip bgp dampening dampened-paths' ".
	"route-views.routeviews.org |";

&testdir($dir_dam);
&getbgp($filename_dam1,$filename_dam2,$runexpect_dam,$dir_dam);


&getdate;

$dir_ip=$path_ip.$year.".".$mon;
$filename_ip1=$dir_ip."/oix-full-snapshot-".$year."-".$mon."-".$mday."-".$hrmin.".dat";
$filename_ip2="oix-full-snapshot-".$year."-".$mon."-".$mday."-".$hrmin.".dat";

$runexpect_ip="/usr/local/bin/clogin -f /usr/local/etc/.cloginrc ".
	"-c 'show ip bgp' route-views.routeviews.org |";

&testdir($dir_ip);
&getbgp($filename_ip1,$filename_ip2,$runexpect_ip,$dir_ip);

## For Abiel - 061901 
##   Added file in html htdoc root contain filename of recently
##   completed route-view files.

$http_damp="oix-route-views-damp/".$year.".".$mon."/".$filename_dam2.".bz2";
$http_norm="oix-route-views/".$year.".".$mon."/".$filename_ip2.".bz2";
$files="http://archive.routeviews.org/$http_damp\nhttp://archive.routeviews.org/$http_norm";
$run="echo \"$files\" > /var/www/html/location.txt";
system($run);


sub getbgp {
   my $filename1 = $_[0];
   my $filename2 = $_[1];
   my $runexpect = $_[2];
         my $dir = $_[3];
   open(O,">$filename1") || die("cannot open output file");
   open(EXPECT,$runexpect);
   while(<EXPECT>){
      print O;
   }
   close(EXPECT);
   close(O);

   $md5filename=$filename2.".md5";
   $md5sum="cd $dir;/usr/bin/md5sum ".$filename2." >".$md5filename;
   system($md5sum);

   $bzip="cd $dir;/usr/bin/bzip2 ".$filename2;
   system($bzip);

   $symlink="cd $dir;ln -f -s ".$filename2.".bz2 oix-full-snapshot-latest.dat.bz2" if ($filename1 =~ /oix-full-snapshot/);
   $symlink="cd $dir;ln -f -s ".$filename2.".bz2 oix-damp-snapshot-latest.dat.bz2" if ($filename1 =~ /oix-damp-snapshot/);
   system($symlink);  
}

sub getdate{
   $time=time();
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdat) = localtime($time);
   $mon++;
   $hour="0".$hour if($hour < 10);
   $min="0".$min if($min < 10);
   $mon="0".$mon if($mon < 10);
   $mday="0".$mday if($mday < 10);
   $hrmin=$hour.$min;
   $year=$year+1900;
}

sub testdir {
   my $dir=$_[0];
   if (!-e $dir){
      mkdir $dir || die "Could not create $dir";
   }
}
