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