xref: /netbsd-src/usr.bin/rup/rup.c (revision 4b30c543a0b21e3ba94f2c569e9a82b4fdb2075f)
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
35 static char rcsid[] = "$Id: rup.c,v 1.6 1993/09/23 18:37:28 jtc Exp $";
36 #endif /* not lint */
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <netdb.h>
45 #include <rpc/rpc.h>
46 #include <arpa/inet.h>
47 
48 #undef FSHIFT			/* Use protocol's shift and scale values */
49 #undef FSCALE
50 #include <rpcsvc/rstat.h>
51 
52 #define HOST_WIDTH 15
53 
54 char *argv0;
55 
56 struct host_list {
57 	struct host_list *next;
58 	struct in_addr addr;
59 } *hosts;
60 
61 int search_host(struct in_addr addr)
62 {
63 	struct host_list *hp;
64 
65 	if (!hosts)
66 		return(0);
67 
68 	for (hp = hosts; hp != NULL; hp = hp->next) {
69 		if (hp->addr.s_addr == addr.s_addr)
70 			return(1);
71 	}
72 	return(0);
73 }
74 
75 void remember_host(struct in_addr addr)
76 {
77 	struct host_list *hp;
78 
79 	if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
80 		fprintf(stderr, "%s: no memory.\n", argv0);
81 		exit(1);
82 	}
83 	hp->addr.s_addr = addr.s_addr;
84 	hp->next = hosts;
85 	hosts = hp;
86 }
87 
88 rstat_reply(char *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 	printf("%-*s\t", HOST_WIDTH, host);
110 
111 	tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
112 	host_time = *tmp_time;
113 
114 	host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
115 
116 	tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
117 	host_uptime = *tmp_time;
118 
119 	if (host_uptime.tm_yday != 0)
120 		sprintf(days_buf, "%3d day%s, ", host_uptime.tm_yday,
121 			(host_uptime.tm_yday > 1) ? "s" : "");
122 	else
123 		days_buf[0] = '\0';
124 
125 	if (host_uptime.tm_hour != 0)
126 		sprintf(hours_buf, "%2d:%02d, ",
127 			host_uptime.tm_hour, host_uptime.tm_min);
128 	else
129 		if (host_uptime.tm_min != 0)
130 			sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min);
131 		else
132 			hours_buf[0] = '\0';
133 
134 	printf(" %2d:%02d%cm  up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
135 		host_time.tm_hour % 12,
136 		host_time.tm_min,
137 		(host_time.tm_hour >= 12) ? 'p' : 'a',
138 		days_buf,
139 		hours_buf,
140 		(double)host_stat->avenrun[0]/FSCALE,
141 		(double)host_stat->avenrun[1]/FSCALE,
142 		(double)host_stat->avenrun[2]/FSCALE);
143 
144 	remember_host(raddrp->sin_addr);
145 	return(0);
146 }
147 
148 onehost(char *host)
149 {
150 	CLIENT *rstat_clnt;
151 	statstime host_stat;
152 	struct sockaddr_in addr;
153 	struct hostent *hp;
154 
155 	hp = gethostbyname(host);
156 	if (hp == NULL) {
157 		fprintf(stderr, "%s: unknown host \"%s\"\n",
158 			argv0, host);
159 		return(-1);
160 	}
161 
162 	rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
163 	if (rstat_clnt == NULL) {
164 		fprintf(stderr, "%s: %s %s", argv0, host, clnt_spcreateerror(""));
165 		return(-1);
166 	}
167 
168 	bzero((char *)&host_stat, sizeof(host_stat));
169 	if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, NULL) != RPC_SUCCESS) {
170 		fprintf(stderr, "%s: %s: %s\n", argv0, host, clnt_sperror(rstat_clnt, host));
171 		return(-1);
172 	}
173 
174 	addr.sin_addr.s_addr = *(int *)hp->h_addr;
175 	rstat_reply((char *)&host_stat, &addr);
176 }
177 
178 allhosts()
179 {
180 	statstime host_stat;
181 	enum clnt_stat clnt_stat;
182 
183 	clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
184 				   xdr_void, NULL,
185 				   xdr_statstime, &host_stat, rstat_reply);
186 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
187 		fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
188 		exit(1);
189 	}
190 }
191 
192 usage()
193 {
194 	fprintf(stderr, "Usage: %s [hosts ...]\n", argv0);
195 	exit(1);
196 }
197 
198 main(int argc, char *argv[])
199 {
200 	int ch;
201 	extern int optind;
202 
203 	if (!(argv0 = rindex(argv[0], '/')))
204 		argv0 = argv[0];
205 	else
206 		argv0++;
207 
208 	while ((ch = getopt(argc, argv, "?")) != -1)
209 		switch (ch) {
210 		default:
211 			usage();
212 			/*NOTREACHED*/
213 		}
214 
215 	setlinebuf(stdout);
216 	if (argc == optind)
217 		allhosts();
218 	else {
219 		for (; optind < argc; optind++)
220 			(void) onehost(argv[optind]);
221 	}
222 	exit(0);
223 }
224