xref: /freebsd/tools/tools/ath/athstats/main.c (revision 16038816)
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] [-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 <sys/param.h>
45 
46 #include <err.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #include "athstats.h"
54 
55 static struct {
56 	const char *tag;
57 	const char *fmt;
58 } tags[] = {
59   { "default",
60     "input,output,altrate,short,long,xretry,crcerr,crypt,phyerr,rssi,rate"
61   },
62   { "ani",
63     "avgbrssi,avgrssi,avgtxrssi,NI,SI,step,owsd,cwst,NI+,NI-,SI+,SI-,OFDM,CCK,LISTEN"
64   },
65   { "tdma",
66     "input,output,bexmit,tdmau,tdmadj,crcerr,phyerr,phytor,rssi,noise,rate"
67   },
68   { "beacon",
69     "bstuck,bmiss,bexmit,beacons,bmisscount,reset,ofdm,cck,input,output"
70   },
71 };
72 
73 static const char *
74 getfmt(const char *tag)
75 {
76 	int i;
77 	for (i = 0; i < nitems(tags); i++)
78 		if (strcasecmp(tags[i].tag, tag) == 0)
79 			return tags[i].fmt;
80 	return tag;
81 }
82 
83 static int signalled;
84 
85 static void
86 catchalarm(int signo __unused)
87 {
88 	signalled = 1;
89 }
90 
91 int
92 main(int argc, char *argv[])
93 {
94 	struct athstatfoo *wf;
95 	const char *ifname;
96 	int c, banner = 1;
97 
98 	ifname = getenv("ATH");
99 	if (ifname == NULL)
100 		ifname = ATH_DEFAULT;
101 	wf = athstats_new(ifname, getfmt("default"));
102 	while ((c = getopt(argc, argv, "bi:lo:z")) != -1) {
103 		switch (c) {
104 		case 'b':
105 			banner = 0;
106 			break;
107 		case 'i':
108 			wf->setifname(wf, optarg);
109 			break;
110 		case 'l':
111 			wf->print_fields(wf, stdout);
112 			return 0;
113 		case 'o':
114 			wf->setfmt(wf, getfmt(optarg));
115 			break;
116 		case 'z':
117 			wf->zerostats(wf);
118 			break;
119 		default:
120 			errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [-z] [interval]\n", argv[0]);
121 			/*NOTREACHED*/
122 		}
123 	}
124 	argc -= optind;
125 	argv += optind;
126 
127 	if (argc > 0) {
128 		u_long interval = strtoul(argv[0], NULL, 0);
129 		int line, omask;
130 
131 		if (interval < 1)
132 			interval = 1;
133 		signal(SIGALRM, catchalarm);
134 		signalled = 0;
135 		alarm(interval);
136 	banner:
137 		if (banner)
138 			wf->print_header(wf, stdout);
139 		line = 0;
140 	loop:
141 		if (line != 0) {
142 			wf->collect_cur(wf);
143 			wf->print_current(wf, stdout);
144 			wf->update_tot(wf);
145 		} else {
146 			wf->collect_tot(wf);
147 			wf->print_total(wf, stdout);
148 		}
149 		fflush(stdout);
150 		omask = sigblock(sigmask(SIGALRM));
151 		if (!signalled)
152 			sigpause(0);
153 		sigsetmask(omask);
154 		signalled = 0;
155 		alarm(interval);
156 		line++;
157 		if (line == 21)		/* XXX tty line count */
158 			goto banner;
159 		else
160 			goto loop;
161 		/*NOTREACHED*/
162 	} else {
163 		wf->collect_tot(wf);
164 		wf->print_verbose(wf, stdout);
165 	}
166 	return 0;
167 }
168