rup.c revision 27955
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[] = 3627955Scharnier "$Id: rup.c,v 1.7 1997/02/22 19:56:48 peter Exp $"; 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 882343Scsgrrstat_reply(char *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); 1362343Scsgr else 1372343Scsgr hours_buf[0] = '\0'; 1382343Scsgr 1392343Scsgr printf(" %2d:%02d%cm up %9.9s%9.9s load average: %.2f %.2f %.2f\n", 1402343Scsgr host_time.tm_hour % 12, 1412343Scsgr host_time.tm_min, 1422343Scsgr (host_time.tm_hour >= 12) ? 'p' : 'a', 1432343Scsgr days_buf, 1442343Scsgr hours_buf, 1452343Scsgr (double)host_stat->avenrun[0]/FSCALE, 1462343Scsgr (double)host_stat->avenrun[1]/FSCALE, 1472343Scsgr (double)host_stat->avenrun[2]/FSCALE); 1482343Scsgr 1492343Scsgr remember_host(raddrp->sin_addr); 1502343Scsgr return(0); 1512343Scsgr} 1522343Scsgr 15327955Scharnierint 1542343Scsgronehost(char *host) 1552343Scsgr{ 1562343Scsgr CLIENT *rstat_clnt; 1572343Scsgr statstime host_stat; 1582343Scsgr struct sockaddr_in addr; 1592343Scsgr struct hostent *hp; 16021093Speter struct timeval tv; 1618874Srgrimes 1622343Scsgr hp = gethostbyname(host); 1632343Scsgr if (hp == NULL) { 16427955Scharnier warnx("unknown host \"%s\"", host); 1652343Scsgr return(-1); 1662343Scsgr } 1672343Scsgr 1682343Scsgr rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp"); 1692343Scsgr if (rstat_clnt == NULL) { 17027955Scharnier warnx("%s %s", host, clnt_spcreateerror("")); 1712343Scsgr return(-1); 1722343Scsgr } 1732343Scsgr 1742343Scsgr bzero((char *)&host_stat, sizeof(host_stat)); 17521093Speter tv.tv_sec = 15; /* XXX ??? */ 17621093Speter tv.tv_usec = 0; 17721093Speter if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, tv) != RPC_SUCCESS) { 17827955Scharnier warnx("%s: %s", host, clnt_sperror(rstat_clnt, host)); 1792343Scsgr return(-1); 1802343Scsgr } 1812343Scsgr 1822343Scsgr addr.sin_addr.s_addr = *(int *)hp->h_addr; 1832343Scsgr rstat_reply((char *)&host_stat, &addr); 18427955Scharnier return (0); 1852343Scsgr} 1862343Scsgr 18727955Scharniervoid 1882343Scsgrallhosts() 1892343Scsgr{ 1902343Scsgr statstime host_stat; 1912343Scsgr enum clnt_stat clnt_stat; 1922343Scsgr 1932343Scsgr clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS, 1942343Scsgr xdr_void, NULL, 1952343Scsgr xdr_statstime, &host_stat, rstat_reply); 19627955Scharnier if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) 19727955Scharnier errx(1, "%s", clnt_sperrno(clnt_stat)); 1982343Scsgr} 1992343Scsgr 20027955Scharnierstatic void 2012343Scsgrusage() 2022343Scsgr{ 20327955Scharnier fprintf(stderr, "usage: rup [hosts ...]\n"); 2042343Scsgr exit(1); 2052343Scsgr} 2062343Scsgr 20727955Scharnierint 2082343Scsgrmain(int argc, char *argv[]) 2092343Scsgr{ 2102343Scsgr int ch; 2112343Scsgr extern int optind; 2122343Scsgr 2132343Scsgr while ((ch = getopt(argc, argv, "?")) != -1) 2142343Scsgr switch (ch) { 2152343Scsgr default: 2162343Scsgr usage(); 2172343Scsgr /*NOTREACHED*/ 2182343Scsgr } 2198874Srgrimes 2202343Scsgr setlinebuf(stdout); 2212343Scsgr if (argc == optind) 2222343Scsgr allhosts(); 2232343Scsgr else { 2242343Scsgr for (; optind < argc; optind++) 2252343Scsgr (void) onehost(argv[optind]); 2262343Scsgr } 2272343Scsgr exit(0); 2282343Scsgr} 229