digital-divide, total avg.speed was nonsense
[svn/Prometheus-QoS/.git] / parselogs.c
CommitLineData
5692c775 1#include "cll1-0.6.2.h"\r
2#define STRLEN 512\r
3\r
1c9cae56 4/* globals declared in prometheus.c */\r
5692c775 5extern char *log_dir;\r
6extern const char *version;\r
7extern const char *stats_html_signature;\r
8extern const char *ls;\r
9extern char *html_log_dir;\r
10extern int row_odd_even;\r
11extern char *log_url;\r
12extern char *lms_url;\r
13extern long long int line;\r
14\r
1c9cae56 15/* function implemented in prometheus.c */\r
5692c775 16const char *tr_odd_even(void);\r
5692c775 17\r
18struct IpLog\r
19{\r
20 char *name;\r
21 long traffic;\r
22 long guaranted;\r
3856be6a 23 long avg;\r
24 time_t logged_time;\r
5692c775 25 int i;\r
26 int lmsid;\r
27 long l;\r
28 list(IpLog);\r
29} *iplog,*iplogs;\r
30\r
31void parse_ip_log(int argc, char **argv) \r
32{\r
33 char *month, *year, *str, *name="(undefined)", *ptr, *ptr2, *filename;\r
34 long traffic=0l, traffic_month, total=0, guaranted;\r
35 int col, col2, y_ok, m_ok, accept_month, i=1, any_month=0, lmsid;\r
36 char mstr[4], ystr[5];\r
3856be6a 37 long log_timestamp, log_started = 0, log_ended = 0;\r
5692c775 38 FILE *f; \r
39 string(str,STRLEN);\r
40 string(filename,STRLEN);\r
41\r
42 if(argv[1][1]=='l') /* -l */\r
43 {\r
44 if(argc<4)\r
45 {\r
46 puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)");\r
47 exit(-1);\r
48 }\r
49 else\r
50 {\r
51 month=argv[2];\r
52 if(eq(month,"Year")) any_month=1;\r
53 year=argv[3];\r
54 }\r
55 }\r
56 else\r
57 { \r
58 time_t t = time(NULL) - 3600*24 ; /* yesterday's timestamp*/\r
59 struct tm *timep = localtime(&t); \r
60\r
61 if(argv[1][1]=='m') /* -m yestarday - month */\r
62 {\r
63 strftime(mstr, 4, "%b", timep);\r
64 month=mstr;\r
65 strftime(ystr, 5, "%Y", timep);\r
66 year=ystr; \r
67 }\r
68 else /* -y yesterday - year */\r
69 {\r
70 month="Year";\r
71 any_month=1;\r
72 strftime(ystr, 5, "%Y", timep);\r
73 year=ystr;\r
74 }\r
75 }\r
76 printf("Analysing traffic for %s %s ...\n",month,year);\r
77\r
78 /* sorry... next release of C<<1 header file will include for_path_files(name,path) { } macro */\r
79 sprintf(str,"%s %s/",ls,log_dir);\r
80 shell(str);\r
3856be6a 81 /* for_each(file, str, dir, log_dir) */\r
5692c775 82 input(str,STRLEN) \r
83 {\r
84 if(strstr(str,".log"))\r
85 {\r
86 ptr=strrchr(str,'\n');\r
87 if(ptr) *ptr='\0';\r
3856be6a 88 sprintf(filename,"%s/%s", log_dir,str);\r
89 printf("Parsing %s ...", filename);\r
5692c775 90 accept_month=0;\r
91 traffic_month=0;\r
92 guaranted=0;\r
93 lmsid=-1;\r
94 parse(filename)\r
95 {\r
96 y_ok=m_ok=0; \r
97 valid_columns(ptr,_,'\t',col) switch(col)\r
98 {\r
3856be6a 99 case 1: log_timestamp = atol(ptr); break;\r
5692c775 100 case 2: name = ptr;break;\r
101 case 3: traffic = atol(ptr);break;\r
102 /* column number - was 7, now 11...*/\r
103 case 7:\r
104 case 8:\r
105 case 9:\r
106 case 10:\r
107 case 11: if(isalpha(*ptr)) /* character, not numeric string = date, just one*/\r
108 {\r
109 valid_columns(ptr2,ptr,' ',col2) switch(col2)\r
110 {\r
111 case 2: if(any_month || eq(ptr2,month)) m_ok = 1; break;\r
112 case 5: if(eq(ptr2,year)) y_ok = 1; break;\r
113 }\r
114 }\r
115 else\r
116 {\r
117 if(col == 7) guaranted = atol(ptr);\r
118 if(col == 10) lmsid = atoi(ptr);\r
119 }\r
120 }\r
121 \r
122 if(y_ok && m_ok) \r
123 {\r
124 traffic_month += traffic;\r
3856be6a 125 if(log_started == 0)\r
126 {\r
127 log_started = log_timestamp;\r
128 }\r
5692c775 129 accept_month = 1;\r
130 }\r
3856be6a 131 else if (log_started != 0 && log_ended == 0)\r
132 {\r
133 log_ended = log_timestamp;\r
134 }\r
5692c775 135 }\r
136 done; /* ugly macro end */ \r
137\r
138 if(accept_month)\r
139 {\r
3856be6a 140 if(log_ended == 0)\r
141 {\r
142 log_ended = time(NULL);\r
143 }\r
144 \r
5692c775 145 create(iplog,IpLog);\r
146 iplog->name = name;\r
147 iplog->guaranted = guaranted;\r
3856be6a 148 iplog->avg = traffic_month * 8 / (log_ended - log_started); /* Mbps */\r
149 iplog->logged_time = (log_ended - log_started);\r
150 iplog->traffic = traffic_month; /* MB */\r
5692c775 151 iplog->lmsid = lmsid;\r
152 insert(iplog,iplogs,desc_order_by,traffic);\r
153 printf(" %ld MB\n",iplog->traffic);\r
154 }\r
155 else\r
156 {\r
157 puts(" no records.");\r
158 }\r
159 }\r
160 }\r
161 sprintf(str,"%s/%s-%s.html",html_log_dir,year,month);\r
162 printf("Writing %s ... ",str);\r
163 f=fopen(str,"w");\r
164 if(f > 0)\r
165 {\r
d1dd0f04 166// time_t max_logged_time = 0;\r
3856be6a 167\r
5692c775 168 fprintf(f, "<table class=\"decorated last\"><thead>\n\\r
169<tr><th colspan=\"2\">%s %s</th>\n\\r
170<th style=\"text-align: right\">lms</th>\n\\r
171<th colspan=\"2\">Data transfers</th>\n\\r
172<th style=\"text-align: right\">Min.speed</th>\n\\r
3856be6a 173<th style=\"text-align: right\">Avg.speed</th>\n\\r
5692c775 174</tr></thead><tbody>\n ",\r
175 month, year);\r
176\r
177 row_odd_even = 0;\r
178 for_each(iplog, iplogs)\r
179 {\r
d1dd0f04 180/* if(iplog->logged_time > max_logged_time)\r
3856be6a 181 {\r
182 max_logged_time = iplog->logged_time;\r
183 }\r
d1dd0f04 184*/\r
5692c775 185 if(iplog->traffic)\r
186 {\r
187 fprintf(f, "%s<td style=\"text-align: right\">%d</td>\n\\r
3856be6a 188<td style=\"text-align: left\">\\r
189<a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s</td>\n\\r
5692c775 190<td style=\"text-align: right\">", \r
191 tr_odd_even(), i++, log_url, iplog->name, iplog->name); \r
3856be6a 192\r
5692c775 193 if(iplog->lmsid > 0)\r
194 {\r
195 /*base URL will be configurable soon ... */\r
196 fprintf(f, "<a class=\"blue\" target=\"_blank\" href=\"%s%d\">%04d</a>\n", lms_url, iplog->lmsid, iplog->lmsid);\r
197 }\r
198 else if(iplog->lmsid == 0)\r
199 {\r
3856be6a 200 fputs("------",f);\r
5692c775 201 } \r
202 fprintf(f, "<td style=\"text-align: right\">%ld&nbsp;MB</td>\n\\r
3856be6a 203<td style=\"text-align: right\"><strong>%ld&nbsp;GB</strong></td>\n\\r
204<td style=\"text-align: right\">%ld&nbsp;kb/s</th>\\r
205<td style=\"text-align: right\">%ld&nbsp;Mb/s</th>\\r
206</tr>\n",\r
207 iplog->traffic, iplog->traffic>>10, iplog->guaranted, iplog->avg);\r
5692c775 208 total+=iplog->traffic>>10;\r
209 iplog->i=i;\r
210 iplog->l=total;\r
211 }\r
212 }\r
213 fprintf(f,"</tbody><thead><tr>\\r
3856be6a 214<th colspan=\"3\" style=\"text-align: left\">Total:</th>\\r
215<th colspan=\"2\" style=\"text-align: right\"><strong>%ld&nbsp;GB</strong></th>\\r
216<th style=\"text-align: right\"><strong>%Ld&nbsp;kb/s</strong></th>\\r
d1dd0f04 217<th style=\"text-align: right\"><strong>-</strong></th></tr>\\r
218\n", total, line);\r
5692c775 219 fputs("</thead></table>\n", f);\r
220\r
221 row_odd_even = 0;\r
222 if(i>10)\r
223 {\r
224 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\">\n\\r
225<caption>Enterprise Resource Planning (ERP)</caption>\n\\r
226<thead><tr>\n\\r
227<th>Analytic category</th>\n\\r
228<th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\\r
229<th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\\r
230</tr></thead><tbody>\n",f);\r
231\r
232 if_exists(iplog,iplogs,iplog->l>=total/4)\r
233 {\r
234 fprintf(f,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());\r
235 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
236<td style=\"text-align: right\">%d %%</td>\n\\r
237<td style=\"text-align: right\">%ld GB</td>\n\\r
238<td style=\"text-align: right\">%d %%</td></tr>\n",\r
239 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));\r
240 }\r
241 \r
242 if_exists(iplog,iplogs,iplog->i==10)\r
243 {\r
244 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());\r
245 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\\r
246<td style=\"text-align: right\">%d %%</td>\n\\r
247<td style=\"text-align: right\">%ld GB</td>\n\\r
248<td style=\"text-align: right\">%d %%</td></tr>\n",\r
249 (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));\r
250 }\r
251\r
252 if_exists(iplog,iplogs,iplog->l>=total/2)\r
253 {\r
254 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());\r
255 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
256<td style=\"text-align: right\">%d %%</td>\n\\r
257<td style=\"text-align: right\">%ld GB</td>\n\\r
258<td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",\r
259 iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
260 }\r
261\r
262 if_exists(iplog,iplogs,iplog->l>=4*total/5)\r
263 {\r
264 fprintf(f,"%s<td>Top 80%% of traffic</td>\n",tr_odd_even());\r
265 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
266<td style=\"text-align: right\">%d %%</td>\n\\r
267<td style=\"text-align: right\">%ld GB</td>\n\\r
268<td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",\r
269 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));\r
270 }\r
271\r
272 if_exists (iplog,iplogs,iplog->i>=i/5)\r
273 {\r
274 fprintf(f,"%s<td>Top 20%% downloaders</td>\n",tr_odd_even());\r
275 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
276<td style=\"text-align: right\"><strong>%d %%</strong></td>\n\\r
277<td style=\"text-align: right\">%ld GB</td>\n\\r
278<td style=\"text-align: right\">%d %%</td></tr>\n",\r
279 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));\r
280 }\r
281\r
282 if_exists(iplog,iplogs,iplog->i>=i/4)\r
283 {\r
284 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());\r
285 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
286<td style=\"text-align: right\">%d %%</td>\n\\r
287<td style=\"text-align: right\">%ld GB</td>\n\\r
288<td style=\"text-align: right\">%d %%</td></tr>\n",\r
289 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));\r
290 }\r
291\r
292 if_exists(iplog,iplogs,iplog->i>=i/2)\r
293 {\r
294 fprintf(f,"%s<td>Top 50%% downloaders</td>\n",tr_odd_even());\r
295 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
296<td style=\"text-align: right\"><strong>%d %%</strong></td>\n\\r
297<td style=\"text-align: right\">%ld GB</td>\n\\r
298<td style=\"text-align: right\">%d %%</td></tr>\n",\r
299 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));\r
300 }\r
301\r
302 if_exists(iplog,iplogs,iplog->i>=4*i/5)\r
303 {\r
304 fprintf(f,"%s<td>Top 80%% downloaders</td>\n",tr_odd_even());\r
305 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
306<td style=\"text-align: right\">%d %%</td>\n\\r
307<td style=\"text-align: right\">%ld GB</td>\n\\r
308<td style=\"text-align: right\">%d %%</td></tr>\n",\r
309 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));\r
310 }\r
311\r
312 fprintf(f,"</tbody><thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url);\r
313 fprintf(f,"<th style=\"text-align: right\">%d</th>\n\\r
314<th style=\"text-align: right\">100 %%</th>\n\\r
315<th style=\"text-align: right\">%ld GB</th>\n\\r
316<th style=\"text-align: right\">100 %%</th></tr>\n",i-1,total);\r
317 fputs("</thead></table>\n", f);\r
318 }\r
319\r
320 fprintf(f, stats_html_signature, version);\r
321 fclose(f);\r
322 puts("done.");\r
323 }\r
324 else\r
325 {\r
326 perror(str);\r
327 }\r
328}\r
This page took 0.363004 seconds and 4 git commands to generate.