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 --- 18 unchanged lines hidden (view full) --- 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 "$Id: rup.c,v 1.7 1997/02/22 19:56:48 peter Exp $"; |
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; --- 7 unchanged lines hidden (view full) --- 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(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; --- 49 unchanged lines hidden (view full) --- 145 (double)host_stat->avenrun[0]/FSCALE, 146 (double)host_stat->avenrun[1]/FSCALE, 147 (double)host_stat->avenrun[2]/FSCALE); 148 149 remember_host(raddrp->sin_addr); 150 return(0); 151} 152 |
153int |
154onehost(char *host) 155{ 156 CLIENT *rstat_clnt; 157 statstime host_stat; 158 struct sockaddr_in addr; 159 struct hostent *hp; 160 struct timeval tv; 161 162 hp = gethostbyname(host); 163 if (hp == NULL) { |
164 warnx("unknown host \"%s\"", host); |
165 return(-1); 166 } 167 168 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp"); 169 if (rstat_clnt == NULL) { |
170 warnx("%s %s", host, clnt_spcreateerror("")); |
171 return(-1); 172 } 173 174 bzero((char *)&host_stat, sizeof(host_stat)); 175 tv.tv_sec = 15; /* XXX ??? */ 176 tv.tv_usec = 0; 177 if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, tv) != RPC_SUCCESS) { |
178 warnx("%s: %s", host, clnt_sperror(rstat_clnt, host)); |
179 return(-1); 180 } 181 182 addr.sin_addr.s_addr = *(int *)hp->h_addr; 183 rstat_reply((char *)&host_stat, &addr); |
184 return (0); |
185} 186 |
187void |
188allhosts() 189{ 190 statstime host_stat; 191 enum clnt_stat clnt_stat; 192 193 clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS, 194 xdr_void, NULL, 195 xdr_statstime, &host_stat, rstat_reply); |
196 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) 197 errx(1, "%s", clnt_sperrno(clnt_stat)); |
198} 199 |
200static void |
201usage() 202{ |
203 fprintf(stderr, "usage: rup [hosts ...]\n"); |
204 exit(1); 205} 206 |
207int |
208main(int argc, char *argv[]) 209{ 210 int ch; 211 extern int optind; 212 |
213 while ((ch = getopt(argc, argv, "?")) != -1) 214 switch (ch) { 215 default: 216 usage(); 217 /*NOTREACHED*/ 218 } 219 220 setlinebuf(stdout); 221 if (argc == optind) 222 allhosts(); 223 else { 224 for (; optind < argc; optind++) 225 (void) onehost(argv[optind]); 226 } 227 exit(0); 228} |