main.c revision 186904
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: head/tools/tools/ath/athstats/main.c 186904 2009-01-08 17:12:47Z sam $ 30 */ 31 32/* 33 * Simple Atheros-specific tool to inspect and monitor network traffic 34 * statistics. 35 * 36 * athstats [-i interface] [-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 52static 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 67static const char * 68getfmt(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 errx(-1, "unknown tag \%s\"", tag); 76 /*NOTREACHED*/ 77#undef N 78} 79 80static int signalled; 81 82static void 83catchalarm(int signo __unused) 84{ 85 signalled = 1; 86} 87 88int 89main(int argc, char *argv[]) 90{ 91 struct athstatfoo *wf; 92 const char *ifname; 93 int c; 94 95 ifname = getenv("ATH"); 96 if (ifname == NULL) 97 ifname = "ath0"; 98 wf = athstats_new(ifname, getfmt("default")); 99 while ((c = getopt(argc, argv, "i:lo:")) != -1) { 100 switch (c) { 101 case 'i': 102 wf->setifname(wf, optarg); 103 break; 104 case 'l': 105 wf->print_fields(wf, stdout); 106 return 0; 107 case 'o': 108 wf->setfmt(wf, getfmt(optarg)); 109 break; 110 default: 111 errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [interval]\n", argv[0]); 112 /*NOTREACHED*/ 113 } 114 } 115 argc -= optind; 116 argv += optind; 117 118 if (argc > 0) { 119 u_long interval = strtoul(argv[0], NULL, 0); 120 int line, omask; 121 122 if (interval < 1) 123 interval = 1; 124 signal(SIGALRM, catchalarm); 125 signalled = 0; 126 alarm(interval); 127 banner: 128 wf->print_header(wf, stdout); 129 line = 0; 130 loop: 131 if (line != 0) { 132 wf->collect_cur(wf); 133 wf->print_current(wf, stdout); 134 wf->update_tot(wf); 135 } else { 136 wf->collect_tot(wf); 137 wf->print_total(wf, stdout); 138 } 139 fflush(stdout); 140 omask = sigblock(sigmask(SIGALRM)); 141 if (!signalled) 142 sigpause(0); 143 sigsetmask(omask); 144 signalled = 0; 145 alarm(interval); 146 line++; 147 if (line == 21) /* XXX tty line count */ 148 goto banner; 149 else 150 goto loop; 151 /*NOTREACHED*/ 152 } else { 153 wf->collect_tot(wf); 154 wf->print_verbose(wf, stdout); 155 } 156 return 0; 157} 158