]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/traffic.cgi
78640e4f555d6916a4740dc3d8815885443fecfe
[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
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 %pppsettings;
23 my %netsettings;
24
25 &General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
26 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
27
28 my @wday = ($Lang::tr{'wday1'}, $Lang::tr{'wday2'}, $Lang::tr{'wday3'}, $Lang::tr{'wday4'}, $Lang::tr{'wday5'}, $Lang::tr{'wday6'}, $Lang::tr{'wday7'});
29
30 my ($s, $min, $h, $d, $m,$y) = localtime(time);
31 $y+=1900;
32 $m+=1;
33 $m = $m < 10 ? $m = "0".$m : $m;
34
35 $cgiparams{'MONTH'} = $m;
36 &Header::getcgihash(\%cgiparams);
37 $cgiparams{'YEAR'} = $y;
38 &Header::getcgihash(\%cgiparams);
39
40 &Header::showhttpheaders();
41
42 my %selectYear;
43 $selectYear{'YEAR'}{'2001'} = '';
44 $selectYear{'YEAR'}{'2002'} = '';
45 $selectYear{'YEAR'}{'2003'} = '';
46 $selectYear{'YEAR'}{'2004'} = '';
47 $selectYear{'YEAR'}{'2005'} = '';
48 $selectYear{'YEAR'}{'2006'} = '';
49 $selectYear{'YEAR'}{'2007'} = '';
50 $selectYear{'YEAR'}{'2008'} = '';
51 $selectYear{'YEAR'}{'2009'} = '';
52 $selectYear{'YEAR'}{'????'} = '';
53 $selectYear{'YEAR'}{$cgiparams{'YEAR'}} = 'selected=\'selected\'';
54
55
56 my %selected;
57
58 $selected{'MONTH'}{'01'} = '';
59 $selected{'MONTH'}{'02'} = '';
60 $selected{'MONTH'}{'03'} = '';
61 $selected{'MONTH'}{'04'} = '';
62 $selected{'MONTH'}{'05'} = '';
63 $selected{'MONTH'}{'06'} = '';
64 $selected{'MONTH'}{'07'} = '';
65 $selected{'MONTH'}{'08'} = '';
66 $selected{'MONTH'}{'09'} = '';
67 $selected{'MONTH'}{'10'} = '';
68 $selected{'MONTH'}{'11'} = '';
69 $selected{'MONTH'}{'12'} = '';
70 $selected{'MONTH'}{'??'} = '';
71 $selected{'MONTH'}{$cgiparams{'MONTH'}} = 'selected=\'selected\'';
72
73 &Header::openpage($Lang::tr{'sstraffic'}, 1, '');
74
75 &Header::openbigbox('100%', 'left');
76
77 &Header::openbox('100%', 'left', $Lang::tr{'settingsc'});
78
79 print <<END;
80 <table width='80%' align='center'>
81 <tr>
82 <td width='95%' align='left' nowrap='nowrap'>
83 <form method='post' action='/cgi-bin/traffic.cgi'>
84 $Lang::tr{'selecttraffic'}
85 <select name='MONTH'>
86 <option $selected{'MONTH'}{'01'} value='01'>$Lang::tr{'january'}</option>
87 <option $selected{'MONTH'}{'02'} value='02'>$Lang::tr{'february'}</option>
88 <option $selected{'MONTH'}{'03'} value='03'>$Lang::tr{'march'}</option>
89 <option $selected{'MONTH'}{'04'} value='04'>$Lang::tr{'april'}</option>
90 <option $selected{'MONTH'}{'05'} value='05'>$Lang::tr{'may'}</option>
91 <option $selected{'MONTH'}{'06'} value='06'>$Lang::tr{'june'}</option>
92 <option $selected{'MONTH'}{'07'} value='07'>$Lang::tr{'july'}</option>
93 <option $selected{'MONTH'}{'08'} value='08'>$Lang::tr{'august'}</option>
94 <option $selected{'MONTH'}{'09'} value='09'>$Lang::tr{'september'}</option>
95 <option $selected{'MONTH'}{'10'} value='10'>$Lang::tr{'october'}</option>
96 <option $selected{'MONTH'}{'11'} value='11'>$Lang::tr{'november'}</option>
97 <option $selected{'MONTH'}{'12'} value='12'>$Lang::tr{'december'}</option>
98 <option $selected{'MONTH'}{'??'} value='??'>$Lang::tr{'allmsg'}</option>
99 </select>
100 <select name='YEAR'>
101 <option $selectYear{'YEAR'}{'2001'} value='2001'>2001</option>
102 <option $selectYear{'YEAR'}{'2002'} value='2002'>2002</option>
103 <option $selectYear{'YEAR'}{'2003'} value='2003'>2003</option>
104 <option $selectYear{'YEAR'}{'2004'} value='2004'>2004</option>
105 <option $selectYear{'YEAR'}{'2005'} value='2005'>2005</option>
106 <option $selectYear{'YEAR'}{'2006'} value='2006'>2006</option>
107 <option $selectYear{'YEAR'}{'????'} value='????'>$Lang::tr{'allmsg'}</option>
108 </select>
109 <input type='submit' name='ACTION' value='$Lang::tr{'update'}' />
110 </form>
111 </td>
112 <td width='5%' align='center'>
113 <form method='post' action='/cgi-bin/traffics.cgi'>
114 <input type='submit' name='ACTION' value=' > ' />
115 </form>
116 </td>
117 </tr>
118 </table>
119 END
120
121 &Header::closebox();
122
123 &Header::openbox('100%', 'left', $Lang::tr{'traffics'});
124
125 my $dateWidth = '20%';
126 my $netWidth = '34%';
127 my $inOutWidth = '17%';
128
129 # 4 networks
130 if ($netsettings{'CONFIG_TYPE'} =~ /^(5|7)$/) {
131 $dateWidth = '12%';
132 $netWidth = '22%';
133 $inOutWidth = '11%';
134 }
135 # 3 networks
136 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|4|6)$/) {
137 $dateWidth = '16%';
138 $netWidth = '28%';
139 $inOutWidth = '14%';
140 }
141
142 print <<END;
143 <table width='100%'>
144 <tr>
145 <td width='$dateWidth' align='center' class='boldbase'></td>
146 <td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficgreen'}</b></td>
147 END
148
149 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/) {
150 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficblue'}</b></td>";
151 }
152
153 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/) {
154 print "<td width='$netWidth' align='center' class='boldbase' ><b>$Lang::tr{'trafficorange'}</b></td>";
155 }
156
157 print <<END;
158 <td width='$netWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficred'}</b></td>
159 </tr>
160 </table>
161 <table width='100%'>
162 <tr>
163 <td width='$dateWidth' align='center' class='boldbase'><b>$Lang::tr{'trafficdate'}</b></td>
164 <td width='$inOutWidth' align='center' class='boldbase'><font color='#16A61D'><b>$Lang::tr{'trafficin'}</b></font></td>
165 <td width='$inOutWidth' align='center' class='boldbase'><font color='#16A61D'><b>$Lang::tr{'trafficout'}</b></font></td>
166 END
167
168 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/)
169 {
170 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='${Header::colourblue}'><b>$Lang::tr{'trafficin'}</b></font></td>";
171 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='${Header::colourblue}'><b>$Lang::tr{'trafficout'}</b></font></td>";
172 }
173
174 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/)
175 {
176 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='#FF9933'><b>$Lang::tr{'trafficin'}</b></font></td>";
177 print "<td width='$inOutWidth' align='center' class='boldbase'><font color='#FF9933'><b>$Lang::tr{'trafficout'}</b></font></td>";
178 }
179 print <<END;
180 <td width='$inOutWidth' align='center' class='boldbase'><font color='#CE1B31'><b>$Lang::tr{'trafficin'}</b></font></td>
181 <td width='$inOutWidth' align='center' class='boldbase'><font color='#CE1B31'><b>$Lang::tr{'trafficout'}</b></font></td>
182 </tr>
183 END
184
185 my $total_blue_in=0;
186 my $total_blue_out=0;
187 my $total_green_in=0;
188 my $total_green_out=0;
189 my $total_orange_in=0;
190 my $total_orange_out=0;
191 my $total_red_in=0;
192 my $total_red_out=0;
193 my $lines=0;
194
195 my $displayMode = "daily";
196 my $startMonth = $cgiparams{'MONTH'};
197 my $endMonth = $cgiparams{'MONTH'};
198
199 if ($cgiparams{'MONTH'} eq '??') {
200 $displayMode = "monthly";
201 $startMonth = '01';
202 $endMonth = '12';
203 }
204
205 my $start = "$cgiparams{'YEAR'}$startMonth"."01";
206 my $end = "$cgiparams{'YEAR'}$endMonth"."32";
207 my %allDaysBytes = ();
208 my @allDays = &Traffic::calcTraffic(\%allDaysBytes,$start,$end, $displayMode);
209
210
211 foreach (@allDays) {
212 $total_green_in += $allDaysBytes{$_}{${Traffic::green_in}};
213 $total_green_out += $allDaysBytes{$_}{${Traffic::green_out}};
214
215 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/)
216 {
217 $total_blue_in += $allDaysBytes{$_}{${Traffic::blue_in}};
218 $total_blue_out += $allDaysBytes{$_}{${Traffic::blue_out}};
219 }
220
221 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/)
222 {
223 $total_orange_in += $allDaysBytes{$_}{${Traffic::orange_in}};
224 $total_orange_out += $allDaysBytes{$_}{${Traffic::orange_out}};
225 }
226
227 $total_red_in += $allDaysBytes{$_}{${Traffic::red_in}};
228 $total_red_out += $allDaysBytes{$_}{${Traffic::red_out}};
229
230 if ($lines % 2) {
231 print "<tr bgcolor='${Header::table1colour}'>"; }
232 else {
233 print "<tr bgcolor='${Header::table2colour}'>"; }
234
235 printf "<td align='center' nowrap='nowrap'>%s</td>\n", $allDaysBytes{$_}{'Day'};
236 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::green_in}}/1048576);
237 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::green_out}}/1048576);
238
239 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/)
240 {
241 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::blue_in}}/1048576);
242 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::blue_out}}/1048576);
243 }
244 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/)
245 {
246 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::orange_in}}/1048576);
247 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::orange_out}}/1048576);
248 }
249
250 printf "<td align='center' nowrap='nowrap'>%.3f</td>\n", ($allDaysBytes{$_}{${Traffic::red_in}}/1048576);
251 printf "<td align='center' nowrap='nowrap'>%.3f</td></tr>\n", ($allDaysBytes{$_}{${Traffic::red_out}}/1048576);
252
253 $lines++;
254 }
255
256 $total_green_in=sprintf("%.2f", ($total_green_in/1048576));
257 $total_green_out=sprintf("%.2f", ($total_green_out/1048576));
258 $total_blue_in=sprintf("%.2f", ($total_blue_in/1048576));
259 $total_blue_out=sprintf("%.2f", ($total_blue_out/1048576));
260 $total_orange_in=sprintf("%.2f", ($total_orange_in/1048576));
261 $total_orange_out=sprintf("%.2f", ($total_orange_out/1048576));
262 $total_red_in=sprintf("%.2f", ($total_red_in/1048576));
263 $total_red_out=sprintf("%.2f", ($total_red_out/1048576));
264
265 if ($lines % 2) {print "<tr bgcolor='${Header::table1colour}'>"; }
266 else {print "<tr bgcolor='${Header::table2colour}'>"; }
267
268 print <<END;
269 <td align='center' class='boldbase' height='20' nowrap='nowrap'><b>$Lang::tr{'trafficsum'}</b></td>
270 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_green_in MB</b></td>
271 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_green_out MB</b></td>
272 END
273
274 if ($netsettings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/)
275 {
276 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_blue_in MB</b></td>";
277 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_blue_out MB</b></td>";
278 }
279
280 if ($netsettings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/)
281 {
282 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_orange_in MB</b></td>";
283 print "<td align='center' class='boldbase' nowrap='nowrap'><b>$total_orange_out MB</b></td>";
284 }
285
286 print <<END;
287 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_in MB</b></td>
288 <td align='center' class='boldbase' nowrap='nowrap'><b>$total_red_out MB</b></td>
289 </tr>
290 </table>
291 END
292
293 &Header::closebox();
294
295 &Header::closebigbox();
296
297 &Header::closepage();