not tested - initial commit only
[svn/Prometheus-QoS/.git] / parselogs.c
1 #include "cll1-0.6.2.h"
2 #include "cll1-0.6.2.h"
3
4 #define STRLEN 512
5
6 /* globals declared in prometheus.c */
7 extern char *log_dir;
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;
13 extern char *log_url;
14 extern char *lms_url;
15
16 /* function implemented in prometheus.c */
17 const char *tr_odd_even(void);
18
19 struct IpLog
20 {
21 char *name;
22 long traffic;
23 long guaranted;
24 long avg;
25 time_t logged_time;
26 int i;
27 int lmsid;
28 long l;
29 list(IpLog);
30 } *iplog,*iplogs;
31
32 void parse_ip_log(int argc, char **argv)
33 {
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;
39 FILE *f;
40 string(str,STRLEN);
41 string(filename,STRLEN);
42
43 if(argv[1][1]=='l') /* -l */
44 {
45 if(argc<4)
46 {
47 puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)");
48 exit(-1);
49 }
50 else
51 {
52 month=argv[2];
53 if(eq(month,"Year")) any_month=1;
54 year=argv[3];
55 }
56 }
57 else
58 {
59 time_t t = time(NULL) - 3600*24 ; /* yesterday's timestamp*/
60 struct tm *timep = localtime(&t);
61
62 if(argv[1][1]=='m') /* -m yestarday - month */
63 {
64 strftime(mstr, 4, "%b", timep);
65 month=mstr;
66 strftime(ystr, 5, "%Y", timep);
67 year=ystr;
68 }
69 else /* -y yesterday - year */
70 {
71 month="Year";
72 any_month=1;
73 strftime(ystr, 5, "%Y", timep);
74 year=ystr;
75 }
76 }
77 printf("Analysing traffic for %s %s ...\n",month,year);
78
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);
81 shell(str);
82 /* for_each(file, str, dir, log_dir) */
83 input(str,STRLEN)
84 {
85 if(strstr(str,".log"))
86 {
87 ptr=strrchr(str,'\n');
88 if(ptr) *ptr='\0';
89 sprintf(filename,"%s/%s", log_dir,str);
90 printf("Parsing %s ...", filename);
91 accept_month=0;
92 traffic_month=0;
93 guaranted=0;
94 lmsid=-1;
95 parse(filename)
96 {
97 y_ok=m_ok=0;
98 valid_columns(ptr,_,'\t',col) switch(col)
99 {
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...*/
104 case 7:
105 case 8:
106 case 9:
107 case 10:
108 case 11: if(isalpha(*ptr)) /* character, not numeric string = date, just one*/
109 {
110 valid_columns(ptr2,ptr,' ',col2) switch(col2)
111 {
112 case 2: if(any_month || eq(ptr2,month)) m_ok = 1; break;
113 case 5: if(eq(ptr2,year)) y_ok = 1; break;
114 }
115 }
116 else
117 {
118 if(col == 7) guaranted = atol(ptr);
119 if(col == 10) lmsid = atoi(ptr);
120 }
121 }
122
123 if(y_ok && m_ok)
124 {
125 traffic_month += traffic;
126 if(log_started == 0)
127 {
128 log_started = log_timestamp;
129 }
130 accept_month = 1;
131 }
132 else if (log_started != 0 && log_ended == 0)
133 {
134 log_ended = log_timestamp;
135 }
136 }
137 done; /* ugly macro end */
138
139 if(accept_month)
140 {
141 if(log_ended == 0)
142 {
143 log_ended = time(NULL);
144 }
145
146 create(iplog,IpLog);
147 iplog->name = name;
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);
155 }
156 else
157 {
158 puts(" no records.");
159 }
160 }
161 }
162 sprintf(str,"%s/%s-%s.html",html_log_dir,year,month);
163 printf("Writing %s ... ",str);
164 f=fopen(str,"w");
165 if(f > 0)
166 {
167 // time_t max_logged_time = 0;
168
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 ",
176 month, year);
177
178 row_odd_even = 0;
179 for_each(iplog, iplogs)
180 {
181 /* if(iplog->logged_time > max_logged_time)
182 {
183 max_logged_time = iplog->logged_time;
184 }
185 */
186 if(iplog->traffic)
187 {
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);
193
194 if(iplog->lmsid > 0)
195 {
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);
198 }
199 else if(iplog->lmsid == 0)
200 {
201 fputs("------",f);
202 }
203 fprintf(f, "<td style=\"text-align: right\">%ld&nbsp;MB</td>\n\
204 <td style=\"text-align: right\"><strong>%ld&nbsp;GB</strong></td>\n\
205 <td style=\"text-align: right\">%ld&nbsp;kb/s</th>\
206 <td style=\"text-align: right\">%ld&nbsp;Mb/s</th>\
207 </tr>\n",
208 iplog->traffic, iplog->traffic>>10, iplog->guaranted, iplog->avg);
209 total+=iplog->traffic>>10;
210 iplog->i=i;
211 iplog->l=total;
212 }
213 }
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&nbsp;GB</strong></th>\
217 <th style=\"text-align: right\"></th>\
218 <th style=\"text-align: right\"></th></tr>\
219 \n", total);
220 fputs("</thead></table>\n", f);
221
222 row_odd_even = 0;
223 if(i>10)
224 {
225 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\">\n\
226 <caption>Enterprise Resource Planning (ERP)</caption>\n\
227 <thead><tr>\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);
232
233 if_exists(iplog,iplogs,iplog->l>=total/4)
234 {
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));
241 }
242
243 if_exists(iplog,iplogs,iplog->i==10)
244 {
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));
251 }
252
253 if_exists(iplog,iplogs,iplog->l>=total/2)
254 {
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));
261 }
262
263 if_exists(iplog,iplogs,iplog->l>=4*total/5)
264 {
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));
271 }
272
273 if_exists (iplog,iplogs,iplog->i>=i/5)
274 {
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));
281 }
282
283 if_exists(iplog,iplogs,iplog->i>=i/4)
284 {
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));
291 }
292
293 if_exists(iplog,iplogs,iplog->i>=i/2)
294 {
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));
301 }
302
303 if_exists(iplog,iplogs,iplog->i>=4*i/5)
304 {
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));
311 }
312
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);
319 }
320
321 fprintf(f, stats_html_signature, version);
322 fclose(f);
323 puts("done.");
324 }
325 else
326 {
327 perror(str);
328 }
329 }
This page took 0.437697 seconds and 4 git commands to generate.