Merge SUBDIRS and SUBDIRS3 and their LSYM* versions.
[dragonfly.git] / share / examples / isdn / contrib / isdnd_acct.pl
blob1cd40fca971d665f7c0f8275cdb09c3bb4956c12
1 #!/usr/pkg/bin/perl
3 #ich habe zwei vielleicht n?tzliche Erweiterungen an isdn_pacct
4 #gemacht:
6 # 1) Man kann den Namen der Accounting-Datei angeben. Ich
7 # habe Accounting-Files nach Telekom-Rechnung aufgeteilt
8 # und kann diese so sehr sch?n nachvollziehen.
10 # 2) Die Abrechnung wird nach Einheitenl?ngen aufgelistet.
11 # Leider wird zur Zeit immer Nahzone verwendet (isdnd.rates
12 # wird ausgelesen), und Feiertage stehen als erstes auf
13 # der TODO-Liste. Wenn man dieses Feature durch einen
14 # Switch anschaltet, kann man es sogar unauff?llig in die
15 # Distribution aufnehmen.
17 # Mir hilft diese Abrechnung, an mir zu arbeite und mehr
18 # Tests und Zug?nge nachts durchzuf?hren... Aber die meisten
19 # Einheiten werden immer noch im 90s-Takt verbraucht :-(
21 # $FreeBSD: src/share/examples/isdn/contrib/isdnd_acct.pl,v 1.1.2.1 2001/08/10 14:59:48 obrien Exp $
22 # $DragonFly: src/share/examples/isdn/contrib/isdnd_acct.pl,v 1.2 2003/06/17 04:36:57 dillon Exp $
24 #---------------------------------------------------------------------------
26 # Copyright (c) 1994, 1996 Hellmuth Michaelis. All rights reserved.
28 # Redistribution and use in source and binary forms, with or without
29 # modification, are permitted provided that the following conditions
30 # are met:
31 # 1. Redistributions of source code must retain the above copyright
32 # notice, this list of conditions and the following disclaimer.
33 # 2. Redistributions in binary form must reproduce the above copyright
34 # notice, this list of conditions and the following disclaimer in the
35 # documentation and/or other materials provided with the distribution.
36 # 3. All advertising materials mentioning features or use of this software
37 # must display the following acknowledgement:
38 # This product includes software developed by Hellmuth Michaelis
39 # 4. Neither the name of the author nor the names of any co-contributors
40 # may be used to endorse or promote products derived from this software
41 # without specific prior written permission.
43 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 # SUCH DAMAGE.
55 #---------------------------------------------------------------------------
57 # accounting script for the isdn daemon accounting info
58 # -----------------------------------------------------
60 # last edit-date: [Fri May 25 15:22:26 2001]
62 # -hm my first perl program :-)
63 # -hm sorting the output
64 # -hm adding grand total
66 #---------------------------------------------------------------------------
68 sub wday {
69 local ($y, $m, $d) = @_;
70 local ($nday, @mon);
72 @mon = (0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337);
73 # M A M J J A S O N D J F
75 if ($m > 2) {
76 $m -= 3;
77 } else {
78 $m += 9;
79 $y--;
81 if ($y < 1600) {
82 return -1;
84 $y -= 1600;
85 $nday = $y * 365 + $mon[$m] + $d +
86 int($y / 4) - int($y / 100) + int($y / 400);
87 ($nday + 2) % 7;
90 # where the isdnd accounting file resides
91 if ($#ARGV == 0) {
92 $ACCT_FILE = $ARGV[0];
93 } else {
94 $ACCT_FILE = "/var/log/isdnd.acct";
97 # $PERIOD_FILE = "/usr/local/etc/isdnd.periods";
98 # # read periods that need to be separately listed
99 # if (open(IN, $PERIOD_FILE)) {
100 # while (<IN>) {
101 # chop;
102 # ($start, $end) = split(/ /);
103 # push(@p_start, $start);
104 # push(@p_end, $end);
106 # close(IN);
109 $RATES_FILE = "/etc/isdn/isdnd.rates";
110 if (open(IN, $RATES_FILE)) {
111 while(<IN>) {
112 chop;
113 if (! /^ra0/) {
114 next;
116 ($ra0, $day, $rest) = split(/[ \t]+/, $_, 3);
117 @periods = split(/[ \t]+/, $rest);
118 foreach $period (@periods) {
119 ($h_start, $m_start, $h_end, $m_end, $secs) =
120 $period =~ /(.+)\.(.+)-(.+)\.(.+):(.+)/;
121 for ($h = int($h_start); $h < $h_end; $h++) {
122 $secs{$day, $h} = $secs;
126 close(IN);
129 # the charge for a unit, currently 0,12 DM
130 $UNIT_PRICE = 0.12;
132 # open accounting file
133 open(IN, $ACCT_FILE) ||
134 die "ERROR, cannot open $ACCT_FILE !\n";
136 # set first thru flag
137 $first = 1;
139 # process file line by line
140 while (<IN>)
142 # remove ( and ) from length and bytecounts
143 tr/()//d;
145 # split line into pieces
146 ($from_d, $from_h, $dash, $to_d, $to_h, $name, $units, $secs, $byte)
147 = split(/ /, $_);
149 # get starting date
150 if($first)
152 $from = "$from_d $from_h";
153 $first = 0;
156 # split bytecount
157 ($inb, $outb) = split(/\//, $byte);
159 # if user wants to account time periods, put this to the right
160 # slot(s)
161 ($hour, $minute, $second) = split(/:/, $from_h);
162 ($day, $mon, $year) = split(/\./, $from_d);
163 $day = &wday('19' . $year, $mon, $day);
164 if ($secs{$day, int($hour)}) {
165 $secs = $secs{$day, int($hour)};
166 # process fields
167 $p_secs{$name, $secs} += $secs;
168 $p_calls{$name, $secs}++;
169 $p_units{$name, $secs} += $units;
170 $p_charge{$name, $secs} += $units * $UNIT_PRICE;
171 $p_inbytes{$name, $secs} += $inb;
172 $p_outbytes{$name, $secs} += $outb;
173 $p_bytes{$name, $secs} = $p_bytes{$name, $secs} + $inb + $outb;
176 # process fields
177 $a_secs{$name} += $secs;
178 $a_calls{$name}++;
179 $a_units{$name} += $units;
180 $a_charge{$name} += $units * $UNIT_PRICE;
181 $a_inbytes{$name} += $inb;
182 $a_outbytes{$name} += $outb;
183 $a_bytes{$name} = $a_bytes{$name} + $inb + $outb;
186 # close accouting file
187 close(IN);
189 # write header
190 print "\n";
191 print " ISDN Accounting Report ($from -> $to_d $to_h)\n";
192 print " =================================================================\n";
194 #write the sum for each interface/name
195 foreach $n (sort(keys %a_secs))
197 $o_secs = $a_secs{$n};
198 $gt_secs += $o_secs;
199 $o_calls = $a_calls{$n};
200 $gt_calls += $o_calls;
201 $o_units = $a_units{$n};
202 $gt_units += $o_units;
203 $o_charge = $a_charge{$n};
204 $gt_charge += $o_charge;
205 $o_inbytes = $a_inbytes{$n};
206 $gt_inbytes += $o_inbytes;
207 $o_outbytes = $a_outbytes{$n};
208 $gt_outbytes += $o_outbytes;
209 $o_bytes = $a_bytes{$n};
210 $gt_bytes = $o_bytes;
211 $name = $n;
212 write;
214 foreach $i (keys %p_secs) {
215 ($nam, $secs) = split(/$;/, $i);
216 if ($nam ne $n) {
217 next;
219 $o_secs = $p_secs{$i};
220 $o_calls = $p_calls{$i};
221 $o_units = $p_units{$i};
222 $o_charge = $p_charge{$i};
223 $o_inbytes = $p_inbytes{$i};
224 $o_outbytes = $p_outbytes{$i};
225 $o_bytes = $p_bytes{$i};
226 $name = sprintf(' %5.1fs', $secs / 10);
227 write;
231 $o_secs = $gt_secs;
232 $o_calls = $gt_calls;
233 $o_units = $gt_units;
234 $o_charge = $gt_charge;
235 $o_inbytes = $gt_inbytes;
236 $o_outbytes = $gt_outbytes;
237 $o_bytes = $gt_bytes;
238 $name = "Total";
240 print "======= ====== ===== ===== ======== ============ ============ ============\n";
241 write;
243 print "\n\n";
244 exit;
246 # top of page header
247 format top =
249 Name charge units calls secs inbytes outbytes bytes
250 ------- ------ ----- ----- -------- ------------ ------------ ------------
253 # record template
254 format STDOUT =
255 @<<<<<< @##.## @#### @#### @####### @########### @########### @###########
256 $name, $o_charge, $o_units, $o_calls, $o_secs, $o_inbytes, $o_outbytes, $o_bytes
259 # EOF