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