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