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