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