Adjust some perl & tcl related things in various scripts & utilities.
[dragonfly.git] / share / examples / isdn / contrib / isdnd_acct.pl
CommitLineData
52b73b16 1#!/usr/pkg/bin/perl
984263bc
MD
2#
3#ich habe zwei vielleicht n?tzliche Erweiterungen an isdn_pacct
4#gemacht:
5#
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.
9#
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.
16#
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 :-(
20#
21# $FreeBSD: src/share/examples/isdn/contrib/isdnd_acct.pl,v 1.1.2.1 2001/08/10 14:59:48 obrien Exp $
6e285212 22# $DragonFly: src/share/examples/isdn/contrib/isdnd_acct.pl,v 1.2 2003/06/17 04:36:57 dillon Exp $
984263bc
MD
23#
24#---------------------------------------------------------------------------
25#
26# Copyright (c) 1994, 1996 Hellmuth Michaelis. All rights reserved.
27#
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.
42#
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.
54#
55#---------------------------------------------------------------------------
56#
57# accounting script for the isdn daemon accounting info
58# -----------------------------------------------------
59#
60# last edit-date: [Fri May 25 15:22:26 2001]
61#
62# -hm my first perl program :-)
63# -hm sorting the output
64# -hm adding grand total
65#
66#---------------------------------------------------------------------------
67
68sub wday {
69 local ($y, $m, $d) = @_;
70 local ($nday, @mon);
71
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
74
75 if ($m > 2) {
76 $m -= 3;
77 } else {
78 $m += 9;
79 $y--;
80 }
81 if ($y < 1600) {
82 return -1;
83 }
84 $y -= 1600;
85 $nday = $y * 365 + $mon[$m] + $d +
86 int($y / 4) - int($y / 100) + int($y / 400);
87 ($nday + 2) % 7;
88}
89
90# where the isdnd accounting file resides
91if ($#ARGV == 0) {
92 $ACCT_FILE = $ARGV[0];
93} else {
94 $ACCT_FILE = "/var/log/isdnd.acct";
95}
96
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);
105# }
106# close(IN);
107# }
108
109$RATES_FILE = "/etc/isdn/isdnd.rates";
110if (open(IN, $RATES_FILE)) {
111 while(<IN>) {
112 chop;
113 if (! /^ra0/) {
114 next;
115 }
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;
123 }
124 }
125 }
126 close(IN);
127}
128
129# the charge for a unit, currently 0,12 DM
130$UNIT_PRICE = 0.12;
131
132# open accounting file
133open(IN, $ACCT_FILE) ||
134 die "ERROR, cannot open $ACCT_FILE !\n";
135
136# set first thru flag
137$first = 1;
138
139# process file line by line
140while (<IN>)
141{
142 # remove ( and ) from length and bytecounts
143 tr/()//d;
144
145 # split line into pieces
146 ($from_d, $from_h, $dash, $to_d, $to_h, $name, $units, $secs, $byte)
147 = split(/ /, $_);
148
149 # get starting date
150 if($first)
151 {
152 $from = "$from_d $from_h";
153 $first = 0;
154 }
155
156 # split bytecount
157 ($inb, $outb) = split(/\//, $byte);
158
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;
174 }
175
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;
184}
185
186# close accouting file
187close(IN);
188
189# write header
190print "\n";
191print " ISDN Accounting Report ($from -> $to_d $to_h)\n";
192print " =================================================================\n";
193
194#write the sum for each interface/name
195foreach $n (sort(keys %a_secs))
196{
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;
213
214 foreach $i (keys %p_secs) {
215 ($nam, $secs) = split(/$;/, $i);
216 if ($nam ne $n) {
217 next;
218 }
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;
228 }
229}
230
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";
239
240print "======= ====== ===== ===== ======== ============ ============ ============\n";
241write;
242
243print "\n\n";
244exit;
245
246# top of page header
247format top =
248
249Name charge units calls secs inbytes outbytes bytes
250------- ------ ----- ----- -------- ------------ ------------ ------------
251.
252
253# record template
254format STDOUT =
255@<<<<<< @##.## @#### @#### @####### @########### @########### @###########
256$name, $o_charge, $o_units, $o_calls, $o_secs, $o_inbytes, $o_outbytes, $o_bytes
257.
258
259# EOF