rup.c revision 1.7 1 1.1 brezak /*-
2 1.1 brezak * Copyright (c) 1993, John Brezak
3 1.1 brezak * All rights reserved.
4 1.1 brezak *
5 1.1 brezak * Redistribution and use in source and binary forms, with or without
6 1.1 brezak * modification, are permitted provided that the following conditions
7 1.1 brezak * are met:
8 1.1 brezak * 1. Redistributions of source code must retain the above copyright
9 1.1 brezak * notice, this list of conditions and the following disclaimer.
10 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 brezak * notice, this list of conditions and the following disclaimer in the
12 1.1 brezak * documentation and/or other materials provided with the distribution.
13 1.1 brezak * 3. All advertising materials mentioning features or use of this software
14 1.1 brezak * must display the following acknowledgement:
15 1.1 brezak * This product includes software developed by the University of
16 1.1 brezak * California, Berkeley and its contributors.
17 1.1 brezak * 4. Neither the name of the University nor the names of its contributors
18 1.1 brezak * may be used to endorse or promote products derived from this software
19 1.1 brezak * without specific prior written permission.
20 1.1 brezak *
21 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 brezak * SUCH DAMAGE.
32 1.1 brezak */
33 1.1 brezak
34 1.5 mycroft #ifndef lint
35 1.7 deraadt static char rcsid[] = "$Id: rup.c,v 1.7 1993/11/10 03:52:21 deraadt Exp $";
36 1.5 mycroft #endif /* not lint */
37 1.5 mycroft
38 1.1 brezak #include <stdio.h>
39 1.6 jtc #include <stdlib.h>
40 1.1 brezak #include <string.h>
41 1.1 brezak #include <time.h>
42 1.1 brezak #include <sys/param.h>
43 1.1 brezak #include <sys/socket.h>
44 1.1 brezak #include <netdb.h>
45 1.1 brezak #include <rpc/rpc.h>
46 1.1 brezak #include <arpa/inet.h>
47 1.6 jtc
48 1.6 jtc #undef FSHIFT /* Use protocol's shift and scale values */
49 1.6 jtc #undef FSCALE
50 1.1 brezak #include <rpcsvc/rstat.h>
51 1.1 brezak
52 1.7 deraadt #define HOST_WIDTH 24
53 1.1 brezak
54 1.1 brezak char *argv0;
55 1.1 brezak
56 1.7 deraadt int printtime; /* print the remote host(s)'s time */
57 1.7 deraadt
58 1.1 brezak struct host_list {
59 1.1 brezak struct host_list *next;
60 1.1 brezak struct in_addr addr;
61 1.1 brezak } *hosts;
62 1.1 brezak
63 1.7 deraadt int
64 1.7 deraadt search_host(addr)
65 1.7 deraadt struct in_addr addr;
66 1.1 brezak {
67 1.1 brezak struct host_list *hp;
68 1.1 brezak
69 1.1 brezak if (!hosts)
70 1.1 brezak return(0);
71 1.1 brezak
72 1.1 brezak for (hp = hosts; hp != NULL; hp = hp->next) {
73 1.1 brezak if (hp->addr.s_addr == addr.s_addr)
74 1.1 brezak return(1);
75 1.1 brezak }
76 1.1 brezak return(0);
77 1.1 brezak }
78 1.1 brezak
79 1.7 deraadt void
80 1.7 deraadt remember_host(addr)
81 1.7 deraadt struct in_addr addr;
82 1.1 brezak {
83 1.1 brezak struct host_list *hp;
84 1.1 brezak
85 1.1 brezak if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
86 1.1 brezak fprintf(stderr, "%s: no memory.\n", argv0);
87 1.1 brezak exit(1);
88 1.1 brezak }
89 1.1 brezak hp->addr.s_addr = addr.s_addr;
90 1.1 brezak hp->next = hosts;
91 1.1 brezak hosts = hp;
92 1.1 brezak }
93 1.1 brezak
94 1.7 deraadt rstat_reply(replyp, raddrp)
95 1.7 deraadt char *replyp;
96 1.7 deraadt struct sockaddr_in *raddrp;
97 1.1 brezak {
98 1.1 brezak struct tm *tmp_time;
99 1.1 brezak struct tm host_time;
100 1.1 brezak struct tm host_uptime;
101 1.1 brezak char days_buf[16];
102 1.1 brezak char hours_buf[16];
103 1.1 brezak struct hostent *hp;
104 1.1 brezak char *host;
105 1.1 brezak statstime *host_stat = (statstime *)replyp;
106 1.1 brezak
107 1.1 brezak if (search_host(raddrp->sin_addr))
108 1.1 brezak return(0);
109 1.1 brezak
110 1.1 brezak hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
111 1.1 brezak sizeof(struct in_addr), AF_INET);
112 1.1 brezak if (hp)
113 1.1 brezak host = hp->h_name;
114 1.1 brezak else
115 1.1 brezak host = inet_ntoa(raddrp->sin_addr);
116 1.1 brezak
117 1.7 deraadt printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
118 1.1 brezak
119 1.1 brezak tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
120 1.1 brezak host_time = *tmp_time;
121 1.1 brezak
122 1.1 brezak host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
123 1.1 brezak
124 1.1 brezak tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
125 1.1 brezak host_uptime = *tmp_time;
126 1.1 brezak
127 1.1 brezak if (host_uptime.tm_yday != 0)
128 1.1 brezak sprintf(days_buf, "%3d day%s, ", host_uptime.tm_yday,
129 1.1 brezak (host_uptime.tm_yday > 1) ? "s" : "");
130 1.1 brezak else
131 1.1 brezak days_buf[0] = '\0';
132 1.1 brezak
133 1.1 brezak if (host_uptime.tm_hour != 0)
134 1.1 brezak sprintf(hours_buf, "%2d:%02d, ",
135 1.1 brezak host_uptime.tm_hour, host_uptime.tm_min);
136 1.1 brezak else
137 1.1 brezak if (host_uptime.tm_min != 0)
138 1.1 brezak sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min);
139 1.1 brezak else
140 1.1 brezak hours_buf[0] = '\0';
141 1.1 brezak
142 1.7 deraadt if (printtime)
143 1.7 deraadt printf(" %2d:%02d%cm", host_time.tm_hour % 12,
144 1.7 deraadt host_time.tm_min,
145 1.7 deraadt (host_time.tm_hour >= 12) ? 'p' : 'a');
146 1.7 deraadt
147 1.7 deraadt printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
148 1.7 deraadt days_buf, hours_buf,
149 1.6 jtc (double)host_stat->avenrun[0]/FSCALE,
150 1.6 jtc (double)host_stat->avenrun[1]/FSCALE,
151 1.6 jtc (double)host_stat->avenrun[2]/FSCALE);
152 1.1 brezak
153 1.1 brezak remember_host(raddrp->sin_addr);
154 1.1 brezak return(0);
155 1.1 brezak }
156 1.1 brezak
157 1.7 deraadt onehost(host)
158 1.7 deraadt char *host;
159 1.1 brezak {
160 1.1 brezak CLIENT *rstat_clnt;
161 1.1 brezak statstime host_stat;
162 1.1 brezak struct sockaddr_in addr;
163 1.1 brezak struct hostent *hp;
164 1.1 brezak
165 1.1 brezak hp = gethostbyname(host);
166 1.1 brezak if (hp == NULL) {
167 1.1 brezak fprintf(stderr, "%s: unknown host \"%s\"\n",
168 1.1 brezak argv0, host);
169 1.1 brezak return(-1);
170 1.1 brezak }
171 1.1 brezak
172 1.1 brezak rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
173 1.1 brezak if (rstat_clnt == NULL) {
174 1.1 brezak fprintf(stderr, "%s: %s %s", argv0, host, clnt_spcreateerror(""));
175 1.1 brezak return(-1);
176 1.1 brezak }
177 1.1 brezak
178 1.1 brezak bzero((char *)&host_stat, sizeof(host_stat));
179 1.1 brezak if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, NULL) != RPC_SUCCESS) {
180 1.6 jtc fprintf(stderr, "%s: %s: %s\n", argv0, host, clnt_sperror(rstat_clnt, host));
181 1.1 brezak return(-1);
182 1.1 brezak }
183 1.1 brezak
184 1.1 brezak addr.sin_addr.s_addr = *(int *)hp->h_addr;
185 1.1 brezak rstat_reply((char *)&host_stat, &addr);
186 1.1 brezak }
187 1.1 brezak
188 1.1 brezak allhosts()
189 1.1 brezak {
190 1.1 brezak statstime host_stat;
191 1.1 brezak enum clnt_stat clnt_stat;
192 1.1 brezak
193 1.1 brezak clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
194 1.1 brezak xdr_void, NULL,
195 1.1 brezak xdr_statstime, &host_stat, rstat_reply);
196 1.1 brezak if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
197 1.1 brezak fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
198 1.1 brezak exit(1);
199 1.1 brezak }
200 1.1 brezak }
201 1.1 brezak
202 1.1 brezak usage()
203 1.1 brezak {
204 1.1 brezak fprintf(stderr, "Usage: %s [hosts ...]\n", argv0);
205 1.1 brezak exit(1);
206 1.1 brezak }
207 1.1 brezak
208 1.7 deraadt main(argc, argv)
209 1.7 deraadt int argc;
210 1.7 deraadt char *argv[];
211 1.1 brezak {
212 1.1 brezak int ch;
213 1.1 brezak extern int optind;
214 1.1 brezak
215 1.1 brezak if (!(argv0 = rindex(argv[0], '/')))
216 1.1 brezak argv0 = argv[0];
217 1.1 brezak else
218 1.1 brezak argv0++;
219 1.1 brezak
220 1.7 deraadt while ((ch = getopt(argc, argv, "?t")) != -1)
221 1.1 brezak switch (ch) {
222 1.7 deraadt case 't':
223 1.7 deraadt printtime++;
224 1.7 deraadt break;
225 1.1 brezak default:
226 1.1 brezak usage();
227 1.1 brezak /*NOTREACHED*/
228 1.1 brezak }
229 1.1 brezak
230 1.4 brezak setlinebuf(stdout);
231 1.1 brezak if (argc == optind)
232 1.1 brezak allhosts();
233 1.1 brezak else {
234 1.1 brezak for (; optind < argc; optind++)
235 1.1 brezak (void) onehost(argv[optind]);
236 1.1 brezak }
237 1.1 brezak exit(0);
238 1.1 brezak }
239