]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/traffic.cgi
91101e8361367bc98745f3072a08f90e7f168a1d
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "${General::swroot}/net-traffic/net-traffic-admin.pl";
32 require "${General::swroot}/net-traffic/net-traffic-lib.pl";
35 my %mainsettings = ();
36 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
37 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
39 my @dummy = (@NETTRAFF::months
, @NETTRAFF::longmonths
, $NETTRAFF::colorOk
, $NETTRAFF::colorWarn
, $NETTRAFF::colorMax
);
46 &General
::readhash
("${General::swroot}/ppp/settings", \
%pppsettings);
47 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
49 my @now = localtime(time);
51 $now[5] = $now[5]+1900;
53 $cgiparams{'STARTYEAR'} = $now[5];
54 $cgiparams{'STARTMONTH'} = $now[4];
59 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
61 $startDay = $NETTRAFF::settings
{'STARTDAY'};
62 $endDay = $NETTRAFF::settings
{'STARTDAY'};
65 # this periode started last month
66 if ($now[3] < $startDay)
68 # when current month is january we start in last year december
70 $cgiparams{'STARTYEAR'} = $now[5]-1;
71 $cgiparams{'STARTMONTH'} = 11;
75 $cgiparams{'STARTYEAR'} = $now[5];
76 $cgiparams{'STARTMONTH'} = $now[4]-1;
80 &Header
::getcgihash
(\
%cgiparams);
82 my $selectYearALL = "";
83 $selectYearALL = 'selected=\'selected\'' if($cgiparams{'STARTYEAR'} eq '????');
85 my $selectMonthALL = "";
86 $selectMonthALL = 'selected=\'selected\'' if($cgiparams{'STARTMONTH'} eq '??');
88 &Header
::showhttpheaders
();
89 &Header
::openpage
($Lang::tr
{'sstraffic'}, 1, '');
90 &Header
::openbigbox
('100%', 'left');
91 &Header
::openbox
('100%', 'left', "");
95 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
97 $firstDayTxt = " ($Lang::tr{'monthly volume start day short'}: $NETTRAFF::settings{'STARTDAY'}.)";
101 <table width='100%' align='center'>
103 <td width='90%' align='left' nowrap='nowrap'>
104 <form method='post' action='/cgi-bin/traffic.cgi'>
105 $Lang::tr{'selecttraffic'}$firstDayTxt:
106 <select name='STARTMONTH'>
109 foreach my $month (@NETTRAFF::months
)
112 if ("$month" eq "$cgiparams{'STARTMONTH'}") {
113 print 'selected=\'selected\' '; }
114 print "value='$month'>$NETTRAFF::longmonths[$month]</option>\n";
118 <option $selectMonthALL value='??'>$Lang::tr{'allmsg'}</option>
120 <select name='STARTYEAR'>
123 for (my $index=0; $index<=$#NETTRAFF::years
; $index++) {
125 if ("$NETTRAFF::years[$index]" eq "$cgiparams{'STARTYEAR'}") {
126 print 'selected=\'selected\' '; }
127 print "value='$NETTRAFF::years[$index]'>$NETTRAFF::years[$index]</option>\n";
131 <option $selectYearALL value='????'>$Lang::tr{'allmsg'}</option>
133 <input type='submit' name='ACTION' value='$Lang::tr{'update'}' />
136 <td width='5%' align='center'>
137 <form method='post' action='/cgi-bin/traffics.cgi'>
138 <input type='submit' name='ACTION' value=' > ' />
147 &Header
::openbox
('100%', 'left', "$Lang::tr{'traffics'}");
149 my $dateWidth = '20%';
150 my $netWidth = '34%';
151 my $inOutWidth = '17%';
154 if ($netsettings{'CONFIG_TYPE'} =~ /^(4)$/) {
160 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|3)$/) {
169 <td width='$dateWidth' align='center' class='boldbase'></td>
170 <td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficgreen'}</b></td>
173 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/) {
174 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
177 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/) {
178 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
181 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/) {
182 print "<td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>";
189 <td width='$dateWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficdate'}</b></td>
190 <td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourgreen'><b>$Lang::tr{'trafficin'}</b></font></td>
191 <td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourgreen'><b>$Lang::tr{'trafficout'}</b></font></td>
194 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
196 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='${Header::colourblue}'><b>$Lang::tr{'trafficin'}</b></font></td>";
197 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='${Header::colourblue}'><b>$Lang::tr{'trafficout'}</b></font></td>";
200 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
202 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourorange'><b>$Lang::tr{'trafficin'}</b></font></td>";
203 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourorange'><b>$Lang::tr{'trafficout'}</b></font></td>";
205 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
207 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficin'}</b></font></td>";
208 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficout'}</b></font></td>";
213 my $total_blue_out=0;
214 my $total_green_in=0;
215 my $total_green_out=0;
216 my $total_orange_in=0;
217 my $total_orange_out=0;
224 my $startYear = $cgiparams{'STARTYEAR'};
225 my $endYear = $cgiparams{'STARTYEAR'};
226 my $startMonth = $cgiparams{'STARTMONTH'};
227 my $endMonth = $cgiparams{'STARTMONTH'};
228 my $displayMode = "daily_multi";
231 my $selectedMonth = '0';
233 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
235 $startDay = $NETTRAFF::settings
{'STARTDAY'};
236 $endDay = $NETTRAFF::settings
{'STARTDAY'};
240 if ($cgiparams{'STARTYEAR'} eq '????')
242 # 'show all month' + 'show all years'
243 # OR <selected Month> + 'show all years'
245 # if we have a <selected Month>, we read all traffic but display only the selected month
246 if($cgiparams{'STARTMONTH'} ne '??')
248 $selectedMonth = $cgiparams{'STARTMONTH'} + 1;
249 $selectedMonth = $selectedMonth < 10 ?
$selectedMonth = "0".$selectedMonth : $selectedMonth;
252 $displayMode = "monthly";
253 # start with 1970-01-01
257 # end with next year: 20xx-01-01
258 $endYear = $now[5] + 1;
262 elsif ($cgiparams{'STARTMONTH'} eq '??')
264 # 'show all month' + 200x
265 $displayMode = "monthly";
266 # start with 200x-01-01
269 # end with (200x+1)-01-01
270 $endYear = $startYear + 1;
278 $endMonth = $endMonth + 2;
280 # this periode started last month
281 if ($now[3] < $startDay)
283 # when current month is january we start in last year december
284 if ($endMonth == 1) {
291 # when we are in december, this periode ends next year january
292 if ($startMonth == 12) {
301 $startMonth = $startMonth < 10 ?
$startMonth = "0".$startMonth : $startMonth;
302 $endMonth = $endMonth < 10 ?
$endMonth = "0".$endMonth : $endMonth;
303 $startDay = $startDay < 10 ?
$startDay = "0".$startDay : $startDay;
304 $endDay = $endDay < 10 ?
$endDay = "0".$endDay : $endDay;
306 my $start = "$startYear$startMonth$startDay";
307 my $end = "$endYear$endMonth$endDay";
309 my %allDaysBytes = ();
310 my @allDays = &Traffic
::calcTraffic
(\
%allDaysBytes,$start,$end, $displayMode);
315 # special code for: <selected Month> + 'show all years'
316 if($cgiparams{'STARTMONTH'} ne '??' && $cgiparams{'STARTYEAR'} eq '????')
318 # show only those traffic in the selected month
319 if($allDaysBytes{$_}{'Day'} !~ /^\d\d\d\d-$selectedMonth$/)
325 $total_green_in += $allDaysBytes{$_}{${Traffic
::green_in
}};
326 $total_green_out += $allDaysBytes{$_}{${Traffic
::green_out
}};
328 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
330 $total_blue_in += $allDaysBytes{$_}{${Traffic
::blue_in
}};
331 $total_blue_out += $allDaysBytes{$_}{${Traffic
::blue_out
}};
334 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
336 $total_orange_in += $allDaysBytes{$_}{${Traffic
::orange_in
}};
337 $total_orange_out += $allDaysBytes{$_}{${Traffic
::orange_out
}};
340 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
342 $total_red_in += $allDaysBytes{$_}{${Traffic
::red_in
}};
343 $total_red_out += $allDaysBytes{$_}{${Traffic
::red_out
}};
347 print "<tr bgcolor='$color{'color20'}'>"; }
349 print "<tr bgcolor='$color{'color22'}}'>"; }
351 printf "<td align='center' nowrap='nowrap'>%s</td>\n", $allDaysBytes{$_}{'Day'};
352 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::green_in
}}/1048576);
353 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::green_out
}}/1048576);
355 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
357 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::blue_in
}}/1048576);
358 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::blue_out
}}/1048576);
360 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
362 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::orange_in
}}/1048576);
363 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::orange_out
}}/1048576);
366 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
368 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::red_in
}}/1048576);
369 printf "<td align='center' nowrap='nowrap'>%.3f</td></tr>\n", ($allDaysBytes{$_}{${Traffic
::red_out
}}/1048576);
375 $total_green_in=sprintf("%.2f", ($total_green_in/1048576));
376 $total_green_out=sprintf("%.2f", ($total_green_out/1048576));
377 $total_blue_in=sprintf("%.2f", ($total_blue_in/1048576));
378 $total_blue_out=sprintf("%.2f", ($total_blue_out/1048576));
379 $total_orange_in=sprintf("%.2f", ($total_orange_in/1048576));
380 $total_orange_out=sprintf("%.2f", ($total_orange_out/1048576));
381 $total_red_in=sprintf("%.2f", ($total_red_in/1048576));
382 $total_red_out=sprintf("%.2f", ($total_red_out/1048576));
384 if ($lines % 2) {print "<tr bgcolor='$color{'color20'}'>"; }
385 else {print "<tr bgcolor='$color{'color22'}'>"; }
388 <td align='center' class='boldbase' height='20' nowrap='nowrap'><b>$Lang::tr{'trafficsum'}</b></td>
389 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_green_in MB</b></td>
390 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_green_out MB</b></td>
393 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
395 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_blue_in MB</b></td>";
396 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_blue_out MB</b></td>";
399 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
401 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_orange_in MB</b></td>";
402 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_orange_out MB</b></td>";
404 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
406 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_in MB</b></td>";
407 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_out MB</b></td>";
409 print "</tr></table>";
411 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
413 my $total_red_all = sprintf("%.2f", ($total_red_in + $total_red_out));
415 my $color = $NETTRAFF::colorOk
;
417 my $warnTraff = ($NETTRAFF::settings
{'MONTHLY_VOLUME'} * $NETTRAFF::settings
{'WARN'} / 100);
418 if($NETTRAFF::settings
{'WARN_ON'} eq 'on'
419 && $warnTraff < $total_red_all)
421 $color = $NETTRAFF::colorWarn
;
423 if($NETTRAFF::settings
{'MONTHLY_VOLUME'} < $total_red_all)
425 $color = $NETTRAFF::colorMax
;
430 <tr><td align='center' class='boldbase' nowrap='nowrap' ><b>$Lang::tr{'monthly volume'} ($NETTRAFF::settings{'MONTHLY_VOLUME'} MB)</b></td></tr>
431 <tr><td align='center' class='boldbase' nowrap='nowrap' bgcolor='$color'><b>$total_red_all MB</b></td></tr>
437 &Header
::closebigbox
();
438 &Header
::closepage
();