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