-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwmonitor.pl
executable file
·194 lines (166 loc) · 3.77 KB
/
bwmonitor.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl
# bwmonitor.pl - Attempts to estimate the average bandwidth utilized
# over a given interface.
#
# Copyright (c) 2010-2012, Patrick T. Rutledge III.
#
# You may distribute under the terms of the GNU General Public License.
#
# v1.00 - Initial Public Release
#
# The author assumes no responsibility over the accuracy of the data provided.
# Use this tool at your own risk.
#
use Getopt::Long;
use Pod::Usage;
my $help = 0;
my $defiterations=5;
my $s=1;
my $result = GetOptions ( "count|c=i" => \$iterations,
"int|i=s" => \$int,
"bytes|b" => \$bytes,
"bits|B" => \$bits,
"help|?" => \$help) or pod2usage(2);
pod2usage(1) if $help;
if ( ! $int ) {
print "ERROR: Please provide --int=[interface] or --help.\n";
exit 1;
} else {
$inttest=`/bin/cat /proc/net/dev|grep "^[[:space:]]*$int:"`;
if ( ! $inttest ) {
print "ERROR: Interface $int is invalid.\n";
exit 1;
}
}
if ( $iterations ) {
undef $s;
} else {
$iterations=$defiterations;
}
if ( $iterations < 2 ) {
exit;
}
# Default to bytes, because thats what the kernel reports
if ( ! $bits && ! $bytes ) {
$bytes=1;
}
my @trend;
my $x=1;
my $lastx;
my $oldin;
my $oldout;
my $curin;
my $curout;
my $curbps;
my $totalxfer;
$SIG{INT} = \&average;
print "Realtime bandwidth monitor for interface [$int]";
if ( $s ) {
print " CTRL-C to end.\n";
} else {
print ".\n";
}
# need to add a second to get the diff between first two datapoints
my $runiterations=$iterations+1;
my $x=1;
while ( $x <= $runiterations || $s ) {
my $procout=`/bin/cat /proc/net/dev|grep "^[[:space:]]*$int:"|/bin/sed -e "s/^[[:space:]]*$int://"|/bin/awk '{print \$1":"\$9}'`;
my ($in,$out)=split(/:/,$procout);
chomp($out);
$trend[$x]=$in+$out;
if ( $x != 1 ) {
$lastx=$x-1;
$curbps=$trend[$x]-$trend[$lastx];
$curin=$in-$oldin;
$curout=$out-$oldout;
$totalxfer=$trend[$x]-$trend[1];
print &humanize($curbps) . ": " . &humanize($totalxfer,'total') . " total (" . &humanize($curin,'notsec') . " in/" . &humanize($curout,'notsec') . " out)\n";
}
$avgsecs=$x;
$oldin=$in;
$oldout=$out;
$x++;
sleep(1);
}
# this shouldnt happen.. but
if ( $s ) { exit 1; }
average();
sub humanize {
my ( $in, $flag ) = @_ ;
my $out;
my $outbits;
my $outbytes;
my $suffix;
if ( $in <= 1023 ) {
$outbytes=1;
$outbits=1;
} elsif ( $in <= 1048999 ) {
$suffix="K";
$outbytes=1024;
$outbits=1000;
} elsif ( $in <= 1073999999 ) {
$suffix="M";
$outbytes=1049000;
$outbits=1000000;
} else {
$suffix="G";
$outbytes=1074000000;
$outbits=1000000000;
}
if ( $bits && $flag ne 'total' ) {
if ( $flag eq 'notsec' ) {
$suffix.="b";
} else {
$suffix.="bps";
}
$out=($in*8)/$outbits;
} else {
if ( $flag eq 'notsec' || $flag eq 'total' ) {
$suffix.="B";
} else {
$suffix.="B/s";
}
$out=$in/$outbytes;
}
$out=sprintf("%.2f", $out);
return "$out $suffix";
}
sub average {
if ( @trend ) {
my $total;
my $diff;
my $last;
my $avg;
my $x=1;
while ( $x <= $avgsecs ) {
if ( $x != 1 ) {
$diff=$trend[$x]-$last;
$total=$total+$diff;
}
$last=$trend[$x];
$x++;
}
# Remove extra datapoint
my $avgsecs_display=$avgsecs-1;
if ( $avgsecs_display < 1 ) {
exit;
}
my $avg=$total/$avgsecs_display;
print "\n--- [$int] realtime bandwidth monitor statistics ---\n";
print &humanize($avg) . " avg over " . $avgsecs_display . " seconds, " . &humanize($total,'total') . " transferred.\n";
}
exit 0;
}
__END__
=head1 NAME
bwmonitor.pl - Display realtime network bandwidth utilization for a given interface.
=head1 SYNOPSIS
bwmonitor.pl [options] --int=[interface]
Required:
--int=interface - run test on specified interface
Options:
--count=num - run test over num seconds
--bytes - produce output in bytes
--bits - produce output in bits
--help - this help message
=cut