File: fwctlacctreport

package info (click to toggle)
fwctl 0.28-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,008 kB
  • ctags: 326
  • sloc: perl: 5,060; sh: 61; makefile: 57
file content (469 lines) | stat: -rw-r--r-- 11,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/perl
#
#    fwctlacctreport:   Generates text report from the accounting logs.
#
#    This file is part of Fwctl.
#
#    Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
#
#    Copyright (C) 2000 iNsu Innovations Inc.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#

use strict;

use Fwctl::AcctReport;
use Getopt::Long;

use constant GIG => 1024 ** 3;
use constant MEG => 1024 ** 2;
use constant K   => 1024;

use constant COLS   => 72;

use POSIX qw( strftime );

sub usage(;$) {
print <<EOFU;
usage: fwctlacctreport [--start report_start]
		       [--end report_end | --period report_period ]
		       [--sample day | hour ]
		       [--help]
		       [--names chains ... ]
		       [--report reports ... ]
		       fwctl_acct
EOFU
    exit defined $_[0] ? $_[0] : 1;
}

sub packets_count {
        my $packets = shift;

	my ( $div, $units );
	if ( $packets > 1_000_000 ) {
	    $div = 1_000_000;
	    $units = "M";
	} elsif ($packets > 1_000 ) {
	    $div = 1_000;
	    $units = "k";
	} else {
	    $div = 1;
	    $units = "";
	}
	my ( $quot, $rem ) = $packets->bdiv( $div );

	$quot = substr( "$quot", 1 );
	$rem  = substr( "$rem", 1, 1 );
	return $rem == 0 ? $quot . $units : $quot . "." . $rem . $units;
}

sub bytes_count {
        my $bytes = shift;

	my ( $div, $units );
	if ( $bytes > GIG ) {
	    $div = GIG;
	    $units = "G";
	} elsif ($bytes > MEG ) {
	    $div = MEG;
	    $units = "M";
	} elsif ( $bytes > K ) {
	    $div = K;
	    $units = "k";
	} else {
	    $div = 1;
	    $units = "";
	}
	my ( $quot, $rem ) = $bytes->bdiv( $div );

	$quot = substr( "$quot", 1 );
	$rem  = substr( "$rem", 1, 1 );
	return $rem == 0 ? $quot . $units : $quot . "." . $rem . $units;
}

sub centerf {
    my ($cols, $format, @args ) = @_;
    my $s = sprintf $format, @args;

    my $spacing = int( ($cols - length $s) / 2 );
    return $s . "\n"  if $spacing <= 0;
    return " " x $spacing . $s . " " x $spacing . "\n";
}

sub center {
    centerf( $_[0], '%s', @_[1..$#_]);
}

sub summary_report {
    my ( $report, $sample_unit ) = @_;

    print center( COLS, "SUMMARY" ), "\n";
    printf "%-15s %8s %8s %8s %8s\n", "Name", "Pkts", "Pkts/$sample_unit", "Bytes", "Bytes/$sample_unit";

    my $records = $report->summary_report();
    unless ( @$records ) {
	print center( COLS, "NO RECORDS" ), "\n\n";
	return;
    }

    foreach my $s ( @$records ) {
	printf "%-15s %8s %8s %8s %8s\n", $s->{name}, packets_count( $s->{packets_sum} ), packets_count( $s->{packets_avg} ),
	  bytes_count( $s->{bytes_sum} ), bytes_count( $s->{bytes_avg} );
    }
    print "\n\n";
}

sub print_daily_header {
    my $date = shift;
    printf "%-12s", "Chain";
    for ( 1 .. 7) {
	my ($day,$mon) = (localtime $date)[3,4];
	$mon++;
	printf " %02d-%02d ", $mon, $day;
	$date += 24 * 3600;
    }
    printf "%6s %6s\n",  "Avg", "Total";
}

sub daily_report {
    my ( $report, $what, $field, $func ) = @_;

    print centerf( COLS, '%s PER DAY', $what );
    print "\n";

    my $chains = $report->sample_report;
    my @samples = sort { $a->[0] cmp $b->[0] } map { [ $_, $chains->{$_} ] } keys %$chains;

    unless ( @samples ) {
	print center( COLS, "NO RECORDS" ), "\n\n";
	return;
    }

    my $num_samples = @{$samples[0][1]};
    my $start  = $samples[0][1][0]{start};

    my $wday = (localtime $start)[6];
    my $start = $wday ? $start - $wday * 24 * 3600 : $start;
    my $skip = $wday;

    my $i = 0;
    while ( $i < $num_samples ) {
	my $monday;

	if ($i == 0) {
	    $monday = $start;
	} else {
	    $monday = $samples[0][1][$i]{start};
	}
	print_daily_header( $monday );
	my $index;
	foreach my $s ( @samples ) {
	    $index = $i;
	    my $total = 0;
	    printf "%-12s", $s->[0];
	    if ( $i == 0 ) {
		if ( $skip ) {
		    for ( 0 .. $skip - 1 ) {
			printf "%6s ", "";
		    }
		}
		for ( $skip .. 6 ) {
		    if ( $index < $num_samples ) {
			my $x = $s->[1][$index++]{$field};
			printf "%6s ", $func->( $x );
			$total += $x;
		    } else {
			printf "%6s ", "";
		    }
		}
	    } else {
		for ( 0 .. 6 ) {
		    if ( $index < $num_samples ) {
			my $x = $s->[1][$index++]{$field};
			printf "%6s ", $func->( $x );
			$total += $x;
		    } else {
			printf "%6s ", "";
		    }
		}
	    }
	    printf "%6s ", $func->(  $total / $num_samples );
	    printf "%6s ", $func->(  $total );
	    print "\n";
	}
	$i = $index;
	print "\n";
    }

}

sub print_hourly_header {
    my $hour = shift;
    printf "%-12s", "Chain";
    for ( 1 .. 6) {
	printf " %02d:00 ", $hour++;
    }
    printf "%6s %6s\n",  "Avg", "Total";
}

sub hourly_report {
    my ( $report, $what, $field, $func ) = @_;

    print centerf( COLS, '%s PER HOUR', $what );
    print "\n";

    my $chains = $report->sample_report;
    my @samples = sort { $a->[0] cmp $b->[0] } map { [ $_, $chains->{$_} ] } keys %$chains;

    unless ( @samples ) {
	print center( COLS, "NO RECORDS" ), "\n\n";
	return;
    }

    my $num_samples = @{$samples[0][1]};

    my $start = $samples[0][1][0]{start};
    my $hour  = (localtime $start)[2];
    my $skip  = $hour % 6;
    my $hour  = $hour % 6 ? $hour - ($hour % 6) : $hour;

    print centerf( COLS, 'STATS FOR %s', strftime( '%y-%m-%d', localtime $start) );
    print "\n";
    my $i = 0;
    while ( $i < $num_samples ) {
	if ($hour > 23) {
	    print centerf( COLS, 'STATS FOR %s', strftime( '%y-%m-%d', localtime ($start += 24 * 3600) ) );
	    print "\n";
	    $hour = 0
	};

	print_hourly_header( $hour );
	my $index;
	foreach my $s ( @samples ) {
	    $index = $i;
	    my $total = 0;
	    printf "%-12s", $s->[0];
	    if ( $i == 0 ) {
		if ( $skip ) {
		    for ( 0 .. $skip - 1 ) {
			printf "%6s ", "";
		    }
		}
		for ( $skip .. 5 ) {
		    if ( $index < $num_samples ) {
			my $x = $s->[1][$index++]{$field};
			printf "%6s ", $func->( $x );
			$total += $x;
		    } else {
			printf "%6s ", "";
		    }
		}
	    } else {
		for ( 0 .. 5 ) {
		    if ( $index < $num_samples ) {
			my $x = $s->[1][$index++]{$field};
			printf "%6s ", $func->( $x );
			$total += $x;
		    } else {
			printf "%6s ", "";
		    }
		}
	    }
	    printf "%6s ", $func->(  $total / $num_samples );
	    printf "%6s ", $func->(  $total );
	    print "\n";
	}
	$i = $index;
	print "\n";
	$hour += 6;
    }

}

my $sample_unit;
my %opts = ( sample => "day" );
GetOptions( \%opts, "start=s", "end=s", "period=s", "sample=s",
	    "names=s@", "report=s@", "help"
	  )
  or usage;

usage(0) if $opts{help};

push @{$opts{report}}, "summary" unless exists $opts{report};

if ( exists $opts{sample} && $opts{sample} =~ /^d[ay]*$/i ) {
    $opts{sample} = "1d";
    $sample_unit = "d";
} elsif ( exists $opts{sample} && $opts{sample} =~ /^h[our]*$/i || ! exists $opts{sample} ) {
    $opts{sample} = "1h";
    $sample_unit = "h";
} else {
    die <<EOF;
fwctlacctreport: unknown sample unit: $opts{sample}. (should be hour or day)
EOF
}

$opts{files} = [ @ARGV ];
my $report = new Fwctl::AcctReport( %opts );

my $report_start = strftime "%y-%m-%d %H:%M", localtime $report->start();
my $report_end   = strftime "%y-%m-%d %H:%M", localtime $report->end();

print center( COLS, "IP ACCOUNTING REPORT" );
print center( COLS, "$report_start - $report_end" );
print "\n\n";

foreach my $r ( @{ $opts{report} } ) {
    if ( $r =~ /^s[umary]*/i ) {
	summary_report( $report, $sample_unit );
    } elsif ( $r =~ /^p[ackets]*/i ) {
	if ( $sample_unit eq "h" ) {
	    hourly_report( $report, "PACKETS", "packets", \&packets_count);
	} else {
	    daily_report( $report, "PACKETS", "packets", \&packets_count );
	}
    } elsif ( $r =~ /^b[ytes]*/i ) {
	if ( $sample_unit eq "h" ) {
	    hourly_report( $report, "BYTES", "bytes", \&bytes_count );
	} else {
	    daily_report( $report, "BYTES", "bytes", \&bytes_count );
	}
    } else {
	print STDERR "unknown report type: $r\n";
    }
}

__END__

=pod

=head1 NAME

fwctlacctreport - Generates text reports from the fwctl_acct file.

=head1 SYNOPSIS

fwctlacctreport [--start report_start]
		[--end report_end | --period report_period ]
		[--sample day|hour]
		[--names chain...]
		[--reports report ...]
		logfile ...

fwctlacctreport --help

=head1 DESCRIPTION

B<fwctlacctreport> can be use to generates reports from the output of
the C<fwctl dump-acct> command. It will generates either summary or
histogram-like report for the accounting data.

=head1 INPUT OPTIONS

The records on which the report will be generated can be customized
with the following options.

=over

=item start

Sets the start of the report's period. If the Date::Manip(3) module is
installed, you can use any format that this module can parse. If that
module isn't installed you must use the following format YYYY-MM-DD
HH:MM:SS or any meaningful subset of that format.

If this option is not used, the report will start with the first
record.

=item end

Sets the end of the report's period. If the Date::Manip(3) module is
installed, you can use any format that this module can parse. If that
module is'nt installed you must use the following format YYYY-MM-DD
HH:MM:SS or any meaningful subset of that format.

If this option is not used, the report will end with the last record.

=item period

Sets the length of the report's period. This length is interpreted
relative to the report's start. This option has priority over the
B<end> option.

If you have the Date::Manip module installed, you can use any format
that this module can parse. If that module isn't available, you can
use a subset of the following format X weeks X days X hours X mins X
secs.

=item names

Restrict the report to the chains specified by this option.

You can use this parameter multiple times to specify multiple
possibility. The record will be included if it matches any of those.

=head1 OUTPUT OPTIONS

To customize output you can use the following options :

=over

=item sample

This option can be set to I<day> or I<hour>. This determines if the
program will report hourly sample or daily sample.

=item report

You can use this option to specify the reports that will be generated.
By default, the I<summary> report is generated.

=back

=head1 REPORT

Here are the reports that can be generated :

=over

=item summary

Report that shows the total packets and bytes for each chains.

=item packets

Report that number of packets received by each chain for each hours or
days (depending on the setting of the I<sample> option).


=item bytes

Report that number of bytes received by each chain for each hours or
days (depending on the setting of the I<sample> option).

=back

=head1 AUTHOR

Francis J. Lacoste <francis.lacoste@iNsu.COM>

=head1 COPYRIGHT

Copyright (c) 2000 iNsu Innovations Inc.
All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=head1 SEE ALSO

Fwctl(3) Fwctl::RuleSet(3) fwctl(8) fwctllog(8) Fwctl::AcctReport(3).

=cut