]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame_incremental - html/cgi-bin/traffic.cgi
Ein Paar Dateien fuer die GPLv3 angepasst.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / traffic.cgi
... / ...
CommitLineData
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
22use strict;
23
24# enable only the following on debugging purpose
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31require "${General::swroot}/net-traffic/net-traffic-admin.pl";
32require "${General::swroot}/net-traffic/net-traffic-lib.pl";
33
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
39my @dummy = (@NETTRAFF::months, @NETTRAFF::longmonths, $NETTRAFF::colorOk, $NETTRAFF::colorWarn, $NETTRAFF::colorMax);
40undef(@dummy);
41
42my %cgiparams;
43my %pppsettings;
44my %netsettings;
45
46&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
47&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
48
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}
79
80&Header::getcgihash(\%cgiparams);
81
82my $selectYearALL = "";
83$selectYearALL = 'selected=\'selected\'' if($cgiparams{'STARTYEAR'} eq '????');
84
85my $selectMonthALL = "";
86$selectMonthALL = 'selected=\'selected\'' if($cgiparams{'STARTMONTH'} eq '??');
87
88&Header::showhttpheaders();
89&Header::openpage($Lang::tr{'sstraffic'}, 1, '');
90&Header::openbigbox('100%', 'left');
91&Header::openbox('100%', 'left', "");
92
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}
99
100print <<END;
101 <table width='100%' align='center'>
102 <tr>
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'>
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>
119 </select>
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>
132 </select>
133 <input type='submit' name='ACTION' value='$Lang::tr{'update'}' />
134 </form>
135 </td>
136 <td width='5%' align='center'>
137 <form method='post' action='/cgi-bin/traffics.cgi'>
138 <input type='submit' name='ACTION' value=' > ' />
139 </form>
140 </td>
141 </tr>
142 </table>
143END
144
145&Header::closebox();
146
147&Header::openbox('100%', 'left', "$Lang::tr{'traffics'}");
148
149my $dateWidth = '20%';
150my $netWidth = '34%';
151my $inOutWidth = '17%';
152
153# 4 networks
154if ($netsettings{'CONFIG_TYPE'} =~ /^(4)$/) {
155 $dateWidth = '12%';
156 $netWidth = '22%';
157 $inOutWidth = '11%';
158}
159# 3 networks
160if ($netsettings{'CONFIG_TYPE'} =~ /^(2|3)$/) {
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
173if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/) {
174 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
175}
176
177if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/) {
178 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
179}
180
181if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/) {
182 print "<td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>";
183}
184print <<END;
185 </tr>
186 </table>
187 <table width='100%'>
188 <tr>
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>
192END
193
194if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
195{
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>";
198}
199
200if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
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}
205if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
206{
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>";
209}
210print "</tr>";
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
222
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
265 $displayMode = "monthly";
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 }
297}
298
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
309my %allDaysBytes = ();
310my @allDays = &Traffic::calcTraffic(\%allDaysBytes,$start,$end, $displayMode);
311
312
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
325 $total_green_in += $allDaysBytes{$_}{${Traffic::green_in}};
326 $total_green_out += $allDaysBytes{$_}{${Traffic::green_out}};
327
328 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
329 {
330 $total_blue_in += $allDaysBytes{$_}{${Traffic::blue_in}};
331 $total_blue_out += $allDaysBytes{$_}{${Traffic::blue_out}};
332 }
333
334 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
335 {
336 $total_orange_in += $allDaysBytes{$_}{${Traffic::orange_in}};
337 $total_orange_out += $allDaysBytes{$_}{${Traffic::orange_out}};
338 }
339
340 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
341 {
342 $total_red_in += $allDaysBytes{$_}{${Traffic::red_in}};
343 $total_red_out += $allDaysBytes{$_}{${Traffic::red_out}};
344 }
345
346 if ($lines % 2) {
347 print "<tr bgcolor='$color{'color20'}'>"; }
348 else {
349 print "<tr bgcolor='$color{'color22'}}'>"; }
350
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);
354
355 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
356 {
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);
359 }
360 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
361 {
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);
364 }
365
366 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
367 {
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);
370 }
371
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));
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));
383
384if ($lines % 2) {print "<tr bgcolor='$color{'color20'}'>"; }
385else {print "<tr bgcolor='$color{'color22'}'>"; }
386
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
392
393if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
394{
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
399if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
400{
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}
404if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
405{
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>
433END
434}
435
436&Header::closebox();
437&Header::closebigbox();
438&Header::closepage();