1 /* $NetBSD: rup.c,v 1.22 2000/06/03 19:32:34 fvdl Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, John Brezak 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __RCSID("$NetBSD: rup.c,v 1.22 2000/06/03 19:32:34 fvdl Exp $"); 39 #endif /* not lint */ 40 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 #include <netinet/in.h> 44 #include <arpa/inet.h> 45 46 #include <err.h> 47 #include <netdb.h> 48 #include <rpc/rpc.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <time.h> 53 #include <unistd.h> 54 55 #undef FSHIFT /* Use protocol's shift and scale values */ 56 #undef FSCALE 57 #include <rpcsvc/rstat.h> 58 59 #define HOST_WIDTH 24 60 61 int printtime; /* print the remote host(s)'s time */ 62 63 struct host_list { 64 struct host_list *next; 65 int family; 66 union { 67 struct in6_addr _addr6; 68 struct in_addr _addr4; 69 } addr; 70 } *hosts; 71 72 #define addr6 addr._addr6 73 #define addr4 addr._addr4 74 75 int search_host(struct sockaddr *); 76 void remember_host(struct sockaddr *); 77 78 int 79 search_host(struct sockaddr *sa) 80 { 81 struct host_list *hp; 82 83 if (!hosts) 84 return(0); 85 86 for (hp = hosts; hp != NULL; hp = hp->next) { 87 switch (hp->family) { 88 case AF_INET6: 89 if (!memcmp(&hp->addr6, 90 &((struct sockaddr_in6 *)sa)->sin6_addr, 91 sizeof (struct in6_addr))) 92 return 1; 93 break; 94 case AF_INET: 95 if (!memcmp(&hp->addr4, 96 &((struct sockaddr_in *)sa)->sin_addr, 97 sizeof (struct in_addr))) 98 return 1; 99 break; 100 default: 101 } 102 } 103 return(0); 104 } 105 106 void 107 remember_host(struct sockaddr *sa) 108 { 109 struct host_list *hp; 110 111 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) { 112 err(1, "malloc"); 113 /* NOTREACHED */ 114 } 115 hp->family = sa->sa_family; 116 hp->next = hosts; 117 switch (sa->sa_family) { 118 case AF_INET6: 119 memcpy(&hp->addr6, &((struct sockaddr_in6 *)sa)->sin6_addr, 120 sizeof (struct in6_addr)); 121 break; 122 case AF_INET: 123 memcpy(&hp->addr4, &((struct sockaddr_in *)sa)->sin_addr, 124 sizeof (struct in_addr)); 125 break; 126 default: 127 err(1, "unknown address family"); 128 /* NOTREACHED */ 129 } 130 hosts = hp; 131 } 132 133 struct rup_data { 134 const char *host; 135 struct statstime statstime; 136 }; 137 struct rup_data *rup_data; 138 int rup_data_idx = 0; 139 int rup_data_max = 0; 140 141 enum sort_type { 142 SORT_NONE, 143 SORT_HOST, 144 SORT_LDAV, 145 SORT_UPTIME 146 }; 147 enum sort_type sort_type; 148 149 int compare(struct rup_data *, struct rup_data *); 150 void remember_rup_data(const char *, struct statstime *); 151 int rstat_reply(char *, struct netbuf *, struct netconfig *); 152 int print_rup_data(const char *, statstime *); 153 void onehost(char *); 154 void allhosts(void); 155 int main(int, char *[]); 156 void usage(void); 157 158 int 159 compare(struct rup_data *d1, struct rup_data *d2) 160 { 161 switch(sort_type) { 162 case SORT_HOST: 163 return strcmp(d1->host, d2->host); 164 case SORT_LDAV: 165 return d1->statstime.avenrun[0] 166 - d2->statstime.avenrun[0]; 167 case SORT_UPTIME: 168 return d1->statstime.boottime.tv_sec 169 - d2->statstime.boottime.tv_sec; 170 default: 171 /* something's really wrong here */ 172 abort(); 173 } 174 } 175 176 void 177 remember_rup_data(const char *host, struct statstime *st) 178 { 179 if (rup_data_idx >= rup_data_max) { 180 rup_data_max += 16; 181 rup_data = realloc (rup_data, 182 rup_data_max * sizeof(struct rup_data)); 183 if (rup_data == NULL) { 184 err (1, "realloc"); 185 /* NOTREACHED */ 186 } 187 } 188 189 rup_data[rup_data_idx].host = strdup(host); 190 rup_data[rup_data_idx].statstime = *st; 191 rup_data_idx++; 192 } 193 194 195 int 196 rstat_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf) 197 { 198 char host[NI_MAXHOST]; 199 statstime *host_stat = (statstime *)replyp; 200 struct sockaddr *sa = raddrp->buf; 201 202 if (!search_host(sa)) { 203 if (getnameinfo(sa, sa->sa_len, host, sizeof host, NULL, 0, 0)) 204 return 0; 205 206 remember_host(sa); 207 208 if (sort_type != SORT_NONE) { 209 remember_rup_data(host, host_stat); 210 } else { 211 print_rup_data(host, host_stat); 212 } 213 } 214 215 return (0); 216 } 217 218 219 int 220 print_rup_data(const char *host, statstime *host_stat) 221 { 222 struct tm *tmp_time; 223 struct tm host_time; 224 unsigned ups=0,upm=0,uph=0,upd=0; 225 226 char days_buf[16]; 227 char hours_buf[16]; 228 229 if (printtime) 230 printf("%-*.*s", HOST_WIDTH-4, HOST_WIDTH-4, host); 231 else 232 printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host); 233 234 tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec); 235 host_time = *tmp_time; 236 237 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec; 238 239 ups=host_stat->curtime.tv_sec; 240 upd=ups/(3600*24); 241 ups-=upd*3600*24; 242 uph=ups/3600; 243 ups-=uph*3600; 244 upm=ups/60; 245 246 if (upd != 0) 247 sprintf(days_buf, "%3u day%s, ", upd, 248 (upd > 1) ? "s" : ""); 249 else 250 days_buf[0] = '\0'; 251 252 if (uph != 0) 253 sprintf(hours_buf, "%2u:%02u, ", 254 uph, upm); 255 else 256 if (upm != 0) 257 sprintf(hours_buf, "%2u min%s ", upm, 258 (upm == 1) ? ", " : "s,"); 259 else 260 hours_buf[0] = '\0'; 261 if (printtime) 262 printf(" %2d:%02d%cm", 263 (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12, 264 host_time.tm_min, (host_time.tm_hour >= 12) ? 'p' : 'a'); 265 266 printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n", 267 days_buf, hours_buf, 268 (double)host_stat->avenrun[0]/FSCALE, 269 (double)host_stat->avenrun[1]/FSCALE, 270 (double)host_stat->avenrun[2]/FSCALE); 271 272 return(0); 273 } 274 275 276 void 277 onehost(char *host) 278 { 279 CLIENT *rstat_clnt; 280 statstime host_stat; 281 static struct timeval timeout = {25, 0}; 282 283 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp"); 284 if (rstat_clnt == NULL) { 285 warnx("%s", clnt_spcreateerror(host)); 286 return; 287 } 288 289 memset((char *)&host_stat, 0, sizeof(host_stat)); 290 if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) { 291 warnx("%s", clnt_sperror(rstat_clnt, host)); 292 return; 293 } 294 295 print_rup_data(host, &host_stat); 296 clnt_destroy(rstat_clnt); 297 } 298 299 void 300 allhosts() 301 { 302 statstime host_stat; 303 enum clnt_stat clnt_stat; 304 size_t i; 305 306 if (sort_type != SORT_NONE) { 307 printf("collecting responses..."); 308 fflush(stdout); 309 } 310 311 clnt_stat = rpc_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS, 312 xdr_void, NULL, 313 xdr_statstime, (char*)&host_stat, 314 (resultproc_t)rstat_reply, "udp"); 315 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) { 316 warnx("%s", clnt_sperrno(clnt_stat)); 317 exit(1); 318 } 319 320 if (sort_type != SORT_NONE) { 321 putchar('\n'); 322 qsort(rup_data, rup_data_idx, sizeof(struct rup_data), 323 (int (*)(const void*, const void*))compare); 324 325 for (i = 0; i < rup_data_idx; i++) { 326 print_rup_data(rup_data[i].host, &rup_data[i].statstime); 327 } 328 } 329 } 330 331 int 332 main(int argc, char *argv[]) 333 { 334 int ch; 335 336 sort_type = SORT_NONE; 337 while ((ch = getopt(argc, argv, "dhlt")) != -1) 338 switch (ch) { 339 case 'd': 340 printtime = 1; 341 break; 342 case 'h': 343 sort_type = SORT_HOST; 344 break; 345 case 'l': 346 sort_type = SORT_LDAV; 347 break; 348 case 't': 349 sort_type = SORT_UPTIME; 350 break; 351 default: 352 usage(); 353 /*NOTREACHED*/ 354 } 355 356 setlinebuf(stdout); 357 358 if (argc == optind) 359 allhosts(); 360 else { 361 for (; optind < argc; optind++) 362 onehost(argv[optind]); 363 } 364 365 exit(0); 366 } 367 368 void 369 usage() 370 { 371 fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n"); 372 exit(1); 373 } 374