]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/traffic.cgi
Uploads.cgi entfernt. Die war noch ueber.
[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 width='5%' align='center'>
135 <form method='post' action='/cgi-bin/traffics.cgi'>
136 <input type='submit' name='ACTION' value=' > ' />
137 </form>
138 </td>
139 </tr>
140 </table>
141 END
142
143 &Header::closebox();
144
145 &Header::openbox('100%', 'left', "$Lang::tr{'traffics'}");
146
147 my $dateWidth = '20%';
148 my $netWidth = '34%';
149 my $inOutWidth = '17%';
150
151 # 4 networks
152 if ($netsettings{'CONFIG_TYPE'} =~ /^(4)$/) {
153 $dateWidth = '12%';
154 $netWidth = '22%';
155 $inOutWidth = '11%';
156 }
157 # 3 networks
158 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|3)$/) {
159 $dateWidth = '16%';
160 $netWidth = '28%';
161 $inOutWidth = '14%';
162 }
163
164 print <<END;
165 <table width='100%'>
166 <tr>
167 <td width='$dateWidth' align='center' class='boldbase'></td>
168 <td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficgreen'}</b></td>
169 END
170
171 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/) {
172 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
173 }
174
175 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/) {
176 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
177 }
178
179 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/) {
180 print "<td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>";
181 }
182 print <<END;
183 </tr>
184 </table>
185 <table width='100%'>
186 <tr>
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>
190 END
191
192 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
193 {
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>";
196 }
197
198 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
199 {
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>";
202 }
203 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
204 {
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>";
207 }
208 print "</tr>";
209
210 my $total_blue_in=0;
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;
216 my $total_red_in=0;
217 my $total_red_out=0;
218 my $lines=0;
219
220
221
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";
227 $startDay = '1';
228 $endDay = '1';
229 my $selectedMonth = '0';
230
231 if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
232 {
233 $startDay = $NETTRAFF::settings{'STARTDAY'};
234 $endDay = $NETTRAFF::settings{'STARTDAY'};
235 }
236
237 # "show All ?
238 if ($cgiparams{'STARTYEAR'} eq '????')
239 {
240 # 'show all month' + 'show all years'
241 # OR <selected Month> + 'show all years'
242
243 # if we have a <selected Month>, we read all traffic but display only the selected month
244 if($cgiparams{'STARTMONTH'} ne '??')
245 {
246 $selectedMonth = $cgiparams{'STARTMONTH'} + 1;
247 $selectedMonth = $selectedMonth < 10 ? $selectedMonth = "0".$selectedMonth : $selectedMonth;
248 }
249
250 $displayMode = "monthly";
251 # start with 1970-01-01
252 $startYear = 1970;
253 $startMonth = '1';
254 $startDay = '1';
255 # end with next year: 20xx-01-01
256 $endYear = $now[5] + 1;
257 $endMonth = '1';
258 $endDay = '1';
259 }
260 elsif ($cgiparams{'STARTMONTH'} eq '??')
261 {
262 # 'show all month' + 200x
263 $displayMode = "monthly";
264 # start with 200x-01-01
265 $startMonth = '1';
266 $startDay = '1';
267 # end with (200x+1)-01-01
268 $endYear = $startYear + 1;
269 $endMonth = '1';
270 $endDay = '1';
271 }
272 else
273 {
274 # no "Show All"
275 $startMonth++;
276 $endMonth = $endMonth + 2;
277
278 # this periode started last month
279 if ($now[3] < $startDay)
280 {
281 # when current month is january we start in last year december
282 if ($endMonth == 1) {
283 $startYear--;
284 $startMonth = 12;
285 }
286 }
287 else
288 {
289 # when we are in december, this periode ends next year january
290 if ($startMonth == 12) {
291 $endYear++;
292 $endMonth = 1;
293 }
294 }
295 }
296
297
298
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;
303
304 my $start = "$startYear$startMonth$startDay";
305 my $end = "$endYear$endMonth$endDay";
306
307 my %allDaysBytes = ();
308 my @allDays = &Traffic::calcTraffic(\%allDaysBytes,$start,$end, $displayMode);
309
310
311 foreach (@allDays)
312 {
313 # special code for: <selected Month> + 'show all years'
314 if($cgiparams{'STARTMONTH'} ne '??' && $cgiparams{'STARTYEAR'} eq '????')
315 {
316 # show only those traffic in the selected month
317 if($allDaysBytes{$_}{'Day'} !~ /^\d\d\d\d-$selectedMonth$/)
318 {
319 next;
320 }
321 }
322
323 $total_green_in += $allDaysBytes{$_}{${Traffic::green_in}};
324 $total_green_out += $allDaysBytes{$_}{${Traffic::green_out}};
325
326 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
327 {
328 $total_blue_in += $allDaysBytes{$_}{${Traffic::blue_in}};
329 $total_blue_out += $allDaysBytes{$_}{${Traffic::blue_out}};
330 }
331
332 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
333 {
334 $total_orange_in += $allDaysBytes{$_}{${Traffic::orange_in}};
335 $total_orange_out += $allDaysBytes{$_}{${Traffic::orange_out}};
336 }
337
338 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
339 {
340 $total_red_in += $allDaysBytes{$_}{${Traffic::red_in}};
341 $total_red_out += $allDaysBytes{$_}{${Traffic::red_out}};
342 }
343
344 if ($lines % 2) {
345 print "<tr bgcolor='$color{'color20'}'>"; }
346 else {
347 print "<tr bgcolor='$color{'color22'}}'>"; }
348
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);
352
353 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
354 {
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);
357 }
358 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
359 {
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);
362 }
363
364 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
365 {
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);
368 }
369
370 $lines++;
371 }
372
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));
381
382 if ($lines % 2) {print "<tr bgcolor='$color{'color20'}'>"; }
383 else {print "<tr bgcolor='$color{'color22'}'>"; }
384
385 print <<END;
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>
389 END
390
391 if ($netsettings{'CONFIG_TYPE'} =~ /^(3|4)$/)
392 {
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>";
395 }
396
397 if ($netsettings{'CONFIG_TYPE'} =~ /^(2|4)$/)
398 {
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>";
401 }
402 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/)
403 {
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>";
406 }
407 print "</tr></table>";
408
409 if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on')
410 {
411 my $total_red_all = sprintf("%.2f", ($total_red_in + $total_red_out));
412
413 my $color = $NETTRAFF::colorOk;
414
415 my $warnTraff = ($NETTRAFF::settings{'MONTHLY_VOLUME'} * $NETTRAFF::settings{'WARN'} / 100);
416 if($NETTRAFF::settings{'WARN_ON'} eq 'on'
417 && $warnTraff < $total_red_all)
418 {
419 $color = $NETTRAFF::colorWarn;
420 }
421 if($NETTRAFF::settings{'MONTHLY_VOLUME'} < $total_red_all)
422 {
423 $color = $NETTRAFF::colorMax;
424 }
425
426 print <<END;
427 <table width='100%'>
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>
430 </table>
431 END
432 }
433
434 &Header::closebox();
435 &Header::closebigbox();
436 &Header::closepage();