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