#!/usr/bin/perl # # BWMon # William Riley williams.workbench@gmail.com use strict; use warnings; use Getopt::Long; use Pod::Usage; my ($verbose, $quiet, $help, $man, $interface, $interval); my($rx_last,$tx_last,$rx,$tx,$first); my $result = GetOptions( "verbose" => \$verbose, "help" => \$help, "man" => \$man, "quiet" => \$quiet, "interface=s" => \$interface, "interval=n" => \$interval ) or pod2usage(2); pod2usage(-exitstatus => 0, -verbose => 2) if $man; if($help) { pod2usage(2); } $rx_last = 0; $tx_last = 0; $rx = 0; $tx = 0; $first = 1; $interface or $interface = "eth0"; $interval or $interval = 1; while(1){ open INTERFACE, "/sbin/ifconfig $interface|" or die "Could not open $interface"; while(){ my $line = $_; chomp $line; if($line =~ m/RX bytes:([\d]+).*TX bytes:([\d]+)/){ if($first == 1){ printf "Timestamp,Interface,RX Bytes/Sec,TX Bytes/Sec\n"; $first = 0; $rx_last = $1; $tx_last = $2; }else{ $rx = $1 - $rx_last; $tx = $2 - $tx_last; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); my $timestamp = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year + 1900, $mon, $mday + 1,$hour,$min,$sec); printf "%s,%s,%d,%d\n", $timestamp, $interface, $rx / $interval, $tx / $interval; $rx_last = $1; $tx_last = $2; } } } close INTERFACE; sleep $interval; } exit 0; __END__ =head1 NAME BWMon - Bandwidth monitor =head1 SYNOPSIS BWMon [options] Options: --interface interface to watch (default eth0) --interval how often to check (default 1 second) --help brief help message --man full documentation =head1 OPTIONS =over 12 =item B<--interface> Which interface to watch, i.e. eth0 =item B<--interval> Gather interface statistics every seconds =item B<--help> Print a brief help message and exit. =item B<--man> Print the manual page and exit. =back =head1 DESCRIPTION Monitor the bandwidth on a given interface and report the bytes per second =head1 CHANGELOG June 15, 2007 William Riley williams.workbench@gmail.com =cut