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