rup.c revision 1.23 1 /* $NetBSD: rup.c,v 1.23 2001/01/16 02:43:37 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1993, John Brezak
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: rup.c,v 1.23 2001/01/16 02:43:37 cgd Exp $");
39 #endif /* not lint */
40
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45
46 #include <err.h>
47 #include <netdb.h>
48 #include <rpc/rpc.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 #include <unistd.h>
54
55 #undef FSHIFT /* Use protocol's shift and scale values */
56 #undef FSCALE
57 #include <rpcsvc/rstat.h>
58
59 #define HOST_WIDTH 24
60
61 int printtime; /* print the remote host(s)'s time */
62
63 struct host_list {
64 struct host_list *next;
65 int family;
66 union {
67 struct in6_addr _addr6;
68 struct in_addr _addr4;
69 } addr;
70 } *hosts;
71
72 #define addr6 addr._addr6
73 #define addr4 addr._addr4
74
75 int search_host(struct sockaddr *);
76 void remember_host(struct sockaddr *);
77
78 int
79 search_host(struct sockaddr *sa)
80 {
81 struct host_list *hp;
82
83 if (!hosts)
84 return(0);
85
86 for (hp = hosts; hp != NULL; hp = hp->next) {
87 switch (hp->family) {
88 case AF_INET6:
89 if (!memcmp(&hp->addr6,
90 &((struct sockaddr_in6 *)sa)->sin6_addr,
91 sizeof (struct in6_addr)))
92 return 1;
93 break;
94 case AF_INET:
95 if (!memcmp(&hp->addr4,
96 &((struct sockaddr_in *)sa)->sin_addr,
97 sizeof (struct in_addr)))
98 return 1;
99 break;
100 default:
101 break;
102 }
103 }
104 return(0);
105 }
106
107 void
108 remember_host(struct sockaddr *sa)
109 {
110 struct host_list *hp;
111
112 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
113 err(1, "malloc");
114 /* NOTREACHED */
115 }
116 hp->family = sa->sa_family;
117 hp->next = hosts;
118 switch (sa->sa_family) {
119 case AF_INET6:
120 memcpy(&hp->addr6, &((struct sockaddr_in6 *)sa)->sin6_addr,
121 sizeof (struct in6_addr));
122 break;
123 case AF_INET:
124 memcpy(&hp->addr4, &((struct sockaddr_in *)sa)->sin_addr,
125 sizeof (struct in_addr));
126 break;
127 default:
128 err(1, "unknown address family");
129 /* NOTREACHED */
130 }
131 hosts = hp;
132 }
133
134 struct rup_data {
135 const char *host;
136 struct statstime statstime;
137 };
138 struct rup_data *rup_data;
139 int rup_data_idx = 0;
140 int rup_data_max = 0;
141
142 enum sort_type {
143 SORT_NONE,
144 SORT_HOST,
145 SORT_LDAV,
146 SORT_UPTIME
147 };
148 enum sort_type sort_type;
149
150 int compare(struct rup_data *, struct rup_data *);
151 void remember_rup_data(const char *, struct statstime *);
152 int rstat_reply(char *, struct netbuf *, struct netconfig *);
153 int print_rup_data(const char *, statstime *);
154 void onehost(char *);
155 void allhosts(void);
156 int main(int, char *[]);
157 void usage(void);
158
159 int
160 compare(struct rup_data *d1, struct rup_data *d2)
161 {
162 switch(sort_type) {
163 case SORT_HOST:
164 return strcmp(d1->host, d2->host);
165 case SORT_LDAV:
166 return d1->statstime.avenrun[0]
167 - d2->statstime.avenrun[0];
168 case SORT_UPTIME:
169 return d1->statstime.boottime.tv_sec
170 - d2->statstime.boottime.tv_sec;
171 default:
172 /* something's really wrong here */
173 abort();
174 }
175 }
176
177 void
178 remember_rup_data(const char *host, struct statstime *st)
179 {
180 if (rup_data_idx >= rup_data_max) {
181 rup_data_max += 16;
182 rup_data = realloc (rup_data,
183 rup_data_max * sizeof(struct rup_data));
184 if (rup_data == NULL) {
185 err (1, "realloc");
186 /* NOTREACHED */
187 }
188 }
189
190 rup_data[rup_data_idx].host = strdup(host);
191 rup_data[rup_data_idx].statstime = *st;
192 rup_data_idx++;
193 }
194
195
196 int
197 rstat_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf)
198 {
199 char host[NI_MAXHOST];
200 statstime *host_stat = (statstime *)replyp;
201 struct sockaddr *sa = raddrp->buf;
202
203 if (!search_host(sa)) {
204 if (getnameinfo(sa, sa->sa_len, host, sizeof host, NULL, 0, 0))
205 return 0;
206
207 remember_host(sa);
208
209 if (sort_type != SORT_NONE) {
210 remember_rup_data(host, host_stat);
211 } else {
212 print_rup_data(host, host_stat);
213 }
214 }
215
216 return (0);
217 }
218
219
220 int
221 print_rup_data(const char *host, statstime *host_stat)
222 {
223 struct tm *tmp_time;
224 struct tm host_time;
225 unsigned ups=0,upm=0,uph=0,upd=0;
226
227 char days_buf[16];
228 char hours_buf[16];
229
230 if (printtime)
231 printf("%-*.*s", HOST_WIDTH-4, HOST_WIDTH-4, host);
232 else
233 printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
234
235 tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
236 host_time = *tmp_time;
237
238 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
239
240 ups=host_stat->curtime.tv_sec;
241 upd=ups/(3600*24);
242 ups-=upd*3600*24;
243 uph=ups/3600;
244 ups-=uph*3600;
245 upm=ups/60;
246
247 if (upd != 0)
248 sprintf(days_buf, "%3u day%s, ", upd,
249 (upd > 1) ? "s" : "");
250 else
251 days_buf[0] = '\0';
252
253 if (uph != 0)
254 sprintf(hours_buf, "%2u:%02u, ",
255 uph, upm);
256 else
257 if (upm != 0)
258 sprintf(hours_buf, "%2u min%s ", upm,
259 (upm == 1) ? ", " : "s,");
260 else
261 hours_buf[0] = '\0';
262 if (printtime)
263 printf(" %2d:%02d%cm",
264 (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
265 host_time.tm_min, (host_time.tm_hour >= 12) ? 'p' : 'a');
266
267 printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
268 days_buf, hours_buf,
269 (double)host_stat->avenrun[0]/FSCALE,
270 (double)host_stat->avenrun[1]/FSCALE,
271 (double)host_stat->avenrun[2]/FSCALE);
272
273 return(0);
274 }
275
276
277 void
278 onehost(char *host)
279 {
280 CLIENT *rstat_clnt;
281 statstime host_stat;
282 static struct timeval timeout = {25, 0};
283
284 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
285 if (rstat_clnt == NULL) {
286 warnx("%s", clnt_spcreateerror(host));
287 return;
288 }
289
290 memset((char *)&host_stat, 0, sizeof(host_stat));
291 if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
292 warnx("%s", clnt_sperror(rstat_clnt, host));
293 return;
294 }
295
296 print_rup_data(host, &host_stat);
297 clnt_destroy(rstat_clnt);
298 }
299
300 void
301 allhosts()
302 {
303 statstime host_stat;
304 enum clnt_stat clnt_stat;
305 size_t i;
306
307 if (sort_type != SORT_NONE) {
308 printf("collecting responses...");
309 fflush(stdout);
310 }
311
312 clnt_stat = rpc_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
313 xdr_void, NULL,
314 xdr_statstime, (char*)&host_stat,
315 (resultproc_t)rstat_reply, "udp");
316 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
317 warnx("%s", clnt_sperrno(clnt_stat));
318 exit(1);
319 }
320
321 if (sort_type != SORT_NONE) {
322 putchar('\n');
323 qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
324 (int (*)(const void*, const void*))compare);
325
326 for (i = 0; i < rup_data_idx; i++) {
327 print_rup_data(rup_data[i].host, &rup_data[i].statstime);
328 }
329 }
330 }
331
332 int
333 main(int argc, char *argv[])
334 {
335 int ch;
336
337 sort_type = SORT_NONE;
338 while ((ch = getopt(argc, argv, "dhlt")) != -1)
339 switch (ch) {
340 case 'd':
341 printtime = 1;
342 break;
343 case 'h':
344 sort_type = SORT_HOST;
345 break;
346 case 'l':
347 sort_type = SORT_LDAV;
348 break;
349 case 't':
350 sort_type = SORT_UPTIME;
351 break;
352 default:
353 usage();
354 /*NOTREACHED*/
355 }
356
357 setlinebuf(stdout);
358
359 if (argc == optind)
360 allhosts();
361 else {
362 for (; optind < argc; optind++)
363 onehost(argv[optind]);
364 }
365
366 exit(0);
367 }
368
369 void
370 usage()
371 {
372 fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n");
373 exit(1);
374 }
375