]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/speed.cgi
suricata: Change midstream policy to "pass-flow"
[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-2021 IPFire Twan <info@ipfire.org> #
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 # Function which will return the used interface for the red network zone (red0, ppp0, etc).
29 sub General__get_red_interface() {
30
31 open(IFACE, "/var/ipfire/red/iface") or die "Could not open /var/ipfire/red/iface";
32
33 my $interface = <IFACE>;
34 close(IFACE);
35 chomp $interface;
36
37 return $interface;
38 }
39 #
40 ###############################################################################
41
42 my $data_last = $ENV{'QUERY_STRING'};
43 my $rxb_last = 0;
44 my $txb_last = 0;
45
46 my (@fields, $field, $name, $value);
47 @fields = split(/&/, $data_last);
48 foreach $field (@fields) {
49 ($name, $value) = split(/=/, $field);
50 $value =~ tr/+/ /;
51 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
52 if ("$name" eq "rxb_last") {
53 $rxb_last = $value;
54 } elsif ("$name" eq "txb_last") {
55 $txb_last = $value;
56 }
57 }
58
59 my $interface = &General__get_red_interface();
60
61 open(RX, "/sys/class/net/$interface/statistics/rx_bytes") or die "Could not open /sys/class/net/$interface/statistics/rx_bytes";
62 my $rxb_now = <RX>;
63 close(RX);
64 chomp $rxb_now;
65
66 open(TX, "/sys/class/net/$interface/statistics/tx_bytes") or die "Could not open /sys/class/net/$interface/statistics/tx_bytes";
67 my $txb_now = <TX>;
68 close(TX);
69 chomp $txb_now;
70
71 my ($rx_kbs, $tx_kbs);
72 my $rxb_diff = $rxb_now - $rxb_last;
73 my $txb_diff = $txb_now - $txb_last;
74
75 if(( $rxb_diff == $rxb_now ) && ( $txb_diff == $txb_now ))
76 {
77 $rx_kbs = "0";
78 $tx_kbs = "0";
79 }
80 else
81 {
82 $rx_kbs = $rxb_diff / 1024;
83 $rx_kbs = $rx_kbs / 3.2;
84 $rx_kbs = int($rx_kbs);
85 $tx_kbs = $txb_diff / 1024;
86 $tx_kbs = $tx_kbs / 3.2;
87 $tx_kbs = int($tx_kbs);
88 }
89
90 print "pragma: no-cache\n";
91 print "Content-type: text/xml\n\n";
92 print "<?xml version=\"1.0\"?>\n";
93 print <<END
94 <inetinfo>
95 <rx_kbs>$tx_kbs kb/s</rx_kbs>
96 <tx_kbs>$rx_kbs kb/s</tx_kbs>
97 <rxb>$rxb_now</rxb>
98 <txb>$txb_now</txb>
99 </inetinfo>
100 END
101 ;