1 #include "cll1-0.6.2.h"
2 #include "cll1-0.6.2.h"
6 /* globals declared in prometheus.c */
8 extern const char *version
;
9 extern const char *stats_html_signature
;
10 extern const char *ls
;
11 extern char *html_log_dir
;
12 extern int row_odd_even
;
16 /* function implemented in prometheus.c */
17 const char *tr_odd_even(void);
32 void parse_ip_log(int argc
, char **argv
)
34 char *month
, *year
, *str
, *name
="(undefined)", *ptr
, *ptr2
, *filename
;
35 long traffic
=0l, traffic_month
, total
=0, guaranted
;
36 int col
, col2
, y_ok
, m_ok
, accept_month
, i
=1, any_month
=0, lmsid
;
37 char mstr
[4], ystr
[5];
38 long log_timestamp
, log_started
= 0, log_ended
= 0;
41 string(filename
,STRLEN
);
43 if(argv
[1][1]=='l') /* -l */
47 puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)");
53 if(eq(month
,"Year")) any_month
=1;
59 time_t t
= time(NULL
) - 3600*24 ; /* yesterday's timestamp*/
60 struct tm
*timep
= localtime(&t
);
62 if(argv
[1][1]=='m') /* -m yestarday - month */
64 strftime(mstr
, 4, "%b", timep
);
66 strftime(ystr
, 5, "%Y", timep
);
69 else /* -y yesterday - year */
73 strftime(ystr
, 5, "%Y", timep
);
77 printf("Analysing traffic for %s %s ...\n",month
,year
);
79 /* sorry... next release of C<<1 header file will include for_path_files(name,path) { } macro */
80 sprintf(str
,"%s %s/",ls
,log_dir
);
82 /* for_each(file, str, dir, log_dir) */
85 if(strstr(str
,".log"))
87 ptr
=strrchr(str
,'\n');
89 sprintf(filename
,"%s/%s", log_dir
,str
);
90 printf("Parsing %s ...", filename
);
98 valid_columns(ptr
,_
,'\t',col
) switch(col
)
100 case 1: log_timestamp
= atol(ptr
); break;
101 case 2: name
= ptr
;break;
102 case 3: traffic
= atol(ptr
);break;
103 /* column number - was 7, now 11...*/
108 case 11: if(isalpha(*ptr
)) /* character, not numeric string = date, just one*/
110 valid_columns(ptr2
,ptr
,' ',col2
) switch(col2
)
112 case 2: if(any_month
|| eq(ptr2
,month
)) m_ok
= 1; break;
113 case 5: if(eq(ptr2
,year
)) y_ok
= 1; break;
118 if(col
== 7) guaranted
= atol(ptr
);
119 if(col
== 10) lmsid
= atoi(ptr
);
125 traffic_month
+= traffic
;
128 log_started
= log_timestamp
;
132 else if (log_started
!= 0 && log_ended
== 0)
134 log_ended
= log_timestamp
;
137 done
; /* ugly macro end */
143 log_ended
= time(NULL
);
148 iplog
->guaranted
= guaranted
;
149 iplog
->avg
= traffic_month
* 8 / (log_ended
- log_started
); /* Mbps */
150 iplog
->logged_time
= (log_ended
- log_started
);
151 iplog
->traffic
= traffic_month
; /* MB */
152 iplog
->lmsid
= lmsid
;
153 insert(iplog
,iplogs
,desc_order_by
,traffic
);
154 printf(" %ld MB\n",iplog
->traffic
);
158 puts(" no records.");
162 sprintf(str
,"%s/%s-%s.html",html_log_dir
,year
,month
);
163 printf("Writing %s ... ",str
);
167 // time_t max_logged_time = 0;
169 fprintf(f
, "<table class=\"decorated last\"><thead>\n\
170 <tr><th colspan=\"2\">%s %s</th>\n\
171 <th style=\"text-align: right\">lms</th>\n\
172 <th colspan=\"2\">Data transfers</th>\n\
173 <th style=\"text-align: right\">Min.speed</th>\n\
174 <th style=\"text-align: right\">Avg.speed</th>\n\
175 </tr></thead><tbody>\n ",
179 for_each(iplog
, iplogs
)
181 /* if(iplog->logged_time > max_logged_time)
183 max_logged_time = iplog->logged_time;
188 fprintf(f
, "%s<td style=\"text-align: right\">%d</td>\n\
189 <td style=\"text-align: left\">\
190 <a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s</td>\n\
191 <td style=\"text-align: right\">",
192 tr_odd_even(), i
++, log_url
, iplog
->name
, iplog
->name
);
196 /*base URL will be configurable soon ... */
197 fprintf(f
, "<a class=\"blue\" target=\"_blank\" href=\"%s%d\">%04d</a>\n", lms_url
, iplog
->lmsid
, iplog
->lmsid
);
199 else if(iplog
->lmsid
== 0)
203 fprintf(f
, "<td style=\"text-align: right\">%ld MB</td>\n\
204 <td style=\"text-align: right\"><strong>%ld GB</strong></td>\n\
205 <td style=\"text-align: right\">%ld kb/s</th>\
206 <td style=\"text-align: right\">%ld Mb/s</th>\
208 iplog
->traffic
, iplog
->traffic
>>10, iplog
->guaranted
, iplog
->avg
);
209 total
+=iplog
->traffic
>>10;
214 fprintf(f
,"</tbody><thead><tr>\
215 <th colspan=\"3\" style=\"text-align: left\">Total:</th>\
216 <th colspan=\"2\" style=\"text-align: right\"><strong>%ld GB</strong></th>\
217 <th style=\"text-align: right\"></th>\
218 <th style=\"text-align: right\"></th></tr>\
220 fputs("</thead></table>\n", f
);
225 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\">\n\
226 <caption>Enterprise Resource Planning (ERP)</caption>\n\
228 <th>Analytic category</th>\n\
229 <th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\
230 <th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\
231 </tr></thead><tbody>\n",f
);
233 if_exists(iplog
,iplogs
,iplog
->l
>=total
/4)
235 fprintf(f
,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());
236 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
237 <td style=\"text-align: right\">%d %%</td>\n\
238 <td style=\"text-align: right\">%ld GB</td>\n\
239 <td style=\"text-align: right\">%d %%</td></tr>\n",
240 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
243 if_exists(iplog
,iplogs
,iplog
->i
==10)
245 fprintf(f
,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
246 fprintf(f
,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
247 <td style=\"text-align: right\">%d %%</td>\n\
248 <td style=\"text-align: right\">%ld GB</td>\n\
249 <td style=\"text-align: right\">%d %%</td></tr>\n",
250 (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
253 if_exists(iplog
,iplogs
,iplog
->l
>=total
/2)
255 fprintf(f
,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
256 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
257 <td style=\"text-align: right\">%d %%</td>\n\
258 <td style=\"text-align: right\">%ld GB</td>\n\
259 <td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",
260 iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
263 if_exists(iplog
,iplogs
,iplog
->l
>=4*total
/5)
265 fprintf(f
,"%s<td>Top 80%% of traffic</td>\n",tr_odd_even());
266 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
267 <td style=\"text-align: right\">%d %%</td>\n\
268 <td style=\"text-align: right\">%ld GB</td>\n\
269 <td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",
270 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
273 if_exists (iplog
,iplogs
,iplog
->i
>=i
/5)
275 fprintf(f
,"%s<td>Top 20%% downloaders</td>\n",tr_odd_even());
276 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
277 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
278 <td style=\"text-align: right\">%ld GB</td>\n\
279 <td style=\"text-align: right\">%d %%</td></tr>\n",
280 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
283 if_exists(iplog
,iplogs
,iplog
->i
>=i
/4)
285 fprintf(f
,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
286 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
287 <td style=\"text-align: right\">%d %%</td>\n\
288 <td style=\"text-align: right\">%ld GB</td>\n\
289 <td style=\"text-align: right\">%d %%</td></tr>\n",
290 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
293 if_exists(iplog
,iplogs
,iplog
->i
>=i
/2)
295 fprintf(f
,"%s<td>Top 50%% downloaders</td>\n",tr_odd_even());
296 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
297 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
298 <td style=\"text-align: right\">%ld GB</td>\n\
299 <td style=\"text-align: right\">%d %%</td></tr>\n",
300 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
303 if_exists(iplog
,iplogs
,iplog
->i
>=4*i
/5)
305 fprintf(f
,"%s<td>Top 80%% downloaders</td>\n",tr_odd_even());
306 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
307 <td style=\"text-align: right\">%d %%</td>\n\
308 <td style=\"text-align: right\">%ld GB</td>\n\
309 <td style=\"text-align: right\">%d %%</td></tr>\n",
310 iplog
->i
, (100*iplog
->i
+50)/i
, iplog
->l
, (int)((100*iplog
->l
+50)/total
));
313 fprintf(f
,"</tbody><thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url
);
314 fprintf(f
,"<th style=\"text-align: right\">%d</th>\n\
315 <th style=\"text-align: right\">100 %%</th>\n\
316 <th style=\"text-align: right\">%ld GB</th>\n\
317 <th style=\"text-align: right\">100 %%</th></tr>\n",i
-1,total
);
318 fputs("</thead></table>\n", f
);
321 fprintf(f
, stats_html_signature
, version
);
This page took 0.437697 seconds and 4 git commands to generate.