1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 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 | struct filter_t { |
---|
67 | char *expr; |
---|
68 | struct libtrace_filter_t *filter; |
---|
69 | uint64_t count; |
---|
70 | uint64_t bytes; |
---|
71 | } *filters = NULL; |
---|
72 | int filter_count=0; |
---|
73 | uint64_t totcount; |
---|
74 | uint64_t totbytes; |
---|
75 | |
---|
76 | uint64_t packet_count=UINT64_MAX; |
---|
77 | double packet_interval=UINT32_MAX; |
---|
78 | |
---|
79 | |
---|
80 | struct output_data_t *output; |
---|
81 | |
---|
82 | static void report_results(double ts,uint64_t count,uint64_t bytes) |
---|
83 | { |
---|
84 | int i=0; |
---|
85 | output_set_data_time(output,0,ts); |
---|
86 | output_set_data_int(output,1,count); |
---|
87 | output_set_data_int(output,2,bytes); |
---|
88 | for(i=0;i<filter_count;++i) { |
---|
89 | output_set_data_int(output,i*2+3,filters[i].count); |
---|
90 | output_set_data_int(output,i*2+4,filters[i].bytes); |
---|
91 | filters[i].count=filters[i].bytes=0; |
---|
92 | } |
---|
93 | output_flush_row(output); |
---|
94 | } |
---|
95 | |
---|
96 | /* Process a trace, counting packets that match filter(s) */ |
---|
97 | static void run_trace(char *uri) |
---|
98 | { |
---|
99 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
100 | int i; |
---|
101 | uint64_t count = 0; |
---|
102 | uint64_t bytes = 0; |
---|
103 | double last_ts = 0; |
---|
104 | double ts = 0; |
---|
105 | |
---|
106 | output=output_init(uri,output_format?output_format:"txt"); |
---|
107 | output_add_column(output,"ts"); |
---|
108 | output_add_column(output,"packets"); |
---|
109 | output_add_column(output,"bytes"); |
---|
110 | for(i=0;i<filter_count;++i) { |
---|
111 | char buff[1024]; |
---|
112 | snprintf(buff,sizeof(buff),"%s packets",filters[i].expr); |
---|
113 | output_add_column(output,buff); |
---|
114 | snprintf(buff,sizeof(buff),"%s bytes",filters[i].expr); |
---|
115 | output_add_column(output,buff); |
---|
116 | } |
---|
117 | output_flush_headings(output); |
---|
118 | |
---|
119 | |
---|
120 | trace = trace_create(uri); |
---|
121 | if (trace_is_err(trace)) { |
---|
122 | trace_perror(trace,"trace_create"); |
---|
123 | trace_destroy(trace); |
---|
124 | output_destroy(output); |
---|
125 | return; |
---|
126 | } |
---|
127 | if (trace_start(trace)==-1) { |
---|
128 | trace_perror(trace,"trace_start"); |
---|
129 | trace_destroy(trace); |
---|
130 | output_destroy(output); |
---|
131 | return; |
---|
132 | } |
---|
133 | |
---|
134 | for (;;) { |
---|
135 | int psize; |
---|
136 | dag_record_t *erf_hdr; |
---|
137 | if ((psize = trace_read_packet(trace, packet)) <1) { |
---|
138 | break; |
---|
139 | } |
---|
140 | erf_hdr = (dag_record_t *)packet->header; |
---|
141 | |
---|
142 | if (trace_get_link(packet) == NULL) { |
---|
143 | continue; |
---|
144 | } |
---|
145 | |
---|
146 | ts = trace_get_seconds(packet); |
---|
147 | while (packet_interval!=UINT64_MAX |
---|
148 | &&(last_ts==0 || last_ts<ts)) { |
---|
149 | if (last_ts==0) |
---|
150 | last_ts=ts; |
---|
151 | report_results(last_ts,count,bytes); |
---|
152 | count=0; |
---|
153 | bytes=0; |
---|
154 | last_ts+=packet_interval; |
---|
155 | } |
---|
156 | for(i=0;i<filter_count;++i) { |
---|
157 | if(trace_apply_filter(filters[i].filter,packet)) { |
---|
158 | ++filters[i].count; |
---|
159 | filters[i].bytes+=trace_get_wire_length(packet); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | ++count; |
---|
164 | bytes+=trace_get_wire_length(packet); |
---|
165 | |
---|
166 | |
---|
167 | if (count >= packet_count) { |
---|
168 | report_results(ts,count,bytes); |
---|
169 | count=0; |
---|
170 | bytes=0; |
---|
171 | } |
---|
172 | } |
---|
173 | report_results(ts,count,bytes); |
---|
174 | |
---|
175 | trace_destroy(trace); |
---|
176 | output_destroy(output); |
---|
177 | trace_destroy_packet(packet); |
---|
178 | } |
---|
179 | |
---|
180 | static void usage(char *argv0) |
---|
181 | { |
---|
182 | fprintf(stderr,"Usage:\n" |
---|
183 | "%s flags libtraceuri [libtraceuri...]\n" |
---|
184 | "-i --interval=seconds Duration of reporting interval in seconds\n" |
---|
185 | "-c --count=packets Exit after count packets received\n" |
---|
186 | "-o --output-format=txt|csv|html|png Reporting output format\n" |
---|
187 | "-f --filter=bpf Apply BPF filter. Can be specified multiple times\n" |
---|
188 | "-H --libtrace-help Print libtrace runtime documentation\n" |
---|
189 | ,argv0); |
---|
190 | } |
---|
191 | |
---|
192 | int main(int argc, char *argv[]) { |
---|
193 | |
---|
194 | int i; |
---|
195 | |
---|
196 | while(1) { |
---|
197 | int option_index; |
---|
198 | struct option long_options[] = { |
---|
199 | { "filter", 1, 0, 'f' }, |
---|
200 | { "interval", 1, 0, 'i' }, |
---|
201 | { "count", 1, 0, 'c' }, |
---|
202 | { "output-format", 1, 0, 'o' }, |
---|
203 | { "libtrace-help", 0, 0, 'H' }, |
---|
204 | { NULL, 0, 0, 0 }, |
---|
205 | }; |
---|
206 | |
---|
207 | int c=getopt_long(argc, argv, "c:f:i:o:H", |
---|
208 | long_options, &option_index); |
---|
209 | |
---|
210 | if (c==-1) |
---|
211 | break; |
---|
212 | |
---|
213 | switch (c) { |
---|
214 | case 'f': |
---|
215 | ++filter_count; |
---|
216 | filters=realloc(filters,filter_count*sizeof(struct filter_t)); |
---|
217 | filters[filter_count-1].expr=strdup(optarg); |
---|
218 | filters[filter_count-1].filter=trace_create_filter(optarg); |
---|
219 | filters[filter_count-1].count=0; |
---|
220 | filters[filter_count-1].bytes=0; |
---|
221 | break; |
---|
222 | case 'i': |
---|
223 | packet_interval=atof(optarg); |
---|
224 | break; |
---|
225 | case 'c': |
---|
226 | packet_count=atoi(optarg); |
---|
227 | break; |
---|
228 | case 'o': |
---|
229 | if (output_format) free(output_format); |
---|
230 | output_format=strdup(optarg); |
---|
231 | break; |
---|
232 | case 'H': |
---|
233 | trace_help(); |
---|
234 | exit(1); |
---|
235 | break; |
---|
236 | default: |
---|
237 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
238 | usage(argv[0]); |
---|
239 | return 1; |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | if (packet_count == UINT64_MAX && packet_interval == UINT32_MAX) { |
---|
244 | packet_interval = 300; /* every 5 minutes */ |
---|
245 | } |
---|
246 | |
---|
247 | for(i=optind;i<argc;++i) { |
---|
248 | run_trace(argv[i]); |
---|
249 | } |
---|
250 | |
---|
251 | return 0; |
---|
252 | } |
---|