simple iptables based packet limiter to deflect DoS attacks on our clients
[svn/Prometheus-QoS/.git] / htmlandlogs.c
1 #include "cll1-0.6.2.h"
2 #include "ipstruct.h"
3 #define STRLEN 512
4
5 extern int row_odd_even;
6 extern int use_jquery_popups;
7 extern struct IP *ips, *ip, *sharedip;
8 extern struct Group *groups, *group;
9 extern struct Keyword *keyword, *defaultkeyword, *keywords;
10 extern const int highest_priority;
11 extern const char *version;
12 extern const char *stats_html_signature;
13 extern char *jquery_url;
14 extern int keywordcount;
15 extern long long int line;
16 extern int dry_run;
17 extern int qos_proxy;
18 extern char *title;
19 extern char *log_url;
20 extern int found_lmsid;
21 extern char *lms_url;
22 extern char *log_dir;
23 extern char *ip6prefix;
24
25 const char *tr_odd_even(void);
26 /* implemented in prometheus.c, shared with parselogs.c */
27
28 void append_log(struct IP *self) /*using global variables*/
29 {
30 char *d, *str;
31 FILE *f;
32
33 date(d); /* this is typical cll1.h macro - prints current date */
34 string(str, STRLEN);
35 sprintf(str, "%s/%s.log", log_dir, self->name);
36
37 /*-----------------------------------------------------------------*/
38 printf("Writing traffic log %s ...\n", str);
39 /*-----------------------------------------------------------------*/
40 f = fopen(str, "a");
41 if(f > 0)
42 {
43 fprintf(f, "%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%d\t%s",
44 time(NULL), self->name, self->traffic, self->direct, self->proxy,
45 self->upload, self->min, self->max, self->desired, self->lmsid, d); /* d = date*/
46 fclose(f);
47 }
48 else
49 {
50 perror(str);
51 }
52 }
53
54 void write_htmlandlogs(char *html, char *d, int total, int just_preview)
55 {
56 int i;
57 char *str;
58 FILE *f=fopen(html, "w");
59
60 string(str,STRLEN);
61 if(f > 0)
62 {
63 int count=1;
64 i=0;
65
66 if(use_jquery_popups)
67 {
68 fprintf(f,"<script type=\"text/javascript\" src=\"%s\"></script>\n", jquery_url);
69 }
70 fputs("<table class=\"decorated last\">\n\
71 <caption>Bandwidth classes</caption>\n\
72 <thead><tr>\n\
73 <th style=\"text-align: right\">#</th>\n\
74 <th style=\"text-align: right\">group</th>\n\
75 <th style=\"text-align: right\">IPs</th>\n\
76 <th style=\"text-align: right\">requested</th>\n",f);
77 fprintf(f,"<th colspan=\"%d\">data limits</th>\n", keywordcount);
78 fputs("</tr></thead><tbody>\n",f);
79
80 row_odd_even = 0;
81 for_each(group, groups)
82 {
83 #ifdef DEBUG
84 printf("%d kb/s group: %d bandwidth requested: %d kb/s\n",group->min,group->count,group->desired);
85 #endif
86 fprintf(f, "%s<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d&nbsp;kb/s</td>",
87 tr_odd_even(), count, group->min);
88 fprintf(f, "<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d&nbsp;kb/s</td>",
89 group->count, group->desired);
90
91 for_each(keyword, keywords) if(keyword->ip_count)
92 {
93 fprintf(f, "<td style=\"text-align: right\"><span style=\"color:#%s\">%d&nbsp;MB</span></td>",
94 keyword->html_color, group->min*keyword->data_limit);
95 }
96 i += group->desired;
97 total += group->count;
98 count++;
99 }
100 #ifdef DEBUG
101 printf("Total groups: %d Total bandwidth requested: %d kb/s\nAGGREGATION: 1/%d\n",
102 count, i, i/line);
103 #endif
104 fprintf(f,"</tr></tbody>\n\
105 <thead><tr>\n\
106 <th colspan=\"2\" style=\"text-align: left\">Line %Ld kb/s</td>",line);
107 fprintf(f,"<th style=\"text-align: right\">%d</td><th style=\"text-align: right\">%d kb/s</td>",total,i);
108
109 for_each(keyword, keywords) if(keyword->ip_count)
110 {
111 fprintf(f,"<th style=\"text-align: right\">%d IPs</th>",keyword->ip_count);
112 }
113 fprintf(f,"</tr><tr><th colspan=\"4\">Aggregation 1/%d</th>\n", (int)(0.5+i/line));
114 fprintf(f,"<th colspan=\"%d\">%d traffic classes</th></tr>\n", keywordcount, total);
115
116 fputs("</thead></table>\n",f);
117 }
118 else
119 {
120 perror(html);
121 }
122
123 i=0;
124 if(f > 0)
125 {
126 unsigned long long total_traffic=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum = 0;
127 int active_classes = 0;
128 int colspan = 12;
129 struct Sum {unsigned long long l; int i; list(Sum);} *sum,*sums = NULL;
130 int limit_count = 0, prio_count = 0;
131 int popup_button = 0;
132 /* IPv6 vs. IPv4 stats */
133 unsigned long long pkts4 =0, pkts6 = 0, bytes4 = 0, bytes6 = 0;
134 int count4 = 0, count6 = 0;
135 double perc6;
136
137 if(qos_proxy)
138 {
139 colspan++;
140 }
141
142 fprintf(f,"<p><table class=\"decorated last\">\n<caption>%s",title);
143 fprintf(f," (%s)</caption>\n", d);
144 fputs("<thead><tr>\n<th colspan=\"3\">&nbsp;</th>\n",f);
145 fputs("<th style=\"text-align: right\">credit</th>\n\
146 <th style=\"text-align: right\">FUP</th>\n\
147 <th style=\"text-align: right\">total</th>\n\
148 <th style=\"text-align: right\">down</th>\n",f);
149 if(qos_proxy)
150 {
151 fputs("<th style=\"text-align: right\">proxy</th>\n",f);
152 }
153 fputs("<th style=\"text-align: right\">up</th>\n\
154 <th style=\"text-align: right\">min</th>\n\
155 <th style=\"text-align: right\">max</th>\n\
156 <th style=\"text-align: right\">limit</th>\n\
157 <th>&nbsp;</th>\n\
158 </tr><tr>\n\
159 <th style=\"text-align: right\">#</th>\n\
160 <th>hostname [+sharing]</th>\n\
161 <th style=\"text-align: right\">LMS</th>\n\
162 <th style=\"text-align: right\">MB</th>\n\
163 <th style=\"text-align: right\">MB</th>\n\
164 <th style=\"text-align: right\">MB</th>\n\
165 <th style=\"text-align: right\">MB</th>\n\
166 <th style=\"text-align: right\">MB</th>\n\
167 <th style=\"text-align: right\">kb/s</th>\n\
168 <th style=\"text-align: right\">kb/s</th>\n\
169 <th style=\"text-align: right\">kb/s</th>\n\
170 <th>prio</th>\n\
171 </tr></thead><tbody>\n",f);
172
173 row_odd_even = 0;
174 for_each(ip,ips) if(!use_jquery_popups || !ip->sharing)
175 {
176 char *f1="", *f2="";
177 i++;
178
179 if(ip->max < ip->desired)
180 {
181 f1 = "<span style=\"color:red\">";
182 f2 = "</span>";
183 limit_count++;
184 }
185 else if(ip->prio > highest_priority+1)
186 {
187 f1 = "<span style=\"color:brown\">";
188 f2 = "</span>";
189 prio_count++;
190 }
191
192 #ifdef DEBUG
193 printf("%03d. %-22s %10Lu (%d/%d)\n",i ,ip->name, ip->traffic, ip->min, ip->max);
194 #endif
195 /* hostnames -------------------------------------- */
196 fprintf(f,"%s<td style=\"text-align: right\"><a name=\"%s\"></a>%d</td><td><a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s</a>\n",
197 tr_odd_even(), ip->name, i, log_url, ip->name, ip->name);
198
199 if(use_jquery_popups)
200 {
201 fprintf(f, "<span id=\"sharing_%d\" style=\"display:none\">",i);
202 popup_button=0;
203
204 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing) && !sharedip->v6) /* IPv4 only */
205 {
206 fprintf(f, "<br /><a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s</a>\n",
207 log_url, sharedip->name, sharedip->name);
208 popup_button++;
209 }
210
211 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing) && sharedip->v6) /* IPv6 only */
212 {
213 fprintf(f, "<br /><a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s/64</a>\n",
214 log_url, sharedip->addr, sharedip->addr);
215 popup_button++;
216 }
217
218 fputs("</span>\n",f);
219 if(popup_button)
220 {
221 fprintf(f, "<span>[<a class=\"blue\" href=\"#\" onClick=\"$(this).parent().hide();$(\'#sharing_%d\').show();$(\'#download_%d\').show();$(\'#upload_%d\').show();return(false);\" style=\"cursor: pointer;\">+%d</a>]</span>",
222 i, i, i, popup_button);
223 }
224 }
225 fputs("</td>\n",f);
226 /* ----------------------------------------------- */
227
228 if(found_lmsid)
229 {
230 fputs("<td style=\"text-align: right\">",f);
231 if(ip->lmsid > 0)
232 {
233 fprintf(f,"<a class=\"blue\" target=\"_blank\" href=\"%s%d\">%04d</a>\n", lms_url, ip->lmsid, ip->lmsid);
234 }
235 else if(ip->lmsid == 0)
236 {
237 fputs("-------",f);
238 }
239 fputs("</td>\n",f);
240 }
241 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->credit);
242 fprintf(f,"<td style=\"text-align: right\"><span style=\"color:#%s\">%Lu</span></td>",
243 ip->keyword->html_color, ip->realquota);
244 fprintf(f,"<td style=\"text-align: right\">%s%Lu%s", f1, ip->traffic, f2);
245
246 /* download --------------------------------------- */
247 fprintf(f,"</td><td style=\"text-align: right\">%Lu", ip->direct);
248 if(use_jquery_popups)
249 {
250 fprintf(f,"<span id=\"download_%d\" style=\"display:none\">", i);
251 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing) && !sharedip->v6) /* IPv4 only */
252 {
253 fprintf(f,"<br />%Lu", sharedip->direct);
254 }
255 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing) && sharedip->v6) /* IPv6 only */
256 {
257 fprintf(f,"<br />%Lu", sharedip->direct);
258 }
259 fputs("</span>\n",f);
260 }
261 fputs("</td>\n",f);
262 /* ----------------------------------------------- */
263
264 if(qos_proxy)
265 {
266 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->proxy);
267 }
268 /* upload ---------------------------------------- */
269 fprintf(f,"<td style=\"text-align: right\">%Lu", ip->upload);
270 if(use_jquery_popups)
271 {
272 fprintf(f,"<span id=\"upload_%d\" style=\"display:none\">", i);
273 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing) && !sharedip->v6) /* IPv4 only */
274 {
275 fprintf(f,"<br />%Lu", sharedip->upload);
276 }
277 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing) && sharedip->v6) /* IPv6 only */
278 {
279 fprintf(f,"<br />%Lu", sharedip->upload);
280 }
281 fputs("</span>\n",f);
282 }
283 fputs("</td>\n",f);
284 /* ----------------------------------------------- */
285
286 fprintf(f, "<td style=\"text-align: right\">%d</td>\n\
287 <td style=\"text-align: right\">%d</td>\n\
288 <td style=\"text-align: right\">%s%d%s</td>\n\
289 <td>%s%d%s</td></tr>\n",
290 ip->min, ip->desired,
291 f1, ip->max, f2,
292 f1, ip->prio, f2);
293
294 total_traffic+=ip->traffic;
295 total_direct+=ip->direct;
296 total_proxy+=ip->proxy;
297 total_upload+=ip->upload;
298 if(ip->traffic>0)
299 {
300 active_classes++;
301 tmp_sum+=ip->traffic;
302 create(sum,Sum);
303 sum->l=tmp_sum;
304 sum->i=active_classes;
305 insert(sum,sums,order_by,i);
306 }
307
308 if(!just_preview)
309 {
310 append_log(ip);
311 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))
312 {
313 append_log(sharedip);
314 }
315 }
316 }
317 fprintf(f,"</tbody><thead><tr>\n\
318 <th colspan=\"%d\" style=\"text-align: left\">%d CLASSES</th>", colspan-7, i);
319 fprintf(f, "<th style=\"text-align: right\">%Lu</th><th style=\"text-align: right\">%Lu</th>\n",
320 total_traffic, total_direct);
321 if(qos_proxy)
322 {
323 fprintf(f," <th style=\"text-align: right\">%Lu</th>\n", total_proxy);
324 }
325 fprintf(f, "<th style=\"text-align: right\">%Lu</th>", total_upload);
326 fprintf(f, "<th colspan=\"4\"><span style=\"color:red\">LIMIT %dx</span> <span style=\"color:brown\">LOW-PRIO %dx</span></th></tr>\n</thead></table>\n",
327 limit_count, prio_count);
328
329 if(ip6prefix)
330 {
331 for_each(ip, ips)
332 {
333 if(ip->v6)
334 {
335 bytes6 += ip->upload + ip->direct;
336 pkts6 += ip->pktsdown + ip->pktsup;
337 count6++;
338 }
339 else
340 {
341 bytes4 += ip->upload + ip->direct;
342 pkts4 += ip->pktsdown + ip->pktsup;
343 count4++;
344 }
345 }
346
347 perc6=(double)(bytes6)/(bytes4+bytes6)*100;
348 fputs("<p><table class=\"decorated last\"><caption>IP protocols usage</caption>\n",f);
349 fprintf(f, "%s<td>Total %d IPv4 addreses</td><td style=\"text-align: right\">%Lu MB (%.2f %%)</td><td style=\"text-align: right\">%Lu packets (%.2f %%)</td></tr>\n",
350 tr_odd_even(), count4, bytes4, (double)(bytes4)/(bytes4+bytes6)*100, pkts4, (float)(100*pkts4)/(pkts4+pkts6));
351 fprintf(f, "%s<td>Total %d IPv6 /64 ranges</td><td style=\"text-align: right\">%Lu MB (%.2f %%)</td><td style=\"text-align: right\">%Lu packets (%.2f %%)</td></tr>\n",
352 tr_odd_even(), count6, bytes6, perc6, pkts6, (float)(100*pkts6)/(pkts4+pkts6));
353 fputs("</table></p>\n", f);
354 }
355
356 row_odd_even = 0;
357 if(active_classes>10)
358 {
359 int top20_count=0,top20_perc1=0;
360 long long top20_perc2=0;
361 unsigned long long top20_sum=0l;
362
363 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\"><caption>Enterprise Resource Planning (ERP)</caption>\n",f);
364 fputs("<thead><tr>\n\
365 <th>Analytic category</th>\n\
366 <th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\
367 <th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\
368 </tr></thead><tbody>\n",f);
369
370 if_exists(sum,sums,sum->l >= total_traffic/4)
371 {
372 fprintf(f,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());
373 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
374 <td style=\"text-align: right\">%d %%</td>\n\
375 <td style=\"text-align: right\">%Lu MB</td>\n\
376 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
377 sum->i, (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);
378 }
379
380 if_exists(sum,sums,sum->i == 10)
381 {
382 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
383 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
384 <td style=\"text-align: right\">%d %%</td>\n\
385 <td style=\"text-align: right\">%Lu MB</td>\n\
386 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
387 (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);
388 }
389
390 if_exists(sum,sums,sum->l >= total_traffic/2)
391 {
392 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
393 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
394 <td style=\"text-align: right\">%d %%</td>\n\
395 <td style=\"text-align: right\">%Lu MB</td>\n\
396 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
397 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
398 }
399
400 if_exists(sum,sums,sum->l >= 4*total_traffic/5)
401 {
402 fprintf(f,"%s<td>Top 80%% of traffic</td>\n", tr_odd_even());
403 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
404 <td style=\"text-align: right\">%d %%</td>\n\
405 <td style=\"text-align: right\">%Lu MB</td>\n\
406 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
407 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
408 }
409
410 if_exists(sum,sums,sum->i >= (active_classes+1)/5)
411 {
412 fprintf(f,"%s<td>Top 20%% downloaders</td>\n", tr_odd_even());
413 top20_count=sum->i;
414 top20_perc1=(100*sum->i+50)/active_classes;
415 top20_sum=sum->l;
416 top20_perc2=(100*sum->l+50)/total_traffic;
417 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
418 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
419 <td style=\"text-align: right\">%Lu MB</td>\n\
420 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
421 top20_count,top20_perc1,top20_sum,top20_perc2);
422 }
423
424 if_exists(sum,sums,sum->i >= (active_classes+1)/4)
425 {
426 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
427 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
428 <td style=\"text-align: right\">%d %%</td>\n\
429 <td style=\"text-align: right\">%Lu MB</td>\n\
430 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
431 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
432 }
433
434 if_exists(sum,sums,sum->i>=(active_classes+1)/2)
435 {
436 fprintf(f,"%s<td>Top 50%% downloaders</td>\n", tr_odd_even());
437 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
438 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
439 <td style=\"text-align: right\">%Lu MB</td>\n\
440 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
441 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
442 }
443
444 if_exists(sum,sums,sum->i >= 4*(active_classes+1)/5)
445 {
446 fprintf(f,"%s<td>Top 80%% downloaders</td>\n", tr_odd_even());
447 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
448 <td style=\"text-align: right\">%d %%</td>\n\
449 <td style=\"text-align: right\">%Lu MB</td>\n\
450 <td style=\"text-align: right\">%Ld %%</td></tr></tbody>\n",
451 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
452 }
453
454 fprintf(f,"<thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url);
455 fprintf(f,"<th style=\"text-align: right\">%d</th>\n\
456 <th style=\"text-align: right\">100 %%</th>\n\
457 <th style=\"text-align: right\">%Lu MB</th>\n\
458 <th style=\"text-align: right\">100 %%</th></tr>\n",active_classes,total_traffic);
459 fputs("</thead></table></p>\n", f);
460
461 /* write basic ERP data to log directory */
462 if(!just_preview)
463 {
464 FILE *iplog;
465 sprintf(str,"%s/ERP.log",log_dir);
466 iplog=fopen(str,"a");
467 if(iplog)
468 {
469 fprintf(iplog, "%ld\t%d\t%d %%\t%Lu M\t%Ld %%\tACTIVE %d\tTRAFFIC %Lu M\tCLASSES %d\tFUP-LIMIT %d\tLOW-PRIO %d\tIPv6 %Lu M\t%.2f %%\t%s",
470 time(NULL), top20_count, top20_perc1, top20_sum, top20_perc2,
471 active_classes, total_traffic, i, limit_count, prio_count,
472 bytes6, perc6, d); /* d = date*/
473 fclose(iplog);
474 }
475 else
476 {
477 perror(str);
478 }
479 }
480 }
481
482 fprintf(f, stats_html_signature, version);
483 fclose(f);
484 }
485 }
This page took 0.568184 seconds and 4 git commands to generate.