[mrtg] calculate_traffic.pl to use dd/mm; NOT mm/dd; how to ?

Voytek Eymont voytek at sbt.net.au
Thu Apr 20 10:48:51 MEST 2000


I know ZILCH about perl...
can anyone help me to modify calculate_traffic.pl so it will do 'dd/mm',
not 'mm/dd', pls ?

I did open the file in text editor, and starred long, and longer at it,
but, am no wiser...

-----------------------------------------

#!/usr/local/bin/perl
 
#-----------------------------------------------------------------------
# calculate_traffic.pl <logfile> <start_date> <end_date>
#
# $Id: calculate_traffic.pl,v 1.2 1998/03/05 01:10:20 mrtg Exp $
#
# This script takes a log file name, a start date, and an
# end date as the three mandatory arguments to the script.
# It returns the total Octets In and Out from that interface
# between those two dates, inclusive.  The start date and 
# end date MUST be specified as: MM-DD-YYYY-HH:MM, and the
# start date MUST be earlier than the end date.
#-----------------------------------------------------------------------
 
#-----------------------------------------------------------------------
# GLOBALS
 
use strict;
use Time::Local;

require 5.004;
 
$| = 1 ;
 
my($DEBUG)     = 0;

my($topdir)    = "/www/mrtg/router";
my($datestr)   = "";
my($start_date)= "";
my($end_date)  = "";
my($logfile)   = "";

#-----------------------------------------------------------------------
 
#-----------------------------------------------------------------------
sub errorexit {
   print ("$0:  Error:  @_.\n");
   exit(21);
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
sub get_args {
   my($logfile,$start,$end,$start_str,$end_str);
   unless( $#ARGV == 2 ) {
      my($name) = `basename $0` ;
      $name =~ s/\s+//g ;
      print STDERR "usage: $name <logfile> <start_date> <end_date>\n" ;
      print STDERR "   where <start_date> and <end_date> are in the\n";
      print STDERR "   format: MM-DD-YYYY-HH:MM\n";
      exit(1) ;
   }
   $logfile = $ARGV[0];
   if (! -r $logfile) {
      &errorexit("Couldn't read $logfile: $!");
   }
   $start_str = $ARGV[1];
   $end_str = $ARGV[2];
   if (($start_str eq "") || ($end_str eq "")) {
      &errorexit("Sorry, null dates NOT allowed!");
   }
   $start_str =~ m/^(\d\d)\-(\d\d)\-(\d\d\d\d)\-(\d\d)\:(\d\d)$/ or
      &errorexit("You specified an invalid start date: $start_str");
   $start = timelocal(0,$5,$4,$2,$1-1,$3);
   $end_str =~ m/^(\d\d)\-(\d\d)\-(\d\d\d\d)\-(\d\d)\:(\d\d)$/ or
      &errorexit("You specified an invalid end date: $end_str");
   $end = timelocal(0,$5,$4,$2,$1-1,$3);
   if ($start > $end) {
      # just swap them, don't complain
      my($tmp_date) = $start;
      $start = $end;
      $end = $tmp_date;
   }
   return($logfile,$start,$end);
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
sub print_params {
   my($log,$start,$end) = @_ if @_;
   my($start_str, $end_str);
   $start_str = scalar localtime($start);
   $end_str = scalar localtime($end);
   print STDERR "LOG: $log\n";
   print STDERR "START RAW: $start START STRING: $start_str\n";
   print STDERR "END RAW: $end END STRING: $end_str\n";
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
sub get_totals {
   my($logfile,$start,$end) = @_ if @_;
   my($itot,$otot,$interval) = (0,0,0);
   my($aline,$curr_date,$avg_in,$avg_out,$pin,$pout);
   # File is in reverse, from end to start, so we count down...
   my($prev_date) = time;
   open(LOG,"$logfile") or &errorexit("Couldn't open $logfile: $!");
   while (<LOG>) {
      $aline = $_;
      chomp($aline);
      ($curr_date,$avg_in,$avg_out,$pin,$pout) = split(' ',$aline);
      $interval = $prev_date - $curr_date; 
      $prev_date = $curr_date;
      next if ($curr_date > $end);
      next if ($curr_date < $start);
      $itot = $itot + ($avg_in * $interval);
      $otot = $otot + ($avg_out * $interval);
      print STDERR "IN: $avg_in * $interval = $itot\tOUT: $avg_out * $interval = $otot\n" if ($DEBUG);
   }
   return($itot,$otot);
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
sub print_totals {
   my($in,$out) = @_ if @_;
   print STDERR "INTERVAL TOTAL IN: $in\n";
   print STDERR "INTERVAL TOTAL OUT: $out\n";
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# MAIN

($logfile,$start_date,$end_date) = &get_args;
&print_params($logfile,$start_date,$end_date);

my($in_tot,$out_tot) = &get_totals($logfile,$start_date,$end_date);
&print_totals($in_tot,$out_tot);
#-----------------------------------------------------------------------
#



Voytek Eymont
SBT Information Systems Pty Ltd
http://www.sbt.net.au/links/
phone +61-2 9310-1144 fax +61-2 9310-1118 

--
Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Archive     http://www.ee.ethz.ch/~slist/mrtg
FAQ         http://faq.mrtg.org    Homepage     http://www.mrtg.org



More information about the mrtg mailing list