]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/speed.cgi
speed.cgi: reduce system load by copying two general-functions.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / speed.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 ###############################################################################
23 # functions copied from general-functions.pl for speed improvement because #
24 # loading and initializing the whole general-functions.pl every second create #
25 # high system load #
26 ###########################################OA##################################
27 #
28 # Returns the output of a shell command
29 sub General__system_output($) {
30 my @command = @_;
31 my $pid;
32 my @output = ();
33
34 unless ($pid = open(OUTPUT, "-|")) {
35 open(STDERR, ">&STDOUT");
36 exec { ${command[0]} } @command;
37 die "Could not execute @command: $!";
38 }
39
40 waitpid($pid, 0);
41
42 while (<OUTPUT>) {
43 push(@output, $_);
44 }
45
46 close(OUTPUT);
47 return @output;
48 }
49 #
50 # Function which will return the used interface for the red network zone (red0, ppp0, etc).
51 sub General__get_red_interface() {
52
53 open(IFACE, "/var/ipfire/red/iface") or die "Could not open /var/ipfire/red/iface";
54
55 my $interface = <IFACE>;
56 close(IFACE);
57 chomp $interface;
58
59 return $interface;
60 }
61 #
62 ###############################################################################
63
64 my $data_last = $ENV{'QUERY_STRING'};
65 my $rxb_last = 0;
66 my $txb_last = 0;
67
68 my (@fields, $field, $name, $value);
69 @fields = split(/&/, $data_last);
70 foreach $field (@fields) {
71 ($name, $value) = split(/=/, $field);
72 $value =~ tr/+/ /;
73 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
74 if ("$name" eq "rxb_last") {
75 $rxb_last = $value;
76 } elsif ("$name" eq "txb_last") {
77 $txb_last = $value;
78 }
79 }
80
81 my $interface = &General__get_red_interface();
82 my @data_now = &General__system_output("ip", "-s", "link", "show", "$interface");
83
84 my $lastline;
85 my $rxb_now = 0;
86 my $txb_now = 0;
87 foreach (@data_now) {
88 if ( $lastline =~ /RX/ ) {
89 @fields = split(/ /, $_);
90 $rxb_now = $fields[4];
91 } elsif ( $lastline =~ /TX/ ) {
92 @fields = split(/ /, $_);
93 $txb_now = $fields[4];
94 }
95 $lastline = $_;
96 }
97
98 my ($rx_kbs, $tx_kbs);
99 my $rxb_diff = $rxb_now - $rxb_last;
100 my $txb_diff = $txb_now - $txb_last;
101
102 if(( $rxb_diff == $rxb_now ) && ( $txb_diff == $txb_now ))
103 {
104 $rx_kbs = "0";
105 $tx_kbs = "0";
106 }
107 else
108 {
109 $rx_kbs = $rxb_diff / 1024;
110 $rx_kbs = $rx_kbs / 3.2;
111 $rx_kbs = int($rx_kbs);
112 $tx_kbs = $txb_diff / 1024;
113 $tx_kbs = $tx_kbs / 3.2;
114 $tx_kbs = int($tx_kbs);
115 }
116
117 print "pragma: no-cache\n";
118 print "Content-type: text/xml\n\n";
119 print "<?xml version=\"1.0\"?>\n";
120 print <<END
121 <inetinfo>
122 <rx_kbs>$tx_kbs kb/s</rx_kbs>
123 <tx_kbs>$rx_kbs kb/s</tx_kbs>
124 <rxb>$rxb_now</rxb>
125 <txb>$txb_now</txb>
126 </inetinfo>
127 END
128 ;