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