#!/usr/local/bin/perl

# From: "Joaquim Baptista - DI [pxquim]" <px@educom2.fct.unl.pt>
# Subject: statistics from log file

# I have just been cooking this script for myself.  I call it daily and then
# email the result to the system operator (myself ;-), just before rotating
# the logs.

while (<>) {
# print if /send|received/;
  if    (/\[(\d+)\]: calling\s+(\S+)/ ) {
    $call{$1}=$2;
    $att{$2}++;
  }
  elsif (/\[(\d+)\]: chat got "CONNECT"/) {
    $conn{$call{$1}}++;
  }
  elsif (/\[(\d+)\]: chat got "BUSY"/) {
    $busy{$call{$1}}++;
  }
  elsif (/\[(\d+)\]: received (\d+) bytes in (\d+)/) {
    $br{$call{$1}} += $2;
    $sr{$call{$1}} += $3;
  }
  elsif (/\[(\d+)\]: sent (\d+) bytes in (\d+)/) {
    $bs{$call{$1}} += $2;
    $ss{$call{$1}} += $3;
  }
}

print "Trafic by system:\n";
print "Attempts Busy Conn   Time   Received    Sent   System\n";
for $s (sort keys %att) {
  printf("%7d  %3d  %3d %s   %6dKb  %4dKb   %s\n",
         $att{$s}, $busy{$s}, $conn{$s}, &mmss($sr{$s}+$ss{$s}),
          $br{$s}/1024, $bs{$s}/1024, $s);
}
exit 0;

print "Connections:\n";
print "Fidonet trafic received:\n";
for (sort keys %br) {
  printf("%6dKb %s   %s\n", $br{$_}/1024, &mmss($sr{$_}), $_);
}
print "\nFidonet trafic sent:\n";
for (sort keys %bs) {
  printf("%6dKb %s   %s\n", $bs{$_}/1024, &mmss($ss{$_}), $_);
}
exit 0;

sub mmss {
  local($s)= @_;
  sprintf("%4d:%2.2d", $s/60, $s-int($s/60)*60);
}
