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