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