xref: /freebsd/usr.bin/ruptime/ruptime.c (revision 73b018e3)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993, 1994
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
31e88b3b4fSPhilippe Charnier static const char copyright[] =
329b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993, 1994\n\
339b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
349b50d902SRodney W. Grimes #endif /* not lint */
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifndef lint
37a827060aSMark Murray static const char sccsid[] = "@(#)ruptime.c	8.2 (Berkeley) 4/5/94";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
40a827060aSMark Murray #include <sys/cdefs.h>
41a827060aSMark Murray __FBSDID("$FreeBSD$");
42a827060aSMark Murray 
439b50d902SRodney W. Grimes #include <sys/param.h>
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <protocols/rwhod.h>
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <dirent.h>
489b50d902SRodney W. Grimes #include <err.h>
49821df508SXin LI #include <errno.h>
509b50d902SRodney W. Grimes #include <fcntl.h>
519b50d902SRodney W. Grimes #include <stdio.h>
529b50d902SRodney W. Grimes #include <stdlib.h>
539b50d902SRodney W. Grimes #include <string.h>
549b50d902SRodney W. Grimes #include <time.h>
559b50d902SRodney W. Grimes #include <unistd.h>
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes struct hs {
5873b018e3SEd Schouten 	struct	whod hs_wd;
599b50d902SRodney W. Grimes 	int	hs_nusers;
609b50d902SRodney W. Grimes } *hs;
6102dfd2b2SSuleiman Souhlal #define	LEFTEARTH(h)	(now - (h) > 4*24*60*60)
6273b018e3SEd Schouten #define	ISDOWN(h)	(now - (h)->hs_wd.wd_recvtime > 11 * 60)
6373b018e3SEd Schouten #define	WHDRSIZE	__offsetof(struct whod, wd_we)
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes size_t nhosts;
669b50d902SRodney W. Grimes time_t now;
679b50d902SRodney W. Grimes int rflg = 1;
68e7cb5ef7SJuli Mallett DIR *dirp;
699b50d902SRodney W. Grimes 
70d3cb5dedSWarner Losh int	 hscmp(const void *, const void *);
71a827060aSMark Murray char	*interval(time_t, const char *);
72d3cb5dedSWarner Losh int	 lcmp(const void *, const void *);
73d3cb5dedSWarner Losh void	 morehosts(void);
74e7cb5ef7SJuli Mallett void	 ruptime(const char *, int, int (*)(const void *, const void *));
75d3cb5dedSWarner Losh int	 tcmp(const void *, const void *);
76d3cb5dedSWarner Losh int	 ucmp(const void *, const void *);
77d3cb5dedSWarner Losh void	 usage(void);
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes int
80a827060aSMark Murray main(int argc, char *argv[])
819b50d902SRodney W. Grimes {
82d3cb5dedSWarner Losh 	int (*cmp)(const void *, const void *);
83e7cb5ef7SJuli Mallett 	int aflg, ch;
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes 	aflg = 0;
869b50d902SRodney W. Grimes 	cmp = hscmp;
871c8af878SWarner Losh 	while ((ch = getopt(argc, argv, "alrut")) != -1)
889b50d902SRodney W. Grimes 		switch (ch) {
899b50d902SRodney W. Grimes 		case 'a':
909b50d902SRodney W. Grimes 			aflg = 1;
919b50d902SRodney W. Grimes 			break;
929b50d902SRodney W. Grimes 		case 'l':
939b50d902SRodney W. Grimes 			cmp = lcmp;
949b50d902SRodney W. Grimes 			break;
959b50d902SRodney W. Grimes 		case 'r':
969b50d902SRodney W. Grimes 			rflg = -1;
979b50d902SRodney W. Grimes 			break;
989b50d902SRodney W. Grimes 		case 't':
999b50d902SRodney W. Grimes 			cmp = tcmp;
1009b50d902SRodney W. Grimes 			break;
1019b50d902SRodney W. Grimes 		case 'u':
1029b50d902SRodney W. Grimes 			cmp = ucmp;
1039b50d902SRodney W. Grimes 			break;
1049b50d902SRodney W. Grimes 		default:
1059b50d902SRodney W. Grimes 			usage();
1069b50d902SRodney W. Grimes 		}
1079b50d902SRodney W. Grimes 	argc -= optind;
1089b50d902SRodney W. Grimes 	argv += optind;
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes 	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
1119b50d902SRodney W. Grimes 		err(1, "%s", _PATH_RWHODIR);
1129b50d902SRodney W. Grimes 
113e7cb5ef7SJuli Mallett 	ruptime(*argv, aflg, cmp);
114e7cb5ef7SJuli Mallett 	while (*argv++ != NULL) {
115e7cb5ef7SJuli Mallett 		if (*argv == NULL)
116e7cb5ef7SJuli Mallett 			break;
117e7cb5ef7SJuli Mallett 		ruptime(*argv, aflg, cmp);
1189b50d902SRodney W. Grimes 	}
1199b50d902SRodney W. Grimes 	exit(0);
1209b50d902SRodney W. Grimes }
1219b50d902SRodney W. Grimes 
1229b50d902SRodney W. Grimes char *
123a827060aSMark Murray interval(time_t tval, const char *updown)
1249b50d902SRodney W. Grimes {
1259b50d902SRodney W. Grimes 	static char resbuf[32];
1269b50d902SRodney W. Grimes 	int days, hours, minutes;
1279b50d902SRodney W. Grimes 
128fdb7c7dcSJoerg Wunsch 	if (tval < 0) {
1299b50d902SRodney W. Grimes 		(void)snprintf(resbuf, sizeof(resbuf), "%s      ??:??", updown);
1309b50d902SRodney W. Grimes 		return (resbuf);
1319b50d902SRodney W. Grimes 	}
13273b018e3SEd Schouten 	/* Round to minutes. */
133656dcd43SGarrett Wollman 	minutes = (tval + (60 - 1)) / 60;
134656dcd43SGarrett Wollman 	hours = minutes / 60;
135656dcd43SGarrett Wollman 	minutes %= 60;
136656dcd43SGarrett Wollman 	days = hours / 24;
137656dcd43SGarrett Wollman 	hours %= 24;
1389b50d902SRodney W. Grimes 	if (days)
1399b50d902SRodney W. Grimes 		(void)snprintf(resbuf, sizeof(resbuf),
14073b018e3SEd Schouten 		    "%s %4d+%02d:%02d", updown, days, hours, minutes);
1419b50d902SRodney W. Grimes 	else
1429b50d902SRodney W. Grimes 		(void)snprintf(resbuf, sizeof(resbuf),
1439b50d902SRodney W. Grimes 		    "%s      %2d:%02d", updown, hours, minutes);
1449b50d902SRodney W. Grimes 	return (resbuf);
1459b50d902SRodney W. Grimes }
1469b50d902SRodney W. Grimes 
147a827060aSMark Murray #define	HS(a)	((const struct hs *)(a))
1489b50d902SRodney W. Grimes 
1499b50d902SRodney W. Grimes /* Alphabetical comparison. */
1509b50d902SRodney W. Grimes int
151a827060aSMark Murray hscmp(const void *a1, const void *a2)
1529b50d902SRodney W. Grimes {
1539b50d902SRodney W. Grimes 	return (rflg *
15473b018e3SEd Schouten 	    strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));
1559b50d902SRodney W. Grimes }
1569b50d902SRodney W. Grimes 
1579b50d902SRodney W. Grimes /* Load average comparison. */
1589b50d902SRodney W. Grimes int
159a827060aSMark Murray lcmp(const void *a1, const void *a2)
1609b50d902SRodney W. Grimes {
1619b50d902SRodney W. Grimes 	if (ISDOWN(HS(a1)))
1629b50d902SRodney W. Grimes 		if (ISDOWN(HS(a2)))
1639b50d902SRodney W. Grimes 			return (tcmp(a1, a2));
1649b50d902SRodney W. Grimes 		else
1659b50d902SRodney W. Grimes 			return (rflg);
1669b50d902SRodney W. Grimes 	else if (ISDOWN(HS(a2)))
1679b50d902SRodney W. Grimes 		return (-rflg);
1689b50d902SRodney W. Grimes 	else
1699b50d902SRodney W. Grimes 		return (rflg *
17073b018e3SEd Schouten 		   (HS(a2)->hs_wd.wd_loadav[0] - HS(a1)->hs_wd.wd_loadav[0]));
1719b50d902SRodney W. Grimes }
1729b50d902SRodney W. Grimes 
173e7cb5ef7SJuli Mallett void
174e7cb5ef7SJuli Mallett ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
175e7cb5ef7SJuli Mallett {
176e7cb5ef7SJuli Mallett 	struct hs *hsp;
177e7cb5ef7SJuli Mallett 	struct whod *wd;
178e7cb5ef7SJuli Mallett 	struct whoent *we;
179e7cb5ef7SJuli Mallett 	struct dirent *dp;
180e7cb5ef7SJuli Mallett 	const char *hostname;
181e7cb5ef7SJuli Mallett 	int fd, i, maxloadav;
182e7cb5ef7SJuli Mallett 	size_t hspace;
18373b018e3SEd Schouten 	ssize_t cc;
184e7cb5ef7SJuli Mallett 
185e7cb5ef7SJuli Mallett 	rewinddir(dirp);
186e7cb5ef7SJuli Mallett 	hsp = NULL;
187e7cb5ef7SJuli Mallett 	maxloadav = -1;
188887019fbSAntoine Brodin 	(void)time(&now);
189e7cb5ef7SJuli Mallett 	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
190e7cb5ef7SJuli Mallett 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
191e7cb5ef7SJuli Mallett 			continue;
192e7cb5ef7SJuli Mallett 		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
193e7cb5ef7SJuli Mallett 			warn("%s", dp->d_name);
194e7cb5ef7SJuli Mallett 			continue;
195e7cb5ef7SJuli Mallett 		}
196e7cb5ef7SJuli Mallett 
197e7cb5ef7SJuli Mallett 		if (nhosts == hspace) {
198e7cb5ef7SJuli Mallett 			if ((hs =
199e7cb5ef7SJuli Mallett 			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
200e7cb5ef7SJuli Mallett 				err(1, NULL);
201e7cb5ef7SJuli Mallett 			hsp = hs + nhosts;
202e7cb5ef7SJuli Mallett 		}
203e7cb5ef7SJuli Mallett 
20473b018e3SEd Schouten 		wd = &hsp->hs_wd;
20573b018e3SEd Schouten 		cc = read(fd, wd, sizeof(*wd));
20673b018e3SEd Schouten 		(void)close(fd);
20773b018e3SEd Schouten 		if (cc < (ssize_t)WHDRSIZE)
20873b018e3SEd Schouten 			continue;
209e7cb5ef7SJuli Mallett 
21073b018e3SEd Schouten 		if (host != NULL) {
21173b018e3SEd Schouten 			hostname = wd->wd_hostname;
21273b018e3SEd Schouten 			if (strcasecmp(hostname, host) != 0)
21373b018e3SEd Schouten 				continue;
21473b018e3SEd Schouten 		}
21573b018e3SEd Schouten 		if (LEFTEARTH(wd->wd_recvtime))
21673b018e3SEd Schouten 			continue;
21773b018e3SEd Schouten 
21873b018e3SEd Schouten 		for (i = 0; i < 2; i++)
219e7cb5ef7SJuli Mallett 			if (wd->wd_loadav[i] > maxloadav)
220e7cb5ef7SJuli Mallett 				maxloadav = wd->wd_loadav[i];
221e7cb5ef7SJuli Mallett 
22273b018e3SEd Schouten 		for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
22373b018e3SEd Schouten 		    (char *)(we + 1) <= (char *)wd + cc; we++)
224e7cb5ef7SJuli Mallett 			if (aflg || we->we_idle < 3600)
225e7cb5ef7SJuli Mallett 				++hsp->hs_nusers;
226e7cb5ef7SJuli Mallett 		++hsp;
227e7cb5ef7SJuli Mallett 		++nhosts;
228e7cb5ef7SJuli Mallett 	}
229e7cb5ef7SJuli Mallett 	if (nhosts == 0) {
230e7cb5ef7SJuli Mallett 		if (host == NULL)
231e7cb5ef7SJuli Mallett 			errx(1, "no hosts in %s", _PATH_RWHODIR);
232e7cb5ef7SJuli Mallett 		else
233e7cb5ef7SJuli Mallett 			warnx("host %s not in %s", host, _PATH_RWHODIR);
234e7cb5ef7SJuli Mallett 	}
235e7cb5ef7SJuli Mallett 
236e7cb5ef7SJuli Mallett 	qsort(hs, nhosts, sizeof(hs[0]), cmp);
237e7cb5ef7SJuli Mallett 	for (i = 0; i < (int)nhosts; i++) {
238e7cb5ef7SJuli Mallett 		hsp = &hs[i];
23973b018e3SEd Schouten 		wd = &hsp->hs_wd;
240e7cb5ef7SJuli Mallett 		if (ISDOWN(hsp)) {
24173b018e3SEd Schouten 			(void)printf("%-25.25s%s\n", wd->wd_hostname,
24273b018e3SEd Schouten 			    interval(now - hsp->hs_wd.wd_recvtime, "down"));
243e7cb5ef7SJuli Mallett 			continue;
244e7cb5ef7SJuli Mallett 		}
245e7cb5ef7SJuli Mallett 		(void)printf(
246e5bc6d56SDavid E. O'Brien 		    "%-25.25s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
24773b018e3SEd Schouten 		    wd->wd_hostname,
24873b018e3SEd Schouten 		    interval((time_t)wd->wd_sendtime -
24973b018e3SEd Schouten 		        (time_t)wd->wd_boottime, "  up"),
250e7cb5ef7SJuli Mallett 		    hsp->hs_nusers,
251e7cb5ef7SJuli Mallett 		    hsp->hs_nusers == 1 ? ", " : "s,",
252e7cb5ef7SJuli Mallett 		    maxloadav >= 1000 ? 5 : 4,
25373b018e3SEd Schouten 		        wd->wd_loadav[0] / 100.0,
254e7cb5ef7SJuli Mallett 		    maxloadav >= 1000 ? 5 : 4,
25573b018e3SEd Schouten 		        wd->wd_loadav[1] / 100.0,
256e7cb5ef7SJuli Mallett 		    maxloadav >= 1000 ? 5 : 4,
25773b018e3SEd Schouten 		        wd->wd_loadav[2] / 100.0);
258e7cb5ef7SJuli Mallett 	}
259e7cb5ef7SJuli Mallett 	free(hs);
260e7cb5ef7SJuli Mallett 	hs = NULL;
261e7cb5ef7SJuli Mallett }
262e7cb5ef7SJuli Mallett 
2639b50d902SRodney W. Grimes /* Number of users comparison. */
2649b50d902SRodney W. Grimes int
265a827060aSMark Murray ucmp(const void *a1, const void *a2)
2669b50d902SRodney W. Grimes {
2679b50d902SRodney W. Grimes 	if (ISDOWN(HS(a1)))
2689b50d902SRodney W. Grimes 		if (ISDOWN(HS(a2)))
2699b50d902SRodney W. Grimes 			return (tcmp(a1, a2));
2709b50d902SRodney W. Grimes 		else
2719b50d902SRodney W. Grimes 			return (rflg);
2729b50d902SRodney W. Grimes 	else if (ISDOWN(HS(a2)))
2739b50d902SRodney W. Grimes 		return (-rflg);
2749b50d902SRodney W. Grimes 	else
2759b50d902SRodney W. Grimes 		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
2769b50d902SRodney W. Grimes }
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes /* Uptime comparison. */
2799b50d902SRodney W. Grimes int
280a827060aSMark Murray tcmp(const void *a1, const void *a2)
2819b50d902SRodney W. Grimes {
2829b50d902SRodney W. Grimes 	return (rflg * (
28373b018e3SEd Schouten 		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd.wd_recvtime - now
28473b018e3SEd Schouten 		    : HS(a2)->hs_wd.wd_sendtime - HS(a2)->hs_wd.wd_boottime)
2859b50d902SRodney W. Grimes 		-
28673b018e3SEd Schouten 		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd.wd_recvtime - now
28773b018e3SEd Schouten 		    : HS(a1)->hs_wd.wd_sendtime - HS(a1)->hs_wd.wd_boottime)
2889b50d902SRodney W. Grimes 	));
2899b50d902SRodney W. Grimes }
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes void
292a827060aSMark Murray usage(void)
2939b50d902SRodney W. Grimes {
294f682f10cSRuslan Ermilov 	(void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");
2959b50d902SRodney W. Grimes 	exit(1);
2969b50d902SRodney W. Grimes }
297