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