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