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