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