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