xref: /freebsd/usr.bin/ruptime/ruptime.c (revision ce4946daa5ce852d28008dac492029500ab2ee95)
1 /*
2  * Copyright (c) 1983, 1993, 1994
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)ruptime.c	8.2 (Berkeley) 4/5/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 
50 #include <protocols/rwhod.h>
51 
52 #include <dirent.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.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 #define LEFTEARTH(h)		(now - (h)->hs_wd->wd_recvtime > 4*24*60*60)
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 __P((const void *, const void *));
76 char	*interval __P((time_t, char *));
77 int	 lcmp __P((const void *, const void *));
78 void	 morehosts __P((void));
79 int	 tcmp __P((const void *, const void *));
80 int	 ucmp __P((const void *, const void *));
81 void	 usage __P((void));
82 
83 int
84 main(argc, argv)
85 	int argc;
86 	char **argv;
87 {
88 	struct dirent *dp;
89 	struct hs *hsp;
90 	struct whod *wd;
91 	struct whoent *we;
92 	DIR *dirp;
93 	size_t hspace;
94 	int aflg, cc, ch, fd, i, maxloadav;
95 	char buf[sizeof(struct whod)];
96 	int (*cmp) __P((const void *, const void *));
97 
98 	aflg = 0;
99 	cmp = hscmp;
100 	while ((ch = getopt(argc, argv, "alrut")) != -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, NULL);
146 			hsp = hs + nhosts;
147 		}
148 
149 		if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
150 			err(1, NULL);
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(1, "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 (LEFTEARTH(hsp))
172 			continue;
173 		if (ISDOWN(hsp)) {
174 			(void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
175 			    interval(now - hsp->hs_wd->wd_recvtime, "down"));
176 			continue;
177 		}
178 		(void)printf(
179 		    "%-12.12s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
180 		    hsp->hs_wd->wd_hostname,
181 		    interval((time_t)hsp->hs_wd->wd_sendtime -
182 			(time_t)hsp->hs_wd->wd_boottime, "  up"),
183 		    hsp->hs_nusers,
184 		    hsp->hs_nusers == 1 ? ", " : "s,",
185 		    maxloadav >= 1000 ? 5 : 4,
186 			hsp->hs_wd->wd_loadav[0] / 100.0,
187 		    maxloadav >= 1000 ? 5 : 4,
188 		        hsp->hs_wd->wd_loadav[1] / 100.0,
189 		    maxloadav >= 1000 ? 5 : 4,
190 		        hsp->hs_wd->wd_loadav[2] / 100.0);
191 	}
192 	exit(0);
193 }
194 
195 char *
196 interval(tval, updown)
197 	time_t tval;
198 	char *updown;
199 {
200 	static char resbuf[32];
201 	int days, hours, minutes;
202 
203 	if (tval < 0) {
204 		(void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
205 		return (resbuf);
206 	}
207 						/* round to minutes. */
208 	minutes = (tval + (60 - 1)) / 60;
209 	hours = minutes / 60;
210 	minutes %= 60;
211 	days = hours / 24;
212 	hours %= 24;
213 	if (days)
214 		(void)snprintf(resbuf, sizeof(resbuf),
215 		    "%s %3d+%02d:%02d", updown, days, hours, minutes);
216 	else
217 		(void)snprintf(resbuf, sizeof(resbuf),
218 		    "%s     %2d:%02d", updown, hours, minutes);
219 	return (resbuf);
220 }
221 
222 #define	HS(a)	((struct hs *)(a))
223 
224 /* Alphabetical comparison. */
225 int
226 hscmp(a1, a2)
227 	const void *a1, *a2;
228 {
229 	return (rflg *
230 	    strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
231 }
232 
233 /* Load average comparison. */
234 int
235 lcmp(a1, a2)
236 	const void *a1, *a2;
237 {
238 	if (ISDOWN(HS(a1)))
239 		if (ISDOWN(HS(a2)))
240 			return (tcmp(a1, a2));
241 		else
242 			return (rflg);
243 	else if (ISDOWN(HS(a2)))
244 		return (-rflg);
245 	else
246 		return (rflg *
247 		   (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
248 }
249 
250 /* Number of users comparison. */
251 int
252 ucmp(a1, a2)
253 	const void *a1, *a2;
254 {
255 	if (ISDOWN(HS(a1)))
256 		if (ISDOWN(HS(a2)))
257 			return (tcmp(a1, a2));
258 		else
259 			return (rflg);
260 	else if (ISDOWN(HS(a2)))
261 		return (-rflg);
262 	else
263 		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
264 }
265 
266 /* Uptime comparison. */
267 int
268 tcmp(a1, a2)
269 	const void *a1, *a2;
270 {
271 	return (rflg * (
272 		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
273 		    : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
274 		-
275 		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
276 		    : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
277 	));
278 }
279 
280 void
281 usage()
282 {
283 	(void)fprintf(stderr, "usage: ruptime [-alrut]\n");
284 	exit(1);
285 }
286