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