2 * Copyright (c) 1993, John Brezak
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/usr.bin/rup/rup.c,v 1.11.2.2 2001/07/02 23:43:04 mikeh Exp $
34 * $DragonFly: src/usr.bin/rup/rup.c,v 1.3 2004/09/14 18:36:40 joerg Exp $
43 #include <sys/param.h>
44 #include <sys/socket.h>
47 #include <rpc/pmap_clnt.h>
48 #include <arpa/inet.h>
49 #undef FSHIFT /* Use protocol's shift and scale values */
51 #include <rpcsvc/rstat.h>
56 struct host_list *next;
61 search_host(struct in_addr addr)
68 for (hp = hosts; hp != NULL; hp = hp->next) {
69 if (hp->addr.s_addr == addr.s_addr)
76 remember_host(struct in_addr addr)
80 if ((hp = malloc(sizeof(struct host_list))) == NULL)
81 err(1, "malloc failed");
82 hp->addr.s_addr = addr.s_addr;
88 rstat_reply(char *replyp, struct sockaddr_in *raddrp)
92 struct tm host_uptime;
97 statstime *host_stat = (statstime *)replyp;
99 if (search_host(raddrp->sin_addr))
102 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
103 sizeof(struct in_addr), AF_INET);
107 host = inet_ntoa(raddrp->sin_addr);
109 /* truncate hostname to fit nicely into field */
110 if (strlen(host) > HOST_WIDTH)
111 host[HOST_WIDTH] = '\0';
113 printf("%-*s\t", HOST_WIDTH, host);
115 tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
116 host_time = *tmp_time;
118 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
120 tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
121 host_uptime = *tmp_time;
123 #define updays (host_stat->curtime.tv_sec / 86400)
124 if (host_uptime.tm_yday != 0)
125 snprintf(days_buf, sizeof days_buf, "%3d day%s, ", updays,
126 (updays > 1) ? "s" : "");
130 if (host_uptime.tm_hour != 0)
131 snprintf(hours_buf, sizeof hours_buf, "%2d:%02d, ",
132 host_uptime.tm_hour, host_uptime.tm_min);
133 else if (host_uptime.tm_min != 0)
134 snprintf(hours_buf, sizeof hours_buf, "%2d mins, ",
136 else if (host_stat->curtime.tv_sec < 60)
137 snprintf(hours_buf, sizeof hours_buf, "%2d secs, ",
142 printf(" %2d:%02d%cm up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
143 (host_time.tm_hour % 12) ? host_time.tm_hour % 12 : 12,
145 (host_time.tm_hour >= 12) ? 'p' : 'a',
148 (double)host_stat->avenrun[0]/FSCALE,
149 (double)host_stat->avenrun[1]/FSCALE,
150 (double)host_stat->avenrun[2]/FSCALE);
152 remember_host(raddrp->sin_addr);
161 struct sockaddr_in addr;
165 hp = gethostbyname(host);
171 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
172 if (rstat_clnt == NULL) {
173 clnt_pcreateerror(host);
177 bzero(&host_stat, sizeof(host_stat));
178 tv.tv_sec = 15; /* XXX ??? */
180 if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, tv) != RPC_SUCCESS) {
181 clnt_perror(rstat_clnt, host);
182 clnt_destroy(rstat_clnt);
186 addr.sin_addr.s_addr = *(int *)hp->h_addr;
187 rstat_reply((char *)&host_stat, &addr);
188 clnt_destroy(rstat_clnt);
196 enum clnt_stat clnt_stat;
198 clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
200 xdr_statstime, (char *)&host_stat, rstat_reply);
201 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
202 errx(1, "%s", clnt_sperrno(clnt_stat));
208 fprintf(stderr, "usage: rup [hosts ...]\n");
213 main(int argc, char *argv[])
217 while ((ch = getopt(argc, argv, "")) != -1)
228 for (; optind < argc; optind++)
229 (void) onehost(argv[optind]);