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