1 /*- 2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTABILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 * 29 * $FreeBSD: src/tools/tools/ath/athstats/main.c,v 1.8 2009/03/02 05:07:05 sam Exp $ 30 */ 31 32 /* 33 * Simple Atheros-specific tool to inspect and monitor network traffic 34 * statistics. 35 * 36 * athstats [-i interface] [-bz] [-l] [-o fmtstring] [interval] 37 * 38 * (default interface is ath0). If interval is specified a rolling output 39 * a la netstat -i is displayed every interval seconds. The format of 40 * the rolling display can be controlled a la ps. The -l option will 41 * print a list of all possible statistics for use with the -o option. 42 */ 43 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <signal.h> 47 #include <unistd.h> 48 #include <err.h> 49 50 #include "athstats.h" 51 52 static struct { 53 const char *tag; 54 const char *fmt; 55 } tags[] = { 56 { "default", 57 "input,output,altrate,short,long,xretry,crcerr,crypt,phyerr,rssi,rate" 58 }, 59 { "ani", 60 "avgbrssi,avgrssi,avgtxrssi,NI,SI,step,owsd,cwst,NI+,NI-,SI+,SI-,OFDM,CCK,LISTEN" 61 }, 62 { "tdma", 63 "input,output,bexmit,tdmau,tdmadj,crcerr,phyerr,phytor,rssi,noise,rate" 64 }, 65 }; 66 67 static const char * 68 getfmt(const char *tag) 69 { 70 #define N(a) (sizeof(a)/sizeof(a[0])) 71 int i; 72 for (i = 0; i < N(tags); i++) 73 if (strcasecmp(tags[i].tag, tag) == 0) 74 return tags[i].fmt; 75 return tag; 76 #undef N 77 } 78 79 static int signalled; 80 81 static void 82 catchalarm(int signo __unused) 83 { 84 signalled = 1; 85 } 86 87 int 88 main(int argc, char *argv[]) 89 { 90 struct athstatfoo *wf; 91 const char *ifname; 92 int c, banner = 1; 93 94 ifname = getenv("ATH"); 95 if (ifname == NULL) 96 ifname = "ath0"; 97 wf = athstats_new(ifname, getfmt("default")); 98 while ((c = getopt(argc, argv, "bi:lo:z")) != -1) { 99 switch (c) { 100 case 'b': 101 banner = 0; 102 break; 103 case 'i': 104 wf->setifname(wf, optarg); 105 break; 106 case 'l': 107 wf->print_fields(wf, stdout); 108 return 0; 109 case 'o': 110 wf->setfmt(wf, getfmt(optarg)); 111 break; 112 case 'z': 113 wf->zerostats(wf); 114 break; 115 default: 116 errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [-z] [interval]\n", argv[0]); 117 /*NOTREACHED*/ 118 } 119 } 120 argc -= optind; 121 argv += optind; 122 123 if (argc > 0) { 124 u_long interval = strtoul(argv[0], NULL, 0); 125 int line, omask; 126 127 if (interval < 1) 128 interval = 1; 129 signal(SIGALRM, catchalarm); 130 signalled = 0; 131 alarm(interval); 132 banner: 133 if (banner) 134 wf->print_header(wf, stdout); 135 line = 0; 136 loop: 137 if (line != 0) { 138 wf->collect_cur(wf); 139 wf->print_current(wf, stdout); 140 wf->update_tot(wf); 141 } else { 142 wf->collect_tot(wf); 143 wf->print_total(wf, stdout); 144 } 145 fflush(stdout); 146 omask = sigblock(sigmask(SIGALRM)); 147 if (!signalled) 148 sigpause(0); 149 sigsetmask(omask); 150 signalled = 0; 151 alarm(interval); 152 line++; 153 if (line == 21) /* XXX tty line count */ 154 goto banner; 155 else 156 goto loop; 157 /*NOTREACHED*/ 158 } else { 159 wf->collect_tot(wf); 160 wf->print_verbose(wf, stdout); 161 } 162 return 0; 163 } 164