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