xref: /freebsd/usr.bin/rup/rup.c (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1993, John Brezak
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 
43 #include <rpc/rpc.h>
44 #include <rpc/pmap_clnt.h>
45 
46 #undef FSHIFT			/* Use protocol's shift and scale values */
47 #undef FSCALE
48 
49 #include <rpcsvc/rstat.h>
50 
51 #include <arpa/inet.h>
52 
53 #include <err.h>
54 #include <netdb.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <time.h>
59 #include <unistd.h>
60 
61 #define HOST_WIDTH 15
62 
63 struct host_list {
64 	struct host_list *next;
65 	struct in_addr addr;
66 } *hosts;
67 
68 static int
69 search_host(struct in_addr addr)
70 {
71 	struct host_list *hp;
72 
73 	if (!hosts)
74 		return(0);
75 
76 	for (hp = hosts; hp != NULL; hp = hp->next) {
77 		if (hp->addr.s_addr == addr.s_addr)
78 			return(1);
79 	}
80 	return(0);
81 }
82 
83 static void
84 remember_host(struct in_addr addr)
85 {
86 	struct host_list *hp;
87 
88 	if (!(hp = (struct host_list *)malloc(sizeof(struct host_list))))
89 		errx(1, "no memory");
90 	hp->addr.s_addr = addr.s_addr;
91 	hp->next = hosts;
92 	hosts = hp;
93 }
94 
95 static bool_t
96 rstat_reply(caddr_t replyp, struct sockaddr_in *raddrp)
97 {
98 	struct tm *tmp_time;
99 	struct tm host_time;
100 	struct tm host_uptime;
101 	char days_buf[16];
102 	char hours_buf[16];
103 	struct hostent *hp;
104 	char *host;
105 	statstime *host_stat = (statstime *)replyp;
106 	time_t tmp_time_t;
107 
108 	if (search_host(raddrp->sin_addr))
109 		return(0);
110 
111 	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
112 			   sizeof(struct in_addr), AF_INET);
113 	if (hp)
114 		host = hp->h_name;
115 	else
116 		host = inet_ntoa(raddrp->sin_addr);
117 
118 	/* truncate hostname to fit nicely into field */
119 	if (strlen(host) > HOST_WIDTH)
120 		host[HOST_WIDTH] = '\0';
121 
122 	printf("%-*s\t", HOST_WIDTH, host);
123 
124 	if (sizeof(time_t) == sizeof(host_stat->curtime.tv_sec)) {
125 		tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
126 		host_time = *tmp_time;
127 
128 		host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
129 
130 		tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
131 		host_uptime = *tmp_time;
132 	}
133 	else {			/* non-32-bit time_t */
134 		tmp_time_t = host_stat->curtime.tv_sec;
135 		tmp_time = localtime(&tmp_time_t);
136 		host_time = *tmp_time;
137 
138 		host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
139 
140 		tmp_time_t = host_stat->curtime.tv_sec;
141 		tmp_time = gmtime(&tmp_time_t);
142 		host_uptime = *tmp_time;
143 	}
144 
145 	#define updays (host_stat->curtime.tv_sec  / 86400)
146 	if (host_uptime.tm_yday != 0)
147 		sprintf(days_buf, "%3d day%s, ", updays,
148 			(updays > 1) ? "s" : "");
149 	else
150 		days_buf[0] = '\0';
151 
152 	if (host_uptime.tm_hour != 0)
153 		sprintf(hours_buf, "%2d:%02d, ",
154 			host_uptime.tm_hour, host_uptime.tm_min);
155 	else
156 		if (host_uptime.tm_min != 0)
157 			sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min);
158 		else if (host_stat->curtime.tv_sec < 60)
159 			sprintf(hours_buf, "%2d secs, ", host_uptime.tm_sec);
160 		else
161 			hours_buf[0] = '\0';
162 
163 	printf(" %2d:%02d%cm  up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
164 		(host_time.tm_hour % 12) ? host_time.tm_hour % 12 : 12,
165 		host_time.tm_min,
166 		(host_time.tm_hour >= 12) ? 'p' : 'a',
167 		days_buf,
168 		hours_buf,
169 		(double)host_stat->avenrun[0]/FSCALE,
170 		(double)host_stat->avenrun[1]/FSCALE,
171 		(double)host_stat->avenrun[2]/FSCALE);
172 
173 	remember_host(raddrp->sin_addr);
174 	return(0);
175 }
176 
177 static int
178 onehost(char *host)
179 {
180 	CLIENT *rstat_clnt;
181 	statstime host_stat;
182 	struct sockaddr_in addr;
183 	struct hostent *hp;
184 	struct timeval tv;
185 
186 	hp = gethostbyname(host);
187 	if (hp == NULL) {
188 		warnx("unknown host \"%s\"", host);
189 		return(-1);
190 	}
191 
192 	rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
193 	if (rstat_clnt == NULL) {
194 		warnx("%s %s", host, clnt_spcreateerror(""));
195 		return(-1);
196 	}
197 
198 	bzero((char *)&host_stat, sizeof(host_stat));
199 	tv.tv_sec = 15;	/* XXX ??? */
200 	tv.tv_usec = 0;
201 	if (clnt_call(rstat_clnt, RSTATPROC_STATS,
202 	    (xdrproc_t)xdr_void, NULL,
203 	    (xdrproc_t)xdr_statstime, &host_stat, tv) != RPC_SUCCESS) {
204 		warnx("%s: %s", host, clnt_sperror(rstat_clnt, host));
205 		clnt_destroy(rstat_clnt);
206 		return(-1);
207 	}
208 
209 	addr.sin_addr.s_addr = *(int *)hp->h_addr;
210 	rstat_reply((caddr_t)&host_stat, &addr);
211 	clnt_destroy(rstat_clnt);
212 	return (0);
213 }
214 
215 static void
216 allhosts(void)
217 {
218 	statstime host_stat;
219 	enum clnt_stat clnt_stat;
220 
221 	clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
222 				   (xdrproc_t)xdr_void, NULL,
223 				   (xdrproc_t)xdr_statstime, &host_stat,
224 				   (resultproc_t)rstat_reply);
225 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
226 		errx(1, "%s", clnt_sperrno(clnt_stat));
227 }
228 
229 static void
230 usage(void)
231 {
232 	fprintf(stderr, "usage: rup [host ...]\n");
233 	exit(1);
234 }
235 
236 int
237 main(int argc, char *argv[])
238 {
239 	int ch;
240 
241 	while ((ch = getopt(argc, argv, "?")) != -1)
242 		switch (ch) {
243 		default:
244 			usage();
245 			/*NOTREACHED*/
246 		}
247 
248 	setlinebuf(stdout);
249 	if (argc == optind)
250 		allhosts();
251 	else {
252 		for (; optind < argc; optind++)
253 			(void) onehost(argv[optind]);
254 	}
255 	exit(0);
256 }
257