1/*- 2 * SPDX-License-Identifier: BSD-4-Clause 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 37#include <sys/param.h> 38#include <sys/socket.h> 39 40#include <rpc/rpc.h> 41#include <rpc/pmap_clnt.h> 42 43#undef FSHIFT /* Use protocol's shift and scale values */ 44#undef FSCALE 45 46#include <rpcsvc/rstat.h> 47 48#include <arpa/inet.h> 49 50#include <err.h> 51#include <netdb.h> 52#include <stdio.h> 53#include <string.h> 54#include <stdlib.h> 55#include <time.h> 56#include <unistd.h> 57 58#define HOST_WIDTH 15 59 60static struct host_list { 61 struct host_list *next; 62 struct in_addr addr; 63} *hosts; 64 65static int 66search_host(struct in_addr addr) 67{ 68 struct host_list *hp; 69 70 if (!hosts) 71 return(0); 72 73 for (hp = hosts; hp != NULL; hp = hp->next) { 74 if (hp->addr.s_addr == addr.s_addr) 75 return(1); 76 } 77 return(0); 78} 79 80static void 81remember_host(struct in_addr addr) 82{ 83 struct host_list *hp; 84 85 if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) 86 errx(1, "no memory"); 87 hp->addr.s_addr = addr.s_addr; 88 hp->next = hosts; 89 hosts = hp; 90} 91 92static bool_t 93rstat_reply(statstime *host_stat, struct sockaddr_in *raddrp) 94{ 95 struct tm *tmp_time; 96 struct tm host_time; 97 struct tm host_uptime; 98 char days_buf[16]; 99 char hours_buf[16]; 100 struct hostent *hp; 101 char *host; 102 time_t tmp_time_t; 103 104 if (search_host(raddrp->sin_addr)) 105 return(0); 106 107 hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr, 108 sizeof(struct in_addr), AF_INET); 109 if (hp) 110 host = hp->h_name; 111 else 112 host = inet_ntoa(raddrp->sin_addr); 113 114 /* truncate hostname to fit nicely into field */ 115 if (strlen(host) > HOST_WIDTH) 116 host[HOST_WIDTH] = '\0'; 117 118 printf("%-*s\t", HOST_WIDTH, host); 119 120 tmp_time_t = host_stat->curtime.tv_sec; 121 tmp_time = localtime(&tmp_time_t); 122 host_time = *tmp_time; 123 124 host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec; 125 126 tmp_time_t = host_stat->curtime.tv_sec; 127 tmp_time = gmtime(&tmp_time_t); 128 host_uptime = *tmp_time; 129 130 #define updays (host_stat->curtime.tv_sec / 86400) 131 if (host_uptime.tm_yday != 0) 132 sprintf(days_buf, "%3d day%s, ", updays, 133 (updays > 1) ? "s" : ""); 134 else 135 days_buf[0] = '\0'; 136 137 if (host_uptime.tm_hour != 0) 138 sprintf(hours_buf, "%2d:%02d, ", 139 host_uptime.tm_hour, host_uptime.tm_min); 140 else 141 if (host_uptime.tm_min != 0) 142 sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min); 143 else if (host_stat->curtime.tv_sec < 60) 144 sprintf(hours_buf, "%2d secs, ", host_uptime.tm_sec); 145 else 146 hours_buf[0] = '\0'; 147 148 printf(" %2d:%02d%cm up %9.9s%9.9s load average: %.2f %.2f %.2f\n", 149 (host_time.tm_hour % 12) ? host_time.tm_hour % 12 : 12, 150 host_time.tm_min, 151 (host_time.tm_hour >= 12) ? 'p' : 'a', 152 days_buf, 153 hours_buf, 154 (double)host_stat->avenrun[0]/FSCALE, 155 (double)host_stat->avenrun[1]/FSCALE, 156 (double)host_stat->avenrun[2]/FSCALE); 157 158 remember_host(raddrp->sin_addr); 159 return(0); 160} 161 162static int 163onehost(char *host) 164{ 165 CLIENT *rstat_clnt; 166 statstime host_stat; 167 struct sockaddr_in addr; 168 struct hostent *hp; 169 struct timeval tv; 170 171 hp = gethostbyname(host); 172 if (hp == NULL) { 173 warnx("unknown host \"%s\"", host); 174 return(-1); 175 } 176 177 rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp"); 178 if (rstat_clnt == NULL) { 179 warnx("%s %s", host, clnt_spcreateerror("")); 180 return(-1); 181 } 182 183 bzero((char *)&host_stat, sizeof(host_stat)); 184 tv.tv_sec = 15; /* XXX ??? */ 185 tv.tv_usec = 0; 186 if (clnt_call(rstat_clnt, RSTATPROC_STATS, 187 (xdrproc_t)xdr_void, NULL, 188 (xdrproc_t)xdr_statstime, &host_stat, tv) != RPC_SUCCESS) { 189 warnx("%s: %s", host, clnt_sperror(rstat_clnt, host)); 190 clnt_destroy(rstat_clnt); 191 return(-1); 192 } 193 194 memcpy(&addr.sin_addr.s_addr, hp->h_addr, sizeof(int)); 195 rstat_reply(&host_stat, &addr); 196 clnt_destroy(rstat_clnt); 197 return (0); 198} 199 200static void 201allhosts(void) 202{ 203 statstime host_stat; 204 enum clnt_stat clnt_stat; 205 206 clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS, 207 (xdrproc_t)xdr_void, NULL, 208 (xdrproc_t)xdr_statstime, &host_stat, 209 (resultproc_t)rstat_reply); 210 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) 211 errx(1, "%s", clnt_sperrno(clnt_stat)); 212} 213 214static void 215usage(void) 216{ 217 fprintf(stderr, "usage: rup [host ...]\n"); 218 exit(1); 219} 220 221int 222main(int argc, char *argv[]) 223{ 224 int ch; 225 226 while ((ch = getopt(argc, argv, "?")) != -1) 227 switch (ch) { 228 default: 229 usage(); 230 } 231 232 setlinebuf(stdout); 233 if (argc == optind) 234 allhosts(); 235 else { 236 for (; optind < argc; optind++) 237 (void) onehost(argv[optind]); 238 } 239 exit(0); 240} 241