file reorg, pathnames.h, paths.h
[unix-history] / usr / src / usr.bin / ruptime / ruptime.c
CommitLineData
22e155fc 1/*
e8dcae5f
KB
2 * Copyright (c) 1983 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22e155fc
DF
16 */
17
18#ifndef lint
19char copyright[] =
e8dcae5f 20"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
22e155fc 21 All rights reserved.\n";
e8dcae5f 22#endif /* not lint */
22e155fc 23
be0e2d23 24#ifndef lint
435e8dff 25static char sccsid[] = "@(#)ruptime.c 5.6 (Berkeley) %G%";
e8dcae5f 26#endif /* not lint */
be0e2d23 27
ed0072ae 28#include <sys/param.h>
7123e2dc 29#include <sys/dir.h>
e8dcae5f 30#include <sys/file.h>
5a7285fa 31#include <protocols/rwhod.h>
e8dcae5f 32#include <stdio.h>
be0e2d23
BJ
33
34#define NHOSTS 100
35int nhosts;
e8dcae5f 36struct hs {
be0e2d23
BJ
37 struct whod *hs_wd;
38 int hs_nusers;
39} hs[NHOSTS];
40struct whod awhod;
be0e2d23
BJ
41
42#define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we))
e8dcae5f 43#define down(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
be0e2d23 44
e8dcae5f
KB
45time_t now;
46int rflg = 1;
47int hscmp(), ucmp(), lcmp(), tcmp();
508c25be 48
be0e2d23
BJ
49main(argc, argv)
50 int argc;
51 char **argv;
52{
e8dcae5f
KB
53 extern char *optarg;
54 extern int optind;
be0e2d23
BJ
55 register struct hs *hsp = hs;
56 register struct whod *wd;
57 register struct whoent *we;
e8dcae5f
KB
58 register DIR *dirp;
59 struct direct *dp;
60 int aflg, cc, ch, f, i, maxloadav;
61 char buf[sizeof(struct whod)];
508c25be 62 int (*cmp)() = hscmp;
e8dcae5f
KB
63 time_t time();
64 char *interval(), *malloc();
65
66 aflg = 0;
67 maxloadav = -1;
68 while ((ch = getopt(argc, argv, "alrut")) != EOF)
69 switch((char)ch) {
70 case 'a':
71 aflg = 1;
72 break;
73 case 'l':
74 cmp = lcmp;
75 break;
76 case 'r':
77 rflg = -1;
78 break;
79 case 't':
80 cmp = tcmp;
81 break;
82 case 'u':
83 cmp = ucmp;
84 break;
85 default:
86 fprintf(stderr, "usage: ruptime [-alrut]\n");
87 exit(1);
88 }
be0e2d23 89
435e8dff
KB
90 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
91 perror(_PATH_RWHODIR);
be0e2d23
BJ
92 exit(1);
93 }
b9484359 94 while (dp = readdir(dirp)) {
e8dcae5f 95 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
be0e2d23
BJ
96 continue;
97 if (nhosts == NHOSTS) {
e8dcae5f 98 fprintf(stderr, "ruptime: too many hosts\n");
be0e2d23
BJ
99 exit(1);
100 }
e8dcae5f 101 f = open(dp->d_name, O_RDONLY, 0);
be0e2d23 102 if (f > 0) {
a39120ab 103 cc = read(f, buf, sizeof(struct whod));
be0e2d23 104 if (cc >= WHDRSIZE) {
e8dcae5f 105 /* NOSTRICT */
be0e2d23
BJ
106 hsp->hs_wd = (struct whod *)malloc(WHDRSIZE);
107 wd = (struct whod *)buf;
e8dcae5f 108 bcopy(wd, hsp->hs_wd, WHDRSIZE);
be0e2d23 109 hsp->hs_nusers = 0;
acf8f1b2
BJ
110 for (i = 0; i < 2; i++)
111 if (wd->wd_loadav[i] > maxloadav)
112 maxloadav = wd->wd_loadav[i];
be0e2d23
BJ
113 we = (struct whoent *)(buf+cc);
114 while (--we >= wd->wd_we)
115 if (aflg || we->we_idle < 3600)
116 hsp->hs_nusers++;
117 nhosts++; hsp++;
118 }
e8dcae5f 119 (void)close(f);
be0e2d23 120 }
be0e2d23 121 }
e8dcae5f
KB
122 if (!nhosts) {
123 printf("ruptime: no hosts!?!\n");
be0e2d23
BJ
124 exit(1);
125 }
e8dcae5f
KB
126 qsort((char *)hs, nhosts, sizeof (hs[0]), cmp);
127 (void)time(&now);
be0e2d23
BJ
128 for (i = 0; i < nhosts; i++) {
129 hsp = &hs[i];
508c25be 130 if (down(hsp)) {
cad6a03e 131 printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
be0e2d23
BJ
132 interval(now - hsp->hs_wd->wd_recvtime, "down"));
133 continue;
134 }
cad6a03e 135 printf("%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n",
be0e2d23
BJ
136 hsp->hs_wd->wd_hostname,
137 interval(hsp->hs_wd->wd_sendtime -
de3b21e8 138 hsp->hs_wd->wd_boottime, " up"),
be0e2d23
BJ
139 hsp->hs_nusers,
140 hsp->hs_nusers == 1 ? ", " : "s,",
acf8f1b2
BJ
141 maxloadav >= 1000 ? 5 : 4,
142 hsp->hs_wd->wd_loadav[0] / 100.0,
143 maxloadav >= 1000 ? 5 : 4,
144 hsp->hs_wd->wd_loadav[1] / 100.0,
145 maxloadav >= 1000 ? 5 : 4,
146 hsp->hs_wd->wd_loadav[2] / 100.0);
be0e2d23
BJ
147 cfree(hsp->hs_wd);
148 }
149 exit(0);
150}
151
152char *
e8dcae5f
KB
153interval(tval, updown)
154 time_t tval;
be0e2d23
BJ
155 char *updown;
156{
157 static char resbuf[32];
158 int days, hours, minutes;
159
e8dcae5f
KB
160 if (tval < 0 || tval > 365*24*60*60) {
161 (void)sprintf(resbuf, " %s ??:??", updown);
162 return(resbuf);
be0e2d23 163 }
e8dcae5f 164 minutes = (tval + 59) / 60; /* round to minutes */
be0e2d23
BJ
165 hours = minutes / 60; minutes %= 60;
166 days = hours / 24; hours %= 24;
167 if (days)
e8dcae5f 168 (void)sprintf(resbuf, "%s %2d+%02d:%02d",
be0e2d23
BJ
169 updown, days, hours, minutes);
170 else
e8dcae5f 171 (void)sprintf(resbuf, "%s %2d:%02d",
be0e2d23 172 updown, hours, minutes);
e8dcae5f 173 return(resbuf);
be0e2d23
BJ
174}
175
176hscmp(h1, h2)
177 struct hs *h1, *h2;
178{
e8dcae5f 179 return(rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
be0e2d23 180}
508c25be
EC
181
182/*
183 * Compare according to load average.
184 */
185lcmp(h1, h2)
186 struct hs *h1, *h2;
187{
508c25be
EC
188 if (down(h1))
189 if (down(h2))
e8dcae5f 190 return(tcmp(h1, h2));
508c25be 191 else
e8dcae5f 192 return(rflg);
508c25be 193 else if (down(h2))
e8dcae5f 194 return(-rflg);
508c25be 195 else
e8dcae5f 196 return(rflg *
44bbb118 197 (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
508c25be
EC
198}
199
200/*
201 * Compare according to number of users.
202 */
203ucmp(h1, h2)
204 struct hs *h1, *h2;
205{
508c25be
EC
206 if (down(h1))
207 if (down(h2))
e8dcae5f 208 return(tcmp(h1, h2));
508c25be 209 else
e8dcae5f 210 return(rflg);
508c25be 211 else if (down(h2))
e8dcae5f 212 return(-rflg);
508c25be 213 else
e8dcae5f 214 return(rflg * (h2->hs_nusers - h1->hs_nusers));
508c25be
EC
215}
216
217/*
218 * Compare according to uptime.
219 */
220tcmp(h1, h2)
221 struct hs *h1, *h2;
222{
e8dcae5f 223 return(rflg * (
508c25be 224 (down(h2) ? h2->hs_wd->wd_recvtime - now
de3b21e8 225 : h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime)
508c25be
EC
226 -
227 (down(h1) ? h1->hs_wd->wd_recvtime - now
de3b21e8 228 : h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)
44bbb118 229 ));
508c25be 230}