more information in json output (for mailing system)
[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
e48d46c9 228 ip->keyword->html_color, ip->realquota);\r
9a56ab25 229 fprintf(f,"<td style=\"text-align: right\">%s%Lu%s", f1, ip->traffic, f2);\r
230\r
231 /* download --------------------------------------- */\r
232 fprintf(f,"</td><td style=\"text-align: right\">%Lu", ip->direct);\r
233 if(use_jquery_popups)\r
234 {\r
235 fprintf(f,"<span id=\"download_%d\" style=\"display:none\">",i);\r
236 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing))\r
237 {\r
238 fprintf(f,"<br />%Lu", sharedip->direct);\r
239 }\r
240 fputs("</span>\n",f);\r
241 }\r
242 fputs("</td>\n",f);\r
243 /* ----------------------------------------------- */\r
244\r
245 if(qos_proxy)\r
246 {\r
247 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->proxy);\r
248 }\r
249 /* upload ---------------------------------------- */\r
250 fprintf(f,"<td style=\"text-align: right\">%Lu", ip->upload);\r
251 if(use_jquery_popups)\r
252 {\r
253 fprintf(f,"<span id=\"upload_%d\" style=\"display:none\">",i);\r
254 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))\r
255 {\r
256 fprintf(f,"<br />%Lu", sharedip->upload);\r
257 }\r
258 fputs("</span>\n",f);\r
259 }\r
260 fputs("</td>\n",f);\r
261 /* ----------------------------------------------- */\r
262\r
263 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
264<td style=\"text-align: right\">%d</td>\n\\r
265<td style=\"text-align: right\">%s%d%s</td>\n\\r
266<td>%s%d%s</td></tr>\n",\r
267 ip->min, ip->desired, \r
268 f1, ip->max, f2, \r
269 f1, ip->prio, f2);\r
270\r
271 total_traffic+=ip->traffic;\r
272 total_direct+=ip->direct;\r
273 total_proxy+=ip->proxy;\r
274 total_upload+=ip->upload;\r
275 if(ip->traffic>0)\r
276 {\r
277 active_classes++;\r
278 tmp_sum+=ip->traffic;\r
279 create(sum,Sum);\r
280 sum->l=tmp_sum;\r
281 sum->i=active_classes;\r
282 insert(sum,sums,order_by,i);\r
283 }\r
284\r
285 if(!just_preview)\r
286 {\r
287 append_log(ip);\r
288 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))\r
289 {\r
290 append_log(sharedip);\r
291 }\r
292 }\r
293 }\r
294 fprintf(f,"</tbody><thead><tr>\n\\r
295<th colspan=\"%d\" style=\"text-align: left\">%d CLASSES</th>", colspan-7, i);\r
296 fprintf(f,"<th style=\"text-align: right\">%Lu</th><th style=\"text-align: right\">%Lu</th>\n", total_traffic, total_direct);\r
297 if(qos_proxy)\r
298 {\r
299 fprintf(f,"<th style=\"text-align: right\">%Lu</th>\n", total_proxy);\r
300 }\r
301 fprintf(f,"<th style=\"text-align: right\">%Lu</th>", total_upload);\r
302 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
303\r
304 row_odd_even = 0;\r
305 if(active_classes>10)\r
306 {\r
307 int top20_count=0,top20_perc1=0;\r
308 long long top20_perc2=0;\r
309 unsigned long long top20_sum=0l;\r
310 \r
311 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\"><caption>Enterprise Resource Planning (ERP)</caption>\n",f);\r
312 fputs("<thead><tr>\n\\r
313<th>Analytic category</th>\n\\r
314<th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\\r
315<th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\\r
316</tr></thead><tbody>\n",f);\r
317\r
318 if_exists(sum,sums,sum->l>=total_traffic/4)\r
319 {\r
320 fprintf(f,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());\r
321 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
322<td style=\"text-align: right\">%d %%</td>\n\\r
323<td style=\"text-align: right\">%Lu MB</td>\n\\r
324<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
325 sum->i, (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);\r
326 }\r
327 \r
328 if_exists(sum,sums,sum->i==10)\r
329 {\r
330 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());\r
331 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\\r
332<td style=\"text-align: right\">%d %%</td>\n\\r
333<td style=\"text-align: right\">%Lu MB</td>\n\\r
334<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
335 (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);\r
336 }\r
337\r
338 if_exists(sum,sums,sum->l>=total_traffic/2)\r
339 {\r
340 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());\r
341 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
342<td style=\"text-align: right\">%d %%</td>\n\\r
343<td style=\"text-align: right\">%Lu MB</td>\n\\r
344<td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",\r
345 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
346 }\r
347\r
348 if_exists(sum,sums,sum->l>=4*total_traffic/5)\r
349 {\r
350 fprintf(f,"%s<td>Top 80%% of traffic</td>\n", tr_odd_even());\r
351 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
352<td style=\"text-align: right\">%d %%</td>\n\\r
353<td style=\"text-align: right\">%Lu MB</td>\n\\r
354<td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",\r
355 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
356 }\r
357\r
358 if_exists(sum,sums,sum->i>=(active_classes+1)/5)\r
359 {\r
360 fprintf(f,"%s<td>Top 20%% downloaders</td>\n", tr_odd_even());\r
361 top20_count=sum->i;\r
362 top20_perc1=(100*sum->i+50)/active_classes;\r
363 top20_sum=sum->l;\r
364 top20_perc2=(100*sum->l+50)/total_traffic;\r
365 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
366<td style=\"text-align: right\"><strong>%d %%</strong></td>\n\\r
367<td style=\"text-align: right\">%Lu MB</td>\n\\r
368<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
369 top20_count,top20_perc1,top20_sum,top20_perc2);\r
370 }\r
371\r
372 if_exists(sum,sums,sum->i>=(active_classes+1)/4)\r
373 {\r
374 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());\r
375 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
376<td style=\"text-align: right\">%d %%</td>\n\\r
377<td style=\"text-align: right\">%Lu MB</td>\n\\r
378<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
379 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
380 }\r
381\r
382 if_exists(sum,sums,sum->i>=(active_classes+1)/2)\r
383 {\r
384 fprintf(f,"%s<td>Top 50%% downloaders</td>\n", tr_odd_even());\r
385 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
386<td style=\"text-align: right\"><strong>%d %%</strong></td>\n\\r
387<td style=\"text-align: right\">%Lu MB</td>\n\\r
388<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
389 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
390 }\r
391\r
392 if_exists(sum,sums,sum->i>=4*(active_classes+1)/5)\r
393 {\r
394 fprintf(f,"%s<td>Top 80%% downloaders</td>\n", tr_odd_even());\r
395 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
396<td style=\"text-align: right\">%d %%</td>\n\\r
397<td style=\"text-align: right\">%Lu MB</td>\n\\r
398<td style=\"text-align: right\">%Ld %%</td></tr></tbody>\n",\r
399 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
400 }\r
401\r
402 fprintf(f,"<thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url);\r
403 fprintf(f,"<th style=\"text-align: right\">%d</th>\n\\r
404<th style=\"text-align: right\">100 %%</th>\n\\r
405<th style=\"text-align: right\">%Lu MB</th>\n\\r
406<th style=\"text-align: right\">100 %%</th></tr>\n",active_classes,total_traffic);\r
407 fputs("</thead></table>\n", f);\r
408\r
409 /* write basic ERP data to log directory */\r
410 if(!just_preview)\r
411 {\r
412 FILE *iplog;\r
413 sprintf(str,"%s/ERP.log",log_dir);\r
414 iplog=fopen(str,"a");\r
415 if(iplog)\r
416 {\r
417 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
418 time(NULL), top20_count, top20_perc1, top20_sum, top20_perc2, \r
419 active_classes, total_traffic, i, limit_count, prio_count, d); /* d = date*/\r
420 fclose(iplog);\r
421 }\r
422 else\r
423 {\r
424 perror(str);\r
425 }\r
426 }\r
427 }\r
428\r
429 fprintf(f, stats_html_signature, version);\r
430 fclose(f);\r
431 }\r
432}
This page took 0.408251 seconds and 4 git commands to generate.