2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)ruptime.c 8.2 (Berkeley) 4/5/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
49 #include <protocols/rwhod.h>
66 #define LEFTEARTH(h) (now - (h) > 4*24*60*60)
67 #define ISDOWN(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
68 #define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we))
75 int hscmp(const void *, const void *);
76 char *interval(time_t, const char *);
77 int lcmp(const void *, const void *);
79 void ruptime(const char *, int, int (*)(const void *, const void *));
80 int tcmp(const void *, const void *);
81 int ucmp(const void *, const void *);
85 main(int argc, char *argv[])
87 int (*cmp)(const void *, const void *);
92 while ((ch = getopt(argc, argv, "alrut")) != -1)
115 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
116 err(1, "%s", _PATH_RWHODIR);
118 ruptime(*argv, aflg, cmp);
119 while (*argv++ != NULL) {
122 ruptime(*argv, aflg, cmp);
128 interval(time_t tval, const char *updown)
130 static char resbuf[32];
131 int days, hours, minutes;
134 (void)snprintf(resbuf, sizeof(resbuf), " %s ??:??", updown);
137 /* round to minutes. */
138 minutes = (tval + (60 - 1)) / 60;
139 hours = minutes / 60;
144 (void)snprintf(resbuf, sizeof(resbuf),
145 "%s %3d+%02d:%02d", updown, days, hours, minutes);
147 (void)snprintf(resbuf, sizeof(resbuf),
148 "%s %2d:%02d", updown, hours, minutes);
152 #define HS(a) ((const struct hs *)(a))
154 /* Alphabetical comparison. */
156 hscmp(const void *a1, const void *a2)
159 strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
162 /* Load average comparison. */
164 lcmp(const void *a1, const void *a2)
168 return (tcmp(a1, a2));
171 else if (ISDOWN(HS(a2)))
175 (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
179 ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
185 const char *hostname;
186 char buf[sizeof(struct whod)];
187 int fd, i, maxloadav;
195 for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
196 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
198 if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
199 warn("%s", dp->d_name);
202 cc = read(fd, buf, sizeof(struct whod));
205 hostname = ((struct whod *)buf)->wd_hostname;
206 if (strcasecmp(hostname, host) != 0)
212 if (LEFTEARTH(((struct whod *)buf)->wd_recvtime))
214 if (nhosts == hspace) {
216 realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
221 if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
223 memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
225 for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
226 if (wd->wd_loadav[i] > maxloadav)
227 maxloadav = wd->wd_loadav[i];
229 for (hsp->hs_nusers = 0,
230 we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
231 if (aflg || we->we_idle < 3600)
238 errx(1, "no hosts in %s", _PATH_RWHODIR);
240 warnx("host %s not in %s", host, _PATH_RWHODIR);
243 qsort(hs, nhosts, sizeof(hs[0]), cmp);
244 for (i = 0; i < (int)nhosts; i++) {
247 (void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
248 interval(now - hsp->hs_wd->wd_recvtime, "down"));
252 "%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n",
253 hsp->hs_wd->wd_hostname,
254 interval((time_t)hsp->hs_wd->wd_sendtime -
255 (time_t)hsp->hs_wd->wd_boottime, " up"),
257 hsp->hs_nusers == 1 ? ", " : "s,",
258 maxloadav >= 1000 ? 5 : 4,
259 hsp->hs_wd->wd_loadav[0] / 100.0,
260 maxloadav >= 1000 ? 5 : 4,
261 hsp->hs_wd->wd_loadav[1] / 100.0,
262 maxloadav >= 1000 ? 5 : 4,
263 hsp->hs_wd->wd_loadav[2] / 100.0);
270 /* Number of users comparison. */
272 ucmp(const void *a1, const void *a2)
276 return (tcmp(a1, a2));
279 else if (ISDOWN(HS(a2)))
282 return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
285 /* Uptime comparison. */
287 tcmp(const void *a1, const void *a2)
290 (ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
291 : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
293 (ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
294 : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
301 (void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");