xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/ruptime.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <sys/param.h>
43*0Sstevel@tonic-gate #include <stdio.h>
44*0Sstevel@tonic-gate #include <dirent.h>
45*0Sstevel@tonic-gate #include <stdlib.h>
46*0Sstevel@tonic-gate #include <strings.h>
47*0Sstevel@tonic-gate #include <unistd.h>
48*0Sstevel@tonic-gate #include <fcntl.h>
49*0Sstevel@tonic-gate #include <protocols/rwhod.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate static DIR	*dirp;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #define	HOSTLIM	100
54*0Sstevel@tonic-gate static int	hostslim = HOSTLIM;
55*0Sstevel@tonic-gate static int	nhosts;
56*0Sstevel@tonic-gate struct	hs {
57*0Sstevel@tonic-gate 	struct	whod *hs_wd;
58*0Sstevel@tonic-gate 	int	hs_nusers;
59*0Sstevel@tonic-gate };
60*0Sstevel@tonic-gate static int	hscmp(), ucmp(), lcmp(), tcmp();
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define	RWHODIR		"/var/spool/rwho"
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static char	*interval();
65*0Sstevel@tonic-gate static time_t	now;
66*0Sstevel@tonic-gate static int	aflg;
67*0Sstevel@tonic-gate static int	rflg = 1;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate #define	down(h)		(now - (h)->hs_wd->wd_recvtime > 11 * 60)
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /* ARGSUSED */
72*0Sstevel@tonic-gate main(int argc, char **argv)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	struct dirent *dp;
75*0Sstevel@tonic-gate 	int f, i;
76*0Sstevel@tonic-gate 	struct whod *buf;
77*0Sstevel@tonic-gate 	int cc;
78*0Sstevel@tonic-gate 	char *name;
79*0Sstevel@tonic-gate 	struct hs *hs;
80*0Sstevel@tonic-gate 	struct hs *hsp;
81*0Sstevel@tonic-gate 	struct whod *wd;
82*0Sstevel@tonic-gate 	struct whoent *we;
83*0Sstevel@tonic-gate 	int maxloadav = 0;
84*0Sstevel@tonic-gate 	int (*cmp)() = hscmp;
85*0Sstevel@tonic-gate 	ptrdiff_t hoff;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	name = *argv;
88*0Sstevel@tonic-gate 	while (*++argv)
89*0Sstevel@tonic-gate 		while (**argv)
90*0Sstevel@tonic-gate 			switch (*(*argv)++) {
91*0Sstevel@tonic-gate 			case 'a':
92*0Sstevel@tonic-gate 				aflg++;
93*0Sstevel@tonic-gate 				break;
94*0Sstevel@tonic-gate 			case 'l':
95*0Sstevel@tonic-gate 				cmp = lcmp;
96*0Sstevel@tonic-gate 				break;
97*0Sstevel@tonic-gate 			case 'u':
98*0Sstevel@tonic-gate 				cmp = ucmp;
99*0Sstevel@tonic-gate 				break;
100*0Sstevel@tonic-gate 			case 't':
101*0Sstevel@tonic-gate 				cmp = tcmp;
102*0Sstevel@tonic-gate 				break;
103*0Sstevel@tonic-gate 			case 'r':
104*0Sstevel@tonic-gate 				rflg = -rflg;
105*0Sstevel@tonic-gate 				break;
106*0Sstevel@tonic-gate 			case '-':
107*0Sstevel@tonic-gate 				break;
108*0Sstevel@tonic-gate 			default:
109*0Sstevel@tonic-gate 				(void) fprintf(stderr, "Usage: %s [ -alrtu ]"
110*0Sstevel@tonic-gate 				    " (choose at most one of l, t, or u)\n",
111*0Sstevel@tonic-gate 				    name);
112*0Sstevel@tonic-gate 				exit(1);
113*0Sstevel@tonic-gate 			}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if ((hs = malloc(hostslim * sizeof (struct hs))) == NULL) {
116*0Sstevel@tonic-gate 		(void) fprintf(stderr, "initial hs malloc failed\n");
117*0Sstevel@tonic-gate 		exit(1);
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 	hsp = hs;
120*0Sstevel@tonic-gate 	if ((buf = malloc(sizeof (struct whod))) == NULL) {
121*0Sstevel@tonic-gate 		(void) fprintf(stderr, "initial buf malloc failed\n");
122*0Sstevel@tonic-gate 		exit(1);
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	if (chdir(RWHODIR) < 0) {
126*0Sstevel@tonic-gate 		perror(RWHODIR);
127*0Sstevel@tonic-gate 		exit(1);
128*0Sstevel@tonic-gate 	}
129*0Sstevel@tonic-gate 	dirp = opendir(".");
130*0Sstevel@tonic-gate 	if (dirp == NULL) {
131*0Sstevel@tonic-gate 		perror(RWHODIR);
132*0Sstevel@tonic-gate 		exit(1);
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
135*0Sstevel@tonic-gate 		if (dp->d_ino == 0)
136*0Sstevel@tonic-gate 			continue;
137*0Sstevel@tonic-gate 		if (strncmp(dp->d_name, "whod.", 5))
138*0Sstevel@tonic-gate 			continue;
139*0Sstevel@tonic-gate 		if (nhosts == hostslim) {
140*0Sstevel@tonic-gate 			/*
141*0Sstevel@tonic-gate 			 * We trust that the file system's limit on the number
142*0Sstevel@tonic-gate 			 * of files in a directory will kick in long before
143*0Sstevel@tonic-gate 			 * integer overflow.
144*0Sstevel@tonic-gate 			 */
145*0Sstevel@tonic-gate 			hostslim = hostslim << 1;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 			/*
148*0Sstevel@tonic-gate 			 * hsp points into an area about to be moved,
149*0Sstevel@tonic-gate 			 * so we first remember its offset into hs[],
150*0Sstevel@tonic-gate 			 * then restore it after realloc() has moved
151*0Sstevel@tonic-gate 			 * the data.
152*0Sstevel@tonic-gate 			 */
153*0Sstevel@tonic-gate 			hoff = hsp - hs;
154*0Sstevel@tonic-gate 			hs = realloc(hs, hostslim * sizeof (struct hs));
155*0Sstevel@tonic-gate 			if (hs == NULL) {
156*0Sstevel@tonic-gate 				(void) fprintf(stderr, "too many hosts\n");
157*0Sstevel@tonic-gate 				exit(1);
158*0Sstevel@tonic-gate 			}
159*0Sstevel@tonic-gate 			hsp = hs + hoff;
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 		f = open(dp->d_name, 0);
162*0Sstevel@tonic-gate 		if (f > 0) {
163*0Sstevel@tonic-gate 			int whdrsize = sizeof (*buf) - sizeof (buf->wd_we);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 			cc = read(f, buf, sizeof (struct whod));
166*0Sstevel@tonic-gate 			if (cc >= whdrsize) {
167*0Sstevel@tonic-gate 				hsp->hs_wd = malloc(whdrsize);
168*0Sstevel@tonic-gate 				wd = buf;
169*0Sstevel@tonic-gate 				bcopy((char *)buf, (char *)hsp->hs_wd,
170*0Sstevel@tonic-gate 				    whdrsize);
171*0Sstevel@tonic-gate 				hsp->hs_nusers = 0;
172*0Sstevel@tonic-gate 				for (i = 0; i < 2; i++)
173*0Sstevel@tonic-gate 					if (wd->wd_loadav[i] > maxloadav)
174*0Sstevel@tonic-gate 						maxloadav = wd->wd_loadav[i];
175*0Sstevel@tonic-gate 				/* LINTED:  pointer alignment */
176*0Sstevel@tonic-gate 				we = (struct whoent *)(((char *)buf)+cc);
177*0Sstevel@tonic-gate 				while (--we >= wd->wd_we)
178*0Sstevel@tonic-gate 					if (aflg || we->we_idle < 3600)
179*0Sstevel@tonic-gate 						hsp->hs_nusers++;
180*0Sstevel@tonic-gate 				nhosts++; hsp++;
181*0Sstevel@tonic-gate 			}
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 		(void) close(f);
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate 	(void) time(&now);
186*0Sstevel@tonic-gate 	qsort((char *)hs, nhosts, sizeof (hs[0]), cmp);
187*0Sstevel@tonic-gate 	if (nhosts == 0) {
188*0Sstevel@tonic-gate 		(void) printf("no hosts!?!\n");
189*0Sstevel@tonic-gate 		exit(1);
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 	for (i = 0; i < nhosts; i++) {
192*0Sstevel@tonic-gate 		hsp = &hs[i];
193*0Sstevel@tonic-gate 		if (down(hsp)) {
194*0Sstevel@tonic-gate 			(void) printf("%-12s%s\n", hsp->hs_wd->wd_hostname,
195*0Sstevel@tonic-gate 			    interval((int)(now - hsp->hs_wd->wd_recvtime),
196*0Sstevel@tonic-gate 				"down"));
197*0Sstevel@tonic-gate 			continue;
198*0Sstevel@tonic-gate 		}
199*0Sstevel@tonic-gate 		(void) printf("%-12s%s,  %4d user%s  load %*.2f,"
200*0Sstevel@tonic-gate 		    " %*.2f, %*.2f\n",
201*0Sstevel@tonic-gate 		    hsp->hs_wd->wd_hostname,
202*0Sstevel@tonic-gate 		    interval(hsp->hs_wd->wd_sendtime -
203*0Sstevel@tonic-gate 			hsp->hs_wd->wd_boottime, "  up"),
204*0Sstevel@tonic-gate 		    hsp->hs_nusers,
205*0Sstevel@tonic-gate 		    hsp->hs_nusers == 1 ? ", " : "s,",
206*0Sstevel@tonic-gate 		    maxloadav >= 1000 ? 5 : 4,
207*0Sstevel@tonic-gate 			hsp->hs_wd->wd_loadav[0] / 100.0,
208*0Sstevel@tonic-gate 		    maxloadav >= 1000 ? 5 : 4,
209*0Sstevel@tonic-gate 			hsp->hs_wd->wd_loadav[1] / 100.0,
210*0Sstevel@tonic-gate 		    maxloadav >= 1000 ? 5 : 4,
211*0Sstevel@tonic-gate 			hsp->hs_wd->wd_loadav[2] / 100.0);
212*0Sstevel@tonic-gate 		free(hsp->hs_wd);
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	return (0);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate static char *
219*0Sstevel@tonic-gate interval(int time, char *updown)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	static char resbuf[32];
222*0Sstevel@tonic-gate 	int days, hours, minutes;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	if (time < 0 || time > 10*365*24*60*60) {
225*0Sstevel@tonic-gate 		(void) sprintf(resbuf, "   %s ??:??", updown);
226*0Sstevel@tonic-gate 		return (resbuf);
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 	minutes = (time + 59) / 60;		/* round to minutes */
229*0Sstevel@tonic-gate 	hours = minutes / 60; minutes %= 60;
230*0Sstevel@tonic-gate 	days = hours / 24; hours %= 24;
231*0Sstevel@tonic-gate 	if (days)
232*0Sstevel@tonic-gate 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
233*0Sstevel@tonic-gate 		    updown, days, hours, minutes);
234*0Sstevel@tonic-gate 	else
235*0Sstevel@tonic-gate 		(void) sprintf(resbuf, "%s    %2d:%02d",
236*0Sstevel@tonic-gate 		    updown, hours, minutes);
237*0Sstevel@tonic-gate 	return (resbuf);
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate static int
241*0Sstevel@tonic-gate hscmp(struct hs *h1, struct hs *h2)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	return (rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * Compare according to load average.
249*0Sstevel@tonic-gate  */
250*0Sstevel@tonic-gate static int
251*0Sstevel@tonic-gate lcmp(struct hs *h1, struct hs *h2)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	if (down(h1))
255*0Sstevel@tonic-gate 		if (down(h2))
256*0Sstevel@tonic-gate 			return (tcmp(h1, h2));
257*0Sstevel@tonic-gate 		else
258*0Sstevel@tonic-gate 			return (rflg);
259*0Sstevel@tonic-gate 	else if (down(h2))
260*0Sstevel@tonic-gate 		return (-rflg);
261*0Sstevel@tonic-gate 	else
262*0Sstevel@tonic-gate 		return (rflg *
263*0Sstevel@tonic-gate 			(h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate /*
267*0Sstevel@tonic-gate  * Compare according to number of users.
268*0Sstevel@tonic-gate  */
269*0Sstevel@tonic-gate static int
270*0Sstevel@tonic-gate ucmp(struct hs *h1, struct hs *h2)
271*0Sstevel@tonic-gate {
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	if (down(h1))
274*0Sstevel@tonic-gate 		if (down(h2))
275*0Sstevel@tonic-gate 			return (tcmp(h1, h2));
276*0Sstevel@tonic-gate 		else
277*0Sstevel@tonic-gate 			return (rflg);
278*0Sstevel@tonic-gate 	else if (down(h2))
279*0Sstevel@tonic-gate 		return (-rflg);
280*0Sstevel@tonic-gate 	else
281*0Sstevel@tonic-gate 		return (rflg * (h2->hs_nusers - h1->hs_nusers));
282*0Sstevel@tonic-gate }
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate /*
285*0Sstevel@tonic-gate  * Compare according to uptime.
286*0Sstevel@tonic-gate  */
287*0Sstevel@tonic-gate static int
288*0Sstevel@tonic-gate tcmp(struct hs *h1, struct hs *h2)
289*0Sstevel@tonic-gate {
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	return (rflg * (
292*0Sstevel@tonic-gate 		(down(h2) ? h2->hs_wd->wd_recvtime - now :
293*0Sstevel@tonic-gate 		    h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime)
294*0Sstevel@tonic-gate 		-
295*0Sstevel@tonic-gate 		(down(h1) ? h1->hs_wd->wd_recvtime - now :
296*0Sstevel@tonic-gate 		    h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)));
297*0Sstevel@tonic-gate }
298