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
55static 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};
69
70static const char *
71getfmt(const char *tag)
72{
73	int i;
74	for (i = 0; i < nitems(tags); i++)
75		if (strcasecmp(tags[i].tag, tag) == 0)
76			return tags[i].fmt;
77	return tag;
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, banner = 1;
94
95	ifname = getenv("ATH");
96	if (ifname == NULL)
97		ifname = ATH_DEFAULT;
98	wf = athstats_new(ifname, getfmt("default"));
99	while ((c = getopt(argc, argv, "bi:lo:z")) != -1) {
100		switch (c) {
101		case 'b':
102			banner = 0;
103			break;
104		case 'i':
105			wf->setifname(wf, optarg);
106			break;
107		case 'l':
108			wf->print_fields(wf, stdout);
109			return 0;
110		case 'o':
111			wf->setfmt(wf, getfmt(optarg));
112			break;
113		case 'z':
114			wf->zerostats(wf);
115			break;
116		default:
117			errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [-z] [interval]\n", argv[0]);
118			/*NOTREACHED*/
119		}
120	}
121	argc -= optind;
122	argv += optind;
123
124	if (argc > 0) {
125		u_long interval = strtoul(argv[0], NULL, 0);
126		int line, omask;
127
128		if (interval < 1)
129			interval = 1;
130		signal(SIGALRM, catchalarm);
131		signalled = 0;
132		alarm(interval);
133	banner:
134		if (banner)
135			wf->print_header(wf, stdout);
136		line = 0;
137	loop:
138		if (line != 0) {
139			wf->collect_cur(wf);
140			wf->print_current(wf, stdout);
141			wf->update_tot(wf);
142		} else {
143			wf->collect_tot(wf);
144			wf->print_total(wf, stdout);
145		}
146		fflush(stdout);
147		omask = sigblock(sigmask(SIGALRM));
148		if (!signalled)
149			sigpause(0);
150		sigsetmask(omask);
151		signalled = 0;
152		alarm(interval);
153		line++;
154		if (line == 21)		/* XXX tty line count */
155			goto banner;
156		else
157			goto loop;
158		/*NOTREACHED*/
159	} else {
160		wf->collect_tot(wf);
161		wf->print_verbose(wf, stdout);
162	}
163	return 0;
164}
165