]>
git.ipfire.org Git - people/pmueller/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/trafficadm.cgi'>
136 <input type='submit' name='ACTION' value='$Lang::tr{'net-traffic configuration'}' />
139 <td width='5%' align='center'>
140 <form method='post' action='/cgi-bin/traffics.cgi'>
141 <input type='submit' name='ACTION' value=' > ' />
150 &Header
::openbox
('100%', 'left', "$Lang::tr{'traffics'}");
152 my $dateWidth = '20%';
153 my $netWidth = '34%';
154 my $inOutWidth = '17%';
157 if ($netsettings{'CONFIG_TYPE'} =~ /^(4)$/) {
163 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|3)$/) {
172 <td width='$dateWidth' align='center' class='boldbase'></td>
173 <td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficgreen'}</b></td>
176 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/) {
177 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
180 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/) {
181 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
184 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/) {
185 print "<td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>";
192 <td width='$dateWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficdate'}</b></td>
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>
197 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
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>";
203 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
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>";
208 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
210 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficin'}</b></font></td>";
211 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficout'}</b></font></td>";
216 my $total_blue_out=0;
217 my $total_green_in=0;
218 my $total_green_out=0;
219 my $total_orange_in=0;
220 my $total_orange_out=0;
227 my $startYear = $cgiparams{'STARTYEAR'};
228 my $endYear = $cgiparams{'STARTYEAR'};
229 my $startMonth = $cgiparams{'STARTMONTH'};
230 my $endMonth = $cgiparams{'STARTMONTH'};
231 my $displayMode = "daily_multi";
234 my $selectedMonth = '0';
236 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
238 $startDay = $NETTRAFF::settings
{'STARTDAY'};
239 $endDay = $NETTRAFF::settings
{'STARTDAY'};
243 if ($cgiparams{'STARTYEAR'} eq '????')
245 # 'show all month' + 'show all years'
246 # OR <selected Month> + 'show all years'
248 # if we have a <selected Month>, we read all traffic but display only the selected month
249 if($cgiparams{'STARTMONTH'} ne '??')
251 $selectedMonth = $cgiparams{'STARTMONTH'} + 1;
252 $selectedMonth = $selectedMonth < 10 ?
$selectedMonth = "0".$selectedMonth : $selectedMonth;
255 $displayMode = "monthly";
256 # start with 1970-01-01
260 # end with next year: 20xx-01-01
261 $endYear = $now[5] + 1;
265 elsif ($cgiparams{'STARTMONTH'} eq '??')
267 # 'show all month' + 200x
268 $displayMode = "monthly";
269 # start with 200x-01-01
272 # end with (200x+1)-01-01
273 $endYear = $startYear + 1;
281 $endMonth = $endMonth + 2;
283 # this periode started last month
284 if ($now[3] < $startDay)
286 # when current month is january we start in last year december
287 if ($endMonth == 1) {
294 # when we are in december, this periode ends next year january
295 if ($startMonth == 12) {
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;
309 my $start = "$startYear$startMonth$startDay";
310 my $end = "$endYear$endMonth$endDay";
312 my %allDaysBytes = ();
313 my @allDays = &Traffic
::calcTraffic
(\
%allDaysBytes,$start,$end, $displayMode);
318 # special code for: <selected Month> + 'show all years'
319 if($cgiparams{'STARTMONTH'} ne '??' && $cgiparams{'STARTYEAR'} eq '????')
321 # show only those traffic in the selected month
322 if($allDaysBytes{$_}{'Day'} !~ /^\d\d\d\d-$selectedMonth$/)
328 $total_green_in += $allDaysBytes{$_}{${Traffic
::green_in
}};
329 $total_green_out += $allDaysBytes{$_}{${Traffic
::green_out
}};
331 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
333 $total_blue_in += $allDaysBytes{$_}{${Traffic
::blue_in
}};
334 $total_blue_out += $allDaysBytes{$_}{${Traffic
::blue_out
}};
337 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
339 $total_orange_in += $allDaysBytes{$_}{${Traffic
::orange_in
}};
340 $total_orange_out += $allDaysBytes{$_}{${Traffic
::orange_out
}};
343 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
345 $total_red_in += $allDaysBytes{$_}{${Traffic
::red_in
}};
346 $total_red_out += $allDaysBytes{$_}{${Traffic
::red_out
}};
350 print "<tr bgcolor='$color{'color20'}'>"; }
352 print "<tr bgcolor='$color{'color22'}}'>"; }
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);
358 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
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);
363 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
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);
369 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
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);
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));
383 $total_orange_out=sprintf("%.2f", ($total_orange_out/1048576));
384 $total_red_in=sprintf("%.2f", ($total_red_in/1048576));
385 $total_red_out=sprintf("%.2f", ($total_red_out/1048576));
387 if ($lines % 2) {print "<tr bgcolor='$color{'color20'}'>"; }
388 else {print "<tr bgcolor='$color{'color22'}'>"; }
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>
396 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
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>";
402 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
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>";
407 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
409 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_in MB</b></td>";
410 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_out MB</b></td>";
412 print "</tr></table>";
414 if($NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} eq 'on')
416 my $total_red_all = sprintf("%.2f", ($total_red_in + $total_red_out));
418 my $color = $NETTRAFF::colorOk
;
420 my $warnTraff = ($NETTRAFF::settings
{'MONTHLY_VOLUME'} * $NETTRAFF::settings
{'WARN'} / 100);
421 if($NETTRAFF::settings
{'WARN_ON'} eq 'on'
422 && $warnTraff < $total_red_all)
424 $color = $NETTRAFF::colorWarn
;
426 if($NETTRAFF::settings
{'MONTHLY_VOLUME'} < $total_red_all)
428 $color = $NETTRAFF::colorMax
;
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>
440 &Header
::closebigbox
();
441 &Header
::closepage
();