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