main.c revision 161200
1161200Ssam/*-
2161200Ssam * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
3161200Ssam * All rights reserved.
4161200Ssam *
5161200Ssam * Redistribution and use in source and binary forms, with or without
6161200Ssam * modification, are permitted provided that the following conditions
7161200Ssam * are met:
8161200Ssam * 1. Redistributions of source code must retain the above copyright
9161200Ssam *    notice, this list of conditions and the following disclaimer,
10161200Ssam *    without modification.
11161200Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12161200Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13161200Ssam *    redistribution must be conditioned upon including a substantially
14161200Ssam *    similar Disclaimer requirement for further binary redistribution.
15161200Ssam * 3. Neither the names of the above-listed copyright holders nor the names
16161200Ssam *    of any contributors may be used to endorse or promote products derived
17161200Ssam *    from this software without specific prior written permission.
18161200Ssam *
19161200Ssam * Alternatively, this software may be distributed under the terms of the
20161200Ssam * GNU General Public License ("GPL") version 2 as published by the Free
21161200Ssam * Software Foundation.
22161200Ssam *
23161200Ssam * NO WARRANTY
24161200Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25161200Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26161200Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27161200Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28161200Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29161200Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30161200Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31161200Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32161200Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33161200Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34161200Ssam * THE POSSIBILITY OF SUCH DAMAGES.
35161200Ssam *
36161200Ssam * $FreeBSD: head/tools/tools/ath/athstats/main.c 161200 2006-08-10 19:01:16Z sam $
37161200Ssam */
38161200Ssam
39161200Ssam/*
40161200Ssam * Simple Atheros-specific tool to inspect and monitor network traffic
41161200Ssam * statistics.
42161200Ssam *
43161200Ssam *	athstats [-i interface] [-l] [-o fmtstring] [interval]
44161200Ssam *
45161200Ssam * (default interface is ath0).  If interval is specified a rolling output
46161200Ssam * a la netstat -i is displayed every interval seconds.  The format of
47161200Ssam * the rolling display can be controlled a la ps.  The -l option will
48161200Ssam * print a list of all possible statistics for use with the -o option.
49161200Ssam */
50161200Ssam
51161200Ssam#include <stdio.h>
52161200Ssam#include <signal.h>
53161200Ssam#include <unistd.h>
54161200Ssam#include <err.h>
55161200Ssam
56161200Ssam#include "athstats.h"
57161200Ssam
58161200Ssam#define	S_DEFAULT \
59161200Ssam	"input,output,altrate,short,long,xretry,crcerr,crypt,phyerr,rssi,rate"
60161200Ssam
61161200Ssamstatic int signalled;
62161200Ssam
63161200Ssamstatic void
64161200Ssamcatchalarm(int signo __unused)
65161200Ssam{
66161200Ssam	signalled = 1;
67161200Ssam}
68161200Ssam
69161200Ssamint
70161200Ssammain(int argc, char *argv[])
71161200Ssam{
72161200Ssam	struct athstatfoo *wf;
73161200Ssam	int c;
74161200Ssam
75161200Ssam	wf = athstats_new("ath0", S_DEFAULT);
76161200Ssam	while ((c = getopt(argc, argv, "i:lo:")) != -1) {
77161200Ssam		switch (c) {
78161200Ssam		case 'i':
79161200Ssam			wf->setifname(wf, optarg);
80161200Ssam			break;
81161200Ssam		case 'l':
82161200Ssam			wf->print_fields(wf, stdout);
83161200Ssam			return 0;
84161200Ssam		case 'o':
85161200Ssam			wf->setfmt(wf, optarg);
86161200Ssam			break;
87161200Ssam		default:
88161200Ssam			errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [interval]\n", argv[0]);
89161200Ssam			/*NOTREACHED*/
90161200Ssam		}
91161200Ssam	}
92161200Ssam	argc -= optind;
93161200Ssam	argv += optind;
94161200Ssam
95161200Ssam	if (argc > 0) {
96161200Ssam		u_long interval = strtoul(argv[0], NULL, 0);
97161200Ssam		int line, omask;
98161200Ssam
99161200Ssam		if (interval < 1)
100161200Ssam			interval = 1;
101161200Ssam		signal(SIGALRM, catchalarm);
102161200Ssam		signalled = 0;
103161200Ssam		alarm(interval);
104161200Ssam	banner:
105161200Ssam		wf->print_header(wf, stdout);
106161200Ssam		line = 0;
107161200Ssam	loop:
108161200Ssam		if (line != 0) {
109161200Ssam			wf->collect_cur(wf);
110161200Ssam			wf->print_current(wf, stdout);
111161200Ssam			wf->update_tot(wf);
112161200Ssam		} else {
113161200Ssam			wf->collect_tot(wf);
114161200Ssam			wf->print_total(wf, stdout);
115161200Ssam		}
116161200Ssam		fflush(stdout);
117161200Ssam		omask = sigblock(sigmask(SIGALRM));
118161200Ssam		if (!signalled)
119161200Ssam			sigpause(0);
120161200Ssam		sigsetmask(omask);
121161200Ssam		signalled = 0;
122161200Ssam		alarm(interval);
123161200Ssam		line++;
124161200Ssam		if (line == 21)		/* XXX tty line count */
125161200Ssam			goto banner;
126161200Ssam		else
127161200Ssam			goto loop;
128161200Ssam		/*NOTREACHED*/
129161200Ssam	} else {
130161200Ssam		wf->collect_tot(wf);
131161200Ssam		wf->print_verbose(wf, stdout);
132161200Ssam	}
133161200Ssam	return 0;
134161200Ssam}
135