FBB Statistics, old version

Submitted by Thomas Nilsson on

#!/usr/bin/perl -T
#
# Script to generate some statistics from FBB log-files
# If called without week current week is listed
# If called with week number that week is listed
# If called with negative number that week before current
# week is listed
#
# I take no responsibility for the usage. Use it at your
# own risk!
#
# v 0.7 040206 /Thomas, SM0KBD
#
# Changes since 0.6
#
# Added support for "M>" & "M<" records
#
# Modified by G0LGS (Stewart Wilkinson) 19 Jan 2004
# 1. To use FBB Week Calculation Code
# 2. Sort Output by Callsign
# 3. Wrapping correctly at new year if negative offset
#
# Changes since 0.5
# Fixed so that also the nine first weeks of the year works
# (It still doesn wrap around new year correctly though)
# Changes since 0.4
# Some beautifying
# Changes since 0.3
# Get the fbb configuration info from fbb configfile
# (Thanks Tomi, OH2BNS, for hinting me!)
# Tested with -w and fixed some, but not all :-), warnings
# Changes since 0.2
# Updated file handling to work with older perl versions
# (Thanks Paul, G4APL for pointing this out)
#
# GNU general licence applies
#

#
# Full security (Tainted data)
#
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Get fbb configinfo
#
if (`which fbbgetconf 2> /dev/null` eq "") {
print "Have you installed FBB?", "\n";
exit 1;
}
$fbb_log_name = `fbbgetconf data`;
chomp $fbb_log_name;
#
# Which BBS?
#
$fbb_call = `fbbgetconf call`;
chomp $fbb_call;
($fbb_call, $reminder) = split /\./, $fbb_call, 2;

#
# Find the week...
#
if ( length($ARGV[0]) == 0) {
&WeekNo();

} else {
if ( $ARGV[0] > 0) {
$week = $ARGV[0];
}
else {
&WeekNo();
$week = $week + $ARGV[0];
if( $week <= 0) {
$week += 52;
}
}
}

if ($week < 10) {
$week = "0" . $week;
}

#
# Can the log file be opened?
#

$fbb_log_name = $fbb_log_name . "/log/fbblog." . $week;

if ( !( open (FBB_LOG, "< $fbb_log_name" ))) {

print ("Can't open logfile for week ", $week, "\n");
exit 2;
}

#
# Work through the log-file
#

while () {

@_ = split();

TYPE_OF_LOG: {

if (index($_[0],"C",12) == 12) {
#
# A connection was opened
#
$_[1] = uc $_[1];
$calls{$_[1]} = $calls{$_[1]} + 1;
$calls_last{$_[1]} = substr($_[0],0,8);
$bbs_open{substr($_[0],10,2)} = $_[1];
last TYPE_OF_LOG;
}

if ( (index($_[0],"MF",12) == 12) ||
(index($_[0],"M>",12) == 12) ) {
#
# Some data was forwarded
#
$fwd_to = uc substr($_[2],2);
$bytes_fwd = substr($_[3],1,length($_[3])-2);
$bytes_to_bbs{$fwd_to} = $bytes_to_bbs{$fwd_to} +
$bytes_fwd;
$times_to_bbs{$fwd_to} = $times_to_bbs{$fwd_to} + 1;
last TYPE_OF_LOG;
}

if ( (index($_[0],"MW",12) == 12) ||
(index($_[0],"M<",12) == 12) ) {
#
# Some data was received
#
$ch = substr($_[0],10,2);
foreach $token ( @_ ) {
if ( index($token,"[") > -1 ) {
$bytes_rec = substr($token,
index($token,"[")+1,
length($token) - 2);
last;
}
}
$bytes_from_bbs{$bbs_open{$ch}} =
$bytes_from_bbs{$bbs_open{$ch}} + $bytes_rec;
$times_from_bbs{$bbs_open{$ch}} =
$times_from_bbs{$bbs_open{$ch}} + 1;
last TYPE_OF_LOG;
}

if (index($_[0],"X",12) == 12) {
#
# A connection was closed
#
# $bbs_open{substr($_[0],10,2)} = "";
undef $bbs_open{substr($_[0],10,2)};
last TYPE_OF_LOG;
}
}
}

print ("FBB user statistics at $fbb_call for week $week\n\n");
print ("Station Connected # of times Latest (Z-time)\n");

for $call (sort( keys %calls)) {
printf "%-10s %19i %4s/%2s-%2s:%2s\n", $call,
$calls{$call},
substr($calls_last{$call},2,2),
substr($calls_last{$call},0,2),
substr($calls_last{$call},4,2),
substr($calls_last{$call},6,2);
}

print "\n\n";

print ("Received at $fbb_call from BBS's during week $week\n\n");
print ("BBS # of received P/B Amount received\n");
for $bbs_in (sort( keys %bytes_from_bbs)) {
printf "%-10s %17i %16i\n",
$bbs_in,
$times_from_bbs{$bbs_in},
$bytes_from_bbs{$bbs_in};
}

print "\n\n";

print ("Forwarded at $fbb_call to BBS's during week $week\n\n");
print ("BBS # of sent P/B Amount forwarded\n");
for $bbs (sort( keys %bytes_to_bbs)) {
printf "%-10s %13i %17i\n",
$bbs,
$times_to_bbs{$bbs},
$bytes_to_bbs{$bbs};
}

exit;

# PERL Equivalent of FBB Code for calculating week number.

sub WeekNo
{
$secs = time;
$nw=(localtime($secs))[6];
$ny=(localtime($secs))[7];

if ($nw == 0) {
$nw = 6;
}else{
--$nw;
}

if ($ny < $nw)
{
$secs -= (3600 * 24 * ($ny + 1));
$nw=(localtime($secs))[6];
$ny=(localtime($secs))[7];

if ($nw == 0){
$nw = 6;
}else{
--$nw;
}

}

$week = int((7 - $nw + $ny) / 7);
}

Attachment Size
fbb_statistics.txt 4.64 KB