Commit | Line | Data |
---|---|---|
be0e2d23 | 1 | #ifndef lint |
7123e2dc | 2 | static char sccsid[] = "@(#)ruptime.c 4.14 (Berkeley) 83/07/01"; |
be0e2d23 BJ |
3 | #endif |
4 | ||
ed0072ae | 5 | #include <sys/param.h> |
be0e2d23 | 6 | #include <stdio.h> |
7123e2dc SL |
7 | #include <sys/dir.h> |
8 | #include "../etc/rwhod/rwhod.h" | |
be0e2d23 BJ |
9 | |
10 | DIR *etc; | |
11 | ||
12 | #define NHOSTS 100 | |
13 | int nhosts; | |
14 | struct hs { | |
15 | struct whod *hs_wd; | |
16 | int hs_nusers; | |
17 | } hs[NHOSTS]; | |
18 | struct whod awhod; | |
508c25be | 19 | int hscmp(), ucmp(), lcmp(), tcmp(); |
be0e2d23 BJ |
20 | |
21 | #define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we)) | |
fe77431f | 22 | #define RWHODIR "/usr/spool/rwho" |
be0e2d23 BJ |
23 | |
24 | char *interval(); | |
25 | int now; | |
26 | char *malloc(), *sprintf(); | |
27 | int aflg; | |
28 | ||
508c25be EC |
29 | #define down(h) (now - (h)->hs_wd->wd_recvtime > 5 * 60) |
30 | ||
be0e2d23 BJ |
31 | main(argc, argv) |
32 | int argc; | |
33 | char **argv; | |
34 | { | |
35 | struct direct *dp; | |
36 | int f, i, t; | |
37 | char buf[BUFSIZ]; int cc; | |
38 | register struct hs *hsp = hs; | |
39 | register struct whod *wd; | |
40 | register struct whoent *we; | |
acf8f1b2 | 41 | int maxloadav = 0; |
508c25be | 42 | int (*cmp)() = hscmp; |
be0e2d23 BJ |
43 | |
44 | time(&t); | |
45 | argc--, argv++; | |
46 | again: | |
9e120c5f | 47 | if (argc && !strcmp(*argv, "-a")) { |
be0e2d23 BJ |
48 | aflg++; |
49 | argc--, argv++; | |
50 | goto again; | |
51 | } | |
9e120c5f | 52 | if (argc && !strcmp(*argv, "-l")) { |
508c25be EC |
53 | cmp = lcmp; |
54 | argc--, argv++; | |
55 | goto again; | |
56 | } | |
9e120c5f | 57 | if (argc && !strcmp(*argv, "-u")) { |
508c25be EC |
58 | cmp = ucmp; |
59 | argc--, argv++; | |
60 | goto again; | |
61 | } | |
9e120c5f | 62 | if (argc && !strcmp(*argv, "-t")) { |
508c25be EC |
63 | cmp = tcmp; |
64 | argc--, argv++; | |
65 | goto again; | |
66 | } | |
a82aa56a SL |
67 | if (chdir(RWHODIR) < 0) { |
68 | perror(RWHODIR); | |
be0e2d23 BJ |
69 | exit(1); |
70 | } | |
71 | etc = opendir("."); | |
72 | if (etc == NULL) { | |
73 | perror("/etc"); | |
74 | exit(1); | |
75 | } | |
76 | while (dp = readdir(etc)) { | |
77 | if (dp->d_ino == 0) | |
78 | continue; | |
79 | if (strncmp(dp->d_name, "whod.", 5)) | |
80 | continue; | |
81 | if (nhosts == NHOSTS) { | |
82 | fprintf(stderr, "too many hosts\n"); | |
83 | exit(1); | |
84 | } | |
85 | f = open(dp->d_name, 0); | |
86 | if (f > 0) { | |
87 | cc = read(f, buf, BUFSIZ); | |
88 | if (cc >= WHDRSIZE) { | |
89 | hsp->hs_wd = (struct whod *)malloc(WHDRSIZE); | |
90 | wd = (struct whod *)buf; | |
91 | bcopy(buf, hsp->hs_wd, WHDRSIZE); | |
92 | hsp->hs_nusers = 0; | |
acf8f1b2 BJ |
93 | for (i = 0; i < 2; i++) |
94 | if (wd->wd_loadav[i] > maxloadav) | |
95 | maxloadav = wd->wd_loadav[i]; | |
be0e2d23 BJ |
96 | we = (struct whoent *)(buf+cc); |
97 | while (--we >= wd->wd_we) | |
98 | if (aflg || we->we_idle < 3600) | |
99 | hsp->hs_nusers++; | |
100 | nhosts++; hsp++; | |
101 | } | |
102 | } | |
103 | (void) close(f); | |
104 | } | |
be0e2d23 | 105 | (void) time(&now); |
508c25be | 106 | qsort((char *)hs, nhosts, sizeof (hs[0]), cmp); |
be0e2d23 BJ |
107 | if (nhosts == 0) { |
108 | printf("no hosts!?!\n"); | |
109 | exit(1); | |
110 | } | |
111 | for (i = 0; i < nhosts; i++) { | |
112 | hsp = &hs[i]; | |
508c25be | 113 | if (down(hsp)) { |
cad6a03e | 114 | printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname, |
be0e2d23 BJ |
115 | interval(now - hsp->hs_wd->wd_recvtime, "down")); |
116 | continue; | |
117 | } | |
cad6a03e | 118 | printf("%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n", |
be0e2d23 BJ |
119 | hsp->hs_wd->wd_hostname, |
120 | interval(hsp->hs_wd->wd_sendtime - | |
de3b21e8 | 121 | hsp->hs_wd->wd_boottime, " up"), |
be0e2d23 BJ |
122 | hsp->hs_nusers, |
123 | hsp->hs_nusers == 1 ? ", " : "s,", | |
acf8f1b2 BJ |
124 | maxloadav >= 1000 ? 5 : 4, |
125 | hsp->hs_wd->wd_loadav[0] / 100.0, | |
126 | maxloadav >= 1000 ? 5 : 4, | |
127 | hsp->hs_wd->wd_loadav[1] / 100.0, | |
128 | maxloadav >= 1000 ? 5 : 4, | |
129 | hsp->hs_wd->wd_loadav[2] / 100.0); | |
be0e2d23 BJ |
130 | cfree(hsp->hs_wd); |
131 | } | |
132 | exit(0); | |
133 | } | |
134 | ||
135 | char * | |
136 | interval(time, updown) | |
137 | int time; | |
138 | char *updown; | |
139 | { | |
140 | static char resbuf[32]; | |
141 | int days, hours, minutes; | |
142 | ||
143 | if (time < 0 || time > 3*30*24*60*60) { | |
144 | (void) sprintf(resbuf, " %s ??:??", updown); | |
145 | return (resbuf); | |
146 | } | |
147 | minutes = (time + 59) / 60; /* round to minutes */ | |
148 | hours = minutes / 60; minutes %= 60; | |
149 | days = hours / 24; hours %= 24; | |
150 | if (days) | |
151 | (void) sprintf(resbuf, "%s %2d+%02d:%02d", | |
152 | updown, days, hours, minutes); | |
153 | else | |
1518ea81 | 154 | (void) sprintf(resbuf, "%s %2d:%02d", |
be0e2d23 BJ |
155 | updown, hours, minutes); |
156 | return (resbuf); | |
157 | } | |
158 | ||
159 | hscmp(h1, h2) | |
160 | struct hs *h1, *h2; | |
161 | { | |
162 | ||
163 | return (strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname)); | |
164 | } | |
508c25be EC |
165 | |
166 | /* | |
167 | * Compare according to load average. | |
168 | */ | |
169 | lcmp(h1, h2) | |
170 | struct hs *h1, *h2; | |
171 | { | |
172 | ||
173 | if (down(h1)) | |
174 | if (down(h2)) | |
175 | return (tcmp(h1, h2)); | |
176 | else | |
177 | return (1); | |
178 | else if (down(h2)) | |
179 | return (-1); | |
180 | else | |
181 | return (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]); | |
182 | } | |
183 | ||
184 | /* | |
185 | * Compare according to number of users. | |
186 | */ | |
187 | ucmp(h1, h2) | |
188 | struct hs *h1, *h2; | |
189 | { | |
190 | ||
191 | if (down(h1)) | |
192 | if (down(h2)) | |
193 | return (tcmp(h1, h2)); | |
194 | else | |
195 | return (1); | |
196 | else if (down(h2)) | |
197 | return (-1); | |
198 | else | |
199 | return (h2->hs_nusers - h1->hs_nusers); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Compare according to uptime. | |
204 | */ | |
205 | tcmp(h1, h2) | |
206 | struct hs *h1, *h2; | |
207 | { | |
208 | long t1, t2; | |
209 | ||
210 | return ( | |
211 | (down(h2) ? h2->hs_wd->wd_recvtime - now | |
de3b21e8 | 212 | : h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime) |
508c25be EC |
213 | - |
214 | (down(h1) ? h1->hs_wd->wd_recvtime - now | |
de3b21e8 | 215 | : h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime) |
508c25be EC |
216 | ); |
217 | } |