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