1 /* $OpenBSD: rup.c,v 1.24 2009/10/27 23:59:43 deraadt 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. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <time.h> 35 #include <sys/param.h> 36 #include <sys/socket.h> 37 #include <netdb.h> 38 #include <rpc/rpc.h> 39 #include <rpc/pmap_clnt.h> 40 #include <arpa/inet.h> 41 #include <err.h> 42 43 #undef FSHIFT /* Use protocol's shift and scale values */ 44 #undef FSCALE 45 #include <rpcsvc/rstat.h> 46 47 #define HOST_WIDTH 27 48 49 int printtime; /* print the remote host(s)'s time */ 50 51 struct host_list { 52 struct host_list *next; 53 struct in_addr addr; 54 } *hosts; 55 56 void usage(void); 57 int print_rup_data(char *, statstime *host_stat); 58 59 static int 60 search_host(struct in_addr addr) 61 { 62 struct host_list *hp; 63 64 if (!hosts) 65 return(0); 66 67 for (hp = hosts; hp != NULL; hp = hp->next) { 68 if (hp->addr.s_addr == addr.s_addr) 69 return(1); 70 } 71 return(0); 72 } 73 74 static void 75 remember_host(struct in_addr addr) 76 { 77 struct host_list *hp; 78 79 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) { 80 err(1, NULL); 81 /* NOTREACHED */ 82 } 83 hp->addr.s_addr = addr.s_addr; 84 hp->next = hosts; 85 hosts = hp; 86 } 87 88 89 struct rup_data { 90 char *host; 91 struct statstime statstime; 92 }; 93 struct rup_data *rup_data; 94 int rup_data_idx = 0; 95 int rup_data_max = 0; 96 97 enum sort_type { 98 SORT_NONE, 99 SORT_HOST, 100 SORT_LDAV, 101 SORT_UPTIME 102 }; 103 enum sort_type sort_type; 104 105 static int 106 compare(const void *v1, const void *v2) 107 { 108 const struct rup_data *d1 = v1; 109 const struct rup_data *d2 = v2; 110 111 switch(sort_type) { 112 case SORT_HOST: 113 return strcmp(d1->host, d2->host); 114 case SORT_LDAV: 115 return d1->statstime.avenrun[0] 116 - d2->statstime.avenrun[0]; 117 case SORT_UPTIME: 118 return d1->statstime.boottime.tv_sec 119 - d2->statstime.boottime.tv_sec; 120 default: 121 /* something's really wrong here */ 122 abort(); 123 } 124 } 125 126 static void 127 remember_rup_data(char *host, struct statstime *st) 128 { 129 if (rup_data_idx >= rup_data_max) { 130 int newsize; 131 struct rup_data *newrup; 132 133 newsize = rup_data_max + 16; 134 newrup = realloc(rup_data, newsize * sizeof(struct rup_data)); 135 if (newrup == NULL) { 136 err(1, NULL); 137 /* NOTREACHED */ 138 } 139 rup_data = newrup; 140 rup_data_max = newsize; 141 } 142 143 if ((rup_data[rup_data_idx].host = strdup(host)) == NULL) 144 err(1, NULL); 145 rup_data[rup_data_idx].statstime = *st; 146 rup_data_idx++; 147 } 148 149 150 static int 151 rstat_reply(char *replyp, struct sockaddr_in *raddrp) 152 { 153 struct hostent *hp; 154 char *host; 155 statstime *host_stat = (statstime *)replyp; 156 157 if (!search_host(raddrp->sin_addr)) { 158 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr, 159 sizeof(struct in_addr), AF_INET); 160 if (hp) 161 host = hp->h_name; 162 else 163 host = inet_ntoa(raddrp->sin_addr); 164 165 remember_host(raddrp->sin_addr); 166 167 if (sort_type != SORT_NONE) 168 remember_rup_data(host, host_stat); 169 else 170 print_rup_data(host, host_stat); 171 } 172 173 return (0); 174 } 175 176 177 int 178 print_rup_data(char *host, statstime *host_stat) 179 { 180 unsigned int ups = 0, upm = 0, uph = 0, upd = 0; 181 struct tm *tmp_time, host_time; 182 char days_buf[16], hours_buf[16]; 183 184 if (printtime) 185 printf("%-*.*s", HOST_WIDTH-8, HOST_WIDTH-8, host); 186 else 187 printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host); 188 189 tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec); 190 host_time = *tmp_time; 191 192 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec; 193 194 if (host_stat->curtime.tv_sec > 0) 195 ups = host_stat->curtime.tv_sec; 196 upd = ups / (3600 * 24); 197 ups -= upd * 3600 * 24; 198 uph = ups / 3600; 199 ups -= uph * 3600; 200 upm = ups / 60; 201 202 if (upd != 0) 203 snprintf(days_buf, sizeof days_buf, "%3u day%s, ", upd, 204 (upd > 1) ? "s" : ""); 205 else 206 days_buf[0] = '\0'; 207 208 if (uph != 0) 209 snprintf(hours_buf, sizeof hours_buf, "%2u:%02u, ", 210 uph, upm); 211 else 212 if (upm != 0) 213 snprintf(hours_buf, sizeof hours_buf, "%2u min%s ", 214 upm, (upm == 1) ? ", " : "s,"); 215 else 216 hours_buf[0] = '\0'; 217 218 if (printtime) 219 printf(" %2d:%02d%cm", 220 (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12, 221 host_time.tm_min, 222 (host_time.tm_hour >= 12) ? 'p' : 'a'); 223 224 printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n", 225 days_buf, hours_buf, 226 (double)host_stat->avenrun[0] / FSCALE, 227 (double)host_stat->avenrun[1] / FSCALE, 228 (double)host_stat->avenrun[2] / FSCALE); 229 230 return(0); 231 } 232 233 234 static void 235 onehost(char *host) 236 { 237 CLIENT *rstat_clnt; 238 statstime host_stat; 239 static struct timeval timeout = {25, 0}; 240 extern char *__progname; 241 242 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp"); 243 if (rstat_clnt == NULL) { 244 fprintf(stderr, "%s: %s", __progname, 245 clnt_spcreateerror(host)); 246 return; 247 } 248 249 bzero((char *)&host_stat, sizeof(host_stat)); 250 if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, 251 xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) { 252 fprintf(stderr, "%s: %s", __progname, 253 clnt_sperror(rstat_clnt, host)); 254 clnt_destroy(rstat_clnt); 255 return; 256 } 257 258 if (sort_type != SORT_NONE) 259 remember_rup_data(host, &host_stat); 260 else 261 print_rup_data(host, &host_stat); 262 263 clnt_destroy(rstat_clnt); 264 } 265 266 static void 267 allhosts(void) 268 { 269 statstime host_stat; 270 enum clnt_stat clnt_stat; 271 extern char *__progname; 272 273 if (sort_type != SORT_NONE) { 274 printf("collecting responses...\n"); 275 fflush(stdout); 276 } 277 278 clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS, 279 xdr_void, NULL, xdr_statstime, (char *)&host_stat, rstat_reply); 280 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) { 281 fprintf(stderr, "%s: %s\n", __progname, clnt_sperrno(clnt_stat)); 282 exit(1); 283 } 284 } 285 286 int 287 main(int argc, char *argv[]) 288 { 289 int ch; 290 size_t i; 291 extern int optind; 292 293 sort_type = SORT_NONE; 294 while ((ch = getopt(argc, argv, "dhlt")) != -1) 295 switch (ch) { 296 case 'd': 297 printtime = 1; 298 break; 299 case 'h': 300 sort_type = SORT_HOST; 301 break; 302 case 'l': 303 sort_type = SORT_LDAV; 304 break; 305 case 't': 306 sort_type = SORT_UPTIME; 307 break; 308 default: 309 usage(); 310 /*NOTREACHED*/ 311 } 312 313 setlinebuf(stdout); 314 315 if (argc == optind) 316 allhosts(); 317 else { 318 for (; optind < argc; optind++) 319 onehost(argv[optind]); 320 } 321 322 if (sort_type != SORT_NONE) { 323 qsort(rup_data, rup_data_idx, sizeof(struct rup_data), 324 compare); 325 326 for (i = 0; i < rup_data_idx; i++) { 327 print_rup_data(rup_data[i].host, 328 &rup_data[i].statstime); 329 } 330 } 331 332 exit(0); 333 } 334 335 336 void 337 usage(void) 338 { 339 fprintf(stderr, "usage: rup [-dhlt] [host ...]\n"); 340 exit(1); 341 } 342