]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/traffic.cgi
Removed the old svn commands and started with git.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / traffic.cgi
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
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "${General::swroot}/net-traffic/net-traffic-admin.pl";
32 require "${General::swroot}/net-traffic/net-traffic-lib.pl";
33
34 my %color = ();
35 my %mainsettings = ();
36 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
37 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
38
39 my @dummy = (@NETTRAFF::months, @NETTRAFF::longmonths, $NETTRAFF::colorOk, $NETTRAFF::colorWarn, $NETTRAFF::colorMax);
40 undef(@dummy);
41
42 my %cgiparams;
43 my %pppsettings;
44 my %netsettings;
45
46 &General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
47 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
48
49 my @now = localtime(time);
50
51 $now[5] = $now[5]+1900;
52
53 $cgiparams{'STARTYEAR'} = $now[5];
54 $cgiparams{'STARTMONTH'} = $now[4];
55
56 my $startDay = '1';
57 my $endDay = '1';
58
59 if($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
66 if ($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
82 my $selectYearALL = "";
83 $selectYearALL = 'selected=\'selected\'' if($cgiparams{'STARTYEAR'} eq '????');
84
85 my $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
93 my $firstDayTxt = '';
94
95 if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
96 {
97 $firstDayTxt = " ($Lang::tr{'monthly volume start day short'}: $NETTRAFF::settings{'STARTDAY'}.)";
98 }
99
100 print <<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'>
107 END
108
109 foreach 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
117 print <<END;
118 <option $selectMonthALL value='??'>$Lang::tr{'allmsg'}</option>
119 </select>
120 <select name='STARTYEAR'>
121 END
122
123 for (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
130 print <<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>
143 END
144
145 &Header::closebox();
146
147 &Header::openbox('100%', 'left', "$Lang::tr{'traffics'}");
148
149 my $dateWidth = '20%';
150 my $netWidth = '34%';
151 my $inOutWidth = '17%';
152
153 # 4 networks
154 if ($netsettings{'CONFIG_TYPE'} =~ /^(4)$/) {
155 $dateWidth = '12%';
156 $netWidth = '22%';
157 $inOutWidth = '11%';
158 }
159 # 3 networks
160 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|3)$/) {
161 $dateWidth = '16%';
162 $netWidth = '28%';
163 $inOutWidth = '14%';
164 }
165
166 print <<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>
171 END
172
173 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/) {
174 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
175 }
176
177 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/) {
178 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
179 }
180
181 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/) {
182 print "<td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>";
183 }
184 print <<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>
192 END
193
194 if ($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
200 if ($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 }
205 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
206 {
207 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficin'}</b></font></td>";
208 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='$Header::colourred'><b>$Lang::tr{'trafficout'}</b></font></td>";
209 }
210 print "</tr>";
211
212 my $total_blue_in=0;
213 my $total_blue_out=0;
214 my $total_green_in=0;
215 my $total_green_out=0;
216 my $total_orange_in=0;
217 my $total_orange_out=0;
218 my $total_red_in=0;
219 my $total_red_out=0;
220 my $lines=0;
221
222
223
224 my $startYear = $cgiparams{'STARTYEAR'};
225 my $endYear = $cgiparams{'STARTYEAR'};
226 my $startMonth = $cgiparams{'STARTMONTH'};
227 my $endMonth = $cgiparams{'STARTMONTH'};
228 my $displayMode = "daily_multi";
229 $startDay = '1';
230 $endDay = '1';
231 my $selectedMonth = '0';
232
233 if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
234 {
235 $startDay = $NETTRAFF::settings{'STARTDAY'};
236 $endDay = $NETTRAFF::settings{'STARTDAY'};
237 }
238
239 # "show All ?
240 if ($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 }
262 elsif ($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 }
274 else
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
306 my $start = "$startYear$startMonth$startDay";
307 my $end = "$endYear$endMonth$endDay";
308
309 my %allDaysBytes = ();
310 my @allDays = &Traffic::calcTraffic(\%allDaysBytes,$start,$end, $displayMode);
311
312
313 foreach (@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
384 if ($lines % 2) {print "<tr bgcolor='$color{'color20'}'>"; }
385 else {print "<tr bgcolor='$color{'color22'}'>"; }
386
387 print <<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>
391 END
392
393 if ($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
399 if ($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 }
404 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
405 {
406 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_in MB</b></td>";
407 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_out MB</b></td>";
408 }
409 print "</tr></table>";
410
411 if($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>
433 END
434 }
435
436 &Header::closebox();
437 &Header::closebigbox();
438 &Header::closepage();