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