Count bandwidth

Ed: this has been sitting in my “drafts” box for a while now, so I figure I might as well just push it out.

Here’s a script I wrote to count the bandwidth used from the Apache access_log:

#!/usr/bin/perl -w

use strict;

my $access_log = $ARGV[0];
my $total = 0;
my @chunks;

open(LOGFILE, $access_log) or die "Could not open $access_log $!";

# count amount of data.
while(<logfile>)
{
@chunks = split();
if($chunks[9] =~ /\d/)
{ $total += $chunks[9]; }
}

close(LOGFILE);

# convert to human readable; 1024 * 1024 = 1048576.
$total /= 1048576;

print "Amount of bandwidth : $total MB\n";

This entry was posted in General, work and tagged , , , , , , . Bookmark the permalink.

2 Responses to Count bandwidth

  1. Michael says:

    I don’t know Perl so much, so my question may seem to you stupid,
    but I have error when trying to run it :

    ————–
    Use of uninitialized value in split at ./script.pl line 14.
    Use of uninitialized value in pattern match (m//) at ./script.pl line 15.
    ————–

    Seems like “$_” (or “_@”) is not getting any value ? …

    Thanks!

  2. augie says:

    Try now; I updated the post to properly display all the characters. Darn WP was stripping out angle brackets.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>