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