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