]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/speed.cgi
3436dd3b3a7e455677f8279bfbea1109a18bfcc4
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / speed.cgi
1 #!/usr/bin/perl
2 #
3 # IPFire CGIs
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 # (c) The IPFire Team
8 #
9
10 my $data_last = $ENV{'QUERY_STRING'};
11 my $rxb_last = 0;
12 my $txb_last = 0;
13
14 my (@fields, $field, $name, $value);
15 @fields = split(/&/, $data_last);
16 foreach $field (@fields) {
17 ($name, $value) = split(/=/, $field);
18 $value =~ tr/+/ /;
19 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
20 if ("$name" eq "rxb_last") {
21 $rxb_last = $value;
22 } elsif ("$name" eq "txb_last") {
23 $txb_last = $value;
24 }
25 }
26
27 my @data_now = `ip -s link show red0`;
28
29 my $lastline;
30 my $rxb_now = 0;
31 my $txb_now = 0;
32 foreach (@data_now) {
33 if ( $lastline =~ /RX/ ) {
34 @fields = split(/ /, $_);
35 $rxb_now = $fields[4];
36 } elsif ( $lastline =~ /TX/ ) {
37 @fields = split(/ /, $_);
38 $txb_now = $fields[4];
39 }
40 $lastline = $_;
41 }
42
43 my ($rx_kbs, $tx_kbs);
44 my $rxb_diff = $rxb_now - $rxb_last;
45 my $txb_diff = $txb_now - $txb_last;
46
47 if(( $rxb_diff == $rxb_now ) && ( $txb_diff == $txb_now ))
48 {
49 $rx_kbs = "0.00";
50 $tx_kbs = "0.00";
51 }
52 else
53 {
54 $rx_kbs = $rxb_diff / 1024;
55 $rx_kbs = $rx_kbs / 2.2;
56 $rx_kbs = int($rx_kbs);
57 $tx_kbs = $txb_diff / 1024;
58 $tx_kbs = $tx_kbs / 2.2;
59 $tx_kbs = int($tx_kbs);
60 }
61
62 print "Content-type: text/xml\n\n";
63 print "<?xml version=\"1.0\"?>\n";
64 print <<END
65 <inetinfo>
66 <rx_kbs>$tx_kbs kb/s</rx_kbs>
67 <tx_kbs>$rx_kbs kb/s</tx_kbs>
68 <rxb>$rxb_now</rxb>
69 <txb>$txb_now</txb>
70 </inetinfo>
71 END
72 ;