xref: /dragonfly/usr.bin/systat/main.c (revision 60e242c5)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)main.c	8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/systat/main.c,v 1.11.2.1 2001/06/06 20:26:01 tmm Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/time.h>
36 
37 #include <err.h>
38 #include <limits.h>
39 #include <locale.h>
40 #include <nlist.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include "systat.h"
46 #include "extern.h"
47 
48 static struct nlist namelist[] = {
49 #define X_FIRST		0
50 #define	X_HZ		0
51 	{ .n_name = "_hz" },
52 #define	X_STATHZ		1
53 	{ .n_name = "_stathz" },
54 	{ .n_name = "" }
55 };
56 static int     dellave;
57 
58 kvm_t *kd;
59 sig_t	sigtstpdfl;
60 double avenrun[3];
61 int	WideMode;
62 int     col;
63 double	naptime = 5.0;
64 int     verbose = 1;                    /* to report kvm read errs */
65 int     hz, stathz;
66 double	hertz;
67 char    c;
68 char    *namp;
69 char    hostname[MAXHOSTNAMELEN];
70 WINDOW  *wnd;
71 int     CMDLINE;
72 
73 static	WINDOW *wload;			/* one line window for load average */
74 
75 int
76 main(int argc, char **argv)
77 {
78 	char errbuf[_POSIX2_LINE_MAX];
79 	char *op;
80 
81 	setlocale(LC_TIME, "");
82 
83 	argc--, argv++;
84 	while (argc > 0) {
85 		op = argv[0];
86 
87 		if (op[0] == '-' && op[1] && op[2] == 0) {
88 			switch(op[1]) {
89 			case 'w':
90 				WideMode = 1;
91 				break;
92 			default:
93 				fprintf(stderr,
94 					"systat [-w] -cmd [interval]\n");
95 				fprintf(stderr,
96 					"	-pigs\n"
97 					"	-swap\n"
98 					"	-mb\n"
99 					"	-io\n"
100 					"	-vm\n"
101 					"	-pv\n"
102 					"	-netstat\n"
103 					"	-netbw\n"
104 					"	-pf\n"
105 					"	-icmp\n"
106 					"	-ip\n"
107 					"	-tcp\n"
108 					"	-ifstat\n"
109 					"	-altqs\n"
110 					"	-ip6\n"
111 					"	-icmp6\n"
112 					"	-sensors\n");
113 				exit(1);
114 			}
115 		} else if (op[0] == '-') {
116 			struct cmdtab *p;
117 
118 			p = lookup(&argv[0][1]);
119 			if (p == (struct cmdtab *)-1)
120 				errx(1, "%s: ambiguous request", &argv[0][1]);
121 			if (p == NULL)
122 				errx(1, "%s: unknown request", &argv[0][1]);
123 			curcmd = p;
124 		} else {
125 			naptime = strtod(argv[0], NULL);
126 			if (naptime <= 0.0)
127 				naptime = 5.0;
128 		}
129 		argc--, argv++;
130 	}
131 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
132 	if (kd == NULL) {
133 		error("%s", errbuf);
134 		exit(1);
135 	}
136 	signal(SIGINT, die);
137 	signal(SIGQUIT, die);
138 	signal(SIGTERM, die);
139 
140 	/*
141 	 * Initialize display.  Load average appears in a one line
142 	 * window of its own.  Current command's display appears in
143 	 * an overlapping sub-window of stdscr configured by the display
144 	 * routines to minimize update work by curses.
145 	 */
146 	initscr();
147 	CMDLINE = LINES - 1;
148 	wnd = (*curcmd->c_open)();
149 	if (wnd == NULL) {
150 		warnx("couldn't initialize display");
151 		die(0);
152 	}
153 	wload = newwin(1, 0, 3, 20);
154 	if (wload == NULL) {
155 		warnx("couldn't set up load average window");
156 		die(0);
157 	}
158 	if (kvm_nlist(kd, namelist)) {
159 		nlisterr(namelist);
160 		exit(1);
161 	}
162 	if (namelist[X_FIRST].n_type == 0)
163 		errx(1, "couldn't read namelist");
164 	gethostname(hostname, sizeof (hostname));
165 	NREAD(X_HZ, &hz, sizeof(hz));
166 	NREAD(X_STATHZ, &stathz, sizeof(stathz));
167 	hertz = stathz ? stathz : hz;
168 	(*curcmd->c_init)();
169 	curcmd->c_flags |= CF_INIT;
170 	labels();
171 
172 	dellave = 0.0;
173 
174 	signal(SIGALRM, display);
175 	display(0);
176 	noecho();
177 	crmode();
178 	keyboard();
179 	/*NOTREACHED*/
180 
181 	return EXIT_SUCCESS;
182 }
183 
184 void
185 labels(void)
186 {
187 	if (curcmd->c_flags & CF_LOADAV) {
188 		mvaddstr(2, 20,
189 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
190 		mvaddstr(3, 5, "Load Average");
191 	}
192 	(*curcmd->c_label)();
193 #ifdef notdef
194 	mvprintw(21, 25, "CPU usage on %s", hostname);
195 #endif
196 	refresh();
197 }
198 
199 void
200 display(int signo __unused)
201 {
202 	int i, j;
203 	struct itimerval ctv;
204 
205 	/* Get the load average over the last minute. */
206 	(void) getloadavg(avenrun, NELEM(avenrun));
207 	(*curcmd->c_fetch)();
208 	if (curcmd->c_flags & CF_LOADAV) {
209 		j = 5.0*avenrun[0] + 0.5;
210 		dellave = avenrun[0];
211 		c = '|';
212 		wmove(wload, 0, 0); wclrtoeol(wload);
213 		for (i = (j > 50) ? 50 : j; i > 0; i--)
214 			waddch(wload, c);
215 		if (j > 50)
216 			wprintw(wload, " %4.1f", avenrun[0]);
217 	}
218 	(*curcmd->c_refresh)();
219 	if (curcmd->c_flags & CF_LOADAV)
220 		wrefresh(wload);
221 	wrefresh(wnd);
222 	move(CMDLINE, col);
223 	refresh();
224 	ctv.it_interval.tv_sec = 0;
225 	ctv.it_interval.tv_usec = 0;
226 	ctv.it_value.tv_sec = (int)naptime;
227 	ctv.it_value.tv_usec = (naptime - (double)(int)naptime) * 1000000.0;
228 	setitimer(ITIMER_REAL, &ctv, NULL);
229 }
230 
231 void
232 load(void)
233 {
234 
235 	(void) getloadavg(avenrun, NELEM(avenrun));
236 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
237 	    avenrun[0], avenrun[1], avenrun[2]);
238 	clrtoeol();
239 }
240 
241 void
242 die(int signo __unused)
243 {
244 	move(CMDLINE, 0);
245 	clrtoeol();
246 	refresh();
247 	endwin();
248 	exit(0);
249 }
250 
251 #include <stdarg.h>
252 
253 void
254 error(const char *fmt, ...)
255 {
256 	va_list ap;
257 	char buf[255];
258 	int oy, ox;
259 	va_start(ap, fmt);
260 
261 	if (wnd) {
262 		getyx(stdscr, oy, ox);
263 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
264 		clrtoeol();
265 		standout();
266 		mvaddstr(CMDLINE, 0, buf);
267 		standend();
268 		move(oy, ox);
269 		refresh();
270 	} else {
271 		(void) vfprintf(stderr, fmt, ap);
272 		fprintf(stderr, "\n");
273 	}
274 	va_end(ap);
275 }
276 
277 void
278 nlisterr(struct nlist *n_list)
279 {
280 	int i, n;
281 
282 	n = 0;
283 	clear();
284 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
285 	for (i = 0;
286 	    n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
287 		if (n_list[i].n_value == 0)
288 			mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);
289 	move(CMDLINE, 0);
290 	clrtoeol();
291 	refresh();
292 	endwin();
293 	exit(1);
294 }
295