Commit 5574bb19 authored by Peter Pentchev's avatar Peter Pentchev

Reduce the scope of some variables.

parent 65ea172c
......@@ -8,6 +8,7 @@ Change log for ethstats, the network traffic statistics utility
forwards-compatibility
- use autovivification instead of an unwiedly defined-or
- drop a needless sorting while fetching the interface stats
- reduce the scope of some variables
1.1.0 2015/12/11
- use Getopt::Std for command-line option processing
......
......@@ -26,9 +26,7 @@ BEGIN {
};
}
my ($COLOR, $addtime, $count, $iface);
my %prevstat;
my %total = (kb => {}, packets => {});
sub usage($)
{
......@@ -60,13 +58,6 @@ sub version()
say "ethstats $main::VERSION";
}
sub acolor($)
{
my ($color) = @_;
return $COLOR? Term::ANSIColor::color($color): '';
}
sub format_line($ $)
{
my ($name, $dev) = @_;
......@@ -78,11 +69,6 @@ sub format_line($ $)
$dev->{packets}{in}, $dev->{packets}{out});
}
sub reset_totals()
{
%total = (kb => {}, packets => {});
}
sub update_traffic($ $ $)
{
my ($dev, $odev, $period) = @_;
......@@ -116,9 +102,9 @@ sub add_up_totals($ $)
# eth0: 1217210 9400 0 0 0 8 0 11 1207648 8019 0 0 0 0 0 0
# eth1: 2039952 21982 6 0 0 6 0 0 47000710 34813 0 0 0 821 0 0
sub convert($)
sub convert($ $ $)
{
my ($period) = @_;
my ($iface, $period, $total) = @_;
open my $in, '/proc/net/dev' or die "Can't open /proc/net/dev: $!\n";
<$in>; <$in>;
......@@ -146,7 +132,7 @@ sub convert($)
}
close($in);
reset_totals;
%{$total} = (kb => {}, packets => {});
delete $devs{lo};
foreach my $dev (values %devs) {
$prevstat{$dev->{name}} //= { name => $dev->{name} };
......@@ -155,7 +141,7 @@ sub convert($)
in => $dev->{data}->{bytes}->{in} / 1000000 * 8,
out => $dev->{data}->{bytes}->{out} / 1000000 * 8,
};
add_up_totals \%total, $dev->{data};
add_up_totals $total, $dev->{data};
}
return \%devs;
......@@ -173,7 +159,7 @@ MAIN:
usage 0 if $opts{h};
exit 0 if $opts{V} || $opts{h};
$addtime = $opts{t};
my $addtime = $opts{t};
if (defined $opts{n}) {
if ($opts{n} !~ /^([1-9]\d*)$/) {
warn "The period must be a positive integer\n";
......@@ -181,6 +167,7 @@ MAIN:
}
$period = $1;
}
my $count;
if (defined $opts{c}) {
if ($opts{c} !~ /^([1-9]\d*)$/) {
warn "The count must be a positive integer\n";
......@@ -188,31 +175,33 @@ MAIN:
}
$count = $1;
}
$iface = $opts{i}; # also works if it isn't defined
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 ($opts{M}) {
$COLOR = 0;
} else {
$COLOR = $have_ansi_color && -t \*STDOUT;
my $iface = $opts{i}; # also works if it isn't defined
my $use_color;
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";
}
$use_color = 1;
} elsif ($opts{M}) {
$use_color = 0;
} else {
$use_color = $have_ansi_color && -t \*STDOUT;
}
my %c = map { $_ => $use_color? Term::ANSIColor::color($_): '' }
qw(yellow reset);
convert 1;
my $total = {};
convert $iface, 1, $total;
sleep 1;
while(1) {
my $devs = convert $period;
my $devs = convert $iface, $period, $total;
print time.' ' if $addtime;
if (scalar keys %{$devs} > 1) {
say acolor('yellow').
format_line('total', \%total).
acolor('reset');
say $c{yellow}.format_line('total', $total).$c{reset};
}
foreach my $dev (sort { $a->{name} cmp $b->{name} } values %{$devs}) {
say format_line($dev->{name}, $dev->{data});
......
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