]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/traffic.cgi
Ein Paar Dateien fuer die GPLv3 angepasst.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / traffic.cgi
CommitLineData
10a04d70 1#!/usr/bin/perl
70df8302
MT
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###############################################################################
10a04d70
MT
21
22use strict;
23
24# enable only the following on debugging purpose
cb5e9c6c
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
10a04d70
MT
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
d81292e0
CS
31require "${General::swroot}/net-traffic/net-traffic-admin.pl";
32require "${General::swroot}/net-traffic/net-traffic-lib.pl";
10a04d70 33
f2fdd0c1
CS
34my %color = ();
35my %mainsettings = ();
36&General::readhash("${General::swroot}/main/settings", \%mainsettings);
37&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
38
d81292e0
CS
39my @dummy = (@NETTRAFF::months, @NETTRAFF::longmonths, $NETTRAFF::colorOk, $NETTRAFF::colorWarn, $NETTRAFF::colorMax);
40undef(@dummy);
41
42my %cgiparams;
10a04d70
MT
43my %pppsettings;
44my %netsettings;
45
46&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
47&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
48
d81292e0
CS
49my @now = localtime(time);
50
51$now[5] = $now[5]+1900;
52
53$cgiparams{'STARTYEAR'} = $now[5];
54$cgiparams{'STARTMONTH'} = $now[4];
55
56my $startDay = '1';
57my $endDay = '1';
58
59if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
60{
61 $startDay = $NETTRAFF::settings{'STARTDAY'};
62 $endDay = $NETTRAFF::settings{'STARTDAY'};
63}
64
65# this periode started last month
66if ($now[3] < $startDay)
67{
68 # when current month is january we start in last year december
69 if ($now[4] == 0) {
70 $cgiparams{'STARTYEAR'} = $now[5]-1;
71 $cgiparams{'STARTMONTH'} = 11;
72 }
73 else
74 {
75 $cgiparams{'STARTYEAR'} = $now[5];
76 $cgiparams{'STARTMONTH'} = $now[4]-1;
77 }
78}
10a04d70 79
10a04d70
MT
80&Header::getcgihash(\%cgiparams);
81
d81292e0
CS
82my $selectYearALL = "";
83$selectYearALL = 'selected=\'selected\'' if($cgiparams{'STARTYEAR'} eq '????');
10a04d70 84
d81292e0
CS
85my $selectMonthALL = "";
86$selectMonthALL = 'selected=\'selected\'' if($cgiparams{'STARTMONTH'} eq '??');
10a04d70 87
d81292e0 88&Header::showhttpheaders();
10a04d70 89&Header::openpage($Lang::tr{'sstraffic'}, 1, '');
10a04d70 90&Header::openbigbox('100%', 'left');
d81292e0 91&Header::openbox('100%', 'left', "");
10a04d70 92
d81292e0
CS
93my $firstDayTxt = '';
94
95if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
96{
97 $firstDayTxt = " ($Lang::tr{'monthly volume start day short'}: $NETTRAFF::settings{'STARTDAY'}.)";
98}
10a04d70
MT
99
100print <<END;
d81292e0 101 <table width='100%' align='center'>
10a04d70 102 <tr>
d81292e0 103 <td width='90%' align='left' nowrap='nowrap'>
10a04d70 104 <form method='post' action='/cgi-bin/traffic.cgi'>
d81292e0
CS
105 $Lang::tr{'selecttraffic'}$firstDayTxt:
106 <select name='STARTMONTH'>
107END
108
109foreach my $month (@NETTRAFF::months)
110{
111 print "\t<option ";
112 if ("$month" eq "$cgiparams{'STARTMONTH'}") {
113 print 'selected=\'selected\' '; }
114 print "value='$month'>$NETTRAFF::longmonths[$month]</option>\n";
115}
116
117print <<END;
118 <option $selectMonthALL value='??'>$Lang::tr{'allmsg'}</option>
10a04d70 119 </select>
d81292e0
CS
120 <select name='STARTYEAR'>
121END
122
123for (my $index=0; $index<=$#NETTRAFF::years; $index++) {
124 print "\t<option ";
125 if ("$NETTRAFF::years[$index]" eq "$cgiparams{'STARTYEAR'}") {
126 print 'selected=\'selected\' '; }
127 print "value='$NETTRAFF::years[$index]'>$NETTRAFF::years[$index]</option>\n";
128}
129
130print <<END;
131 <option $selectYearALL value='????'>$Lang::tr{'allmsg'}</option>
10a04d70
MT
132 </select>
133 <input type='submit' name='ACTION' value='$Lang::tr{'update'}' />
134 </form>
d81292e0 135 </td>
10a04d70
MT
136 <td width='5%' align='center'>
137 <form method='post' action='/cgi-bin/traffics.cgi'>
138 <input type='submit' name='ACTION' value=' > ' />
139 </form>
d81292e0 140 </td>
10a04d70
MT
141 </tr>
142 </table>
143END
144
145&Header::closebox();
146
d81292e0 147&Header::openbox('100%', 'left', "$Lang::tr{'traffics'}");
10a04d70
MT
148
149my $dateWidth = '20%';
150my $netWidth = '34%';
151my $inOutWidth = '17%';
152
153# 4 networks
b05768be 154if ($netsettings{'CONFIG_TYPE'} =~ /^(4)$/) {
10a04d70
MT
155 $dateWidth = '12%';
156 $netWidth = '22%';
157 $inOutWidth = '11%';
158}
159# 3 networks
b05768be 160if ($netsettings{'CONFIG_TYPE'} =~ /^(2|3)$/) {
10a04d70
MT
161 $dateWidth = '16%';
162 $netWidth = '28%';
163 $inOutWidth = '14%';
164}
165
166print <<END;
167 <table width='100%'>
168 <tr>
169 <td width='$dateWidth' align='center' class='boldbase'></td>
170 <td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficgreen'}</b></td>
171END
172
b05768be 173if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/) {
10a04d70
MT
174 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
175}
176
b05768be 177if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/) {
10a04d70
MT
178 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
179}
180
afbda815 181if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/) {
d81292e0
CS
182 print "<td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>";
183}
10a04d70 184print <<END;
10a04d70
MT
185 </tr>
186 </table>
187 <table width='100%'>
188 <tr>
189 <td width='$dateWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficdate'}</b></td>
d81292e0
CS
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>
10a04d70
MT
192END
193
b05768be 194if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
d81292e0 195{
10a04d70
MT
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>";
d81292e0 198}
10a04d70 199
b05768be 200if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
d81292e0
CS
201{
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>";
204}
b05768be
CS
205if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
206{
d81292e0
CS
207print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficin'}</b></font></td>";
208print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficout'}</b></font></td>";
b05768be 209}
d81292e0 210print "</tr>";
10a04d70
MT
211
212my $total_blue_in=0;
213my $total_blue_out=0;
214my $total_green_in=0;
215my $total_green_out=0;
216my $total_orange_in=0;
217my $total_orange_out=0;
218my $total_red_in=0;
219my $total_red_out=0;
220my $lines=0;
221
10a04d70 222
d81292e0
CS
223
224my $startYear = $cgiparams{'STARTYEAR'};
225my $endYear = $cgiparams{'STARTYEAR'};
226my $startMonth = $cgiparams{'STARTMONTH'};
227my $endMonth = $cgiparams{'STARTMONTH'};
228my $displayMode = "daily_multi";
229$startDay = '1';
230$endDay = '1';
231my $selectedMonth = '0';
232
233if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
234{
235 $startDay = $NETTRAFF::settings{'STARTDAY'};
236 $endDay = $NETTRAFF::settings{'STARTDAY'};
237}
238
239# "show All ?
240if ($cgiparams{'STARTYEAR'} eq '????')
241{
242 # 'show all month' + 'show all years'
243 # OR <selected Month> + 'show all years'
244
245 # if we have a <selected Month>, we read all traffic but display only the selected month
246 if($cgiparams{'STARTMONTH'} ne '??')
247 {
248 $selectedMonth = $cgiparams{'STARTMONTH'} + 1;
249 $selectedMonth = $selectedMonth < 10 ? $selectedMonth = "0".$selectedMonth : $selectedMonth;
250 }
251
252 $displayMode = "monthly";
253 # start with 1970-01-01
254 $startYear = 1970;
255 $startMonth = '1';
256 $startDay = '1';
257 # end with next year: 20xx-01-01
258 $endYear = $now[5] + 1;
259 $endMonth = '1';
260 $endDay = '1';
261}
262elsif ($cgiparams{'STARTMONTH'} eq '??')
263{
264 # 'show all month' + 200x
10a04d70 265 $displayMode = "monthly";
d81292e0
CS
266 # start with 200x-01-01
267 $startMonth = '1';
268 $startDay = '1';
269 # end with (200x+1)-01-01
270 $endYear = $startYear + 1;
271 $endMonth = '1';
272 $endDay = '1';
273}
274else
275{
276 # no "Show All"
277 $startMonth++;
278 $endMonth = $endMonth + 2;
279
280 # this periode started last month
281 if ($now[3] < $startDay)
282 {
283 # when current month is january we start in last year december
284 if ($endMonth == 1) {
285 $startYear--;
286 $startMonth = 12;
287 }
288 }
289 else
290 {
291 # when we are in december, this periode ends next year january
292 if ($startMonth == 12) {
293 $endYear++;
294 $endMonth = 1;
295 }
296 }
10a04d70
MT
297}
298
d81292e0
CS
299
300
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;
305
306my $start = "$startYear$startMonth$startDay";
307my $end = "$endYear$endMonth$endDay";
308
10a04d70
MT
309my %allDaysBytes = ();
310my @allDays = &Traffic::calcTraffic(\%allDaysBytes,$start,$end, $displayMode);
311
312
d81292e0
CS
313foreach (@allDays)
314{
315 # special code for: <selected Month> + 'show all years'
316 if($cgiparams{'STARTMONTH'} ne '??' && $cgiparams{'STARTYEAR'} eq '????')
317 {
318 # show only those traffic in the selected month
319 if($allDaysBytes{$_}{'Day'} !~ /^\d\d\d\d-$selectedMonth$/)
320 {
321 next;
322 }
323 }
324
10a04d70
MT
325 $total_green_in += $allDaysBytes{$_}{${Traffic::green_in}};
326 $total_green_out += $allDaysBytes{$_}{${Traffic::green_out}};
d81292e0 327
b05768be 328 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
10a04d70
MT
329 {
330 $total_blue_in += $allDaysBytes{$_}{${Traffic::blue_in}};
331 $total_blue_out += $allDaysBytes{$_}{${Traffic::blue_out}};
332 }
d81292e0 333
b05768be 334 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
10a04d70
MT
335 {
336 $total_orange_in += $allDaysBytes{$_}{${Traffic::orange_in}};
337 $total_orange_out += $allDaysBytes{$_}{${Traffic::orange_out}};
338 }
d81292e0 339
b05768be 340 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
d81292e0 341 {
10a04d70
MT
342 $total_red_in += $allDaysBytes{$_}{${Traffic::red_in}};
343 $total_red_out += $allDaysBytes{$_}{${Traffic::red_out}};
d81292e0
CS
344 }
345
10a04d70 346 if ($lines % 2) {
f2fdd0c1 347 print "<tr bgcolor='$color{'color20'}'>"; }
10a04d70 348 else {
d81292e0
CS
349 print "<tr bgcolor='$color{'color22'}}'>"; }
350
10a04d70
MT
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);
d81292e0 354
b05768be 355 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
d81292e0 356 {
10a04d70
MT
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);
d81292e0 359 }
b05768be 360 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
d81292e0 361 {
10a04d70
MT
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);
d81292e0
CS
364 }
365
366 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
b05768be 367 {
10a04d70
MT
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);
d81292e0
CS
370 }
371
10a04d70
MT
372 $lines++;
373}
374
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));
d81292e0 380$total_orange_out=sprintf("%.2f", ($total_orange_out/1048576));
10a04d70
MT
381$total_red_in=sprintf("%.2f", ($total_red_in/1048576));
382$total_red_out=sprintf("%.2f", ($total_red_out/1048576));
d81292e0 383
f2fdd0c1
CS
384if ($lines % 2) {print "<tr bgcolor='$color{'color20'}'>"; }
385else {print "<tr bgcolor='$color{'color22'}'>"; }
d81292e0 386
10a04d70
MT
387print <<END;
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>
391END
d81292e0 392
b05768be 393if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
d81292e0 394{
10a04d70
MT
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>";
397}
398
b05768be 399if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
d81292e0 400{
10a04d70
MT
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>";
403}
b05768be
CS
404if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
405{
d81292e0
CS
406print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_in MB</b></td>";
407print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_out MB</b></td>";
408}
409print "</tr></table>";
410
411if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
412{
413 my $total_red_all = sprintf("%.2f", ($total_red_in + $total_red_out));
414
415 my $color = $NETTRAFF::colorOk;
416
417 my $warnTraff = ($NETTRAFF::settings{'MONTHLY_VOLUME'} * $NETTRAFF::settings{'WARN'} / 100);
418 if($NETTRAFF::settings{'WARN_ON'} eq 'on'
419 && $warnTraff < $total_red_all)
420 {
421 $color = $NETTRAFF::colorWarn;
422 }
423 if($NETTRAFF::settings{'MONTHLY_VOLUME'} < $total_red_all)
424 {
425 $color = $NETTRAFF::colorMax;
426 }
427
428 print <<END;
429 <table width='100%'>
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>
432 </table>
10a04d70 433END
d81292e0 434}
10a04d70
MT
435
436&Header::closebox();
10a04d70 437&Header::closebigbox();
10a04d70 438&Header::closepage();