Merge from vendor branch GCC:
[dragonfly.git] / share / examples / isdn / contrib / isdnd_acct
1 #!/usr/bin/perl
2 #---------------------------------------------------------------------------
3 #
4 # Copyright (c) 1996, 1998 Hellmuth Michaelis. All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27 #---------------------------------------------------------------------------
28 #
29 #       accounting report script for the isdnd daemon accounting info
30 #       -------------------------------------------------------------
31 #
32 #       last edit-date: [Fri May 25 15:28:20 2001]
33 #
34 # $FreeBSD: src/share/examples/isdn/contrib/isdnd_acct,v 1.1.2.1 2001/08/10 14:59:48 obrien Exp $
35 # $DragonFly: src/share/examples/isdn/contrib/isdnd_acct,v 1.2 2003/06/17 04:36:57 dillon Exp $
36 #
37 #---------------------------------------------------------------------------
38
39 # where the isdnd accounting file resides
40 $ACCT_FILE = "/var/log/isdnd.acct";
41
42 # the charge for a unit, currently 0,12 DM
43 $UNIT_PRICE = 0.12;
44
45 # open accounting file
46 open(IN, $ACCT_FILE) ||
47         die "ERROR, cannot open $ACCT_FILE !\n";
48
49 # set first thru flag
50 $first = 1;
51
52 # process file line by line
53 while (<IN>)
54 {
55         # remove ( and ) from length and bytecounts
56         tr/()//d;
57
58         # split line into pieces
59         ($from_d, $from_h, $dash, $to_d, $to_h, $name, $units, $secs, $byte)
60                 = split(/ /, $_);
61
62         # get starting date
63         if($first)
64         {
65                 $from = "$from_d $from_h";
66                 $first = 0;
67         }
68                 
69         # split bytecount
70         ($inb, $outb) = split(/\//, $byte);
71
72         # process fields
73         $a_secs{$name} += $secs;
74         $a_calls{$name}++;
75         $a_units{$name} += $units;
76         $a_charge{$name} += $units * $UNIT_PRICE;
77         $a_inbytes{$name} += $inb;
78         $a_outbytes{$name} += $outb;
79         $a_bytes{$name} = $a_bytes{$name} + $inb + $outb;
80 }
81
82 # close accouting file
83 close(IN);
84
85 # write header
86 print "\n";
87 print "     ISDN Accounting Report   ($from -> $to_d $to_h)\n";
88 print "     =====================================================================\n";
89
90 #write the sum for each interface/name
91 foreach $name (sort(keys %a_secs))
92 {
93         $o_secs = $a_secs{$name};
94         $gt_secs += $o_secs;
95         $o_calls = $a_calls{$name};
96         $gt_calls += $o_calls;
97         $o_units = $a_units{$name};
98         $gt_units += $o_units;
99         $o_charge = $a_charge{$name};
100         $gt_charge += $o_charge;
101         $o_inbytes = $a_inbytes{$name};
102         $gt_inbytes += $o_inbytes;
103         $o_outbytes = $a_outbytes{$name};
104         $gt_outbytes += $o_outbytes;
105         $o_bytes = $a_bytes{$name};
106         $gt_bytes += $o_bytes;
107         write;
108 }
109
110 $o_secs = $gt_secs;
111 $o_calls = $gt_calls;
112 $o_units = $gt_units;
113 $o_charge = $gt_charge;
114 $o_inbytes = $gt_inbytes;
115 $o_outbytes = $gt_outbytes;
116 $o_bytes = $gt_bytes;
117 $name = "Total";
118
119 print "======= ====== ===== ===== ======== ============ ============ ============\n";
120 write;
121
122 print "\n\n";
123 exit;
124
125 # top of page header
126 format top =
127
128 Name    charge units calls     secs      inbytes     outbytes        bytes
129 ------- ------ ----- ----- -------- ------------ ------------ ------------
130 .
131
132 # record template
133 format STDOUT =
134 @<<<<<< @##.## @#### @#### @####### @########### @########### @###########
135 $name,  $o_charge, $o_units, $o_calls, $o_secs, $o_inbytes, $o_outbytes, $o_bytes
136 .
137
138 # EOF