| 1 | # | ||
| 2 | # Report.pm - Module that compiles accounting reports from the data dumped | ||
| 3 | # by the fwctl dump-acct command. | ||
| 4 | # | ||
| 5 | # This file is part of Fwctl. | ||
| 6 | # | ||
| 7 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 8 | # | ||
| 9 | # Copyright (C) 2000 iNsu Innovations Inc. | ||
| 10 | # | ||
| 11 | # This program is free software; you can redistribute it and/or modify | ||
| 12 | # it under the terms of the GNU General Public License as published by | ||
| 13 | # the Free Software Foundation; either version 2 of the License, or | ||
| 14 | # (at your option) any later version. | ||
| 15 | # | ||
| 16 | # This program is free software; you can redistribute it and/or modify | ||
| 17 | # it under the terms same terms as perl itself. | ||
| 18 | # | ||
| 19 | package Fwctl::AcctReport; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | |||
| 23 | use vars qw( $VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA ); | ||
| 24 | |||
| 25 | use Math::BigInt; | ||
| 26 | use Symbol; | ||
| 27 | use Fwctl::Report; | ||
| 28 | use Exporter; | ||
| 29 | |||
| 30 | BEGIN { | ||
| 31 | ($VERSION) = '$Revision: 1.4 $' =~ /(Revision: ([\d.]+))/; | ||
| 32 | @ISA = qw( Exporter ); | ||
| 33 | |||
| 34 | @EXPORT = (); | ||
| 35 | |||
| 36 | @EXPORT_OK = (); | ||
| 37 | |||
| 38 | %EXPORT_TAGS = ( fields => [ qw( TIME PACKETS BYTES NAME ) ], | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | |||
| 42 | BEGIN { | ||
| 43 | # Create the necessary constant | ||
| 44 | my $i = 0; | ||
| 45 | for my $f ( @{$EXPORT_TAGS{fields}} ) { | ||
| 46 | eval "use constant $f => $i;"; | ||
| 47 | $i++; | ||
| 48 | } | ||
| 49 | |||
| 50 | Exporter::export_ok_tags( 'fields' ); | ||
| 51 | }; | ||
| 52 | |||
| 53 | =pod | ||
| 54 | |||
| 55 | =head1 NAME | ||
| 56 | |||
| 57 | Fwctl::AcctReport - Generates reports from C<fwctl dump-acct> command. | ||
| 58 | |||
| 59 | =head1 SYNOPSIS | ||
| 60 | |||
| 61 | use Fwctl::AcctReport; | ||
| 62 | |||
| 63 | my $report = new Fwctl::Report( options ... ); | ||
| 64 | |||
| 65 | my $r = $report->summary_report; | ||
| 66 | |||
| 67 | foreach my $r ( @$src_alias_sum ) { | ||
| 68 | print $r->{name}, " = ", $r->{bytes_sum}, "\n"; | ||
| 69 | } | ||
| 70 | |||
| 71 | =head1 DESCRIPTION | ||
| 72 | |||
| 73 | The Fwctl::AcctReport(3) module can be used to generate packet and bandwith | ||
| 74 | report from the data generated by the C<fwctl dump-acct> command. | ||
| 75 | |||
| 76 | This module can be used as backend for two kinds of report. Summary which | ||
| 77 | compiles the total number of packets and bytes received by each accounting | ||
| 78 | chains over the report's period or histogram type report where you have the | ||
| 79 | packets and bytes total for each sample (hour, day, etc) over the report's | ||
| 80 | period. | ||
| 81 | |||
| 82 | =head1 CAVEATS | ||
| 83 | |||
| 84 | The C<fwctl dump-acct> commands dumps the packets and bytes total | ||
| 85 | since the time of the last invocation. If the last invocation wasn't | ||
| 86 | wholly comprised in the current sample, the total will be | ||
| 87 | interpolated. This means that you should run the C<fwctl dump-acct> | ||
| 88 | command at least as frequently as the smallest sample period you want | ||
| 89 | to use. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub read_records { | ||
| 94 | my $self = shift; | ||
| 95 | |||
| 96 | # Read in the data | ||
| 97 | my $records = []; | ||
| 98 | push @{$self->{opts}{files}}, \*STDIN unless @{$self->{opts}{files}}; | ||
| 99 | FILE: | ||
| 100 | foreach my $file ( @{$self->{opts}{files}} ) { | ||
| 101 | my $fh; | ||
| 102 | if ( ref $file ) { | ||
| 103 | $fh = $file; | ||
| 104 | } elsif ( $file eq "-" ) { | ||
| 105 | $fh = \*STDIN; | ||
| 106 | } else { | ||
| 107 | $fh = gensym; | ||
| 108 | open $fh, $file | ||
| 109 | or do { warn "can't open file $file\n"; next FILE }; | ||
| 110 | } | ||
| 111 | |||
| 112 | # We subtract gives ourselves more records since the entries are | ||
| 113 | # the amount between the current record timestamp and the one before | ||
| 114 | # it. | ||
| 115 | my $start = $self->{start}; | ||
| 116 | $start -= 2 * $self->{sample} if defined $start; | ||
| 117 | |||
| 118 | my $end = $self->{end}; | ||
| 119 | $end += 2 * $self->{sample} if defined $end; | ||
| 120 | |||
| 121 | LINE: | ||
| 122 | while ( my $line = <$fh>) { | ||
| 123 | chomp $line; | ||
| 124 | my @fields = (split /\s+/, $line)[0,2,3,4]; | ||
| 125 | |||
| 126 | if ( ! defined $self->{start}) { | ||
| 127 | $start = $self->{start} = $fields[TIME]; | ||
| 128 | $start -= 2 * $self->{sample}; | ||
| 129 | if ( defined $self->{period} ) { | ||
| 130 | $end = $self->{end} = $self->{start} + $self->{period}; | ||
| 131 | $end += 2 * $self->{sample}; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | # Skip fields outside the period | ||
| 135 | next unless $start <= $fields[TIME] && $end >= $fields[TIME]; | ||
| 136 | |||
| 137 | if ( $self->{opts}{names} ) { | ||
| 138 | foreach my $chain ( @{ $self->{opts}{names} } ) { | ||
| 139 | if ( $chain eq $fields[NAME] ) { | ||
| 140 | push @$records, \@fields; | ||
| 141 | next LINE; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } else { | ||
| 145 | push @$records, \@fields; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | # Records are sorted by time. | ||
| 151 | $self->{records} = [ sort { $a->[TIME] <=> $b->[TIME] } @$records ]; | ||
| 152 | } | ||
| 153 | |||
| 154 | =pod | ||
| 155 | |||
| 156 | =head1 CREATING A NEW REPORT OBJECT | ||
| 157 | |||
| 158 | Ex. my $report = new Fwctl::AcctReport( start => 'yesterday', | ||
| 159 | period => '1 day', | ||
| 160 | sample => '1d', | ||
| 161 | files => [ 'log' ] ); | ||
| 162 | |||
| 163 | =head2 PARAMETERS | ||
| 164 | |||
| 165 | The C<new> method accepts the following parameter : | ||
| 166 | |||
| 167 | =over | ||
| 168 | |||
| 169 | =item files | ||
| 170 | |||
| 171 | Specifies the file from which to read the dumped acctounting informations. | ||
| 172 | It is an array of file handle or file names. If this parameter is not specified | ||
| 173 | the records will be read from STDIN. | ||
| 174 | |||
| 175 | =item start | ||
| 176 | |||
| 177 | Sets the start of the report's period. If the Date::Manip(3) module is | ||
| 178 | installed, you can use any format that this module can parse. If that module | ||
| 179 | isn't installed you must use the following format YYYY-MM-DD HH:MM:SS or any | ||
| 180 | meaningful subset of that format. | ||
| 181 | |||
| 182 | If this option is not used, the report will start with the first record. | ||
| 183 | |||
| 184 | =item end | ||
| 185 | |||
| 186 | Sets the end of the report's period. If the Date::Manip(3) module is | ||
| 187 | installed, you can use any format that this module can parse. If that module | ||
| 188 | isn't installed you must use the following format YYYY-MM-DD HH:MM:SS or any | ||
| 189 | meaningful subset of that format. | ||
| 190 | |||
| 191 | If this option is not used, the report will end with the last record. | ||
| 192 | |||
| 193 | =item period | ||
| 194 | |||
| 195 | Sets the length of the report's period. This length is interpreted relative | ||
| 196 | to the report's start. This option has priority over the B<end> option. | ||
| 197 | |||
| 198 | If you have the Date::Manip module installed, you can use any format that this | ||
| 199 | module can parse. If that module isn't available, you can use a subset of the | ||
| 200 | following format X weeks X days X hours X mins X secs. | ||
| 201 | |||
| 202 | =item sample | ||
| 203 | |||
| 204 | Sets the length the sample's for the report. The sample_report will | ||
| 205 | returns results aggregated over that length. For example, if you use | ||
| 206 | 1d as sample's length, you will get the stats for every day from the | ||
| 207 | beginning of the report until the end. | ||
| 208 | |||
| 209 | If you have the Date::Manip module installed, you can use any format | ||
| 210 | that this module can parse. If that module isn't available, you can | ||
| 211 | use a subset of the following format X weeks X days X hours X mins X | ||
| 212 | secs. | ||
| 213 | |||
| 214 | =item names | ||
| 215 | |||
| 216 | Restrict records to those whose name matches B<names>. | ||
| 217 | |||
| 218 | You can use this parameter multiple times to specify multiple | ||
| 219 | possibility. The record will be included if it matches any of those. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub new { | ||
| 224 | my $proto = shift; | ||
| 225 | my $class = ref $proto || $proto; | ||
| 226 | |||
| 227 | my $self = { opts => { @_ }, | ||
| 228 | records => undef, | ||
| 229 | start => undef, | ||
| 230 | end => undef, | ||
| 231 | period => undef, | ||
| 232 | sample => 3600, # One hour sample | ||
| 233 | sample_report => undef, | ||
| 234 | summary_report => undef, | ||
| 235 | }; | ||
| 236 | |||
| 237 | # Determine start and end of the report; | ||
| 238 | if ( $self->{opts}{start} ) { | ||
| 239 | $self->{start} = Fwctl::Report::parse_date( $self->{opts}{start} ) | ||
| 240 | or die "invalid start date format: $self->{opts}{start}\n"; | ||
| 241 | } | ||
| 242 | |||
| 243 | if ( $self->{opts}{period} ) { | ||
| 244 | $self->{period} = Fwctl::Report::parse_period( $self->{opts}{period} ) | ||
| 245 | or die "invalid period delta: $self->{opts}{period}\n"; | ||
| 246 | if ( $self->{start} ) { | ||
| 247 | $self->{end} = $self->{start} + $self->{period}; | ||
| 248 | } | ||
| 249 | } elsif ( $self->{opts}{end} ) { | ||
| 250 | $self->{end} = Fwctl::Report::parse_date( $self->{opts}{end} ) | ||
| 251 | or die "invalid end date format: $self->{opts}{end}\n"; | ||
| 252 | } else { | ||
| 253 | $self->{end} = time; | ||
| 254 | } | ||
| 255 | |||
| 256 | if ( $self->{opts}{sample} ) { | ||
| 257 | $self->{sample} = Fwctl::Report::parse_period( $self->{opts}{sample} ) | ||
| 258 | or die "invalid sample period: $self->{opts}{sample}\n"; | ||
| 259 | } | ||
| 260 | bless $self, $class; | ||
| 261 | |||
| 262 | $self->read_records; | ||
| 263 | |||
| 264 | $self; | ||
| 265 | } | ||
| 266 | |||
| 267 | =pod | ||
| 268 | |||
| 269 | =head1 METHODS | ||
| 270 | |||
| 271 | =head1 start() | ||
| 272 | |||
| 273 | Return the start of the report in seconds since epoch. | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub start { | ||
| 278 | $_[0]->{start}; | ||
| 279 | } | ||
| 280 | |||
| 281 | =pod | ||
| 282 | |||
| 283 | =head1 end() | ||
| 284 | |||
| 285 | Returns the end of the report in seconds since epoch. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub end { | ||
| 290 | $_[0]->{end}; | ||
| 291 | } | ||
| 292 | |||
| 293 | =pod | ||
| 294 | |||
| 295 | =head1 period() | ||
| 296 | |||
| 297 | Returns the length of the report's period ( $report->end() - $report->start() ) | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | sub period { | ||
| 302 | $_[0]->{end} - $_[0]->{period}; | ||
| 303 | } | ||
| 304 | |||
| 305 | =pod | ||
| 306 | |||
| 307 | =head1 sample() | ||
| 308 | |||
| 309 | Returns the length of the sample period. | ||
| 310 | |||
| 311 | =cut | ||
| 312 | |||
| 313 | sub sample { | ||
| 314 | $_[0]->{sample}; | ||
| 315 | } | ||
| 316 | |||
| 317 | =head1 records() | ||
| 318 | |||
| 319 | Returns an array reference to all the records read and which makes the | ||
| 320 | report's samples. | ||
| 321 | |||
| 322 | =head2 RECORD FIELDS | ||
| 323 | |||
| 324 | Each record is an array ref. You can accessed the individual fields of | ||
| 325 | the record by using the following constants. (Those can be imported by | ||
| 326 | using the C<:fields> import tag.) | ||
| 327 | |||
| 328 | =over | ||
| 329 | |||
| 330 | =item TIME | ||
| 331 | |||
| 332 | The epoch start time of the sample. | ||
| 333 | |||
| 334 | =item NAME | ||
| 335 | |||
| 336 | The name of the accounting chain. | ||
| 337 | |||
| 338 | =item PACKETS | ||
| 339 | |||
| 340 | The number of packets received. | ||
| 341 | |||
| 342 | =item BYTES | ||
| 343 | |||
| 344 | The total number of bytes. | ||
| 345 | |||
| 346 | =back | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | sub records { | ||
| 351 | # Copy the records | ||
| 352 | [ @{$_[0]->{records}} ]; | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | =pod | ||
| 358 | |||
| 359 | =head1 REPORTS | ||
| 360 | |||
| 361 | The following report generation methods are available : | ||
| 362 | |||
| 363 | =head2 summary_report() | ||
| 364 | |||
| 365 | my $r = $report->summary_report(); | ||
| 366 | |||
| 367 | |||
| 368 | Generates a report that shows the compiled statistics for every | ||
| 369 | accounting chain. | ||
| 370 | |||
| 371 | The resulting report is an array ref of hash reference. Each report | ||
| 372 | record's has the following fields. | ||
| 373 | |||
| 374 | =over | ||
| 375 | |||
| 376 | =item name | ||
| 377 | |||
| 378 | The name of the chains. | ||
| 379 | |||
| 380 | =item packets_sum | ||
| 381 | |||
| 382 | The total number of packets over the report's period. | ||
| 383 | |||
| 384 | This number is a Math::BigInt(3) object. | ||
| 385 | |||
| 386 | =item bytes_sum | ||
| 387 | |||
| 388 | The total number of bytes received over the report's period. | ||
| 389 | |||
| 390 | This number is a Math::BigInt(3) object. | ||
| 391 | |||
| 392 | =item packets_avg | ||
| 393 | |||
| 394 | The average number of packets received by that chain by sample. | ||
| 395 | |||
| 396 | This number is a Math::BigInt(3) object. | ||
| 397 | |||
| 398 | =item bytes_avg | ||
| 399 | |||
| 400 | The average number of packets bytes by that chain by sample. | ||
| 401 | |||
| 402 | This number is a Math::BigInt(3) object. | ||
| 403 | |||
| 404 | =back | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | sub summary_report { | ||
| 409 | my ( $self ) = shift; | ||
| 410 | |||
| 411 | return $self->{summary_report} if defined $self->{summary_report}; | ||
| 412 | |||
| 413 | my %chains = (); | ||
| 414 | my $sample_report = $self->sample_report; | ||
| 415 | while ( my ( $chain, $samples ) = each %$sample_report ) { | ||
| 416 | my $stats = { | ||
| 417 | name => $chain, | ||
| 418 | packets_sum => new Math::BigInt( 0 ), | ||
| 419 | bytes_sum => new Math::BigInt( 0 ), | ||
| 420 | packets_avg => 0, | ||
| 421 | bytes_avg => 0, | ||
| 422 | }; | ||
| 423 | |||
| 424 | foreach my $r ( @$samples ) { | ||
| 425 | $stats->{packets_sum} += $r->{packets}; | ||
| 426 | $stats->{bytes_sum} += $r->{bytes}; | ||
| 427 | } | ||
| 428 | $stats->{packets_avg} = $stats->{packets_sum} / @$samples; | ||
| 429 | $stats->{bytes_avg} = $stats->{bytes_sum} / @$samples; | ||
| 430 | |||
| 431 | # Replace samples by stats | ||
| 432 | $chains{$chain} = $stats; | ||
| 433 | } | ||
| 434 | |||
| 435 | # Returns an array ref sorted by name | ||
| 436 | return $self->{summary_report} = [ sort { $a->{name} cmp $b->{name} } values %chains ]; | ||
| 437 | } | ||
| 438 | |||
| 439 | =pod | ||
| 440 | |||
| 441 | =head2 sample_report() | ||
| 442 | |||
| 443 | my $r = $report->sample_report(); | ||
| 444 | |||
| 445 | |||
| 446 | Generates a report that compiles the stats for each sample for each chains. | ||
| 447 | |||
| 448 | The resulting report is an hash reference. Each key is a chain and each value | ||
| 449 | is an array ref to the chain compiled statistics. Each element of the array is | ||
| 450 | an hash reference comprises of the following elements : | ||
| 451 | |||
| 452 | =over | ||
| 453 | |||
| 454 | =item start | ||
| 455 | |||
| 456 | The start time in seconds since epoch for that sample. | ||
| 457 | |||
| 458 | =item packets | ||
| 459 | |||
| 460 | The total number of packets received in that sample. | ||
| 461 | |||
| 462 | This number is a Math::BigInt(3) object. | ||
| 463 | |||
| 464 | =item bytes | ||
| 465 | |||
| 466 | The total number of bytes received in that sample. | ||
| 467 | |||
| 468 | This number is a Math::BigInt(3) object. | ||
| 469 | |||
| 470 | =back | ||
| 471 | |||
| 472 | =cut | ||
| 473 | |||
| 474 | sub sample_report { | ||
| 475 | my ( $self ) = shift; | ||
| 476 | |||
| 477 | # Return cache | ||
| 478 | return $self->{sample_report} if defined $self->{sample_report}; | ||
| 479 | |||
| 480 | my $start = $self->{start}; | ||
| 481 | my $end = $self->{end}; | ||
| 482 | my $sample = $self->{sample}; | ||
| 483 | my $num_samples = int( ($end - $start ) / $sample ); | ||
| 484 | $num_samples++ unless $num_samples * $sample + $start > $end; | ||
| 485 | |||
| 486 | my %chains = (); | ||
| 487 | # Sort records by chain, since records are ordered by time, | ||
| 488 | # all chain's record are also ordered. | ||
| 489 | foreach my $r ( @{ $self->{records} } ) { | ||
| 490 | push @{$chains{$r->[NAME]}}, $r; | ||
| 491 | } | ||
| 492 | |||
| 493 | # Foreach chains build an array containing the number of bytes and packets | ||
| 494 | # by sample period. | ||
| 495 | while ( my ( $chain, $records ) = each %chains ) { | ||
| 496 | # Initialize the samples array for that chain | ||
| 497 | my @samples = ( ) x $num_samples; | ||
| 498 | for ( my $i = 0 ; $i < $num_samples; ++$i ) { | ||
| 499 | $samples[$i] = { | ||
| 500 | start => $start + $i * $sample, | ||
| 501 | packets => new Math::BigInt(0), | ||
| 502 | bytes => new Math::BigInt(0), | ||
| 503 | }; | ||
| 504 | } | ||
| 505 | |||
| 506 | my ($last_s_idx, $last_time) = ( -1, 0 ); | ||
| 507 | RECORD: | ||
| 508 | foreach my $r ( @$records ) { | ||
| 509 | # Find sample in which this record falls | ||
| 510 | my $s_idx = int ( ( $r->[TIME] - $start ) / $sample ); | ||
| 511 | |||
| 512 | # If this record is before the beginning of the | ||
| 513 | # period, save time for normalization and go to | ||
| 514 | # next record | ||
| 515 | if ( $s_idx < 0 ) { | ||
| 516 | $last_time = $r->[TIME]; | ||
| 517 | next RECORD; | ||
| 518 | } | ||
| 519 | |||
| 520 | # Comparing to the last record, is it completely | ||
| 521 | # in this period. | ||
| 522 | if ( $last_s_idx == $s_idx ) { | ||
| 523 | # Add bytes and packet counters | ||
| 524 | $samples[$s_idx]{bytes} += $r->[BYTES]; | ||
| 525 | $samples[$s_idx]{packets} += $r->[PACKETS]; | ||
| 526 | } else { | ||
| 527 | # Distribute pro rata | ||
| 528 | my $this_sample_ratio = ($r->[TIME] - ($start + $s_idx * $sample) ) / $sample; | ||
| 529 | my $last_sample_ratio = 1 - $this_sample_ratio; | ||
| 530 | if ( $s_idx >= $num_samples ) { | ||
| 531 | # This is the last record | ||
| 532 | $samples[$last_s_idx]{bytes} += int( $r->[BYTES] * $last_sample_ratio ); | ||
| 533 | $samples[$last_s_idx]{packets} += int( $r->[PACKETS] * $last_sample_ratio ); | ||
| 534 | last RECORD; | ||
| 535 | } elsif ( $s_idx == 0 ) { | ||
| 536 | # This is the first record | ||
| 537 | $samples[$s_idx]{bytes} += int( $r->[BYTES] * $this_sample_ratio ); | ||
| 538 | $samples[$s_idx]{packets} += int( $r->[PACKETS] * $this_sample_ratio ); | ||
| 539 | } else { | ||
| 540 | $samples[$last_s_idx]{bytes} += int( $r->[BYTES] * $last_sample_ratio ); | ||
| 541 | $samples[$last_s_idx]{packets} += int( $r->[PACKETS] * $last_sample_ratio ); | ||
| 542 | $samples[$s_idx]{bytes} += int( $r->[BYTES] * $this_sample_ratio ); | ||
| 543 | $samples[$s_idx]{packets} += int( $r->[PACKETS] * $this_sample_ratio ); | ||
| 544 | } | ||
| 545 | $last_s_idx = $s_idx; | ||
| 546 | } | ||
| 547 | $last_time = $r->[TIME]; | ||
| 548 | } | ||
| 549 | # Replace records by sample stats | ||
| 550 | $chains{$chain} = \@samples; | ||
| 551 | } | ||
| 552 | |||
| 553 | # Return an hash of array of hash. | ||
| 554 | return $self->{sample_report} = \%chains; | ||
| 555 | } | ||
| 556 | |||
| 557 | 1; | ||
| 558 | |||
| 559 | __END__ |