main.c revision 174571
1/*- 2 * Copyright (c) 2002-2007 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 174571 2007-12-13 02:01:01Z 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 52#define S_DEFAULT \ 53 "input,output,altrate,short,long,xretry,crcerr,crypt,phyerr,rssi,rate" 54 55static int signalled; 56 57static void 58catchalarm(int signo __unused) 59{ 60 signalled = 1; 61} 62 63int 64main(int argc, char *argv[]) 65{ 66 struct athstatfoo *wf; 67 const char *ifname; 68 int c; 69 70 ifname = getenv("ATH"); 71 if (ifname == NULL) 72 ifname = "ath0"; 73 wf = athstats_new(ifname, S_DEFAULT); 74 while ((c = getopt(argc, argv, "i:lo:")) != -1) { 75 switch (c) { 76 case 'i': 77 wf->setifname(wf, optarg); 78 break; 79 case 'l': 80 wf->print_fields(wf, stdout); 81 return 0; 82 case 'o': 83 wf->setfmt(wf, optarg); 84 break; 85 default: 86 errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [interval]\n", argv[0]); 87 /*NOTREACHED*/ 88 } 89 } 90 argc -= optind; 91 argv += optind; 92 93 if (argc > 0) { 94 u_long interval = strtoul(argv[0], NULL, 0); 95 int line, omask; 96 97 if (interval < 1) 98 interval = 1; 99 signal(SIGALRM, catchalarm); 100 signalled = 0; 101 alarm(interval); 102 banner: 103 wf->print_header(wf, stdout); 104 line = 0; 105 loop: 106 if (line != 0) { 107 wf->collect_cur(wf); 108 wf->print_current(wf, stdout); 109 wf->update_tot(wf); 110 } else { 111 wf->collect_tot(wf); 112 wf->print_total(wf, stdout); 113 } 114 fflush(stdout); 115 omask = sigblock(sigmask(SIGALRM)); 116 if (!signalled) 117 sigpause(0); 118 sigsetmask(omask); 119 signalled = 0; 120 alarm(interval); 121 line++; 122 if (line == 21) /* XXX tty line count */ 123 goto banner; 124 else 125 goto loop; 126 /*NOTREACHED*/ 127 } else { 128 wf->collect_tot(wf); 129 wf->print_verbose(wf, stdout); 130 } 131 return 0; 132} 133