1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | /* This program takes a series of traces and bpf filters and outputs how many |
---|
32 | * bytes/packets every time interval |
---|
33 | */ |
---|
34 | |
---|
35 | #include <stdio.h> |
---|
36 | #include <stdlib.h> |
---|
37 | #include <assert.h> |
---|
38 | #include <string.h> |
---|
39 | #include <sys/time.h> |
---|
40 | #include <sys/types.h> |
---|
41 | #include <time.h> |
---|
42 | |
---|
43 | #include <netinet/in.h> |
---|
44 | #include <netinet/in_systm.h> |
---|
45 | #include <netinet/tcp.h> |
---|
46 | #include <netinet/ip.h> |
---|
47 | #include <netinet/ip_icmp.h> |
---|
48 | #include <arpa/inet.h> |
---|
49 | #include <sys/socket.h> |
---|
50 | #include <getopt.h> |
---|
51 | #include <inttypes.h> |
---|
52 | #include <lt_inttypes.h> |
---|
53 | |
---|
54 | #include "libtrace.h" |
---|
55 | #include "output.h" |
---|
56 | #include "rt_protocol.h" |
---|
57 | #include "dagformat.h" |
---|
58 | |
---|
59 | #ifndef UINT32_MAX |
---|
60 | #define UINT32_MAX 0xffffffffU |
---|
61 | #endif |
---|
62 | |
---|
63 | struct libtrace_t *trace; |
---|
64 | char *output_format=NULL; |
---|
65 | |
---|
66 | int merge_inputs = 0; |
---|
67 | |
---|
68 | struct filter_t { |
---|
69 | char *expr; |
---|
70 | struct libtrace_filter_t *filter; |
---|
71 | uint64_t count; |
---|
72 | uint64_t bytes; |
---|
73 | } *filters = NULL; |
---|
74 | int filter_count=0; |
---|
75 | uint64_t totcount; |
---|
76 | uint64_t totbytes; |
---|
77 | |
---|
78 | uint64_t packet_count=UINT64_MAX; |
---|
79 | double packet_interval=UINT32_MAX; |
---|
80 | |
---|
81 | |
---|
82 | struct output_data_t *output; |
---|
83 | |
---|
84 | static void report_results(double ts,uint64_t count,uint64_t bytes) |
---|
85 | { |
---|
86 | int i=0; |
---|
87 | output_set_data_time(output,0,ts); |
---|
88 | output_set_data_int(output,1,count); |
---|
89 | output_set_data_int(output,2,bytes); |
---|
90 | for(i=0;i<filter_count;++i) { |
---|
91 | output_set_data_int(output,i*2+3,filters[i].count); |
---|
92 | output_set_data_int(output,i*2+4,filters[i].bytes); |
---|
93 | filters[i].count=filters[i].bytes=0; |
---|
94 | } |
---|
95 | output_flush_row(output); |
---|
96 | } |
---|
97 | |
---|
98 | static void create_output(char *title) { |
---|
99 | int i; |
---|
100 | |
---|
101 | output=output_init(title,output_format?output_format:"txt"); |
---|
102 | if (!output) { |
---|
103 | fprintf(stderr,"Failed to create output file\n"); |
---|
104 | return; |
---|
105 | } |
---|
106 | output_add_column(output,"ts"); |
---|
107 | output_add_column(output,"packets"); |
---|
108 | output_add_column(output,"bytes"); |
---|
109 | for(i=0;i<filter_count;++i) { |
---|
110 | char buff[1024]; |
---|
111 | snprintf(buff,sizeof(buff),"%s packets",filters[i].expr); |
---|
112 | output_add_column(output,buff); |
---|
113 | snprintf(buff,sizeof(buff),"%s bytes",filters[i].expr); |
---|
114 | output_add_column(output,buff); |
---|
115 | } |
---|
116 | output_flush_headings(output); |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | /* Process a trace, counting packets that match filter(s) */ |
---|
121 | static void run_trace(char *uri) |
---|
122 | { |
---|
123 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
124 | int i; |
---|
125 | uint64_t count = 0; |
---|
126 | uint64_t bytes = 0; |
---|
127 | double last_ts = 0; |
---|
128 | double ts = 0; |
---|
129 | |
---|
130 | if (!merge_inputs) |
---|
131 | create_output(uri); |
---|
132 | |
---|
133 | if (output == NULL) |
---|
134 | return; |
---|
135 | |
---|
136 | trace = trace_create(uri); |
---|
137 | if (trace_is_err(trace)) { |
---|
138 | trace_perror(trace,"trace_create"); |
---|
139 | trace_destroy(trace); |
---|
140 | output_destroy(output); |
---|
141 | return; |
---|
142 | } |
---|
143 | if (trace_start(trace)==-1) { |
---|
144 | trace_perror(trace,"trace_start"); |
---|
145 | trace_destroy(trace); |
---|
146 | output_destroy(output); |
---|
147 | return; |
---|
148 | } |
---|
149 | |
---|
150 | for (;;) { |
---|
151 | int psize; |
---|
152 | if ((psize = trace_read_packet(trace, packet)) <1) { |
---|
153 | break; |
---|
154 | } |
---|
155 | |
---|
156 | if (trace_get_packet_buffer(packet,NULL,NULL) == NULL) { |
---|
157 | continue; |
---|
158 | } |
---|
159 | |
---|
160 | ts = trace_get_seconds(packet); |
---|
161 | |
---|
162 | if (last_ts == 0) |
---|
163 | last_ts = ts; |
---|
164 | |
---|
165 | while (packet_interval != UINT64_MAX && last_ts<ts) { |
---|
166 | report_results(last_ts,count,bytes); |
---|
167 | count=0; |
---|
168 | bytes=0; |
---|
169 | last_ts+=packet_interval; |
---|
170 | } |
---|
171 | for(i=0;i<filter_count;++i) { |
---|
172 | if(trace_apply_filter(filters[i].filter,packet)) { |
---|
173 | ++filters[i].count; |
---|
174 | filters[i].bytes+=trace_get_wire_length(packet); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | ++count; |
---|
179 | bytes+=trace_get_wire_length(packet); |
---|
180 | |
---|
181 | |
---|
182 | if (count >= packet_count) { |
---|
183 | report_results(ts,count,bytes); |
---|
184 | count=0; |
---|
185 | bytes=0; |
---|
186 | } |
---|
187 | } |
---|
188 | report_results(ts,count,bytes); |
---|
189 | |
---|
190 | if (trace_is_err(trace)) |
---|
191 | trace_perror(trace,"%s",uri); |
---|
192 | |
---|
193 | trace_destroy(trace); |
---|
194 | |
---|
195 | if (!merge_inputs) |
---|
196 | output_destroy(output); |
---|
197 | |
---|
198 | trace_destroy_packet(packet); |
---|
199 | } |
---|
200 | |
---|
201 | static void usage(char *argv0) |
---|
202 | { |
---|
203 | fprintf(stderr,"Usage:\n" |
---|
204 | "%s flags libtraceuri [libtraceuri...]\n" |
---|
205 | "-i --interval=seconds Duration of reporting interval in seconds\n" |
---|
206 | "-c --count=packets Exit after count packets received\n" |
---|
207 | "-o --output-format=txt|csv|html|png Reporting output format\n" |
---|
208 | "-f --filter=bpf Apply BPF filter. Can be specified multiple times\n" |
---|
209 | "-m --merge-inputs Do not create separate outputs for each input trace\n" |
---|
210 | "-H --libtrace-help Print libtrace runtime documentation\n" |
---|
211 | ,argv0); |
---|
212 | } |
---|
213 | |
---|
214 | int main(int argc, char *argv[]) { |
---|
215 | |
---|
216 | int i; |
---|
217 | |
---|
218 | while(1) { |
---|
219 | int option_index; |
---|
220 | struct option long_options[] = { |
---|
221 | { "filter", 1, 0, 'f' }, |
---|
222 | { "interval", 1, 0, 'i' }, |
---|
223 | { "count", 1, 0, 'c' }, |
---|
224 | { "output-format", 1, 0, 'o' }, |
---|
225 | { "libtrace-help", 0, 0, 'H' }, |
---|
226 | { "merge-inputs", 0, 0, 'm' }, |
---|
227 | { NULL, 0, 0, 0 }, |
---|
228 | }; |
---|
229 | |
---|
230 | int c=getopt_long(argc, argv, "c:f:i:o:Hm", |
---|
231 | long_options, &option_index); |
---|
232 | |
---|
233 | if (c==-1) |
---|
234 | break; |
---|
235 | |
---|
236 | switch (c) { |
---|
237 | case 'f': |
---|
238 | ++filter_count; |
---|
239 | filters=realloc(filters,filter_count*sizeof(struct filter_t)); |
---|
240 | filters[filter_count-1].expr=strdup(optarg); |
---|
241 | filters[filter_count-1].filter=trace_create_filter(optarg); |
---|
242 | filters[filter_count-1].count=0; |
---|
243 | filters[filter_count-1].bytes=0; |
---|
244 | break; |
---|
245 | case 'i': |
---|
246 | packet_interval=atof(optarg); |
---|
247 | break; |
---|
248 | case 'c': |
---|
249 | packet_count=atoi(optarg); |
---|
250 | break; |
---|
251 | case 'o': |
---|
252 | if (output_format) free(output_format); |
---|
253 | output_format=strdup(optarg); |
---|
254 | break; |
---|
255 | case 'm': |
---|
256 | merge_inputs = 1; |
---|
257 | break; |
---|
258 | case 'H': |
---|
259 | trace_help(); |
---|
260 | exit(1); |
---|
261 | break; |
---|
262 | default: |
---|
263 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
264 | usage(argv[0]); |
---|
265 | return 1; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | if (packet_count == UINT64_MAX && packet_interval == UINT32_MAX) { |
---|
270 | packet_interval = 300; /* every 5 minutes */ |
---|
271 | } |
---|
272 | |
---|
273 | if (optind >= argc) |
---|
274 | return 0; |
---|
275 | |
---|
276 | fprintf(stderr,"output format: '%s'\n",output_format); |
---|
277 | |
---|
278 | |
---|
279 | |
---|
280 | if (merge_inputs) { |
---|
281 | /* If we're merging the inputs, we only want to create all |
---|
282 | * the column headers etc. once rather than doing them once |
---|
283 | * per trace */ |
---|
284 | |
---|
285 | /* This is going to "name" the output based on the first |
---|
286 | * provided URI - admittedly not ideal */ |
---|
287 | create_output(argv[optind]); |
---|
288 | if (output == NULL) |
---|
289 | return 0; |
---|
290 | |
---|
291 | } |
---|
292 | |
---|
293 | for(i=optind;i<argc;++i) { |
---|
294 | run_trace(argv[i]); |
---|
295 | } |
---|
296 | |
---|
297 | if (merge_inputs) { |
---|
298 | /* Clean up after ourselves */ |
---|
299 | output_destroy(output); |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | return 0; |
---|
304 | } |
---|