ip protocol usage statistics and more
[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
133 if(qos_proxy)
134 {
135 colspan++;
136 }
137
138 fprintf(f,"<p><table class=\"decorated last\">\n<caption>%s",title);
139 fprintf(f," (%s)</caption>\n", d);
140 fputs("<thead><tr>\n<th colspan=\"3\">&nbsp;</th>\n",f);
141 fputs("<th style=\"text-align: right\">credit</th>\n\
142 <th style=\"text-align: right\">FUP</th>\n\
143 <th style=\"text-align: right\">total</th>\n\
144 <th style=\"text-align: right\">down</th>\n",f);
145 if(qos_proxy)
146 {
147 fputs("<th style=\"text-align: right\">proxy</th>\n",f);
148 }
149 fputs("<th style=\"text-align: right\">up</th>\n\
150 <th style=\"text-align: right\">min</th>\n\
151 <th style=\"text-align: right\">max</th>\n\
152 <th style=\"text-align: right\">limit</th>\n\
153 <th>&nbsp;</th>\n\
154 </tr><tr>\n\
155 <th style=\"text-align: right\">#</th>\n\
156 <th>hostname [+sharing]</th>\n\
157 <th style=\"text-align: right\">LMS</th>\n\
158 <th style=\"text-align: right\">MB</th>\n\
159 <th style=\"text-align: right\">MB</th>\n\
160 <th style=\"text-align: right\">MB</th>\n\
161 <th style=\"text-align: right\">MB</th>\n\
162 <th style=\"text-align: right\">MB</th>\n\
163 <th style=\"text-align: right\">kb/s</th>\n\
164 <th style=\"text-align: right\">kb/s</th>\n\
165 <th style=\"text-align: right\">kb/s</th>\n\
166 <th>prio</th>\n\
167 </tr></thead><tbody>\n",f);
168
169 row_odd_even = 0;
170 for_each(ip,ips) if(!use_jquery_popups || !ip->sharing)
171 {
172 char *f1="", *f2="";
173 i++;
174
175 if(ip->max < ip->desired)
176 {
177 f1 = "<span style=\"color:red\">";
178 f2 = "</span>";
179 limit_count++;
180 }
181 else if(ip->prio > highest_priority+1)
182 {
183 f1 = "<span style=\"color:brown\">";
184 f2 = "</span>";
185 prio_count++;
186 }
187
188 #ifdef DEBUG
189 printf("%03d. %-22s %10Lu (%d/%d)\n",i ,ip->name, ip->traffic, ip->min, ip->max);
190 #endif
191 /* hostnames -------------------------------------- */
192 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",
193 tr_odd_even(), ip->name, i, log_url, ip->name, ip->name);
194
195 if(use_jquery_popups)
196 {
197 fprintf(f, "<span id=\"sharing_%d\" style=\"display:none\">",i);
198 popup_button=0;
199
200 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing) && !sharedip->v6) /* IPv4 only */
201 {
202 fprintf(f, "<br /><a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s</a>\n",
203 log_url, sharedip->name, sharedip->name);
204 popup_button++;
205 }
206
207 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing) && sharedip->v6) /* IPv6 only */
208 {
209 fprintf(f, "<br /><a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s/64</a>\n",
210 log_url, sharedip->addr, sharedip->addr);
211 popup_button++;
212 }
213
214 fputs("</span>\n",f);
215 if(popup_button)
216 {
217 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>",
218 i, i, i, popup_button);
219 }
220 }
221 fputs("</td>\n",f);
222 /* ----------------------------------------------- */
223
224 if(found_lmsid)
225 {
226 fputs("<td style=\"text-align: right\">",f);
227 if(ip->lmsid > 0)
228 {
229 fprintf(f,"<a class=\"blue\" target=\"_blank\" href=\"%s%d\">%04d</a>\n", lms_url, ip->lmsid, ip->lmsid);
230 }
231 else if(ip->lmsid == 0)
232 {
233 fputs("-------",f);
234 }
235 fputs("</td>\n",f);
236 }
237 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->credit);
238 fprintf(f,"<td style=\"text-align: right\"><span style=\"color:#%s\">%Lu</span></td>",
239 ip->keyword->html_color, ip->realquota);
240 fprintf(f,"<td style=\"text-align: right\">%s%Lu%s", f1, ip->traffic, f2);
241
242 /* download --------------------------------------- */
243 fprintf(f,"</td><td style=\"text-align: right\">%Lu", ip->direct);
244 if(use_jquery_popups)
245 {
246 fprintf(f,"<span id=\"download_%d\" style=\"display:none\">", i);
247 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing) && !sharedip->v6) /* IPv4 only */
248 {
249 fprintf(f,"<br />%Lu", sharedip->direct);
250 }
251 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing) && sharedip->v6) /* IPv6 only */
252 {
253 fprintf(f,"<br />%Lu", sharedip->direct);
254 }
255 fputs("</span>\n",f);
256 }
257 fputs("</td>\n",f);
258 /* ----------------------------------------------- */
259
260 if(qos_proxy)
261 {
262 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->proxy);
263 }
264 /* upload ---------------------------------------- */
265 fprintf(f,"<td style=\"text-align: right\">%Lu", ip->upload);
266 if(use_jquery_popups)
267 {
268 fprintf(f,"<span id=\"upload_%d\" style=\"display:none\">", i);
269 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing) && !sharedip->v6) /* IPv4 only */
270 {
271 fprintf(f,"<br />%Lu", sharedip->upload);
272 }
273 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing) && sharedip->v6) /* IPv6 only */
274 {
275 fprintf(f,"<br />%Lu", sharedip->upload);
276 }
277 fputs("</span>\n",f);
278 }
279 fputs("</td>\n",f);
280 /* ----------------------------------------------- */
281
282 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
283 <td style=\"text-align: right\">%d</td>\n\
284 <td style=\"text-align: right\">%s%d%s</td>\n\
285 <td>%s%d%s</td></tr>\n",
286 ip->min, ip->desired,
287 f1, ip->max, f2,
288 f1, ip->prio, f2);
289
290 total_traffic+=ip->traffic;
291 total_direct+=ip->direct;
292 total_proxy+=ip->proxy;
293 total_upload+=ip->upload;
294 if(ip->traffic>0)
295 {
296 active_classes++;
297 tmp_sum+=ip->traffic;
298 create(sum,Sum);
299 sum->l=tmp_sum;
300 sum->i=active_classes;
301 insert(sum,sums,order_by,i);
302 }
303
304 if(!just_preview)
305 {
306 append_log(ip);
307 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))
308 {
309 append_log(sharedip);
310 }
311 }
312 }
313 fprintf(f,"</tbody><thead><tr>\n\
314 <th colspan=\"%d\" style=\"text-align: left\">%d CLASSES</th>", colspan-7, i);
315 fprintf(f,"<th style=\"text-align: right\">%Lu</th><th style=\"text-align: right\">%Lu</th>\n", total_traffic, total_direct);
316 if(qos_proxy)
317 {
318 fprintf(f,"<th style=\"text-align: right\">%Lu</th>\n", total_proxy);
319 }
320 fprintf(f,"<th style=\"text-align: right\">%Lu</th>", total_upload);
321 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",limit_count,prio_count);
322
323 if(ip6prefix)
324 {
325 unsigned long long pkts4 =0, pkts6 = 0, bytes4 = 0, bytes6 = 0;
326 for_each(ip, ips)
327 {
328 if(ip->v6)
329 {
330 bytes6 += ip->traffic;
331 pkts6 += ip->pktsdown+ip->pktsup;
332 }
333 else
334 {
335 bytes4 += ip->traffic;
336 pkts4 += ip->pktsdown+ip->pktsup;
337 }
338 }
339
340 fputs("<p><table class=\"decorated last\"><caption>IP protocols usage</caption>\n",f);
341 fprintf(f, "%s<td>Total IPv4</td><td style=\"text-align: right\">%Lu MB (%.2f %%)</td><td style=\"text-align: right\">%Lu packets (%d %%)</td></tr>\n",
342 tr_odd_even(), bytes4, (float)(100*bytes4)/(bytes4+bytes6), pkts4, (float)(100*pkts4)/(pkts4+pkts6));
343 fprintf(f, "%s<td>Total IPv6</td><td style=\"text-align: right\">%Lu MB (%.2f %%)</td><td style=\"text-align: right\">%Lu packets (%d %%)</td></tr>\n",
344 tr_odd_even(), bytes6, (float)(100*bytes6)/(bytes4+bytes6), pkts6, (float)(100*pkts6)/(pkts4+pkts6));
345 fputs("</table>\n", f);
346 }
347
348 row_odd_even = 0;
349 if(active_classes>10)
350 {
351 int top20_count=0,top20_perc1=0;
352 long long top20_perc2=0;
353 unsigned long long top20_sum=0l;
354
355 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\"><caption>Enterprise Resource Planning (ERP)</caption>\n",f);
356 fputs("<thead><tr>\n\
357 <th>Analytic category</th>\n\
358 <th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\
359 <th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\
360 </tr></thead><tbody>\n",f);
361
362 if_exists(sum,sums,sum->l>=total_traffic/4)
363 {
364 fprintf(f,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());
365 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
366 <td style=\"text-align: right\">%d %%</td>\n\
367 <td style=\"text-align: right\">%Lu MB</td>\n\
368 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
369 sum->i, (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);
370 }
371
372 if_exists(sum,sums,sum->i==10)
373 {
374 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
375 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
376 <td style=\"text-align: right\">%d %%</td>\n\
377 <td style=\"text-align: right\">%Lu MB</td>\n\
378 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
379 (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);
380 }
381
382 if_exists(sum,sums,sum->l>=total_traffic/2)
383 {
384 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
385 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
386 <td style=\"text-align: right\">%d %%</td>\n\
387 <td style=\"text-align: right\">%Lu MB</td>\n\
388 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
389 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
390 }
391
392 if_exists(sum,sums,sum->l>=4*total_traffic/5)
393 {
394 fprintf(f,"%s<td>Top 80%% of traffic</td>\n", tr_odd_even());
395 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
396 <td style=\"text-align: right\">%d %%</td>\n\
397 <td style=\"text-align: right\">%Lu MB</td>\n\
398 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
399 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
400 }
401
402 if_exists(sum,sums,sum->i>=(active_classes+1)/5)
403 {
404 fprintf(f,"%s<td>Top 20%% downloaders</td>\n", tr_odd_even());
405 top20_count=sum->i;
406 top20_perc1=(100*sum->i+50)/active_classes;
407 top20_sum=sum->l;
408 top20_perc2=(100*sum->l+50)/total_traffic;
409 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
410 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
411 <td style=\"text-align: right\">%Lu MB</td>\n\
412 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
413 top20_count,top20_perc1,top20_sum,top20_perc2);
414 }
415
416 if_exists(sum,sums,sum->i>=(active_classes+1)/4)
417 {
418 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
419 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
420 <td style=\"text-align: right\">%d %%</td>\n\
421 <td style=\"text-align: right\">%Lu MB</td>\n\
422 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
423 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
424 }
425
426 if_exists(sum,sums,sum->i>=(active_classes+1)/2)
427 {
428 fprintf(f,"%s<td>Top 50%% downloaders</td>\n", tr_odd_even());
429 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
430 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
431 <td style=\"text-align: right\">%Lu MB</td>\n\
432 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
433 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
434 }
435
436 if_exists(sum,sums,sum->i>=4*(active_classes+1)/5)
437 {
438 fprintf(f,"%s<td>Top 80%% downloaders</td>\n", tr_odd_even());
439 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
440 <td style=\"text-align: right\">%d %%</td>\n\
441 <td style=\"text-align: right\">%Lu MB</td>\n\
442 <td style=\"text-align: right\">%Ld %%</td></tr></tbody>\n",
443 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
444 }
445
446 fprintf(f,"<thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url);
447 fprintf(f,"<th style=\"text-align: right\">%d</th>\n\
448 <th style=\"text-align: right\">100 %%</th>\n\
449 <th style=\"text-align: right\">%Lu MB</th>\n\
450 <th style=\"text-align: right\">100 %%</th></tr>\n",active_classes,total_traffic);
451 fputs("</thead></table>\n", f);
452
453 /* write basic ERP data to log directory */
454 if(!just_preview)
455 {
456 FILE *iplog;
457 sprintf(str,"%s/ERP.log",log_dir);
458 iplog=fopen(str,"a");
459 if(iplog)
460 {
461 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\t%s",
462 time(NULL), top20_count, top20_perc1, top20_sum, top20_perc2,
463 active_classes, total_traffic, i, limit_count, prio_count, d); /* d = date*/
464 fclose(iplog);
465 }
466 else
467 {
468 perror(str);
469 }
470 }
471 }
472
473 fprintf(f, stats_html_signature, version);
474 fclose(f);
475 }
476 }
This page took 0.691198 seconds and 4 git commands to generate.