#!/usr/bin/perl -w
use strict;
use Time::Local;

# script to summarize and mail yesterday's diald log 
# for monitoring purposes
# by Wookey, for Aleph One Ltd
# v1.3 1999.09.03

my $yesterday = time()-86400;
my @yesterday = localtime ($yesterday);
my $mailto = "user\@your.machine.net";
my $sendmail = "/usr/sbin/sendmail";
my $tmpfile = "/tmp/dialdtoday";

my %months = ("Jan",0,"Feb",1,"Mar",2,"Apr",3, "May",4,"Jun",5,"Jul",6,
           "Aug",7,"Sep",8,"Oct",9,"Nov",10,"Dec",11);

#print ("Time now:$yesterday[4],$yesterday[3],$yesterday[5]\n");

my $montht="Jan";
my $day=0;
my $hour=0;
my $min=0;
my $sec=0;
my $year=0; 

#set counters for summary
my $calls=0;
my $connections=0;
my $onlinetime=0;
my $bytesin=0;
my $bytesout=0;

#now send the mail to the upgrade server
open PIPE, "|$sendmail -t > /dev/null" or die "couldn't run sendmail:$!";
open TMPFILE, ">$tmpfile" or die "couldn't open $tmpfile:$!";

print PIPE <<END or die "problem writing to sendmail";
To: $mailto
From: "Cron script" <postmaster\@localhost>
Subject: Yesterday's diald log


END
#run through file printing all the lines that were yesterday
while (<STDIN>)
{
  if (/^\w* (\w*)\s+(\d*) (\d*):(\d*):(\d*) (\d*) */)  
  {
    $montht=$1;
    $day=$2;
    $hour=$3;
    $min=$4;
    $sec=$5;
    $year=$6;
  }
  #print "$months{$montht},$day,$year\n";
  if ($months{$montht}==$yesterday[4] && $day==$yesterday[3] && $year-1900==$yesterday[5])
  {
      / Calling site / and $calls++;
      / Connected to / and $connections++;
      / Call duration (\d*) / and $onlinetime += $1;
      if (/IP transmitted (\d*) bytes and received (\d*) bytes/)
       { $bytesin += $1; $bytesout += $2} 
      print TMPFILE $_;
  }
 
}
close TMPFILE;
print PIPE "Total Calls: $calls\n";
print PIPE "Failed connects: ",($calls-$connections),"\n";
my @hms = localtime($onlinetime);
print PIPE "Total time: $onlinetime seconds (",$hms[2],"h,",$hms[1],"m,",$hms[0],"s)\n";
printf PIPE "KBytes out: %d,  KBytes in: %d\n\n",($bytesout/1024),($bytesin/1024);

#now copy contents of tempfile onto the end
open TMPFILE, "$tmpfile" or die "couldn't open $tmpfile for reading:$!";
while (<TMPFILE>)
{
  print PIPE;
}

close PIPE or die "error from sendmail";

exit 0;

