Commit 36fba2a9 authored by Peter Pentchev's avatar Peter Pentchev

Use Perl strict mode.

parent 797b39ff
......@@ -8,6 +8,8 @@ Change log for ethstats, the network traffic statistics utility
- add the -h (help) and -V (version) command-line options
- add the -c count command-line option
- declare a 5.010 minimum Perl version dependency
- update the Perl source style a bit:
- use strict mode
1.0 (unknown)
- released into the public domain by Drew Strieb
......
#!/usr/bin/perl
use v5.010;
use strict;
use Getopt::Std;
......@@ -40,7 +41,11 @@ sub version()
# Any changes I make are released into the public domain.
# - M. Drew Streib <dtype@dtype.org>
$COLOR = 0;
my $period = 10;
my ($COLOR, $addtime, $count, $iface);
my ($numdevs, $tkbin, $tkbout, $tpackin, $tpackout);
my (%kbin, %kbout, %packin, %packout);
my (%obytesin, %obytesout, %opackin, %opackout);
#uncomment this and set $COLOR = 1 if you have the right Perl module
#
......@@ -50,8 +55,6 @@ $COLOR = 0;
$| = 1;
$period = 10;
#Inter-| Receive | Transmit
# face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
# lo: 2356 32 0 0 0 0 0 0 2356 32 0 0 0 0 0 0
......@@ -59,7 +62,6 @@ $period = 10;
# eth1: 2039952 21982 6 0 0 6 0 0 47000710 34813 0 0 0 821 0 0
my %opts;
my ($count, $iface);
getopts('c:hi:n:tV', \%opts) or usage(1);
version() if $opts{'V'};
......@@ -83,7 +85,7 @@ if (defined($opts{'c'})) {
}
$iface = $opts{'i'}; # also works if it isn't defined
$op = $period;
my $op = $period;
$period = 1;
convert();
sleep $period;
......@@ -99,7 +101,7 @@ if ($numdevs > 1) {
print "\n";
}
foreach $dev(sort keys %kbin) {
foreach my $dev(sort keys %kbin) {
printf " %4s: %7.2f Mb/s In %7.2f Mb/s Out", $dev, $kbin{$dev}, $kbout{$dev};
printf " - %8.1f p/s In %8.1f p/s Out", $packin{$dev}, $packout{$dev};
print "\n";
......@@ -115,13 +117,14 @@ if ($numdevs > 1) {
sub convert {
open(IN, "/proc/net/dev") || die("Can't open ip_acct: $!\n");
<IN>; <IN>;
while($l = <IN>) {
my (%bytesin, %bytesout);
while(my $l = <IN>) {
chop($l);
($dev, $rest) = split(/:/, $l);
my ($dev, $rest) = split(/:/, $l);
$dev =~ s/\s//g;
next if defined($iface) && $dev ne $iface;
$rest =~ s/^\s+//;
@devarr = split(/\s+/, $rest);
my @devarr = split(/\s+/, $rest);
$bytesin{$dev} = @devarr[0]; $bytesout{$dev} = @devarr[8];
$packin{$dev} = @devarr[1]; $packout{$dev} = @devarr[9];
}
......@@ -133,12 +136,12 @@ sub convert {
$tkbin = 0;
$tkbout = 0;
foreach $dev(sort keys %bytesin) {
foreach my $dev(sort keys %bytesin) {
next if($dev eq "lo");
$numdevs++;
# packets in/out
$packdiffin = ($packin{$dev} - $opackin{$dev});
$packdiffout = ($packout{$dev} - $opackout{$dev});
my $packdiffin = ($packin{$dev} - $opackin{$dev});
my $packdiffout = ($packout{$dev} - $opackout{$dev});
$packdiffin += 4294967296 if($packdiffin<0);
$packdiffout += 4294967296 if($packdiffout<0);
$opackin{$dev} = $packin{$dev};
......@@ -147,8 +150,8 @@ sub convert {
$packout{$dev} = $packdiffout / $period;
# bytes in/out
$diffin = ($bytesin{$dev} - $obytesin{$dev});
$diffout = ($bytesout{$dev} - $obytesout{$dev});
my $diffin = ($bytesin{$dev} - $obytesin{$dev});
my $diffout = ($bytesout{$dev} - $obytesout{$dev});
$diffin += 4294967296 if($diffin<0);
$diffout += 4294967296 if($diffout<0);
$obytesin{$dev} = $bytesin{$dev};
......@@ -165,7 +168,7 @@ sub convert {
}
sub tquad {
$n = shift;
my $n = shift;
my $a = ($n & 0xff000000) >> 24;
my $b = ($n & 0x00ff0000) >> 16;
my $c = ($n & 0x0000ff00) >> 8;
......@@ -174,8 +177,8 @@ sub tquad {
}
sub toquad {
$h = shift;
my $h = shift;
$h =~ /(\S\S)(\S\S)(\S\S)(\S\S)/;
$a = $1; $b = $2; $c = $3; $d = $4;
my ($a, $b, $c, $d) = ($1, $2, $3, $4);
return sprintf("%d.%d.%d.%d", "0x$a", "0x$b", "0x$c", "0x$d");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment