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