xref: /netbsd-src/usr.bin/ruptime/ruptime.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: ruptime.c,v 1.10 2001/01/05 07:14:46 mjl Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #endif /* not lint */
41 
42 #ifndef lint
43 /*static char sccsid[] = "from: @(#)ruptime.c	8.2 (Berkeley) 4/5/94";*/
44 __RCSID("$NetBSD: ruptime.c,v 1.10 2001/01/05 07:14:46 mjl Exp $");
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 
49 #include <protocols/rwhod.h>
50 
51 #include <dirent.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59 #include <tzfile.h>
60 #include <unistd.h>
61 
62 struct hs {
63 	struct	whod *hs_wd;
64 	int	hs_nusers;
65 } *hs;
66 struct	whod awhod;
67 
68 #define	ISDOWN(h)		(now - (h)->hs_wd->wd_recvtime > 11 * 60)
69 #define	WHDRSIZE	(sizeof (awhod) - sizeof (awhod.wd_we))
70 
71 size_t nhosts;
72 time_t now;
73 int rflg = 1;
74 
75 int	 hscmp(const void *, const void *);
76 char	*interval(time_t, char *);
77 int	 lcmp(const void *, const void *);
78 int	 main(int, char **);
79 void	 morehosts(void);
80 int	 tcmp(const void *, const void *);
81 int	 ucmp(const void *, const void *);
82 void	 usage(void);
83 
84 int
85 main(int argc, char **argv)
86 {
87 	struct dirent *dp;
88 	struct hs *hsp;
89 	struct whod *wd;
90 	struct whoent *we;
91 	DIR *dirp;
92 	size_t hspace;
93 	int aflg, cc, ch, fd, i, maxloadav;
94 	char buf[sizeof(struct whod)];
95 	int (*cmp)(const void *, const void *);
96 
97 	hsp = NULL;
98 	aflg = 0;
99 	cmp = hscmp;
100 	while ((ch = getopt(argc, argv, "alrtu")) != -1)
101 		switch (ch) {
102 		case 'a':
103 			aflg = 1;
104 			break;
105 		case 'l':
106 			cmp = lcmp;
107 			break;
108 		case 'r':
109 			rflg = -1;
110 			break;
111 		case 't':
112 			cmp = tcmp;
113 			break;
114 		case 'u':
115 			cmp = ucmp;
116 			break;
117 		default:
118 			usage();
119 		}
120 	argc -= optind;
121 	argv += optind;
122 
123 	if (argc != 0)
124 		usage();
125 
126 	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
127 		err(1, "%s", _PATH_RWHODIR);
128 
129 	maxloadav = -1;
130 	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
131 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
132 			continue;
133 		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
134 			warn("%s", dp->d_name);
135 			continue;
136 		}
137 		cc = read(fd, buf, sizeof(struct whod));
138 		(void)close(fd);
139 
140 		if (cc < WHDRSIZE)
141 			continue;
142 		if (nhosts == hspace) {
143 			if ((hs =
144 			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
145 				err(1, "realloc");
146 			hsp = hs + nhosts;
147 		}
148 
149 		if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
150 			err(1, "malloc");
151 		memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
152 
153 		for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
154 			if (wd->wd_loadav[i] > maxloadav)
155 				maxloadav = wd->wd_loadav[i];
156 
157 		for (hsp->hs_nusers = 0,
158 		    we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
159 			if (aflg || we->we_idle < 3600)
160 				++hsp->hs_nusers;
161 		++hsp;
162 		++nhosts;
163 	}
164 	if (nhosts == 0)
165 		errx(0, "no hosts in %s", _PATH_RWHODIR);
166 
167 	(void)time(&now);
168 	qsort(hs, nhosts, sizeof (hs[0]), cmp);
169 	for (i = 0; i < nhosts; i++) {
170 		hsp = &hs[i];
171 		if (ISDOWN(hsp)) {
172 			(void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
173 			    interval(now - hsp->hs_wd->wd_recvtime, "down"));
174 			continue;
175 		}
176 		(void)printf(
177 		    "%-12.12s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
178 		    hsp->hs_wd->wd_hostname,
179 		    interval((time_t)hsp->hs_wd->wd_sendtime -
180 			(time_t)hsp->hs_wd->wd_boottime, "  up"),
181 		    hsp->hs_nusers,
182 		    hsp->hs_nusers == 1 ? ", " : "s,",
183 		    maxloadav >= 1000 ? 5 : 4,
184 			hsp->hs_wd->wd_loadav[0] / 100.0,
185 		    maxloadav >= 1000 ? 5 : 4,
186 		        hsp->hs_wd->wd_loadav[1] / 100.0,
187 		    maxloadav >= 1000 ? 5 : 4,
188 		        hsp->hs_wd->wd_loadav[2] / 100.0);
189 	}
190 	exit(0);
191 }
192 
193 char *
194 interval(time_t tval, char *updown)
195 {
196 	static char resbuf[32];
197 	int days, hours, minutes;
198 
199 	if (tval < 0) {
200 		(void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
201 		return (resbuf);
202 	}
203 						/* round to minutes. */
204 	minutes = (tval + (SECSPERMIN - 1)) / SECSPERMIN;
205 	hours = minutes / MINSPERHOUR;
206 	minutes %= MINSPERHOUR;
207 	days = hours / HOURSPERDAY;
208 	hours %= HOURSPERDAY;
209 	if (days)
210 		(void)snprintf(resbuf, sizeof(resbuf),
211 		    "%s%4d+%02d:%02d", updown, days, hours, minutes);
212 	else
213 		(void)snprintf(resbuf, sizeof(resbuf),
214 		    "%s     %2d:%02d", updown, hours, minutes);
215 	return (resbuf);
216 }
217 
218 #define	HS(a)	((struct hs *)(a))
219 
220 /* Alphabetical comparison. */
221 int
222 hscmp(a1, a2)
223 	const void *a1, *a2;
224 {
225 	return (rflg *
226 	    strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
227 }
228 
229 /* Load average comparison. */
230 int
231 lcmp(const void *a1, const void *a2)
232 {
233 	if (ISDOWN(HS(a1))) {
234 		if (ISDOWN(HS(a2)))
235 			return (tcmp(a1, a2));
236 		else
237 			return (rflg);
238 	} else if (ISDOWN(HS(a2))) {
239 		return (-rflg);
240 	} else
241 		return (rflg *
242 		   (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
243 }
244 
245 /* Number of users comparison. */
246 int
247 ucmp(const void *a1, const void *a2)
248 {
249 	if (ISDOWN(HS(a1))) {
250 		if (ISDOWN(HS(a2)))
251 			return (tcmp(a1, a2));
252 		else
253 			return (rflg);
254 	} else if (ISDOWN(HS(a2))) {
255 		return (-rflg);
256 	} else
257 		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
258 }
259 
260 /* Uptime comparison. */
261 int
262 tcmp(const void *a1, const void *a2)
263 {
264 	return (rflg * (
265 		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
266 		    : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
267 		-
268 		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
269 		    : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
270 	));
271 }
272 
273 void
274 usage(void)
275 {
276 	(void)fprintf(stderr, "usage: ruptime [-alrtu]\n");
277 	exit(1);
278 }
279