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