Blame bin/mrtg-traffic-sum.lib64

Packit 3f632f
#!/usr/bin/perl -w
Packit 3f632f
Packit 3f632f
# Changes in revision 1.9
Packit 3f632f
Packit 3f632f
# Added an option for --range, which may be "current" for month-to-date or "previous" for last month
Packit 3f632f
# Changed default date range to 00:00:00 on 1st of month to 23:59:59 at end of month
Packit 3f632f
# Changed the default units to MB, more relevant to non-datacentre users :-)
Packit 3f632f
# Added the option for units=GB, to allow for previous behaviour of this script
Packit 3f632f
# Changed default minimum to 0, to report any info that is available by default 
Packit 3f632f
# Updated help section with the above changes, fixed a couple of typos
Packit 3f632f
Packit 3f632f
Packit 3f632f
use strict;
Packit 3f632f
use Getopt::Long;
Packit 3f632f
use Pod::Usage;
Packit 3f632f
Packit 3f632f
BEGIN {
Packit 3f632f
    # Automatic OS detection ... do NOT touch
Packit 3f632f
    if ( $^O =~ /^(?:(ms)?(dos|win(32|nt)?))/i ) {
Packit 3f632f
        $main::OS = 'NT';
Packit 3f632f
        $main::SL = '\\';
Packit 3f632f
        $main::PS = ';';
Packit 3f632f
    } elsif ( $^O =~ /^VMS$/i ) {
Packit 3f632f
        $main::OS = 'VMS';
Packit 3f632f
        $main::SL = '.';
Packit 3f632f
        $main::PS = ':';
Packit 3f632f
    } else {
Packit 3f632f
        $main::OS = 'UNIX';
Packit 3f632f
        $main::SL = '/';
Packit 3f632f
        $main::PS = ':';
Packit 3f632f
    }
Packit 3f632f
}
Packit 3f632f
Packit 3f632f
use FindBin;
Packit 3f632f
use lib "${FindBin::Bin}";
Packit 3f632f
use lib "${FindBin::Bin}${main::SL}..${main::SL}lib${main::SL}mrtg2";
Packit 3f632f
use MRTG_lib "2.090017";
Packit 3f632f
use POSIX qw(mktime);
Packit 3f632f
Packit 3f632f
'$Revision: 1.9 $ ' =~ /Revision: (\S*)/;
Packit 3f632f
my $Revision = $1;
Packit 3f632f
Packit 3f632f
my $sendmail = "/usr/lib/sendmail";
Packit 3f632f
$sendmail = "/usr/sbin/sendmail" if -x "/usr/sbin/sendmail";
Packit 3f632f
Packit 3f632f
# main loop
Packit 3f632f
my %opt = ( 
Packit 3f632f
    'catch' => '(?:description|port name):\s*\s*([^<\s]+[^<]*?)\s*
Packit 3f632f
#    'catch' => 'td>\s*([^<\s]+COLO[^<]*?)\s*
Packit 3f632f
    'min' => 0,
Packit 3f632f
    'range' => "previous",
Packit 3f632f
    'units' => "MB"
Packit 3f632f
 );
Packit 3f632f
sub walklog ($$$$$);
Packit 3f632f
sub sumlog($$$);
Packit 3f632f
sub mailout($$);
Packit 3f632f
Packit 3f632f
sub main()
Packit 3f632f
{
Packit 3f632f
    # parse options
Packit 3f632f
    GetOptions(\%opt, 'min=i','help|h', 'catch=s', 'email=s','version','range=s','units=s') or exit(1);
Packit 3f632f
    if($opt{help})     { pod2usage(1) }
Packit 3f632f
    if($opt{man})      { pod2usage(-exitstatus => 0, -verbose => 2) }
Packit 3f632f
    if($opt{version})  { print "mrtg-traffic-sum $Revision\n"; exit(0) }
Packit 3f632f
    my $cfgfile = shift @ARGV;
Packit 3f632f
    pod2usage(1) unless $cfgfile and -r $cfgfile;
Packit 3f632f
    my %rcfg;
Packit 3f632f
    my %cfg;
Packit 3f632f
    my @routers;
Packit 3f632f
    my @target;
Packit 3f632f
    my $report = "Subject: Traffic total for '$cfgfile' ($Revision) ";
Packit 3f632f
    readcfg($cfgfile,\@routers,\%cfg,\%rcfg);
Packit 3f632f
    cfgcheck(\@routers, \%cfg, \%rcfg, \@target);
Packit 3f632f
    walklog \@routers, \%cfg, \%rcfg,\$report,\%opt;
Packit 3f632f
    if ($opt{email}) {
Packit 3f632f
	mailout \$report,$opt{email};
Packit 3f632f
    } else {
Packit 3f632f
	print $report;
Packit 3f632f
    }
Packit 3f632f
}
Packit 3f632f
Packit 3f632f
main;
Packit 3f632f
Packit 3f632f
sub walklog ($$$$$){
Packit 3f632f
    my $routers = shift;
Packit 3f632f
    my $cfg = shift;
Packit 3f632f
    my $rcfg = shift;
Packit 3f632f
    my $report = shift;
Packit 3f632f
    my $opt = shift;
Packit 3f632f
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(time);
Packit 3f632f
      my $start = mktime(0, 0, 0, 1, $mon-1, $year, 0, 0, 0);
Packit 3f632f
      my $end = mktime(0, 0, 0, 1, $mon, $year, 0, 0, 0)-1;
Packit 3f632f
    if ($opt{range} eq "previous") {
Packit 3f632f
      $start = mktime(0, 0, 0, 1, $mon-1, $year, 0, 0, 0);
Packit 3f632f
      $end = mktime(0, 0, 0, 1, $mon, $year, 0, 0, 0)-1;
Packit 3f632f
    }
Packit 3f632f
    if ($opt{range} eq "current") {
Packit 3f632f
      $start = mktime(0, 0, 0, 1, $mon, $year, 0, 0, 0);
Packit 3f632f
      $end = mktime(0, 0, 0, 1, $mon+1, $year, 0, 0, 0)-1;
Packit 3f632f
    } 
Packit 3f632f
Packit 3f632f
    my($m,$y) = (localtime($start))[4,5];
Packit 3f632f
    $y += 1900;
Packit 3f632f
    $m ++;
Packit 3f632f
    $$report .= sprintf "%04d/%02d\n\n", $y,$m;
Packit 3f632f
Packit 3f632f
    $$report .= "Start: ".localtime($start)."\n";
Packit 3f632f
    $$report .= "End:   ".localtime($end)."\n";
Packit 3f632f
if ($opt{'units'} eq "GB") {
Packit 3f632f
    $$report .= sprintf "%-40s  %15s\n","Interface","In+Out in GB";
Packit 3f632f
} else {
Packit 3f632f
    $$report .= sprintf "%-40s  %15s\n","Interface","In+Out in MB";
Packit 3f632f
}
Packit 3f632f
    $$report .= ("-" x 78)."\n";
Packit 3f632f
    foreach my $r (@$routers) {
Packit 3f632f
        my $label;
Packit 3f632f
	if ($rcfg->{pagetop}{$r} =~ /$opt{catch}/i ) {
Packit 3f632f
	    $label = $1;
Packit 3f632f
	    $label .= " $2" if $2;
Packit 3f632f
            my ($in,$out) = sumlog $cfg->{logdir}.$rcfg->{directory}{$r}."$r.log",$start,$end;
Packit 3f632f
            my $sum = ($in+$out)/(10**6);
Packit 3f632f
if ($opt{'units'} eq "GB") {
Packit 3f632f
            $sum = ($in+$out)/(10**9);
Packit 3f632f
} else {
Packit 3f632f
            $sum = ($in+$out)/(10**6);
Packit 3f632f
}
Packit 3f632f
            next if $sum < $opt{'min'};
Packit 3f632f
            $$report .= sprintf "%-40s  %15.0f\n",$label,$sum;
Packit 3f632f
        }
Packit 3f632f
    }
Packit 3f632f
}
Packit 3f632f
Packit 3f632f
sub sumlog ($$$) {
Packit 3f632f
    my $log = shift;
Packit 3f632f
    my $start = shift;
Packit 3f632f
    my $end = shift;
Packit 3f632f
    my $in = 0.0;
Packit 3f632f
    my $out = 0.0;    
Packit 3f632f
    if (open L, "<",$log){
Packit 3f632f
            my @this=($end+1,0,0);
Packit 3f632f
            my @prev;
Packit 3f632f
            while (<L>) {
Packit 3f632f
        	chomp;
Packit 3f632f
        	@prev = @this;
Packit 3f632f
        	@this = (split /\s+/)[0,1,2];
Packit 3f632f
Packit 3f632f
        	if ($prev[0] <= $end) {
Packit 3f632f
        	    my $diff = $prev[0] - $this[0];
Packit 3f632f
        	    $in += $diff * $prev[1];
Packit 3f632f
        	    $out += $diff * $prev[2];
Packit 3f632f
	        }
Packit 3f632f
        	last if $this[0] <= $start;
Packit 3f632f
            }
Packit 3f632f
            close L;
Packit 3f632f
    } else {
Packit 3f632f
        warn "WARN Skipping $log\n";
Packit 3f632f
    }       
Packit 3f632f
    return $in,$out;
Packit 3f632f
}
Packit 3f632f
Packit 3f632f
sub mailout ($$) {
Packit 3f632f
    my $data = shift;
Packit 3f632f
    my $rec = shift;
Packit 3f632f
    open S, "|-" or  exec $sendmail,'-f',$rec,$rec;
Packit 3f632f
    print S <
Packit 3f632f
From: $rec
Packit 3f632f
To: $rec
Packit 3f632f
$$data
Packit 3f632f
MESSAGE
Packit 3f632f
    close S;
Packit 3f632f
Packit 3f632f
}
Packit 3f632f
1
Packit 3f632f
Packit 3f632f
__END__
Packit 3f632f
Packit 3f632f
=head1 NAME
Packit 3f632f
Packit 3f632f
mrtg-traffic-sum - Builds monthly traffic summary from mrtg log files
Packit 3f632f
Packit 3f632f
=head1 SYNOPSIS
Packit 3f632f
Packit 3f632f
B<mrtg-traffic-sum> [I<options>...] B<config-file>
Packit 3f632f
Packit 3f632f
     --man           show man-page and exit
Packit 3f632f
 -h, --help          display this help and exit
Packit 3f632f
     --version       output version information and exit
Packit 3f632f
     --catch=regexp  filter out things that match this in PageTop
Packit 3f632f
     --email=address email the result
Packit 3f632f
     --min=gigabytes minimal number of gigabites required for report
Packit 3f632f
     --range=<when>  Specify "current" for month-to-date, "previous" for last complete month	
Packit 3f632f
     --units=[GB|MB] Display results in gigabytes (default is MB)
Packit 3f632f
Packit 3f632f
=head1 DESCRIPTION
Packit 3f632f
Packit 3f632f
The mrtg-traffic-sum goes through the mrtg logs for the targets in the
Packit 3f632f
the config file specified and builds the traffic total for the last
Packit 3f632f
month in Giga Bytes. (Note in communications Giga is 10^9).
Packit 3f632f
Packit 3f632f
The result of this analysis can be sent via email to an address of
Packit 3f632f
your choice using the B<--email> option.
Packit 3f632f
Packit 3f632f
With the B<--catch> option you can specify a regular expression which
Packit 3f632f
will be matched against the contents of the PageTop settings. The
Packit 3f632f
regular expression should return a value into $1 and possibly into
Packit 3f632f
$2. This will then be used as description for the appropriate traffic
Packit 3f632f
sum. By default the catch is set to:
Packit 3f632f
Packit 3f632f
 (?:description|port name):\s*\s*([^< ]*?[^<]*?)\s*
Packit 3f632f
Packit 3f632f
By default, all traffic which is over one megabyte gets reported. Use the
Packit 3f632f
B<--min> switch to change this number.
Packit 3f632f
Packit 3f632f
=head1 COPYRIGHT
Packit 3f632f
Packit 3f632f
Copyright (c) 2002 by Tobias Oetiker. All rights reserved.
Packit 3f632f
Packit 3f632f
=head1 LICENSE
Packit 3f632f
Packit 3f632f
This program is free software; you can redistribute it and/or modify
Packit 3f632f
it under the terms of the GNU General Public License as published by
Packit 3f632f
the Free Software Foundation; either version 2 of the License, or
Packit 3f632f
(at your option) any later version.
Packit 3f632f
Packit 3f632f
This program is distributed in the hope that it will be useful,
Packit 3f632f
but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3f632f
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 3f632f
GNU General Public License for more details.
Packit 3f632f
Packit 3f632f
You should have received a copy of the GNU General Public License
Packit 3f632f
along with this program; if not, write to the Free Software
Packit 3f632f
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Packit 3f632f
Packit 3f632f
=head1 AUTHOR
Packit 3f632f
Packit 3f632f
Tobias Oetiker E<lt>tobi@oetiker.chE<gt>,
Packit 3f632f
Keith Dunnett E<lt>keith@dunnett.nameE<gt>
Packit 3f632f
Packit 3f632f
=head1 HISTORY
Packit 3f632f
Packit 3f632f
 2002-07-13 to Initial Version
Packit 3f632f
 2009-12-38 kd New features
Packit 3f632f
Packit 3f632f
=cut
Packit 3f632f
Packit 3f632f
# Emacs Configuration
Packit 3f632f
#
Packit 3f632f
# Local Variables:
Packit 3f632f
# mode: cperl
Packit 3f632f
# eval: (cperl-set-style "PerlStyle")
Packit 3f632f
# mode: flyspell
Packit 3f632f
# mode: flyspell-prog
Packit 3f632f
# End:
Packit 3f632f
#
Packit 3f632f
# vi: sw=4 et