rup.c revision 85039
12343Scsgr/*- 22343Scsgr * Copyright (c) 1993, John Brezak 32343Scsgr * All rights reserved. 42343Scsgr * 52343Scsgr * Redistribution and use in source and binary forms, with or without 62343Scsgr * modification, are permitted provided that the following conditions 72343Scsgr * are met: 82343Scsgr * 1. Redistributions of source code must retain the above copyright 92343Scsgr * notice, this list of conditions and the following disclaimer. 102343Scsgr * 2. Redistributions in binary form must reproduce the above copyright 112343Scsgr * notice, this list of conditions and the following disclaimer in the 122343Scsgr * documentation and/or other materials provided with the distribution. 132343Scsgr * 3. All advertising materials mentioning features or use of this software 142343Scsgr * must display the following acknowledgement: 152343Scsgr * This product includes software developed by the University of 162343Scsgr * California, Berkeley and its contributors. 172343Scsgr * 4. Neither the name of the University nor the names of its contributors 182343Scsgr * may be used to endorse or promote products derived from this software 192343Scsgr * without specific prior written permission. 202343Scsgr * 212343Scsgr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 222343Scsgr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 232343Scsgr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 242343Scsgr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 252343Scsgr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 262343Scsgr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 272343Scsgr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 282343Scsgr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 292343Scsgr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 302343Scsgr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312343Scsgr * SUCH DAMAGE. 322343Scsgr */ 332343Scsgr 342343Scsgr#ifndef lint 3527955Scharnierstatic const char rcsid[] = 3650477Speter "$FreeBSD: head/usr.bin/rup/rup.c 85039 2001-10-17 01:44:34Z fenner $"; 372343Scsgr#endif /* not lint */ 382343Scsgr 3927955Scharnier#include <err.h> 402343Scsgr#include <stdio.h> 412343Scsgr#include <stdlib.h> 422343Scsgr#include <string.h> 432343Scsgr#include <time.h> 4427955Scharnier#include <unistd.h> 452343Scsgr#include <sys/param.h> 462343Scsgr#include <sys/socket.h> 472343Scsgr#include <netdb.h> 482343Scsgr#include <rpc/rpc.h> 4927955Scharnier#include <rpc/pmap_clnt.h> 502343Scsgr#include <arpa/inet.h> 512343Scsgr#undef FSHIFT /* Use protocol's shift and scale values */ 522343Scsgr#undef FSCALE 532343Scsgr#include <rpcsvc/rstat.h> 542343Scsgr 552343Scsgr#define HOST_WIDTH 15 562343Scsgr 572343Scsgrstruct host_list { 582343Scsgr struct host_list *next; 592343Scsgr struct in_addr addr; 602343Scsgr} *hosts; 612343Scsgr 622343Scsgrint search_host(struct in_addr addr) 632343Scsgr{ 642343Scsgr struct host_list *hp; 658874Srgrimes 662343Scsgr if (!hosts) 672343Scsgr return(0); 682343Scsgr 692343Scsgr for (hp = hosts; hp != NULL; hp = hp->next) { 702343Scsgr if (hp->addr.s_addr == addr.s_addr) 712343Scsgr return(1); 722343Scsgr } 732343Scsgr return(0); 742343Scsgr} 752343Scsgr 762343Scsgrvoid remember_host(struct in_addr addr) 772343Scsgr{ 782343Scsgr struct host_list *hp; 792343Scsgr 8027955Scharnier if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) 8127955Scharnier errx(1, "no memory"); 822343Scsgr hp->addr.s_addr = addr.s_addr; 832343Scsgr hp->next = hosts; 842343Scsgr hosts = hp; 852343Scsgr} 862343Scsgr 8727955Scharnierint 8874462Salfredrstat_reply(caddr_t replyp, struct sockaddr_in *raddrp) 892343Scsgr{ 902343Scsgr struct tm *tmp_time; 912343Scsgr struct tm host_time; 922343Scsgr struct tm host_uptime; 932343Scsgr char days_buf[16]; 942343Scsgr char hours_buf[16]; 952343Scsgr struct hostent *hp; 962343Scsgr char *host; 972343Scsgr statstime *host_stat = (statstime *)replyp; 982343Scsgr 992343Scsgr if (search_host(raddrp->sin_addr)) 1002343Scsgr return(0); 1018874Srgrimes 1022343Scsgr hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr, 1032343Scsgr sizeof(struct in_addr), AF_INET); 1042343Scsgr if (hp) 1052343Scsgr host = hp->h_name; 1062343Scsgr else 1072343Scsgr host = inet_ntoa(raddrp->sin_addr); 1082343Scsgr 1092343Scsgr /* truncate hostname to fit nicely into field */ 1102343Scsgr if (strlen(host) > HOST_WIDTH) 1112343Scsgr host[HOST_WIDTH] = '\0'; 1122343Scsgr 1132343Scsgr printf("%-*s\t", HOST_WIDTH, host); 1142343Scsgr 1152343Scsgr tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec); 1162343Scsgr host_time = *tmp_time; 1172343Scsgr 1182343Scsgr host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec; 1192343Scsgr 1202343Scsgr tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec); 1212343Scsgr host_uptime = *tmp_time; 1222343Scsgr 12312445Swpaul #define updays (host_stat->curtime.tv_sec / 86400) 1242343Scsgr if (host_uptime.tm_yday != 0) 12512445Swpaul sprintf(days_buf, "%3d day%s, ", updays, 12612445Swpaul (updays > 1) ? "s" : ""); 1272343Scsgr else 1282343Scsgr days_buf[0] = '\0'; 1292343Scsgr 1302343Scsgr if (host_uptime.tm_hour != 0) 1312343Scsgr sprintf(hours_buf, "%2d:%02d, ", 1322343Scsgr host_uptime.tm_hour, host_uptime.tm_min); 1332343Scsgr else 1342343Scsgr if (host_uptime.tm_min != 0) 1352343Scsgr sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min); 13685039Sfenner else if (host_stat->curtime.tv_sec < 60) 13785039Sfenner sprintf(hours_buf, "%2d secs, ", host_uptime.tm_sec); 1382343Scsgr else 1392343Scsgr hours_buf[0] = '\0'; 1402343Scsgr 1412343Scsgr printf(" %2d:%02d%cm up %9.9s%9.9s load average: %.2f %.2f %.2f\n", 14234993Sdanny (host_time.tm_hour % 12) ? host_time.tm_hour % 12 : 12, 1432343Scsgr host_time.tm_min, 1442343Scsgr (host_time.tm_hour >= 12) ? 'p' : 'a', 1452343Scsgr days_buf, 1462343Scsgr hours_buf, 1472343Scsgr (double)host_stat->avenrun[0]/FSCALE, 1482343Scsgr (double)host_stat->avenrun[1]/FSCALE, 1492343Scsgr (double)host_stat->avenrun[2]/FSCALE); 1502343Scsgr 1512343Scsgr remember_host(raddrp->sin_addr); 1522343Scsgr return(0); 1532343Scsgr} 1542343Scsgr 15527955Scharnierint 1562343Scsgronehost(char *host) 1572343Scsgr{ 1582343Scsgr CLIENT *rstat_clnt; 1592343Scsgr statstime host_stat; 1602343Scsgr struct sockaddr_in addr; 1612343Scsgr struct hostent *hp; 16221093Speter struct timeval tv; 1638874Srgrimes 1642343Scsgr hp = gethostbyname(host); 1652343Scsgr if (hp == NULL) { 16627955Scharnier warnx("unknown host \"%s\"", host); 1672343Scsgr return(-1); 1682343Scsgr } 1692343Scsgr 1702343Scsgr rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp"); 1712343Scsgr if (rstat_clnt == NULL) { 17227955Scharnier warnx("%s %s", host, clnt_spcreateerror("")); 1732343Scsgr return(-1); 1742343Scsgr } 1752343Scsgr 1762343Scsgr bzero((char *)&host_stat, sizeof(host_stat)); 17721093Speter tv.tv_sec = 15; /* XXX ??? */ 17821093Speter tv.tv_usec = 0; 17921093Speter if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, tv) != RPC_SUCCESS) { 18027955Scharnier warnx("%s: %s", host, clnt_sperror(rstat_clnt, host)); 18178456Smikeh clnt_destroy(rstat_clnt); 1822343Scsgr return(-1); 1832343Scsgr } 1842343Scsgr 1852343Scsgr addr.sin_addr.s_addr = *(int *)hp->h_addr; 18674462Salfred rstat_reply((caddr_t)&host_stat, &addr); 18778456Smikeh clnt_destroy(rstat_clnt); 18827955Scharnier return (0); 1892343Scsgr} 1902343Scsgr 19127955Scharniervoid 1922343Scsgrallhosts() 1932343Scsgr{ 1942343Scsgr statstime host_stat; 1952343Scsgr enum clnt_stat clnt_stat; 1962343Scsgr 1972343Scsgr clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS, 1982343Scsgr xdr_void, NULL, 19929468Sjkh xdr_statstime, (char *)&host_stat, rstat_reply); 20027955Scharnier if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) 20127955Scharnier errx(1, "%s", clnt_sperrno(clnt_stat)); 2022343Scsgr} 2032343Scsgr 20427955Scharnierstatic void 2052343Scsgrusage() 2062343Scsgr{ 20727955Scharnier fprintf(stderr, "usage: rup [hosts ...]\n"); 2082343Scsgr exit(1); 2092343Scsgr} 2102343Scsgr 21127955Scharnierint 2122343Scsgrmain(int argc, char *argv[]) 2132343Scsgr{ 2142343Scsgr int ch; 2152343Scsgr 2162343Scsgr while ((ch = getopt(argc, argv, "?")) != -1) 2172343Scsgr switch (ch) { 2182343Scsgr default: 2192343Scsgr usage(); 2202343Scsgr /*NOTREACHED*/ 2212343Scsgr } 2228874Srgrimes 2232343Scsgr setlinebuf(stdout); 2242343Scsgr if (argc == optind) 2252343Scsgr allhosts(); 2262343Scsgr else { 2272343Scsgr for (; optind < argc; optind++) 2282343Scsgr (void) onehost(argv[optind]); 2292343Scsgr } 2302343Scsgr exit(0); 2312343Scsgr} 232