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, MERCHANTIBILITY 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$ 30 */ 31 32 /* 33 * Simple Atheros-specific tool to inspect and monitor network traffic 34 * statistics. 35 * 36 * athstats [-i interface] [-z] [-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; 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, "i:lo:z")) != -1) { 99 switch (c) { 100 case 'i': 101 wf->setifname(wf, optarg); 102 break; 103 case 'l': 104 wf->print_fields(wf, stdout); 105 return 0; 106 case 'o': 107 wf->setfmt(wf, getfmt(optarg)); 108 break; 109 case 'z': 110 wf->zerostats(wf); 111 break; 112 default: 113 errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [-z] [interval]\n", argv[0]); 114 /*NOTREACHED*/ 115 } 116 } 117 argc -= optind; 118 argv += optind; 119 120 if (argc > 0) { 121 u_long interval = strtoul(argv[0], NULL, 0); 122 int line, omask; 123 124 if (interval < 1) 125 interval = 1; 126 signal(SIGALRM, catchalarm); 127 signalled = 0; 128 alarm(interval); 129 banner: 130 wf->print_header(wf, stdout); 131 line = 0; 132 loop: 133 if (line != 0) { 134 wf->collect_cur(wf); 135 wf->print_current(wf, stdout); 136 wf->update_tot(wf); 137 } else { 138 wf->collect_tot(wf); 139 wf->print_total(wf, stdout); 140 } 141 fflush(stdout); 142 omask = sigblock(sigmask(SIGALRM)); 143 if (!signalled) 144 sigpause(0); 145 sigsetmask(omask); 146 signalled = 0; 147 alarm(interval); 148 line++; 149 if (line == 21) /* XXX tty line count */ 150 goto banner; 151 else 152 goto loop; 153 /*NOTREACHED*/ 154 } else { 155 wf->collect_tot(wf); 156 wf->print_verbose(wf, stdout); 157 } 158 return 0; 159 } 160