]>
git.saurik.com Git - apple/network_cmds.git/blob - ruptime.tproj/ruptime.c
4a0d4e0083765ac930dd93f15d572d81bace5b24
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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,
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1983, 1993, 1994
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
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.
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
58 static char copyright
[] =
59 "@(#) Copyright (c) 1983, 1993, 1994\n\
60 The Regents of the University of California. All rights reserved.\n";
63 #include <sys/param.h>
65 #include <protocols/rwhod.h>
84 #define ISDOWN(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
85 #define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we))
91 int hscmp
__P((const void *, const void *));
92 char *interval
__P((time_t, char *));
93 int lcmp
__P((const void *, const void *));
94 void morehosts
__P((void));
95 int tcmp
__P((const void *, const void *));
96 int ucmp
__P((const void *, const void *));
97 void usage
__P((void));
111 int aflg
, cc
, ch
, fd
, i
, maxloadav
;
112 char buf
[sizeof(struct whod
)];
113 int (*cmp
) __P((const void *, const void *));
117 while ((ch
= getopt(argc
, argv
, "alrut")) != EOF
)
143 if (chdir(_PATH_RWHODIR
) || (dirp
= opendir(".")) == NULL
)
144 err(1, "%s", _PATH_RWHODIR
);
147 for (nhosts
= hspace
= 0; (dp
= readdir(dirp
)) != NULL
;) {
148 if (dp
->d_ino
== 0 || strncmp(dp
->d_name
, "whod.", 5))
150 if ((fd
= open(dp
->d_name
, O_RDONLY
, 0)) < 0) {
151 warn("%s", dp
->d_name
);
154 cc
= read(fd
, buf
, sizeof(struct whod
));
159 if (nhosts
== hspace
) {
161 realloc(hs
, (hspace
+= 40) * sizeof(*hs
))) == NULL
)
166 if ((hsp
->hs_wd
= malloc((size_t)WHDRSIZE
)) == NULL
)
168 memmove(hsp
->hs_wd
, buf
, (size_t)WHDRSIZE
);
170 for (wd
= (struct whod
*)buf
, i
= 0; i
< 2; ++i
)
171 if (wd
->wd_loadav
[i
] > maxloadav
)
172 maxloadav
= wd
->wd_loadav
[i
];
174 for (hsp
->hs_nusers
= 0,
175 we
= (struct whoent
*)(buf
+ cc
); --we
>= wd
->wd_we
;)
176 if (aflg
|| we
->we_idle
< 3600)
182 errx(0, "no hosts in %s.", _PATH_RWHODIR
);
185 qsort(hs
, nhosts
, sizeof(hs
[0]), cmp
);
186 for (i
= 0; i
< nhosts
; i
++) {
189 (void)printf("%-12.12s%s\n", hsp
->hs_wd
->wd_hostname
,
190 interval(now
- hsp
->hs_wd
->wd_recvtime
, "down"));
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"),
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);
211 interval(tval
, updown
)
215 static char resbuf
[32];
216 int days
, hours
, minutes
;
218 if (tval
< 0 || tval
> DAYSPERNYEAR
* SECSPERDAY
) {
219 (void)snprintf(resbuf
, sizeof(resbuf
), " %s ??:??", updown
);
222 /* round to minutes. */
223 minutes
= (tval
+ (SECSPERMIN
- 1)) / SECSPERMIN
;
224 hours
= minutes
/ MINSPERHOUR
;
225 minutes
%= MINSPERHOUR
;
226 days
= hours
/ HOURSPERDAY
;
227 hours
%= HOURSPERDAY
;
229 (void)snprintf(resbuf
, sizeof(resbuf
),
230 "%s %2d+%02d:%02d", updown
, days
, hours
, minutes
);
232 (void)snprintf(resbuf
, sizeof(resbuf
),
233 "%s %2d:%02d", updown
, hours
, minutes
);
237 #define HS(a) ((struct hs *)(a))
239 /* Alphabetical comparison. */
245 strcmp(HS(a1
)->hs_wd
->wd_hostname
, HS(a2
)->hs_wd
->wd_hostname
));
248 /* Load average comparison. */
255 return (tcmp(a1
, a2
));
258 else if (ISDOWN(HS(a2
)))
262 (HS(a2
)->hs_wd
->wd_loadav
[0] - HS(a1
)->hs_wd
->wd_loadav
[0]));
265 /* Number of users comparison. */
272 return (tcmp(a1
, a2
));
275 else if (ISDOWN(HS(a2
)))
278 return (rflg
* (HS(a2
)->hs_nusers
- HS(a1
)->hs_nusers
));
281 /* Uptime comparison. */
287 (ISDOWN(HS(a2
)) ? HS(a2
)->hs_wd
->wd_recvtime
- now
288 : HS(a2
)->hs_wd
->wd_sendtime
- HS(a2
)->hs_wd
->wd_boottime
)
290 (ISDOWN(HS(a1
)) ? HS(a1
)->hs_wd
->wd_recvtime
- now
291 : HS(a1
)->hs_wd
->wd_sendtime
- HS(a1
)->hs_wd
->wd_boottime
)
298 (void)fprintf(stderr
, "usage: ruptime [-alrut]\n");