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