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