2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1993, John Brezak
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/socket.h>
44 #include <rpc/pmap_clnt.h>
46 #undef FSHIFT /* Use protocol's shift and scale values */
49 #include <rpcsvc/rstat.h>
51 #include <arpa/inet.h>
63 static struct host_list {
64 struct host_list *next;
69 search_host(struct in_addr addr)
76 for (hp = hosts; hp != NULL; hp = hp->next) {
77 if (hp->addr.s_addr == addr.s_addr)
84 remember_host(struct in_addr addr)
88 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list))))
90 hp->addr.s_addr = addr.s_addr;
96 rstat_reply(statstime *host_stat, struct sockaddr_in *raddrp)
100 struct tm host_uptime;
107 if (search_host(raddrp->sin_addr))
110 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
111 sizeof(struct in_addr), AF_INET);
115 host = inet_ntoa(raddrp->sin_addr);
117 /* truncate hostname to fit nicely into field */
118 if (strlen(host) > HOST_WIDTH)
119 host[HOST_WIDTH] = '\0';
121 printf("%-*s\t", HOST_WIDTH, host);
123 tmp_time_t = host_stat->curtime.tv_sec;
124 tmp_time = localtime(&tmp_time_t);
125 host_time = *tmp_time;
127 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
129 tmp_time_t = host_stat->curtime.tv_sec;
130 tmp_time = gmtime(&tmp_time_t);
131 host_uptime = *tmp_time;
133 #define updays (host_stat->curtime.tv_sec / 86400)
134 if (host_uptime.tm_yday != 0)
135 sprintf(days_buf, "%3d day%s, ", updays,
136 (updays > 1) ? "s" : "");
140 if (host_uptime.tm_hour != 0)
141 sprintf(hours_buf, "%2d:%02d, ",
142 host_uptime.tm_hour, host_uptime.tm_min);
144 if (host_uptime.tm_min != 0)
145 sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min);
146 else if (host_stat->curtime.tv_sec < 60)
147 sprintf(hours_buf, "%2d secs, ", host_uptime.tm_sec);
151 printf(" %2d:%02d%cm up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
152 (host_time.tm_hour % 12) ? host_time.tm_hour % 12 : 12,
154 (host_time.tm_hour >= 12) ? 'p' : 'a',
157 (double)host_stat->avenrun[0]/FSCALE,
158 (double)host_stat->avenrun[1]/FSCALE,
159 (double)host_stat->avenrun[2]/FSCALE);
161 remember_host(raddrp->sin_addr);
170 struct sockaddr_in addr;
174 hp = gethostbyname(host);
176 warnx("unknown host \"%s\"", host);
180 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
181 if (rstat_clnt == NULL) {
182 warnx("%s %s", host, clnt_spcreateerror(""));
186 bzero((char *)&host_stat, sizeof(host_stat));
187 tv.tv_sec = 15; /* XXX ??? */
189 if (clnt_call(rstat_clnt, RSTATPROC_STATS,
190 (xdrproc_t)xdr_void, NULL,
191 (xdrproc_t)xdr_statstime, &host_stat, tv) != RPC_SUCCESS) {
192 warnx("%s: %s", host, clnt_sperror(rstat_clnt, host));
193 clnt_destroy(rstat_clnt);
197 memcpy(&addr.sin_addr.s_addr, hp->h_addr, sizeof(int));
198 rstat_reply(&host_stat, &addr);
199 clnt_destroy(rstat_clnt);
207 enum clnt_stat clnt_stat;
209 clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
210 (xdrproc_t)xdr_void, NULL,
211 (xdrproc_t)xdr_statstime, &host_stat,
212 (resultproc_t)rstat_reply);
213 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
214 errx(1, "%s", clnt_sperrno(clnt_stat));
220 fprintf(stderr, "usage: rup [host ...]\n");
225 main(int argc, char *argv[])
229 while ((ch = getopt(argc, argv, "?")) != -1)
239 for (; optind < argc; optind++)
240 (void) onehost(argv[optind]);