separate mega-module htmlandlogs.c
[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 f=fopen(str,"a");
36 if(f > 0)
37 {
38 fprintf(f,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%d\t%s",
39 time(NULL), self->name, self->traffic, self->direct, self->proxy,
40 self->upload, self->min, self->max, self->desired, self->lmsid, d); /* d = date*/
41 fclose(f);
42 }
43 else
44 {
45 perror(str);
46 }
47 }
48
49 void write_htmlandlogs(char *html, char *d, int total, int just_preview)
50 {
51 int i;
52 char *str;
53 FILE *f=fopen(html,"w");
54
55 string(str,STRLEN);
56 if(f > 0)
57 {
58 int count=1;
59 i=0;
60
61 /*-----------------------------------------------------------------*/
62 printf("Writing statistics summary into HTML page %s ...\n", html);
63 /*-----------------------------------------------------------------*/
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))
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 fputs("</span>\n",f);
204 if(popup_button)
205 {
206 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>",
207 i, i, i, popup_button);
208 }
209 }
210 fputs("</td>\n",f);
211 /* ----------------------------------------------- */
212
213 if(found_lmsid)
214 {
215 fputs("<td style=\"text-align: right\">",f);
216 if(ip->lmsid > 0)
217 {
218 fprintf(f,"<a class=\"blue\" target=\"_blank\" href=\"%s%d\">%04d</a>\n", lms_url, ip->lmsid, ip->lmsid);
219 }
220 else if(ip->lmsid == 0)
221 {
222 fputs("-------",f);
223 }
224 fputs("</td>\n",f);
225 }
226 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->credit);
227 fprintf(f,"<td style=\"text-align: right\"><span style=\"color:#%s\">%Lu</span></td>",
228 ip->keyword->html_color,
229 ip->credit+(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20)));
230 fprintf(f,"<td style=\"text-align: right\">%s%Lu%s", f1, ip->traffic, f2);
231
232 /* download --------------------------------------- */
233 fprintf(f,"</td><td style=\"text-align: right\">%Lu", ip->direct);
234 if(use_jquery_popups)
235 {
236 fprintf(f,"<span id=\"download_%d\" style=\"display:none\">",i);
237 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing))
238 {
239 fprintf(f,"<br />%Lu", sharedip->direct);
240 }
241 fputs("</span>\n",f);
242 }
243 fputs("</td>\n",f);
244 /* ----------------------------------------------- */
245
246 if(qos_proxy)
247 {
248 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->proxy);
249 }
250 /* upload ---------------------------------------- */
251 fprintf(f,"<td style=\"text-align: right\">%Lu", ip->upload);
252 if(use_jquery_popups)
253 {
254 fprintf(f,"<span id=\"upload_%d\" style=\"display:none\">",i);
255 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))
256 {
257 fprintf(f,"<br />%Lu", sharedip->upload);
258 }
259 fputs("</span>\n",f);
260 }
261 fputs("</td>\n",f);
262 /* ----------------------------------------------- */
263
264 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
265 <td style=\"text-align: right\">%d</td>\n\
266 <td style=\"text-align: right\">%s%d%s</td>\n\
267 <td>%s%d%s</td></tr>\n",
268 ip->min, ip->desired,
269 f1, ip->max, f2,
270 f1, ip->prio, f2);
271
272 total_traffic+=ip->traffic;
273 total_direct+=ip->direct;
274 total_proxy+=ip->proxy;
275 total_upload+=ip->upload;
276 if(ip->traffic>0)
277 {
278 active_classes++;
279 tmp_sum+=ip->traffic;
280 create(sum,Sum);
281 sum->l=tmp_sum;
282 sum->i=active_classes;
283 insert(sum,sums,order_by,i);
284 }
285
286 if(!just_preview)
287 {
288 append_log(ip);
289 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))
290 {
291 append_log(sharedip);
292 }
293 }
294 }
295 fprintf(f,"</tbody><thead><tr>\n\
296 <th colspan=\"%d\" style=\"text-align: left\">%d CLASSES</th>", colspan-7, i);
297 fprintf(f,"<th style=\"text-align: right\">%Lu</th><th style=\"text-align: right\">%Lu</th>\n", total_traffic, total_direct);
298 if(qos_proxy)
299 {
300 fprintf(f,"<th style=\"text-align: right\">%Lu</th>\n", total_proxy);
301 }
302 fprintf(f,"<th style=\"text-align: right\">%Lu</th>", total_upload);
303 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);
304
305 row_odd_even = 0;
306 if(active_classes>10)
307 {
308 int top20_count=0,top20_perc1=0;
309 long long top20_perc2=0;
310 unsigned long long top20_sum=0l;
311
312 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\"><caption>Enterprise Resource Planning (ERP)</caption>\n",f);
313 fputs("<thead><tr>\n\
314 <th>Analytic category</th>\n\
315 <th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\
316 <th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\
317 </tr></thead><tbody>\n",f);
318
319 if_exists(sum,sums,sum->l>=total_traffic/4)
320 {
321 fprintf(f,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());
322 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
323 <td style=\"text-align: right\">%d %%</td>\n\
324 <td style=\"text-align: right\">%Lu MB</td>\n\
325 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
326 sum->i, (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);
327 }
328
329 if_exists(sum,sums,sum->i==10)
330 {
331 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
332 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
333 <td style=\"text-align: right\">%d %%</td>\n\
334 <td style=\"text-align: right\">%Lu MB</td>\n\
335 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
336 (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);
337 }
338
339 if_exists(sum,sums,sum->l>=total_traffic/2)
340 {
341 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
342 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
343 <td style=\"text-align: right\">%d %%</td>\n\
344 <td style=\"text-align: right\">%Lu MB</td>\n\
345 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
346 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
347 }
348
349 if_exists(sum,sums,sum->l>=4*total_traffic/5)
350 {
351 fprintf(f,"%s<td>Top 80%% of traffic</td>\n", tr_odd_even());
352 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
353 <td style=\"text-align: right\">%d %%</td>\n\
354 <td style=\"text-align: right\">%Lu MB</td>\n\
355 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
356 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
357 }
358
359 if_exists(sum,sums,sum->i>=(active_classes+1)/5)
360 {
361 fprintf(f,"%s<td>Top 20%% downloaders</td>\n", tr_odd_even());
362 top20_count=sum->i;
363 top20_perc1=(100*sum->i+50)/active_classes;
364 top20_sum=sum->l;
365 top20_perc2=(100*sum->l+50)/total_traffic;
366 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
367 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
368 <td style=\"text-align: right\">%Lu MB</td>\n\
369 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
370 top20_count,top20_perc1,top20_sum,top20_perc2);
371 }
372
373 if_exists(sum,sums,sum->i>=(active_classes+1)/4)
374 {
375 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
376 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
377 <td style=\"text-align: right\">%d %%</td>\n\
378 <td style=\"text-align: right\">%Lu MB</td>\n\
379 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
380 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
381 }
382
383 if_exists(sum,sums,sum->i>=(active_classes+1)/2)
384 {
385 fprintf(f,"%s<td>Top 50%% downloaders</td>\n", tr_odd_even());
386 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
387 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
388 <td style=\"text-align: right\">%Lu MB</td>\n\
389 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
390 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
391 }
392
393 if_exists(sum,sums,sum->i>=4*(active_classes+1)/5)
394 {
395 fprintf(f,"%s<td>Top 80%% downloaders</td>\n", tr_odd_even());
396 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
397 <td style=\"text-align: right\">%d %%</td>\n\
398 <td style=\"text-align: right\">%Lu MB</td>\n\
399 <td style=\"text-align: right\">%Ld %%</td></tr></tbody>\n",
400 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
401 }
402
403 fprintf(f,"<thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url);
404 fprintf(f,"<th style=\"text-align: right\">%d</th>\n\
405 <th style=\"text-align: right\">100 %%</th>\n\
406 <th style=\"text-align: right\">%Lu MB</th>\n\
407 <th style=\"text-align: right\">100 %%</th></tr>\n",active_classes,total_traffic);
408 fputs("</thead></table>\n", f);
409
410 /* write basic ERP data to log directory */
411 if(!just_preview)
412 {
413 FILE *iplog;
414 sprintf(str,"%s/ERP.log",log_dir);
415 iplog=fopen(str,"a");
416 if(iplog)
417 {
418 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",
419 time(NULL), top20_count, top20_perc1, top20_sum, top20_perc2,
420 active_classes, total_traffic, i, limit_count, prio_count, d); /* d = date*/
421 fclose(iplog);
422 }
423 else
424 {
425 perror(str);
426 }
427 }
428 }
429
430 fprintf(f, stats_html_signature, version);
431 fclose(f);
432 }
433 }
This page took 0.808459 seconds and 4 git commands to generate.