#!/opt/bin/perl

# This script contains some code from Lineparser by Dave Anderson
# (angio@aros.net)

# This finds ppl who get dropped < $timeout. for the supplied date

$timeout=1; # minutes
$today=@ARGV[0];

if ($today eq "") { 
    $today=`date +%m/%d/%y`;
    chop $today;
}
print "Scanning for $today..\n";
print "Scan as of " . `date` ;
print "Cutoff at $timeout minutes.\n";

for ($ts=1;$ts<=7;$ts++) {
    open(IN, "/opt/radius/etc/radacct/ts-$ts.sky.net/detail") ||
	die "Could not open file for ts-$ts\n";
    
    $begin_record = 1;
    $end_record = 0;
   
    while (<IN>) {
	chop;
	if (!length($_)) {
	    if ($end_record) {
	      if ( $date eq $today ) {
		($hour,$min,$sec)=split(/:/,$daytime);
		$total{"ts$ts port $port"}++;
		$totaltime{$hour}++;

		if ( $time/60 < $timeout ) { 
		    $drops{"ts$ts port $port"}++;
		    $droptime{"$hour"}++;
		    $dropsattime{"$hour$min"}= sprintf "%s%s-%s ",$dropsattime{"$hour$min"},$ts,$port;
		}
	      }
	    }
	    $end_record = 0;
	    $begin_record = 1;
	    next;
	}
	if ($begin_record && /^[a-zA-Z]/) {
	    ($wday, $month, $mday, $daytime, $year) = split;
	    $month = &convert_month($month);
	    $year =~ s/19//;
	    $date = sprintf("%2.2d/%2.2d/%2.2d",
			    $month, $mday, $year);
	    $begin_record = 0;
	    $end_record = 1;
	    next;
	}
	if ($begin_record) {
	    next;
	}
	if (/User-Name/) { 
	    s/[^\"]*"([^\"]*).*/$1/;
		s/\s+//g;
		$username = sprintf("%-8s", $_);
		next;
	}
	if (/Acct-Status-Type/) {
		if (!/Stop/) {
			$begin_record = $end_record = 0;
			next;
		}
	}
	if (/Client-Port-Id/) {
		s/Client-Port-Id = //;
		$port = sprintf("%2.2d", $_);
		next;
	}
	if (/User-Service-Type/) {
		if (/Framed/) {
			$service = "net";
		} else {
			$service = "log";
		}
		next;
	}
	if (/Framed-Address/) {
		s/Framed-Address = //;
		$host = $_;
	}
	if (/Login-Host/) {
		s/Login-Host = //;
		$host = $_;
	}
	if (/Acct-Session-Time/) {
		s/Acct-Session-Time = //;
		s/\s+//g;
		$time = $_;
		next;
	}
}
close (IN);
}

print "SUMMARY OF PERCENT DROPPED for $today by TIME\n";
@hours=("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24");
foreach (@hours) {
  print "$_:00-$_:59 :";
  if ( $totaltime{$_} == 0 ) {
    print " NO LOGINS"; 
  } else {
    printf (" %2.2d percent of %3.3d ", $droptime{$_} / $totaltime{$_} * 100, $totaltime{$_});
    for ($i=0;$i<$droptime{$_}/$totaltime{$_}*100;$i++) { print "X"; }
  }
  print "\n";
}

print "SUMMARY OF DROPS in 5 minute periods\n";
foreach(@hours) {
  $hour=$_;
  foreach("02","05","08","11","14","17","20","23","26","29","32","35","38","41","44","47","50","53","56","59") {
    $min=$_; $t="$hour$min";
    $a=$dropsattime{$t} . $dropsattime{$t-1} . $dropsattime{$t-2} . $dropsattime{$t+1} . $dropsattime{$t+2};
    $num=split / /,$a;
    if ($num > 1) {
      print "$t +/- 2 min: $a\n";
    }
  }
}

print "SUMMARY OF DROPS for $today by LINE\n";
for ($ts=1;$ts<=7;$ts++) {
   for ($port=1;$port<=30;$port++) {
      $_=sprintf("ts$ts port %2.2d",$port);
      print "$_ "; 
      SWITCH: {
         /ts1 port/        && do { print "intel   "; last SWITCH; };
         /ts2 port/        && do { print "cardinal"; last SWITCH; };
         /ts3 port/        && do { print "usr     "; last SWITCH; };
         /ts4 port/        && do { print "usr     "; last SWITCH; };
         /ts5 port/        && do { print "cardinal"; last SWITCH; };
         /ts6 port/        && do { print "none    "; last SWITCH; };
         /ts7 port/        && do { print "usr     "; last SWITCH; };
         print "unknown ";
      }

      if ( $total{$_} == 0 ) {
         print " NO LOGINS"; 
      } else {
         printf (" %2d percent of %3d ", $drops{$_} / $total{$_} * 100, $total{$_});
         for ($i=0;$i<$drops{$_}/$total{$_}*60;$i++) { print "X"; }
      }
      print "\n";
   } 
}

sub convert_month {
	local($_) = $_[0];
	if ($_ eq "Jan") { "01"; }
	elsif ($_ eq "Feb") { "02"; }
	elsif ($_ eq "Mar") { "03"; }
	elsif ($_ eq "Apr") { "04"; }
	elsif ($_ eq "May") { "05"; }
	elsif ($_ eq "Jun") { "06"; }
	elsif ($_ eq "Jul") { "07"; }
	elsif ($_ eq "Aug") { "08"; }
	elsif ($_ eq "Sep") { "09"; }
	elsif ($_ eq "Oct") { "10"; }
	elsif ($_ eq "Nov") { "11"; }
	elsif ($_ eq "Dec") { "12"; }
	else { "-1"; }
}
