1 #include "cll1-0.6.2.h"
4 /* globals declared in prometheus.c */
6 extern const char *version
;
7 extern const char *stats_html_signature
;
9 extern char *html_log_dir
;
10 extern int row_odd_even
;
13 extern long long int line
;
15 /* function implemented in prometheus.c */
16 const char *tr_odd_even(void);
31 void parse_ip_log(int argc
, char **argv
)
33 char *month
, *year
, *str
, *name
="(undefined)", *ptr
, *ptr2
, *filename
;
34 long traffic
=0l, traffic_month
, total
=0, guaranted
;
35 int col
, col2
, y_ok
, m_ok
, accept_month
, i
=1, any_month
=0, lmsid
;
36 char mstr
[4], ystr
[5];
37 long log_timestamp
, log_started
= 0, log_ended
= 0;
40 string(filename
,STRLEN
);
42 if(argv
[1][1]=='l') /* -l */
46 puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)");
52 if(eq(month
,"Year")) any_month
=1;
58 time_t t
= time(NULL
) - 3600*24 ; /* yesterday's timestamp*/
59 struct tm
*timep
= localtime(&t
);
61 if(argv
[1][1]=='m') /* -m yestarday - month */
63 strftime(mstr
, 4, "%b", timep
);
65 strftime(ystr
, 5, "%Y", timep
);
68 else /* -y yesterday - year */
72 strftime(ystr
, 5, "%Y", timep
);
76 printf("Analysing traffic for %s %s ...\n",month
,year
);
78 /* sorry... next release of C<<1 header file will include for_path_files(name,path) { } macro */
79 sprintf(str
,"%s %s/",ls
,log_dir
);
81 /* for_each(file, str, dir, log_dir) */
84 if(strstr(str
,".log"))
86 ptr
=strrchr(str
,'\n');
88 sprintf(filename
,"%s/%s", log_dir
,str
);
89 printf("Parsing %s ...", filename
);
97 valid_columns(ptr
,_
,'\t',col
) switch(col
)
99 case 1: log_timestamp
= atol(ptr
); break;
100 case 2: name
= ptr
;break;
101 case 3: traffic
= atol(ptr
);break;
102 /* column number - was 7, now 11...*/
107 case 11: if(isalpha(*ptr
)) /* character, not numeric string = date, just one*/
109 valid_columns(ptr2
,ptr
,' ',col2
) switch(col2
)
111 case 2: if(any_month
|| eq(ptr2
,month
)) m_ok
= 1; break;
112 case 5: if(eq(ptr2
,year
)) y_ok
= 1; break;
117 if(col
== 7) guaranted
= atol(ptr
);
118 if(col
== 10) lmsid
= atoi(ptr
);
124 traffic_month
+= traffic
;
127 log_started
= log_timestamp
;
131 else if (log_started
!= 0 && log_ended
== 0)
133 log_ended
= log_timestamp
;
136 done
; /* ugly macro end */
142 log_ended
= time(NULL
);
147 iplog
->guaranted
= guaranted
;
148 iplog
->avg
= traffic_month
* 8 / (log_ended
- log_started
); /* Mbps */
149 iplog
->logged_time
= (log_ended
- log_started
);
150 iplog
->traffic
= traffic_month
; /* MB */
151 iplog
->lmsid
= lmsid
;
152 insert(iplog
,iplogs
,desc_order_by
,traffic
);
153 printf(" %ld MB\n",iplog
->traffic
);
157 puts(" no records.");
161 sprintf(str
,"%s/%s-%s.html",html_log_dir
,year
,month
);
162 printf("Writing %s ... ",str
);
166 time_t max_logged_time
= 0;
168 fprintf(f
, "<table class=\"decorated last\"><thead>\n\
169 <tr><th colspan=\"2\">%s %s</th>\n\
170 <th style=\"text-align: right\">lms</th>\n\
171 <th colspan=\"2\">Data transfers</th>\n\
172 <th style=\"text-align: right\">Min.speed</th>\n\
173 <th style=\"text-align: right\">Avg.speed</th>\n\
174 </tr></thead><tbody>\n ",
178 for_each(iplog
, iplogs
)
180 if(iplog
->logged_time
> max_logged_time
)
182 max_logged_time
= iplog
->logged_time
;
187 fprintf(f
, "%s<td style=\"text-align: right\">%d</td>\n\
188 <td style=\"text-align: left\">\
189 <a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s</td>\n\
190 <td style=\"text-align: right\">",
191 tr_odd_even(), i
++, log_url
, iplog
->name
, iplog
->name
);
195 /*base URL will be configurable soon ... */
196 fprintf(f
, "<a class=\"blue\" target=\"_blank\" href=\"%s%d\">%04d</a>\n", lms_url
, iplog
->lmsid
, iplog
->lmsid
);
198 else if(iplog
->lmsid
== 0)
202 fprintf(f
, "<td style=\"text-align: right\">%ld MB</td>\n\
203 <td style=\"text-align: right\"><strong>%ld GB</strong></td>\n\
204 <td style=\"text-align: right\">%ld kb/s</th>\
205 <td style=\"text-align: right\">%ld Mb/s</th>\
207 iplog
->traffic
, iplog
->traffic
>>10, iplog
->guaranted
, iplog
->avg
);
208 total
+=iplog
->traffic
>>10;
213 fprintf(f
,"</tbody><thead><tr>\
214 <th colspan=\"3\" style=\"text-align: left\">Total:</th>\
215 <th colspan=\"2\" style=\"text-align: right\"><strong>%ld GB</strong></th>\
216 <th style=\"text-align: right\"><strong>%Ld kb/s</strong></th>\
217 <th style=\"text-align: right\"><strong>%Ld kb/s</strong></th></tr>\
218 \n", total
, line
, (8*(total
<<20))/max_logged_time
/i
);
219 fputs("</thead></table>\n", f
);
224 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\">\n\
225 <caption>Enterprise Resource Planning (ERP)</caption>\n\
227 <th>Analytic category</th>\n\
228 <th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\
229 <th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\
230 </tr></thead><tbody>\n",f
);
232 if_exists(iplog
,iplogs
,iplog
->l
>=total
/4)
234 fprintf(f
,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());
235 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
236 <td style=\"text-align: right\">%d %%</td>\n\
237 <td style=\"text-align: right\">%ld GB</td>\n\
238 <td style=\"text-align: right\">%d %%</td></tr>\n",
239 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
242 if_exists(iplog
,iplogs
,iplog
->i
==10)
244 fprintf(f
,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
245 fprintf(f
,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
246 <td style=\"text-align: right\">%d %%</td>\n\
247 <td style=\"text-align: right\">%ld GB</td>\n\
248 <td style=\"text-align: right\">%d %%</td></tr>\n",
249 (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
252 if_exists(iplog
,iplogs
,iplog
->l
>=total
/2)
254 fprintf(f
,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
255 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
256 <td style=\"text-align: right\">%d %%</td>\n\
257 <td style=\"text-align: right\">%ld GB</td>\n\
258 <td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",
259 iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
262 if_exists(iplog
,iplogs
,iplog
->l
>=4*total
/5)
264 fprintf(f
,"%s<td>Top 80%% of traffic</td>\n",tr_odd_even());
265 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
266 <td style=\"text-align: right\">%d %%</td>\n\
267 <td style=\"text-align: right\">%ld GB</td>\n\
268 <td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",
269 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
272 if_exists (iplog
,iplogs
,iplog
->i
>=i
/5)
274 fprintf(f
,"%s<td>Top 20%% downloaders</td>\n",tr_odd_even());
275 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
276 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
277 <td style=\"text-align: right\">%ld GB</td>\n\
278 <td style=\"text-align: right\">%d %%</td></tr>\n",
279 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
282 if_exists(iplog
,iplogs
,iplog
->i
>=i
/4)
284 fprintf(f
,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
285 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
286 <td style=\"text-align: right\">%d %%</td>\n\
287 <td style=\"text-align: right\">%ld GB</td>\n\
288 <td style=\"text-align: right\">%d %%</td></tr>\n",
289 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
292 if_exists(iplog
,iplogs
,iplog
->i
>=i
/2)
294 fprintf(f
,"%s<td>Top 50%% downloaders</td>\n",tr_odd_even());
295 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
296 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
297 <td style=\"text-align: right\">%ld GB</td>\n\
298 <td style=\"text-align: right\">%d %%</td></tr>\n",
299 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
302 if_exists(iplog
,iplogs
,iplog
->i
>=4*i
/5)
304 fprintf(f
,"%s<td>Top 80%% downloaders</td>\n",tr_odd_even());
305 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
306 <td style=\"text-align: right\">%d %%</td>\n\
307 <td style=\"text-align: right\">%ld GB</td>\n\
308 <td style=\"text-align: right\">%d %%</td></tr>\n",
309 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
312 fprintf(f
,"</tbody><thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url
);
313 fprintf(f
,"<th style=\"text-align: right\">%d</th>\n\
314 <th style=\"text-align: right\">100 %%</th>\n\
315 <th style=\"text-align: right\">%ld GB</th>\n\
316 <th style=\"text-align: right\">100 %%</th></tr>\n",i
-1,total
);
317 fputs("</thead></table>\n", f
);
320 fprintf(f
, stats_html_signature
, version
);
This page took 0.484 seconds and 4 git commands to generate.