Commit 3c642743 authored by Peter Pentchev's avatar Peter Pentchev

Use fewer parentheses and quotes.

parent 18ec51a4
......@@ -17,6 +17,7 @@ Change log for ethstats, the network traffic statistics utility
- remove the unused tquad() and toquad() functions
- break the output formatting out into format_line()
- use the version module
- use fewer parentheses and quotes
1.0 (unknown)
- released into the public domain by Drew Strieb
......
......@@ -16,6 +16,8 @@ BEGIN {
};
}
sub convert();
sub usage($)
{
my ($err) = @_;
......@@ -85,36 +87,36 @@ $| = 1;
my %opts;
getopts('Cc:hi:Mn:tV', \%opts) or usage(1);
version() if $opts{'V'};
usage(0) if $opts{'h'};
exit(0) if $opts{'V'} || $opts{'h'};
getopts 'Cc:hi:Mn:tV', \%opts or usage(1);
version if $opts{V};
usage 0 if $opts{h};
exit 0 if $opts{V} || $opts{h};
$addtime = 1 if defined($opts{'t'});
if (defined($opts{'n'})) {
if ($opts{'n'} !~ /^([1-9]\d*)$/) {
warn("The period must be a positive integer\n");
usage(1);
$addtime = $opts{t};
if (defined $opts{n}) {
if ($opts{n} !~ /^([1-9]\d*)$/) {
warn "The period must be a positive integer\n";
usage 1;
}
$period = $1;
}
if (defined($opts{'c'})) {
if ($opts{'c'} !~ /^([1-9]\d*)$/) {
warn("The count must be a positive integer\n");
usage(1);
if (defined $opts{c}) {
if ($opts{c} !~ /^([1-9]\d*)$/) {
warn "The count must be a positive integer\n";
usage 1;
}
$count = $1;
}
$iface = $opts{'i'}; # also works if it isn't defined
$iface = $opts{i}; # also works if it isn't defined
if (defined($opts{'C'})) {
if (defined($opts{'M'})) {
if ($opts{C}) {
if ($opts{M}) {
die "The -C and -M options are mutually exclusive\n";
} elsif (!$have_ansi_color) {
die "The Term::ANSIColor Perl module is not available\n";
}
$COLOR = 1;
} elsif (defined($opts{'M'})) {
} elsif ($opts{M}) {
$COLOR = 0;
} else {
$COLOR = $have_ansi_color && -t \*STDOUT;
......@@ -122,40 +124,41 @@ if (defined($opts{'C'})) {
my $op = $period;
$period = 1;
convert();
convert;
sleep $period;
while(1) {
convert();
print time()." " if $addtime;
convert;
print time.' ' if $addtime;
if ($numdevs > 1) {
say acolor('yellow').
format_line('total', $tkbin, $tkbout, $tpackin, $tpackout).
acolor('reset');
}
foreach my $dev(sort keys %kbin) {
foreach my $dev (sort keys %kbin) {
say format_line($dev, $kbin{$dev}, $kbout{$dev},
$packin{$dev}, $packout{$dev});
}
if (defined($count)) {
if (defined $count) {
$count--;
exit(0) if $count < 1;
exit 0 if $count < 1;
}
$period = $op;
sleep $period;
}
sub convert {
open(IN, "/proc/net/dev") || die("Can't open ip_acct: $!\n");
sub convert()
{
open IN, "/proc/net/dev" || die "Can't open ip_acct: $!\n";
<IN>; <IN>;
my (%bytesin, %bytesout);
while(my $l = <IN>) {
chop($l);
my ($dev, $rest) = split(/:/, $l);
while (my $l = <IN>) {
chop $l;
my ($dev, $rest) = split /:/, $l;
$dev =~ s/\s//g;
next if defined($iface) && $dev ne $iface;
next if defined $iface && $dev ne $iface;
$rest =~ s/^\s+//;
my @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];
}
......@@ -167,25 +170,25 @@ sub convert {
$tkbin = 0;
$tkbout = 0;
foreach my $dev(sort keys %bytesin) {
next if($dev eq "lo");
foreach my $dev (sort keys %bytesin) {
next if $dev eq 'lo';
$numdevs++;
# packets in/out
my $packdiffin = ($packin{$dev} - ($opackin{$dev} // 0));
my $packdiffout = ($packout{$dev} - ($opackout{$dev} // 0));
$packdiffin += 4294967296 if($packdiffin<0);
$packdiffout += 4294967296 if($packdiffout<0);
my $packdiffin = $packin{$dev} - ($opackin{$dev} // 0);
my $packdiffout = $packout{$dev} - ($opackout{$dev} // 0);
$packdiffin += 4294967296 if $packdiffin < 0;
$packdiffout += 4294967296 if $packdiffout < 0;
$opackin{$dev} = $packin{$dev};
$opackout{$dev} = $packout{$dev};
$packin{$dev} = $packdiffin / $period;
$packout{$dev} = $packdiffout / $period;
# bytes in/out
my $diffin = ($bytesin{$dev} - ($obytesin{$dev} // 0));
my $diffout = ($bytesout{$dev} - ($obytesout{$dev} // 0));
$diffin += 4294967296 if($diffin<0);
$diffout += 4294967296 if($diffout<0);
my $diffin = $bytesin{$dev} - ($obytesin{$dev} // 0);
my $diffout = $bytesout{$dev} - ($obytesout{$dev} // 0);
$diffin += 4294967296 if $diffin < 0;
$diffout += 4294967296 if $diffout < 0;
$obytesin{$dev} = $bytesin{$dev};
$obytesout{$dev} = $bytesout{$dev};
$kbin{$dev} = $diffin / $period / 1000000 * 8;
......
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