Commit ac449302 authored by Peter Pentchev's avatar Peter Pentchev

Move the main loop down.

parent da0afe2d
......@@ -31,8 +31,6 @@ my ($COLOR, $addtime, $count, $iface);
my %prevstat;
my %total = (kb => {}, packets => {});
sub convert();
sub usage($)
{
my ($err) = @_;
......@@ -86,75 +84,6 @@ sub reset_totals()
%total = (kb => {}, packets => {});
}
$| = 1;
#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
# 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
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};
$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;
}
$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 $op = $period;
$period = 1;
convert;
sleep $period;
while(1) {
my $devs = convert;
print time.' ' if $addtime;
if (scalar keys %{$devs} > 1) {
say acolor('yellow').
format_line('total', \%total).
acolor('reset');
}
foreach my $dev (sort { $a->{name} cmp $b->{name} } values %{$devs}) {
say format_line($dev->{name}, $dev->{data});
}
if (defined $count) {
$count--;
exit 0 if $count < 1;
}
$period = $op;
sleep $period;
}
sub update_traffic($ $)
{
my ($dev, $odev) = @_;
......@@ -184,6 +113,12 @@ sub add_up_totals($ $)
}
}
#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
# 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()
{
open my $in, '/proc/net/dev' or die "Can't open /proc/net/dev: $!\n";
......@@ -226,3 +161,66 @@ sub convert()
return \%devs;
}
$| = 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};
$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;
}
$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 $op = $period;
$period = 1;
convert;
sleep $period;
while(1) {
my $devs = convert;
print time.' ' if $addtime;
if (scalar keys %{$devs} > 1) {
say acolor('yellow').
format_line('total', \%total).
acolor('reset');
}
foreach my $dev (sort { $a->{name} cmp $b->{name} } values %{$devs}) {
say format_line($dev->{name}, $dev->{data});
}
if (defined $count) {
$count--;
exit 0 if $count < 1;
}
$period = $op;
sleep $period;
}
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