rup.c revision 1.19 1 /* $NetBSD: rup.c,v 1.19 2000/03/08 12:10:28 jdolecek 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.19 2000/03/08 12:10:28 jdolecek 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 struct in_addr addr;
66 } *hosts;
67
68 int search_host __P((struct in_addr));
69 void remember_host __P((struct in_addr));
70
71 int
72 search_host(addr)
73 struct in_addr addr;
74 {
75 struct host_list *hp;
76
77 if (!hosts)
78 return(0);
79
80 for (hp = hosts; hp != NULL; hp = hp->next) {
81 if (hp->addr.s_addr == addr.s_addr)
82 return(1);
83 }
84 return(0);
85 }
86
87 void
88 remember_host(addr)
89 struct in_addr addr;
90 {
91 struct host_list *hp;
92
93 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
94 err(1, "malloc");
95 /* NOTREACHED */
96 }
97 hp->addr.s_addr = addr.s_addr;
98 hp->next = hosts;
99 hosts = hp;
100 }
101
102 struct rup_data {
103 const char *host;
104 struct statstime statstime;
105 };
106 struct rup_data *rup_data;
107 int rup_data_idx = 0;
108 int rup_data_max = 0;
109
110 enum sort_type {
111 SORT_NONE,
112 SORT_HOST,
113 SORT_LDAV,
114 SORT_UPTIME
115 };
116 enum sort_type sort_type;
117
118 int compare __P((struct rup_data *, struct rup_data *));
119 void remember_rup_data __P((const char *, struct statstime *));
120 int rstat_reply __P((char *, struct sockaddr_in *));
121 int print_rup_data __P((const char *, statstime *));
122 void onehost __P((char *));
123 void allhosts __P((void));
124 int main __P((int, char *[]));
125 void usage __P((void));
126
127 int
128 compare(d1, d2)
129 struct rup_data *d1;
130 struct rup_data *d2;
131 {
132 switch(sort_type) {
133 case SORT_HOST:
134 return strcmp(d1->host, d2->host);
135 case SORT_LDAV:
136 return d1->statstime.avenrun[0]
137 - d2->statstime.avenrun[0];
138 case SORT_UPTIME:
139 return d1->statstime.boottime.tv_sec
140 - d2->statstime.boottime.tv_sec;
141 default:
142 /* something's really wrong here */
143 abort();
144 }
145 }
146
147 void
148 remember_rup_data(host, st)
149 const char *host;
150 struct statstime *st;
151 {
152 if (rup_data_idx >= rup_data_max) {
153 rup_data_max += 16;
154 rup_data = realloc (rup_data,
155 rup_data_max * sizeof(struct rup_data));
156 if (rup_data == NULL) {
157 err (1, "realloc");
158 /* NOTREACHED */
159 }
160 }
161
162 rup_data[rup_data_idx].host = strdup(host);
163 rup_data[rup_data_idx].statstime = *st;
164 rup_data_idx++;
165 }
166
167
168 int
169 rstat_reply(replyp, raddrp)
170 char *replyp;
171 struct sockaddr_in *raddrp;
172 {
173 struct hostent *hp;
174 char *host;
175 statstime *host_stat = (statstime *)replyp;
176
177 if (!search_host(raddrp->sin_addr)) {
178 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
179 sizeof(struct in_addr), AF_INET);
180
181 if (hp)
182 host = hp->h_name;
183 else
184 host = inet_ntoa(raddrp->sin_addr);
185
186 remember_host(raddrp->sin_addr);
187
188 if (sort_type != SORT_NONE) {
189 remember_rup_data(host, host_stat);
190 } else {
191 print_rup_data(host, host_stat);
192 }
193 }
194
195 return (0);
196 }
197
198
199 int
200 print_rup_data(host, host_stat)
201 const char *host;
202 statstime *host_stat;
203 {
204 struct tm *tmp_time;
205 struct tm host_time;
206 unsigned ups=0,upm=0,uph=0,upd=0;
207
208 char days_buf[16];
209 char hours_buf[16];
210
211 if (printtime)
212 printf("%-*.*s", HOST_WIDTH-4, HOST_WIDTH-4, host);
213 else
214 printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
215
216 tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
217 host_time = *tmp_time;
218
219 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
220
221 ups=host_stat->curtime.tv_sec;
222 upd=ups/(3600*24);
223 ups-=upd*3600*24;
224 uph=ups/3600;
225 ups-=uph*3600;
226 upm=ups/60;
227
228 if (upd != 0)
229 sprintf(days_buf, "%3u day%s, ", upd,
230 (upd > 1) ? "s" : "");
231 else
232 days_buf[0] = '\0';
233
234 if (uph != 0)
235 sprintf(hours_buf, "%2u:%02u, ",
236 uph, upm);
237 else
238 if (upm != 0)
239 sprintf(hours_buf, "%2u min%s ", upm,
240 (upm == 1) ? ", " : "s,");
241 else
242 hours_buf[0] = '\0';
243 if (printtime)
244 printf(" %2d:%02d%cm",
245 (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
246 host_time.tm_min, (host_time.tm_hour >= 12) ? 'p' : 'a');
247
248 printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
249 days_buf, hours_buf,
250 (double)host_stat->avenrun[0]/FSCALE,
251 (double)host_stat->avenrun[1]/FSCALE,
252 (double)host_stat->avenrun[2]/FSCALE);
253
254 return(0);
255 }
256
257
258 void
259 onehost(host)
260 char *host;
261 {
262 CLIENT *rstat_clnt;
263 statstime host_stat;
264 static struct timeval timeout = {25, 0};
265
266 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
267 if (rstat_clnt == NULL) {
268 warnx("%s", clnt_spcreateerror(host));
269 return;
270 }
271
272 memset((char *)&host_stat, 0, sizeof(host_stat));
273 if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
274 warnx("%s", clnt_sperror(rstat_clnt, host));
275 return;
276 }
277
278 print_rup_data(host, &host_stat);
279 clnt_destroy(rstat_clnt);
280 }
281
282 void
283 allhosts()
284 {
285 statstime host_stat;
286 enum clnt_stat clnt_stat;
287 size_t i;
288
289 if (sort_type != SORT_NONE) {
290 printf("collecting responses...");
291 fflush(stdout);
292 }
293
294 clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
295 xdr_void, NULL,
296 xdr_statstime, (char*)&host_stat, rstat_reply);
297 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
298 warnx("%s", clnt_sperrno(clnt_stat));
299 exit(1);
300 }
301
302 if (sort_type != SORT_NONE) {
303 putchar('\n');
304 qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
305 (int (*)(const void*, const void*))compare);
306
307 for (i = 0; i < rup_data_idx; i++) {
308 print_rup_data(rup_data[i].host, &rup_data[i].statstime);
309 }
310 }
311 }
312
313 int
314 main(argc, argv)
315 int argc;
316 char *argv[];
317 {
318 int ch;
319 extern int optind;
320
321 sort_type = SORT_NONE;
322 while ((ch = getopt(argc, argv, "dhlt")) != -1)
323 switch (ch) {
324 case 'd':
325 printtime = 1;
326 break;
327 case 'h':
328 sort_type = SORT_HOST;
329 break;
330 case 'l':
331 sort_type = SORT_LDAV;
332 break;
333 case 't':
334 sort_type = SORT_UPTIME;
335 break;
336 default:
337 usage();
338 /*NOTREACHED*/
339 }
340
341 setlinebuf(stdout);
342
343 if (argc == optind)
344 allhosts();
345 else {
346 for (; optind < argc; optind++)
347 onehost(argv[optind]);
348 }
349
350 exit(0);
351 }
352
353 void
354 usage()
355 {
356 fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n");
357 exit(1);
358 }
359