]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/traffic.cgi
5 # This code is distributed under the terms of the GPL
7 # (c) The SmoothWall Team
9 # $Id: traffic.cgi,v 1.19 2007/01/09 18:59:23 dotzball Exp $
11 # traffic.cgi, v1.1.0 2003/10/18
16 # 18 June 2004 Achim Weber
17 # - added functionality to work with IPCop 1.4.0
18 # - use some functions from ipacsum
22 # enable only the following on debugging purpose
24 use CGI
::Carp
'fatalsToBrowser';
26 require '/var/ipfire/general-functions.pl';
27 require "${General::swroot}/lang.pl";
28 require "${General::swroot}/header.pl";
29 require "${General::swroot}/net-traffic/net-traffic-admin.pl";
30 require "${General::swroot}/net-traffic/net-traffic-lib.pl";
33 my %mainsettings = ();
34 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
35 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
37 my @dummy = (@NETTRAFF::months
, @NETTRAFF::longmonths
, $NETTRAFF::colorOk
, $NETTRAFF::colorWarn
, $NETTRAFF::colorMax
);
44 &General
::readhash
("${General::swroot}/ppp/settings", \
%pppsettings);
45 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
47 my @now = localtime(time);
49 $now[5] = $now[5]+1900;
51 $cgiparams{'STARTYEAR'} = $now[5];
52 $cgiparams{'STARTMONTH'} = $now[4];
57 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
59 $startDay = $NETTRAFF::settings
{'STARTDAY'};
60 $endDay = $NETTRAFF::settings
{'STARTDAY'};
63 # this periode started last month
64 if ($now[3] < $startDay)
66 # when current month is january we start in last year december
68 $cgiparams{'STARTYEAR'} = $now[5]-1;
69 $cgiparams{'STARTMONTH'} = 11;
73 $cgiparams{'STARTYEAR'} = $now[5];
74 $cgiparams{'STARTMONTH'} = $now[4]-1;
78 &Header
::getcgihash
(\
%cgiparams);
80 my $selectYearALL = "";
81 $selectYearALL = 'selected=\'selected\'' if($cgiparams{'STARTYEAR'} eq '????');
83 my $selectMonthALL = "";
84 $selectMonthALL = 'selected=\'selected\'' if($cgiparams{'STARTMONTH'} eq '??');
86 &Header
::showhttpheaders
();
87 &Header
::openpage
($Lang::tr
{'sstraffic'}, 1, '');
88 &Header
::openbigbox
('100%', 'left');
89 &Header
::openbox
('100%', 'left', "");
93 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
95 $firstDayTxt = " ($Lang::tr{'monthly volume start day short'}: $NETTRAFF::settings{'STARTDAY'}.)";
99 <table width='100%' align='center'>
101 <td width='90%' align='left' nowrap='nowrap'>
102 <form method='post' action='/cgi-bin/traffic.cgi'>
103 $Lang::tr{'selecttraffic'}$firstDayTxt:
104 <select name='STARTMONTH'>
107 foreach my $month (@NETTRAFF::months
)
110 if ("$month" eq "$cgiparams{'STARTMONTH'}") {
111 print 'selected=\'selected\' '; }
112 print "value='$month'>$NETTRAFF::longmonths[$month]</option>\n";
116 <option $selectMonthALL value='??'>$Lang::tr{'allmsg'}</option>
118 <select name='STARTYEAR'>
121 for (my $index=0; $index<=$#NETTRAFF::years
; $index++) {
123 if ("$NETTRAFF::years[$index]" eq "$cgiparams{'STARTYEAR'}") {
124 print 'selected=\'selected\' '; }
125 print "value='$NETTRAFF::years[$index]'>$NETTRAFF::years[$index]</option>\n";
129 <option $selectYearALL value='????'>$Lang::tr{'allmsg'}</option>
131 <input type='submit' name='ACTION' value='$Lang::tr{'update'}' />
134 <td width='5%' align='center'>
135 <form method='post' action='/cgi-bin/traffics.cgi'>
136 <input type='submit' name='ACTION' value=' > ' />
145 &Header
::openbox
('100%', 'left', "$Lang::tr{'traffics'}");
147 my $dateWidth = '20%';
148 my $netWidth = '34%';
149 my $inOutWidth = '17%';
152 if ($netsettings{'CONFIG_TYPE'} =~ /^(4)$/) {
158 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|3)$/) {
167 <td width='$dateWidth' align='center' class='boldbase'></td>
168 <td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficgreen'}</b></td>
171 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/) {
172 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
175 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/) {
176 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
179 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/) {
180 print "<td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>";
187 <td width='$dateWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficdate'}</b></td>
188 <td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourgreen'><b>$Lang::tr{'trafficin'}</b></font></td>
189 <td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourgreen'><b>$Lang::tr{'trafficout'}</b></font></td>
192 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
194 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='${Header::colourblue}'><b>$Lang::tr{'trafficin'}</b></font></td>";
195 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='${Header::colourblue}'><b>$Lang::tr{'trafficout'}</b></font></td>";
198 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
200 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourorange'><b>$Lang::tr{'trafficin'}</b></font></td>";
201 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourorange'><b>$Lang::tr{'trafficout'}</b></font></td>";
203 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
205 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficin'}</b></font></td>";
206 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficout'}</b></font></td>";
211 my $total_blue_out=0;
212 my $total_green_in=0;
213 my $total_green_out=0;
214 my $total_orange_in=0;
215 my $total_orange_out=0;
222 my $startYear = $cgiparams{'STARTYEAR'};
223 my $endYear = $cgiparams{'STARTYEAR'};
224 my $startMonth = $cgiparams{'STARTMONTH'};
225 my $endMonth = $cgiparams{'STARTMONTH'};
226 my $displayMode = "daily_multi";
229 my $selectedMonth = '0';
231 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
233 $startDay = $NETTRAFF::settings
{'STARTDAY'};
234 $endDay = $NETTRAFF::settings
{'STARTDAY'};
238 if ($cgiparams{'STARTYEAR'} eq '????')
240 # 'show all month' + 'show all years'
241 # OR <selected Month> + 'show all years'
243 # if we have a <selected Month>, we read all traffic but display only the selected month
244 if($cgiparams{'STARTMONTH'} ne '??')
246 $selectedMonth = $cgiparams{'STARTMONTH'} + 1;
247 $selectedMonth = $selectedMonth < 10 ?
$selectedMonth = "0".$selectedMonth : $selectedMonth;
250 $displayMode = "monthly";
251 # start with 1970-01-01
255 # end with next year: 20xx-01-01
256 $endYear = $now[5] + 1;
260 elsif ($cgiparams{'STARTMONTH'} eq '??')
262 # 'show all month' + 200x
263 $displayMode = "monthly";
264 # start with 200x-01-01
267 # end with (200x+1)-01-01
268 $endYear = $startYear + 1;
276 $endMonth = $endMonth + 2;
278 # this periode started last month
279 if ($now[3] < $startDay)
281 # when current month is january we start in last year december
282 if ($endMonth == 1) {
289 # when we are in december, this periode ends next year january
290 if ($startMonth == 12) {
299 $startMonth = $startMonth < 10 ?
$startMonth = "0".$startMonth : $startMonth;
300 $endMonth = $endMonth < 10 ?
$endMonth = "0".$endMonth : $endMonth;
301 $startDay = $startDay < 10 ?
$startDay = "0".$startDay : $startDay;
302 $endDay = $endDay < 10 ?
$endDay = "0".$endDay : $endDay;
304 my $start = "$startYear$startMonth$startDay";
305 my $end = "$endYear$endMonth$endDay";
307 my %allDaysBytes = ();
308 my @allDays = &Traffic
::calcTraffic
(\
%allDaysBytes,$start,$end, $displayMode);
313 # special code for: <selected Month> + 'show all years'
314 if($cgiparams{'STARTMONTH'} ne '??' && $cgiparams{'STARTYEAR'} eq '????')
316 # show only those traffic in the selected month
317 if($allDaysBytes{$_}{'Day'} !~ /^\d\d\d\d-$selectedMonth$/)
323 $total_green_in += $allDaysBytes{$_}{${Traffic
::green_in
}};
324 $total_green_out += $allDaysBytes{$_}{${Traffic
::green_out
}};
326 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
328 $total_blue_in += $allDaysBytes{$_}{${Traffic
::blue_in
}};
329 $total_blue_out += $allDaysBytes{$_}{${Traffic
::blue_out
}};
332 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
334 $total_orange_in += $allDaysBytes{$_}{${Traffic
::orange_in
}};
335 $total_orange_out += $allDaysBytes{$_}{${Traffic
::orange_out
}};
338 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
340 $total_red_in += $allDaysBytes{$_}{${Traffic
::red_in
}};
341 $total_red_out += $allDaysBytes{$_}{${Traffic
::red_out
}};
345 print "<tr bgcolor='$color{'color20'}'>"; }
347 print "<tr bgcolor='$color{'color22'}}'>"; }
349 printf "<td align='center' nowrap='nowrap'>%s</td>\n", $allDaysBytes{$_}{'Day'};
350 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::green_in
}}/1048576);
351 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::green_out
}}/1048576);
353 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
355 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::blue_in
}}/1048576);
356 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::blue_out
}}/1048576);
358 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
360 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::orange_in
}}/1048576);
361 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::orange_out
}}/1048576);
364 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
366 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic
::red_in
}}/1048576);
367 printf "<td align='center' nowrap='nowrap'>%.3f</td></tr>\n", ($allDaysBytes{$_}{${Traffic
::red_out
}}/1048576);
373 $total_green_in=sprintf("%.2f", ($total_green_in/1048576));
374 $total_green_out=sprintf("%.2f", ($total_green_out/1048576));
375 $total_blue_in=sprintf("%.2f", ($total_blue_in/1048576));
376 $total_blue_out=sprintf("%.2f", ($total_blue_out/1048576));
377 $total_orange_in=sprintf("%.2f", ($total_orange_in/1048576));
378 $total_orange_out=sprintf("%.2f", ($total_orange_out/1048576));
379 $total_red_in=sprintf("%.2f", ($total_red_in/1048576));
380 $total_red_out=sprintf("%.2f", ($total_red_out/1048576));
382 if ($lines % 2) {print "<tr bgcolor='$color{'color20'}'>"; }
383 else {print "<tr bgcolor='$color{'color22'}'>"; }
386 <td align='center' class='boldbase' height='20' nowrap='nowrap'><b>$Lang::tr{'trafficsum'}</b></td>
387 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_green_in MB</b></td>
388 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_green_out MB</b></td>
391 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
393 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_blue_in MB</b></td>";
394 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_blue_out MB</b></td>";
397 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
399 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_orange_in MB</b></td>";
400 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_orange_out MB</b></td>";
402 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
404 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_in MB</b></td>";
405 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_out MB</b></td>";
407 print "</tr></table>";
409 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
411 my $total_red_all = sprintf("%.2f", ($total_red_in + $total_red_out));
413 my $color = $NETTRAFF::colorOk
;
415 my $warnTraff = ($NETTRAFF::settings
{'MONTHLY_VOLUME'} * $NETTRAFF::settings
{'WARN'} / 100);
416 if($NETTRAFF::settings
{'WARN_ON'} eq 'on'
417 && $warnTraff < $total_red_all)
419 $color = $NETTRAFF::colorWarn
;
421 if($NETTRAFF::settings
{'MONTHLY_VOLUME'} < $total_red_all)
423 $color = $NETTRAFF::colorMax
;
428 <tr><td align='center' class='boldbase' nowrap='nowrap' ><b>$Lang::tr{'monthly volume'} ($NETTRAFF::settings{'MONTHLY_VOLUME'} MB)</b></td></tr>
429 <tr><td align='center' class='boldbase' nowrap='nowrap' bgcolor='$color'><b>$total_red_all MB</b></td></tr>
435 &Header
::closebigbox
();
436 &Header
::closepage
();