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