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