1 /* $NetBSD: rup.c,v 1.27 2007/12/15 19:44:53 perry 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.27 2007/12/15 19:44:53 perry 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 static int printtime; /* print the remote host(s)'s time */ 62 63 static 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 static int search_host(struct sockaddr *); 76 static void remember_host(struct sockaddr *); 77 78 static 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 *)(void *)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 *)(void *)sa)->sin_addr, 97 sizeof (struct in_addr))) 98 return 1; 99 break; 100 default: 101 break; 102 } 103 } 104 return 0; 105 } 106 107 static void 108 remember_host(struct sockaddr *sa) 109 { 110 struct host_list *hp; 111 112 if ((hp = malloc(sizeof(struct host_list))) == NULL) { 113 err(1, "malloc"); 114 /* NOTREACHED */ 115 } 116 hp->family = sa->sa_family; 117 hp->next = hosts; 118 switch (sa->sa_family) { 119 case AF_INET6: 120 (void)memcpy(&hp->addr6, 121 &((struct sockaddr_in6 *)(void *)sa)->sin6_addr, 122 sizeof (struct in6_addr)); 123 break; 124 case AF_INET: 125 (void)memcpy(&hp->addr4, 126 &((struct sockaddr_in *)(void *)sa)->sin_addr, 127 sizeof (struct in_addr)); 128 break; 129 default: 130 errx(1, "unknown address family"); 131 /* NOTREACHED */ 132 } 133 hosts = hp; 134 } 135 136 struct rup_data { 137 const char *host; 138 struct statstime statstime; 139 }; 140 static struct rup_data *rup_data; 141 static size_t rup_data_idx = 0; 142 static size_t rup_data_max = 0; 143 144 enum sort_type { 145 SORT_NONE, 146 SORT_HOST, 147 SORT_LDAV, 148 SORT_UPTIME 149 }; 150 static enum sort_type sort_type; 151 152 static int compare(struct rup_data *, struct rup_data *); 153 static void remember_rup_data(const char *, struct statstime *); 154 static int rstat_reply(char *, struct netbuf *, struct netconfig *); 155 static void print_rup_data(const char *, statstime *); 156 static int onehost(char *); 157 static void allhosts(void); 158 static void usage(void) __dead; 159 int main(int, char *[]); 160 161 int 162 compare(struct rup_data *d1, struct rup_data *d2) 163 { 164 switch(sort_type) { 165 case SORT_HOST: 166 return strcmp(d1->host, d2->host); 167 case SORT_LDAV: 168 return d1->statstime.avenrun[0] 169 - d2->statstime.avenrun[0]; 170 case SORT_UPTIME: 171 return d1->statstime.boottime.tv_sec 172 - d2->statstime.boottime.tv_sec; 173 default: 174 /* something's really wrong here */ 175 abort(); 176 /*NOTREACHED*/ 177 } 178 } 179 180 static void 181 remember_rup_data(const char *host, struct statstime *st) 182 { 183 struct rup_data *n; 184 185 if (rup_data_idx >= rup_data_max) { 186 n = realloc(rup_data, 187 (rup_data_max + 16) * sizeof(struct rup_data)); 188 if (n == NULL) { 189 err (1, "realloc"); 190 /* NOTREACHED */ 191 } 192 rup_data = n; 193 rup_data_max += 16; 194 } 195 196 rup_data[rup_data_idx].host = strdup(host); 197 rup_data[rup_data_idx].statstime = *st; 198 rup_data_idx++; 199 } 200 201 202 static int 203 /*ARGSUSED*/ 204 rstat_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf) 205 { 206 char host[NI_MAXHOST]; 207 statstime *host_stat = (statstime *)(void *)replyp; 208 struct sockaddr *sa = raddrp->buf; 209 210 if (!search_host(sa)) { 211 if (getnameinfo(sa, (socklen_t)sa->sa_len, host, sizeof host, 212 NULL, 0, 0)) 213 return 0; 214 215 remember_host(sa); 216 217 if (sort_type != SORT_NONE) { 218 remember_rup_data(host, host_stat); 219 } else { 220 print_rup_data(host, host_stat); 221 } 222 } 223 224 return 0; 225 } 226 227 228 static void 229 print_rup_data(const char *host, statstime *host_stat) 230 { 231 struct tm *tmp_time; 232 struct tm host_time; 233 unsigned ups = 0, upm = 0, uph = 0, upd = 0; 234 time_t now; 235 236 char days_buf[16]; 237 char hours_buf[16]; 238 239 if (printtime) 240 (void)printf("%-*.*s", HOST_WIDTH-4, HOST_WIDTH-4, host); 241 else 242 (void)printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host); 243 244 now = host_stat->curtime.tv_sec; 245 tmp_time = localtime(&now); 246 host_time = *tmp_time; 247 248 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec; 249 250 ups=host_stat->curtime.tv_sec; 251 upd=ups / (3600 * 24); 252 ups-=upd * 3600 * 24; 253 uph=ups / 3600; 254 ups-=uph * 3600; 255 upm=ups / 60; 256 257 if (upd != 0) 258 (void)snprintf(days_buf, sizeof(days_buf), "%3u day%s, ", upd, 259 (upd > 1) ? "s" : ""); 260 else 261 days_buf[0] = '\0'; 262 263 if (uph != 0) 264 (void)snprintf(hours_buf, sizeof(hours_buf), "%2u:%02u, ", 265 uph, upm); 266 else 267 if (upm != 0) 268 (void)snprintf(hours_buf, sizeof(hours_buf), 269 "%2u min%s ", upm, (upm == 1) ? ", " : "s,"); 270 else if (ups < 60) 271 (void)snprintf(hours_buf, sizeof(hours_buf), 272 "%2u secs ", ups); 273 else 274 hours_buf[0] = '\0'; 275 if (printtime) 276 (void)printf(" %2d:%02d%cm", 277 (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12, 278 host_time.tm_min, (host_time.tm_hour >= 12) ? 'p' : 'a'); 279 280 (void)printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n", 281 days_buf, hours_buf, (double)host_stat->avenrun[0]/FSCALE, 282 (double)host_stat->avenrun[1]/FSCALE, 283 (double)host_stat->avenrun[2]/FSCALE); 284 } 285 286 287 static int 288 onehost(char *host) 289 { 290 CLIENT *rstat_clnt; 291 statstime host_stat; 292 static struct timeval timeout = {25, 0}; 293 294 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp"); 295 if (rstat_clnt == NULL) { 296 warnx("%s", clnt_spcreateerror(host)); 297 return 1; 298 } 299 300 (void)memset(&host_stat, 0, sizeof(host_stat)); 301 if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, 302 xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) { 303 warnx("%s", clnt_sperror(rstat_clnt, host)); 304 clnt_destroy(rstat_clnt); 305 return 1; 306 } 307 308 print_rup_data(host, &host_stat); 309 clnt_destroy(rstat_clnt); 310 return 0; 311 } 312 313 static void 314 allhosts(void) 315 { 316 statstime host_stat; 317 enum clnt_stat clnt_stat; 318 size_t i; 319 320 if (sort_type != SORT_NONE) { 321 (void)printf("collecting responses..."); 322 (void)fflush(stdout); 323 } 324 325 clnt_stat = rpc_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS, 326 xdr_void, NULL, xdr_statstime, (caddr_t)(void *)&host_stat, 327 (resultproc_t)rstat_reply, "udp"); 328 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) 329 errx(1, "%s", clnt_sperrno(clnt_stat)); 330 331 if (sort_type != SORT_NONE) { 332 (void)putchar('\n'); 333 qsort(rup_data, rup_data_idx, sizeof(struct rup_data), 334 (int (*)(const void*, const void*))compare); 335 336 for (i = 0; i < rup_data_idx; i++) { 337 print_rup_data(rup_data[i].host, &rup_data[i].statstime); 338 } 339 } 340 } 341 342 int 343 main(int argc, char *argv[]) 344 { 345 int ch, retval; 346 347 setprogname(*argv); 348 sort_type = SORT_NONE; 349 retval = 0; 350 while ((ch = getopt(argc, argv, "dhlt")) != -1) 351 switch (ch) { 352 case 'd': 353 printtime = 1; 354 break; 355 case 'h': 356 sort_type = SORT_HOST; 357 break; 358 case 'l': 359 sort_type = SORT_LDAV; 360 break; 361 case 't': 362 sort_type = SORT_UPTIME; 363 break; 364 default: 365 usage(); 366 /*NOTREACHED*/ 367 } 368 369 (void)setlinebuf(stdout); 370 371 if (argc == optind) 372 allhosts(); 373 else { 374 for (; optind < argc; optind++) 375 retval += onehost(argv[optind]); 376 } 377 378 return retval ? EXIT_FAILURE : EXIT_SUCCESS; 379 } 380 381 void 382 usage(void) 383 { 384 (void)fprintf(stderr, "Usage: %s [-dhlt] [hosts ...]\n", 385 getprogname()); 386 exit(EXIT_SUCCESS); 387 } 388