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:
|
#!/usr/bin/perl
#
# speedometer - print interface speed with dx/dt
#
#
# structure of /proc/net/dev:
# #0: jadajada
# #1: jadajada
# #2: name:bytes_in xxx xxx xxx xxx xxx xxx xxx bytes_out xxx xxx xxx xxx xxx xxx xxx
# # ...... up to 5 ifaces
#
# the interesting numbers are at position 2 (recive) and 10 (transmit)
#
#$debug=1;
use Time::HiRes;
$time = 1; # dt
print "\nSpeedometer - 2002 by Black Star\n\n";
print "listen to interfaces....";
# load interfaces
$dev = "/proc/net/dev";
open(DEV, "$dev");
if ( -e DEV ) { print " found process..."; } else { print "network is down"; last; }
@dev = <DEV>;
# look for ifaces
foreach $line (@dev) {
if ( $line =~ /:/ ) {
@temp = split(/:/, $line);
$temp[0] =~ s/ //g; #delete spaces
push(@ifaces, $temp[0]);
}
}
if (@ifaces) { print " found interfaces:"; } else { print " none or bad interfaces"; last; }
foreach $iface (@ifaces) { print " $iface"; }
close(DEV);
print "\n\nstarting speedometer listening to /proc/net/dev, waiting $time seconds and listen again. To break out hit Ctrl-C\n\n";
while (1==1) { # repeat until kill from outside
print "listen to interfaces....\n";
my @up1;
my @down1;
my @up2;
my @down2;
$starttime = [Time::HiRes::gettimeofday];
open(DEV, "$dev"); @dev = <DEV>;
foreach $line (@dev) { # get load of all ifaces
if ( $line =~ /:/ ) {
$line =~ s/( )+/ /g;
@temp = split(/[: ]/, $line);
foreach $temp (@temp) { $temp =~ s/ //g; }
if ( $debug > 1 ) { foreach $temp1 (@temp) { print "$temp|" } print "\n"; }
push(@up1, $temp[10]);
push(@down1, $temp[2]);
}
}
close(DEV);
#----wait dt
sleep($time);
$stoptime = [Time::HiRes::gettimeofday];
open(DEV, "$dev"); @dev = <DEV>;
foreach $line (@dev) { # get load of all ifaces
if ( $line =~ /:/ ) {
$line =~ s/( )+/ /g;
@temp = split(/[: ]/, $line);
foreach $temp (@temp) { $temp =~ s/ //g; }
if ( $debug > 1 ) { foreach $temp (@temp) { print "$temp|" } print "\n"; }
push(@up2, $temp[10]);
push(@down2, $temp[2]);
}
}
close(DEV);
$elapsed = Time::HiRes::tv_interval($starttime, $stoptime);
# calculate d/dt
foreach $iface (@ifaces) {
$up2 = shift(@up2);
$up1 = shift(@up1);
$down2 = shift(@down2);
$down1 = shift(@down1);
if ( $debug ) {
print "$iface | $up2 | $up1 | $down2 | $down1\n";
} else {
$load_up_bytes = ($up2 - $up1) / $elapsed; $load_up_kbytes = $load_up_bytes / 1024;
$load_down_bytes = ($down2 - $down1) / $elapsed; $load_down_kbytes = $load_down_bytes / 1024;
# delete fuckin point numbers
$load_up_bytes =~ s/\.[0-9]+//; $load_up_kbytes =~ s/\.[0-9]+//;
$load_down_bytes =~ s/\.[0-9]+//; $load_down_kbytes =~ s/\.[0-9]+//;
print "$iface\tup = $load_up_bytes bps\tdown = $load_down_bytes bps\t ||\t up = $load_up_kbytes kbps\t down = $load_down_kbytes kbps\n";
}
}
}
print "\n\nexit code ok...\n\n"; |