Nettraffic aktualisiert.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / traffics.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
10 use strict;
11
12 # enable only the following on debugging purpose
13 use warnings;
14 use CGI::Carp 'fatalsToBrowser';
15
16 require '/var/ipfire/general-functions.pl';
17 require "${General::swroot}/lang.pl";
18 require "${General::swroot}/header.pl";
19 require '/var/ipfire/net-traffic/net-traffic-lib.pl';
20
21 my %cgiparams;
22 my %netsettings;
23
24 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
25
26 my @years=("2007","2008","2009","2010","2011","2012","2013","2014","2015");
27
28 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
29 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
30 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
31 $Lang::tr{'december'} );
32
33 my @months = ( 0,1,2,3,4,5,6,7,8,9,10,11 );
34 my @days = ( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 );
35
36 my @wday = ($Lang::tr{'wday1'}, $Lang::tr{'wday2'}, $Lang::tr{'wday3'}, $Lang::tr{'wday4'}, $Lang::tr{'wday5'}, $Lang::tr{'wday6'}, $Lang::tr{'wday7'});
37
38 my @now = localtime(time);
39
40 $now[5] = $now[5]+1900;
41
42 $cgiparams{'STARTDAY'} = 10;
43 $cgiparams{'STOPDAY'} = 11;
44 $cgiparams{'STARTYEAR'} = $now[5];
45 $cgiparams{'STOPYEAR'} = $now[5];
46
47 # this periode started last month
48 if ($now[3] < 10) {
49 $cgiparams{'STARTMONTH'} = $now[4]-1;
50 $cgiparams{'STOPMONTH'} = $now[4];
51 # when current month is january we start in last year december
52 if ($now[4] == 0) {
53 $cgiparams{'STARTYEAR'} = $now[5]-1;
54 $cgiparams{'STARTMONTH'} = 11;
55 }
56 }
57 else {
58 $cgiparams{'STARTMONTH'} = $now[4];
59 $cgiparams{'STOPMONTH'} = $now[4]+1;
60 # when we are december, this periode ends next year january
61 if ($now[4] == 11) {
62 $cgiparams{'STOPYEAR'} = $now[5]+1;
63 $cgiparams{'STOPMONTH'} = 0;
64 }
65 }
66
67 &Header::getcgihash(\%cgiparams);
68
69 &Header::showhttpheaders();
70
71 &Header::openpage($Lang::tr{'sstraffic'}, 1, '');
72
73 &Header::openbigbox('100%', 'left');
74
75 &Header::openbox('100%', 'left', $Lang::tr{'settingsc'});
76
77 print <<END;
78 <table width='100%' align='center'>
79 <tr>
80 <td width='95%' class='base' align='center'>
81 <form method='post' action='/cgi-bin/traffics.cgi'>
82 $Lang::tr{'trafficfrom'}
83 <select name='STARTDAY'>
84 END
85
86 foreach (@days)
87 {
88 print "\t<option ";
89 if ($_ == $cgiparams{'STARTDAY'}) {
90 print 'selected=\'selected\' '; }
91 print "value='$_'>$_</option>\n";
92 }
93 print <<END;
94 </select>
95 <select name='STARTMONTH'>
96 END
97
98 foreach (@months)
99 {
100 print "\t<option ";
101 if ($_ == $cgiparams{'STARTMONTH'}) {
102 print 'selected=\'selected\' '; }
103 print "value='$_'>$longmonths[$_]</option>\n";
104 }
105
106 print <<END;
107 </select>
108 <select name='STARTYEAR'>
109 END
110
111 for (my $index=0; $index<=$#years; $index++) {
112 print "\t<option ";
113 if ($years[$index] == $cgiparams{'STARTYEAR'}) {
114 print 'selected=\'selected\' '; }
115 print "value='$years[$index]'>$years[$index]</option>\n";
116 }
117
118 print <<END;
119 </select>
120 $Lang::tr{'trafficto'}
121 <select name='STOPDAY'>
122 END
123
124 foreach (@days)
125 {
126 print "\t<option ";
127 if ($_ == $cgiparams{'STOPDAY'})
128 {
129 print 'selected=\'selected\' '; }
130 print "value='$_'>$_</option>\n";
131 }
132
133 print <<END;
134 </select>
135 <select name='STOPMONTH'>
136 END
137
138 foreach (@months)
139 {
140 print "\t<option ";
141 if ($_ == $cgiparams{'STOPMONTH'}) {
142 print 'selected=\'selected\' '; }
143 print "value='$_'>$longmonths[$_]</option>\n";
144 }
145
146 print <<END;
147 </select>
148 <select name='STOPYEAR'>
149 END
150
151 for (my $index = 0; $index <=$#years; $index++) {
152 print "\t<option ";
153 if ($years[$index] == $cgiparams{'STOPYEAR'}) {
154 print 'selected=\'selected\' '; }
155 print "value='$years[$index]'>$years[$index]</option>\n";
156 }
157
158
159 print <<END;
160 </select>
161 <input type='submit' name='ACTION' value='$Lang::tr{'update'}' />
162 </form>
163 </td>
164 <td width='5%' align='center'>
165 <form method='post' action='/cgi-bin/traffic.cgi'>
166 <input type='submit' name='ACTION' value=' > ' />
167 </form>
168 </td>
169 </tr>
170 </table>
171 END
172
173 &Header::closebox();
174
175 &Header::openbox('100%', 'left', $Lang::tr{'traffics'});
176
177 my $dateWidth = '20%';
178 my $netWidth = '34%';
179 my $inOutWidth = '17%';
180
181 # 4 networks
182 if ($netsettings{'CONFIG_TYPE'} =~ /^(5|7)$/) {
183 $dateWidth = '12%';
184 $netWidth = '22%';
185 $inOutWidth = '11%';
186 }
187 # 3 networks
188 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|4|6)$/) {
189 $dateWidth = '16%';
190 $netWidth = '28%';
191 $inOutWidth = '14%';
192 }
193
194 print <<END;
195 <table width='100%'>
196 <tr>
197 <td width='$dateWidth' align='center' class='boldbase'></td>
198 <td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficgreen'}</b></td>
199 END
200
201 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/) {
202 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
203 }
204
205 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/) {
206 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
207 }
208
209 print <<END;
210 <td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>
211 </tr>
212 </table>
213 <table width='100%'>
214 <tr>
215 <td width='$dateWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficdate'}</b></td>
216 <td width='$inOutWidth' align='center' class='boldbase'><font color='#16A61D'><b>$Lang::tr{'trafficin'}</b></font></td>
217 <td width='$inOutWidth' align='center' class='boldbase'><font color='#16A61D'><b>$Lang::tr{'trafficout'}</b></font></td>
218 END
219
220 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/)
221 {
222 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='${Header::colourblue}'><b>$Lang::tr{'trafficin'}</b></font></td>";
223 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='${Header::colourblue}'><b>$Lang::tr{'trafficout'}</b></font></td>";
224 }
225
226 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/)
227 {
228 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='#FF9933'><b>$Lang::tr{'trafficin'}</b></font></td>";
229 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='#FF9933'><b>$Lang::tr{'trafficout'}</b></font></td>";
230 }
231 print <<END;
232 <td width='$inOutWidth' align='center' class='boldbase'><font color='#CE1B31'><b>$Lang::tr{'trafficin'}</b></font></td>
233 <td width='$inOutWidth' align='center' class='boldbase'><font color='#CE1B31'><b>$Lang::tr{'trafficout'}</b></font></td>
234 </tr>
235 END
236
237 my $total_blue_in=0;
238 my $total_blue_out=0;
239 my $total_green_in=0;
240 my $total_green_out=0;
241 my $total_orange_in=0;
242 my $total_orange_out=0;
243 my $total_red_in=0;
244 my $total_red_out=0;
245 my $lines=0;
246
247
248 my $startyear = $cgiparams{'STARTYEAR'};
249 my $stopyear = $cgiparams{'STOPYEAR'};
250
251 my $startMonth = $cgiparams{'STARTMONTH'}+1;
252 $startMonth = $startMonth < 10 ? $startMonth = "0".$startMonth : $startMonth;
253
254 my $endMonth = $cgiparams{'STOPMONTH'}+1;
255 $endMonth = $endMonth < 10 ? $endMonth = "0".$endMonth : $endMonth;
256
257 my $startDay = $cgiparams{'STARTDAY'};
258 $startDay = $startDay < 10 ? $startDay = "0".$startDay : $startDay;
259
260 my $endDay = $cgiparams{'STOPDAY'}+1;
261 $endDay = $endDay < 10 ? $endDay = "0".$endDay : $endDay;
262
263 my $displayMode = "daily_multi";
264 my $start = $startyear.$startMonth.$startDay;
265 my $end = $stopyear.$endMonth.$endDay;
266
267 my %allDaysBytes = ();
268 my @allDays = &Traffic::calcTraffic(\%allDaysBytes,$start,$end, $displayMode);
269
270
271 foreach (@allDays) {
272 $total_green_in += $allDaysBytes{$_}{${Traffic::green_in}};
273 $total_green_out += $allDaysBytes{$_}{${Traffic::green_out}};
274
275 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/)
276 {
277 $total_blue_in += $allDaysBytes{$_}{${Traffic::blue_in}};
278 $total_blue_out += $allDaysBytes{$_}{${Traffic::blue_out}};
279 }
280
281 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/)
282 {
283 $total_orange_in += $allDaysBytes{$_}{${Traffic::orange_in}};
284 $total_orange_out += $allDaysBytes{$_}{${Traffic::orange_out}};
285 }
286
287 $total_red_in += $allDaysBytes{$_}{${Traffic::red_in}};
288 $total_red_out += $allDaysBytes{$_}{${Traffic::red_out}};
289
290 if ($lines % 2) {
291 print "<tr bgcolor='${Header::table1colour}'>"; }
292 else {
293 print "<tr bgcolor='${Header::table2colour}'>"; }
294
295 printf "<td align='center' nowrap='nowrap'>%s</td>\n", $allDaysBytes{$_}{'Day'};
296 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::green_in}}/1048576);
297 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::green_out}}/1048576);
298
299 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/)
300 {
301 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::blue_in}}/1048576);
302 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::blue_out}}/1048576);
303 }
304 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/)
305 {
306 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::orange_in}}/1048576);
307 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::orange_out}}/1048576);
308 }
309
310 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::red_in}}/1048576);
311 printf "<td align='center' nowrap='nowrap'>%.3f</td></tr>\n", ($allDaysBytes{$_}{${Traffic::red_out}}/1048576);
312
313 $lines++;
314 }
315
316 $total_green_in=sprintf("%.2f", ($total_green_in/1048576));
317 $total_green_out=sprintf("%.2f", ($total_green_out/1048576));
318 $total_blue_in=sprintf("%.2f", ($total_blue_in/1048576));
319 $total_blue_out=sprintf("%.2f", ($total_blue_out/1048576));
320 $total_orange_in=sprintf("%.2f", ($total_orange_in/1048576));
321 $total_orange_out=sprintf("%.2f", ($total_orange_out/1048576));
322 $total_red_in=sprintf("%.2f", ($total_red_in/1048576));
323 $total_red_out=sprintf("%.2f", ($total_red_out/1048576));
324
325 if ($lines % 2) {print "<tr bgcolor='${Header::table1colour}'>"; }
326 else {print "<tr bgcolor='${Header::table2colour}'>"; }
327
328 print <<END;
329 <td align='center' class='boldbase' height='20' nowrap='nowrap'><b>$Lang::tr{'trafficsum'}</b></td>
330 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_green_in MB</b></td>
331 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_green_out MB</b></td>
332 END
333
334 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/)
335 {
336 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_blue_in MB</b></td>";
337 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_blue_out MB</b></td>";
338 }
339
340 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/)
341 {
342 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_orange_in MB</b></td>";
343 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_orange_out MB</b></td>";
344 }
345
346 print <<END;
347 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_in MB</b></td>
348 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_out MB</b></td>
349 </tr>
350 </table>
351 END
352
353 &Header::closebox();
354
355 &Header::closebigbox();
356
357 &Header::closepage();