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