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