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