xref: /freebsd/usr.bin/ruptime/ruptime.c (revision 5d53f683)
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  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
345d53f683SMarc G. Fournier /* $Id: ruptime.c,v 1.6 1995/09/08 20:33:21 jkh Exp $ */
353bd85a23SJoerg Wunsch 
369b50d902SRodney W. Grimes #ifndef lint
379b50d902SRodney W. Grimes static char copyright[] =
389b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993, 1994\n\
399b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
409b50d902SRodney W. Grimes #endif /* not lint */
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #ifndef lint
439b50d902SRodney W. Grimes static char sccsid[] = "@(#)ruptime.c	8.2 (Berkeley) 4/5/94";
449b50d902SRodney W. Grimes #endif /* not lint */
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <sys/param.h>
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <protocols/rwhod.h>
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <dirent.h>
519b50d902SRodney W. Grimes #include <err.h>
529b50d902SRodney W. Grimes #include <errno.h>
539b50d902SRodney W. Grimes #include <fcntl.h>
549b50d902SRodney W. Grimes #include <stdio.h>
559b50d902SRodney W. Grimes #include <stdlib.h>
569b50d902SRodney W. Grimes #include <string.h>
579b50d902SRodney W. Grimes #include <time.h>
589b50d902SRodney W. Grimes #include <unistd.h>
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes struct hs {
619b50d902SRodney W. Grimes 	struct	whod *hs_wd;
629b50d902SRodney W. Grimes 	int	hs_nusers;
639b50d902SRodney W. Grimes } *hs;
649b50d902SRodney W. Grimes struct	whod awhod;
655d53f683SMarc G. Fournier #define LEFTEARTH(h)		(now - (h)->hs_wd->wd_recvtime > 4*24*60*60)
669b50d902SRodney W. Grimes #define	ISDOWN(h)		(now - (h)->hs_wd->wd_recvtime > 11 * 60)
679b50d902SRodney W. Grimes #define	WHDRSIZE	(sizeof (awhod) - sizeof (awhod.wd_we))
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes size_t nhosts;
709b50d902SRodney W. Grimes time_t now;
719b50d902SRodney W. Grimes int rflg = 1;
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes int	 hscmp __P((const void *, const void *));
749b50d902SRodney W. Grimes char	*interval __P((time_t, char *));
759b50d902SRodney W. Grimes int	 lcmp __P((const void *, const void *));
769b50d902SRodney W. Grimes void	 morehosts __P((void));
779b50d902SRodney W. Grimes int	 tcmp __P((const void *, const void *));
789b50d902SRodney W. Grimes int	 ucmp __P((const void *, const void *));
799b50d902SRodney W. Grimes void	 usage __P((void));
809b50d902SRodney W. Grimes 
819b50d902SRodney W. Grimes int
829b50d902SRodney W. Grimes main(argc, argv)
839b50d902SRodney W. Grimes 	int argc;
849b50d902SRodney W. Grimes 	char **argv;
859b50d902SRodney W. Grimes {
869b50d902SRodney W. Grimes 	extern int optind;
879b50d902SRodney W. Grimes 	struct dirent *dp;
889b50d902SRodney W. Grimes 	struct hs *hsp;
899b50d902SRodney W. Grimes 	struct whod *wd;
909b50d902SRodney W. Grimes 	struct whoent *we;
919b50d902SRodney W. Grimes 	DIR *dirp;
929b50d902SRodney W. Grimes 	size_t hspace;
939b50d902SRodney W. Grimes 	int aflg, cc, ch, fd, i, maxloadav;
949b50d902SRodney W. Grimes 	char buf[sizeof(struct whod)];
959b50d902SRodney W. Grimes 	int (*cmp) __P((const void *, const void *));
969b50d902SRodney W. Grimes 
979b50d902SRodney W. Grimes 	aflg = 0;
989b50d902SRodney W. Grimes 	cmp = hscmp;
999b50d902SRodney W. Grimes 	while ((ch = getopt(argc, argv, "alrut")) != EOF)
1009b50d902SRodney W. Grimes 		switch (ch) {
1019b50d902SRodney W. Grimes 		case 'a':
1029b50d902SRodney W. Grimes 			aflg = 1;
1039b50d902SRodney W. Grimes 			break;
1049b50d902SRodney W. Grimes 		case 'l':
1059b50d902SRodney W. Grimes 			cmp = lcmp;
1069b50d902SRodney W. Grimes 			break;
1079b50d902SRodney W. Grimes 		case 'r':
1089b50d902SRodney W. Grimes 			rflg = -1;
1099b50d902SRodney W. Grimes 			break;
1109b50d902SRodney W. Grimes 		case 't':
1119b50d902SRodney W. Grimes 			cmp = tcmp;
1129b50d902SRodney W. Grimes 			break;
1139b50d902SRodney W. Grimes 		case 'u':
1149b50d902SRodney W. Grimes 			cmp = ucmp;
1159b50d902SRodney W. Grimes 			break;
1169b50d902SRodney W. Grimes 		default:
1179b50d902SRodney W. Grimes 			usage();
1189b50d902SRodney W. Grimes 		}
1199b50d902SRodney W. Grimes 	argc -= optind;
1209b50d902SRodney W. Grimes 	argv += optind;
1219b50d902SRodney W. Grimes 
1229b50d902SRodney W. Grimes 	if (argc != 0)
1239b50d902SRodney W. Grimes 		usage();
1249b50d902SRodney W. Grimes 
1259b50d902SRodney W. Grimes 	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
1269b50d902SRodney W. Grimes 		err(1, "%s", _PATH_RWHODIR);
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes 	maxloadav = -1;
1299b50d902SRodney W. Grimes 	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
1309b50d902SRodney W. Grimes 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
1319b50d902SRodney W. Grimes 			continue;
1329b50d902SRodney W. Grimes 		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
1339b50d902SRodney W. Grimes 			warn("%s", dp->d_name);
1349b50d902SRodney W. Grimes 			continue;
1359b50d902SRodney W. Grimes 		}
1369b50d902SRodney W. Grimes 		cc = read(fd, buf, sizeof(struct whod));
1379b50d902SRodney W. Grimes 		(void)close(fd);
1389b50d902SRodney W. Grimes 
1399b50d902SRodney W. Grimes 		if (cc < WHDRSIZE)
1409b50d902SRodney W. Grimes 			continue;
1419b50d902SRodney W. Grimes 		if (nhosts == hspace) {
1429b50d902SRodney W. Grimes 			if ((hs =
1439b50d902SRodney W. Grimes 			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
1449b50d902SRodney W. Grimes 				err(1, NULL);
1459b50d902SRodney W. Grimes 			hsp = hs + nhosts;
1469b50d902SRodney W. Grimes 		}
1479b50d902SRodney W. Grimes 
1489b50d902SRodney W. Grimes 		if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
1499b50d902SRodney W. Grimes 			err(1, NULL);
1509b50d902SRodney W. Grimes 		memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes 		for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
1539b50d902SRodney W. Grimes 			if (wd->wd_loadav[i] > maxloadav)
1549b50d902SRodney W. Grimes 				maxloadav = wd->wd_loadav[i];
1559b50d902SRodney W. Grimes 
1569b50d902SRodney W. Grimes 		for (hsp->hs_nusers = 0,
1579b50d902SRodney W. Grimes 		    we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
1589b50d902SRodney W. Grimes 			if (aflg || we->we_idle < 3600)
1599b50d902SRodney W. Grimes 				++hsp->hs_nusers;
1609b50d902SRodney W. Grimes 		++hsp;
1619b50d902SRodney W. Grimes 		++nhosts;
1629b50d902SRodney W. Grimes 	}
1639b50d902SRodney W. Grimes 	if (nhosts == 0)
164b7935a74SJordan K. Hubbard 		errx(1, "no hosts in %s.", _PATH_RWHODIR);
1659b50d902SRodney W. Grimes 
1669b50d902SRodney W. Grimes 	(void)time(&now);
1679b50d902SRodney W. Grimes 	qsort(hs, nhosts, sizeof(hs[0]), cmp);
1689b50d902SRodney W. Grimes 	for (i = 0; i < nhosts; i++) {
1699b50d902SRodney W. Grimes 		hsp = &hs[i];
1705d53f683SMarc G. Fournier 		if (LEFTEARTH(hsp))
1715d53f683SMarc G. Fournier 			continue;
1729b50d902SRodney W. Grimes 		if (ISDOWN(hsp)) {
1739b50d902SRodney W. Grimes 			(void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
1749b50d902SRodney W. Grimes 			    interval(now - hsp->hs_wd->wd_recvtime, "down"));
1759b50d902SRodney W. Grimes 			continue;
1769b50d902SRodney W. Grimes 		}
1779b50d902SRodney W. Grimes 		(void)printf(
1789b50d902SRodney W. Grimes 		    "%-12.12s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
1799b50d902SRodney W. Grimes 		    hsp->hs_wd->wd_hostname,
1809b50d902SRodney W. Grimes 		    interval((time_t)hsp->hs_wd->wd_sendtime -
1819b50d902SRodney W. Grimes 			(time_t)hsp->hs_wd->wd_boottime, "  up"),
1829b50d902SRodney W. Grimes 		    hsp->hs_nusers,
1839b50d902SRodney W. Grimes 		    hsp->hs_nusers == 1 ? ", " : "s,",
1849b50d902SRodney W. Grimes 		    maxloadav >= 1000 ? 5 : 4,
1859b50d902SRodney W. Grimes 			hsp->hs_wd->wd_loadav[0] / 100.0,
1869b50d902SRodney W. Grimes 		    maxloadav >= 1000 ? 5 : 4,
1879b50d902SRodney W. Grimes 		        hsp->hs_wd->wd_loadav[1] / 100.0,
1889b50d902SRodney W. Grimes 		    maxloadav >= 1000 ? 5 : 4,
1899b50d902SRodney W. Grimes 		        hsp->hs_wd->wd_loadav[2] / 100.0);
1909b50d902SRodney W. Grimes 	}
1919b50d902SRodney W. Grimes 	exit(0);
1929b50d902SRodney W. Grimes }
1939b50d902SRodney W. Grimes 
1949b50d902SRodney W. Grimes char *
1959b50d902SRodney W. Grimes interval(tval, updown)
1969b50d902SRodney W. Grimes 	time_t tval;
1979b50d902SRodney W. Grimes 	char *updown;
1989b50d902SRodney W. Grimes {
1999b50d902SRodney W. Grimes 	static char resbuf[32];
2009b50d902SRodney W. Grimes 	int days, hours, minutes;
2019b50d902SRodney W. Grimes 
202fdb7c7dcSJoerg Wunsch 	if (tval < 0) {
2039b50d902SRodney W. Grimes 		(void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
2049b50d902SRodney W. Grimes 		return (resbuf);
2059b50d902SRodney W. Grimes 	}
2069b50d902SRodney W. Grimes 						/* round to minutes. */
207656dcd43SGarrett Wollman 	minutes = (tval + (60 - 1)) / 60;
208656dcd43SGarrett Wollman 	hours = minutes / 60;
209656dcd43SGarrett Wollman 	minutes %= 60;
210656dcd43SGarrett Wollman 	days = hours / 24;
211656dcd43SGarrett Wollman 	hours %= 24;
2129b50d902SRodney W. Grimes 	if (days)
2139b50d902SRodney W. Grimes 		(void)snprintf(resbuf, sizeof(resbuf),
2149b50d902SRodney W. Grimes 		    "%s %2d+%02d:%02d", updown, days, hours, minutes);
2159b50d902SRodney W. Grimes 	else
2169b50d902SRodney W. Grimes 		(void)snprintf(resbuf, sizeof(resbuf),
2179b50d902SRodney W. Grimes 		    "%s    %2d:%02d", updown, hours, minutes);
2189b50d902SRodney W. Grimes 	return (resbuf);
2199b50d902SRodney W. Grimes }
2209b50d902SRodney W. Grimes 
2219b50d902SRodney W. Grimes #define	HS(a)	((struct hs *)(a))
2229b50d902SRodney W. Grimes 
2239b50d902SRodney W. Grimes /* Alphabetical comparison. */
2249b50d902SRodney W. Grimes int
2259b50d902SRodney W. Grimes hscmp(a1, a2)
2269b50d902SRodney W. Grimes 	const void *a1, *a2;
2279b50d902SRodney W. Grimes {
2289b50d902SRodney W. Grimes 	return (rflg *
2299b50d902SRodney W. Grimes 	    strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
2309b50d902SRodney W. Grimes }
2319b50d902SRodney W. Grimes 
2329b50d902SRodney W. Grimes /* Load average comparison. */
2339b50d902SRodney W. Grimes int
2349b50d902SRodney W. Grimes lcmp(a1, a2)
2359b50d902SRodney W. Grimes 	const void *a1, *a2;
2369b50d902SRodney W. Grimes {
2379b50d902SRodney W. Grimes 	if (ISDOWN(HS(a1)))
2389b50d902SRodney W. Grimes 		if (ISDOWN(HS(a2)))
2399b50d902SRodney W. Grimes 			return (tcmp(a1, a2));
2409b50d902SRodney W. Grimes 		else
2419b50d902SRodney W. Grimes 			return (rflg);
2429b50d902SRodney W. Grimes 	else if (ISDOWN(HS(a2)))
2439b50d902SRodney W. Grimes 		return (-rflg);
2449b50d902SRodney W. Grimes 	else
2459b50d902SRodney W. Grimes 		return (rflg *
2469b50d902SRodney W. Grimes 		   (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
2479b50d902SRodney W. Grimes }
2489b50d902SRodney W. Grimes 
2499b50d902SRodney W. Grimes /* Number of users comparison. */
2509b50d902SRodney W. Grimes int
2519b50d902SRodney W. Grimes ucmp(a1, a2)
2529b50d902SRodney W. Grimes 	const void *a1, *a2;
2539b50d902SRodney W. Grimes {
2549b50d902SRodney W. Grimes 	if (ISDOWN(HS(a1)))
2559b50d902SRodney W. Grimes 		if (ISDOWN(HS(a2)))
2569b50d902SRodney W. Grimes 			return (tcmp(a1, a2));
2579b50d902SRodney W. Grimes 		else
2589b50d902SRodney W. Grimes 			return (rflg);
2599b50d902SRodney W. Grimes 	else if (ISDOWN(HS(a2)))
2609b50d902SRodney W. Grimes 		return (-rflg);
2619b50d902SRodney W. Grimes 	else
2629b50d902SRodney W. Grimes 		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
2639b50d902SRodney W. Grimes }
2649b50d902SRodney W. Grimes 
2659b50d902SRodney W. Grimes /* Uptime comparison. */
2669b50d902SRodney W. Grimes int
2679b50d902SRodney W. Grimes tcmp(a1, a2)
2689b50d902SRodney W. Grimes 	const void *a1, *a2;
2699b50d902SRodney W. Grimes {
2709b50d902SRodney W. Grimes 	return (rflg * (
2719b50d902SRodney W. Grimes 		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
2729b50d902SRodney W. Grimes 		    : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
2739b50d902SRodney W. Grimes 		-
2749b50d902SRodney W. Grimes 		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
2759b50d902SRodney W. Grimes 		    : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
2769b50d902SRodney W. Grimes 	));
2779b50d902SRodney W. Grimes }
2789b50d902SRodney W. Grimes 
2799b50d902SRodney W. Grimes void
2809b50d902SRodney W. Grimes usage()
2819b50d902SRodney W. Grimes {
2829b50d902SRodney W. Grimes 	(void)fprintf(stderr, "usage: ruptime [-alrut]\n");
2839b50d902SRodney W. Grimes 	exit(1);
2849b50d902SRodney W. Grimes }
285