#!/usr/bin/perl
#---------------------------------------------------------------------------
#
# Copyright (c) 1996, 1998 Hellmuth Michaelis. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#---------------------------------------------------------------------------
#
#	accounting report script for the isdnd daemon accounting info
#	-------------------------------------------------------------
#
#	last edit-date: [Fri May 25 15:28:20 2001]
#
# $FreeBSD: stable/7/share/examples/isdn/contrib/isdnd_acct 77177 2001-05-25 13:25:59Z hm $
#
#---------------------------------------------------------------------------

# where the isdnd accounting file resides
$ACCT_FILE = "/var/log/isdnd.acct";

# the charge for a unit, currently 0,12 DM
$UNIT_PRICE = 0.12;

# open accounting file
open(IN, $ACCT_FILE) ||
	die "ERROR, cannot open $ACCT_FILE !\n";

# set first thru flag
$first = 1;

# process file line by line
while (<IN>)
{
	# remove ( and ) from length and bytecounts
	tr/()//d;

	# split line into pieces
	($from_d, $from_h, $dash, $to_d, $to_h, $name, $units, $secs, $byte)
		= split(/ /, $_);

	# get starting date
	if($first)
	{
		$from = "$from_d $from_h";
		$first = 0;
	}
		
	# split bytecount
	($inb, $outb) = split(/\//, $byte);

	# process fields
	$a_secs{$name} += $secs;
	$a_calls{$name}++;
	$a_units{$name} += $units;
	$a_charge{$name} += $units * $UNIT_PRICE;
	$a_inbytes{$name} += $inb;
	$a_outbytes{$name} += $outb;
	$a_bytes{$name} = $a_bytes{$name} + $inb + $outb;
}

# close accouting file
close(IN);

# write header
print "\n";
print "     ISDN Accounting Report   ($from -> $to_d $to_h)\n";
print "     =====================================================================\n";

#write the sum for each interface/name
foreach $name (sort(keys %a_secs))
{
	$o_secs = $a_secs{$name};
	$gt_secs += $o_secs;
	$o_calls = $a_calls{$name};
	$gt_calls += $o_calls;
	$o_units = $a_units{$name};
	$gt_units += $o_units;
	$o_charge = $a_charge{$name};
	$gt_charge += $o_charge;
	$o_inbytes = $a_inbytes{$name};
	$gt_inbytes += $o_inbytes;
	$o_outbytes = $a_outbytes{$name};
	$gt_outbytes += $o_outbytes;
	$o_bytes = $a_bytes{$name};
	$gt_bytes += $o_bytes;
	write;
}

$o_secs = $gt_secs;
$o_calls = $gt_calls;
$o_units = $gt_units;
$o_charge = $gt_charge;
$o_inbytes = $gt_inbytes;
$o_outbytes = $gt_outbytes;
$o_bytes = $gt_bytes;
$name = "Total";

print "======= ====== ===== ===== ======== ============ ============ ============\n";
write;

print "\n\n";
exit;

# top of page header
format top =

Name    charge units calls     secs      inbytes     outbytes        bytes
------- ------ ----- ----- -------- ------------ ------------ ------------
.

# record template
format STDOUT =
@<<<<<< @##.## @#### @#### @####### @########### @########### @###########
$name,  $o_charge, $o_units, $o_calls, $o_secs, $o_inbytes, $o_outbytes, $o_bytes
.

# EOF
