Home | History | Annotate | Line # | Download | only in rup
rup.c revision 1.22
      1  1.22      fvdl /*	$NetBSD: rup.c,v 1.22 2000/06/03 19:32:34 fvdl Exp $	*/
      2  1.12   thorpej 
      3   1.1    brezak /*-
      4   1.1    brezak  * Copyright (c) 1993, John Brezak
      5   1.1    brezak  * All rights reserved.
      6   1.1    brezak  *
      7   1.1    brezak  * Redistribution and use in source and binary forms, with or without
      8   1.1    brezak  * modification, are permitted provided that the following conditions
      9   1.1    brezak  * are met:
     10   1.1    brezak  * 1. Redistributions of source code must retain the above copyright
     11   1.1    brezak  *    notice, this list of conditions and the following disclaimer.
     12   1.1    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    brezak  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    brezak  *    documentation and/or other materials provided with the distribution.
     15   1.1    brezak  * 3. All advertising materials mentioning features or use of this software
     16   1.1    brezak  *    must display the following acknowledgement:
     17   1.1    brezak  *	This product includes software developed by the University of
     18   1.1    brezak  *	California, Berkeley and its contributors.
     19   1.1    brezak  * 4. Neither the name of the University nor the names of its contributors
     20   1.1    brezak  *    may be used to endorse or promote products derived from this software
     21   1.1    brezak  *    without specific prior written permission.
     22   1.1    brezak  *
     23   1.1    brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1    brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1    brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1    brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1    brezak  * SUCH DAMAGE.
     34   1.1    brezak  */
     35   1.1    brezak 
     36  1.14  drochner #include <sys/cdefs.h>
     37   1.5   mycroft #ifndef lint
     38  1.22      fvdl __RCSID("$NetBSD: rup.c,v 1.22 2000/06/03 19:32:34 fvdl Exp $");
     39   1.5   mycroft #endif /* not lint */
     40   1.5   mycroft 
     41  1.16     perry #include <sys/types.h>
     42  1.16     perry #include <sys/socket.h>
     43  1.16     perry #include <netinet/in.h>
     44  1.16     perry #include <arpa/inet.h>
     45  1.16     perry 
     46  1.16     perry #include <err.h>
     47  1.16     perry #include <netdb.h>
     48  1.16     perry #include <rpc/rpc.h>
     49   1.1    brezak #include <stdio.h>
     50   1.6       jtc #include <stdlib.h>
     51   1.1    brezak #include <string.h>
     52   1.1    brezak #include <time.h>
     53  1.16     perry #include <unistd.h>
     54   1.6       jtc 
     55   1.6       jtc #undef FSHIFT			/* Use protocol's shift and scale values */
     56   1.6       jtc #undef FSCALE
     57   1.1    brezak #include <rpcsvc/rstat.h>
     58   1.1    brezak 
     59   1.7   deraadt #define HOST_WIDTH 24
     60   1.1    brezak 
     61   1.7   deraadt int printtime;			/* print the remote host(s)'s time */
     62   1.7   deraadt 
     63   1.1    brezak struct host_list {
     64   1.1    brezak 	struct host_list *next;
     65  1.21      fvdl 	int family;
     66  1.21      fvdl 	union {
     67  1.21      fvdl 		struct in6_addr _addr6;
     68  1.21      fvdl 		struct in_addr _addr4;
     69  1.21      fvdl 	} addr;
     70   1.1    brezak } *hosts;
     71   1.1    brezak 
     72  1.21      fvdl #define addr6 addr._addr6
     73  1.21      fvdl #define addr4 addr._addr4
     74  1.21      fvdl 
     75  1.22      fvdl int search_host(struct sockaddr *);
     76  1.22      fvdl void remember_host(struct sockaddr *);
     77  1.14  drochner 
     78   1.7   deraadt int
     79  1.21      fvdl search_host(struct sockaddr *sa)
     80   1.1    brezak {
     81   1.1    brezak 	struct host_list *hp;
     82   1.1    brezak 
     83   1.1    brezak 	if (!hosts)
     84   1.1    brezak 		return(0);
     85   1.1    brezak 
     86   1.1    brezak 	for (hp = hosts; hp != NULL; hp = hp->next) {
     87  1.21      fvdl 		switch (hp->family) {
     88  1.21      fvdl 		case AF_INET6:
     89  1.21      fvdl 			if (!memcmp(&hp->addr6,
     90  1.21      fvdl 			    &((struct sockaddr_in6 *)sa)->sin6_addr,
     91  1.21      fvdl 			    sizeof (struct in6_addr)))
     92  1.21      fvdl 				return 1;
     93  1.21      fvdl 			break;
     94  1.21      fvdl 		case AF_INET:
     95  1.21      fvdl 			if (!memcmp(&hp->addr4,
     96  1.21      fvdl 			    &((struct sockaddr_in *)sa)->sin_addr,
     97  1.21      fvdl 			    sizeof (struct in_addr)))
     98  1.21      fvdl 				return 1;
     99  1.21      fvdl 			break;
    100  1.21      fvdl 		default:
    101  1.21      fvdl 		}
    102   1.1    brezak 	}
    103   1.1    brezak 	return(0);
    104   1.1    brezak }
    105   1.1    brezak 
    106   1.7   deraadt void
    107  1.21      fvdl remember_host(struct sockaddr *sa)
    108   1.1    brezak {
    109   1.1    brezak 	struct host_list *hp;
    110   1.1    brezak 
    111   1.1    brezak 	if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
    112  1.14  drochner 		err(1, "malloc");
    113   1.9       jtc 		/* NOTREACHED */
    114   1.1    brezak 	}
    115  1.21      fvdl 	hp->family = sa->sa_family;
    116   1.1    brezak 	hp->next = hosts;
    117  1.21      fvdl 	switch (sa->sa_family) {
    118  1.21      fvdl 	case AF_INET6:
    119  1.21      fvdl 		memcpy(&hp->addr6, &((struct sockaddr_in6 *)sa)->sin6_addr,
    120  1.21      fvdl 		    sizeof (struct in6_addr));
    121  1.21      fvdl 		break;
    122  1.21      fvdl 	case AF_INET:
    123  1.21      fvdl 		memcpy(&hp->addr4, &((struct sockaddr_in *)sa)->sin_addr,
    124  1.21      fvdl 		    sizeof (struct in_addr));
    125  1.21      fvdl 		break;
    126  1.21      fvdl 	default:
    127  1.21      fvdl 		err(1, "unknown address family");
    128  1.21      fvdl 		/* NOTREACHED */
    129  1.21      fvdl 	}
    130   1.1    brezak 	hosts = hp;
    131   1.1    brezak }
    132   1.1    brezak 
    133   1.9       jtc struct rup_data {
    134  1.19  jdolecek 	const char *host;
    135   1.9       jtc 	struct statstime statstime;
    136   1.9       jtc };
    137   1.9       jtc struct rup_data *rup_data;
    138   1.9       jtc int rup_data_idx = 0;
    139   1.9       jtc int rup_data_max = 0;
    140   1.9       jtc 
    141   1.9       jtc enum sort_type {
    142   1.9       jtc 	SORT_NONE,
    143   1.9       jtc 	SORT_HOST,
    144   1.9       jtc 	SORT_LDAV,
    145   1.9       jtc 	SORT_UPTIME
    146   1.9       jtc };
    147   1.9       jtc enum sort_type sort_type;
    148   1.9       jtc 
    149  1.22      fvdl int compare(struct rup_data *, struct rup_data *);
    150  1.22      fvdl void remember_rup_data(const char *, struct statstime *);
    151  1.22      fvdl int rstat_reply(char *, struct netbuf *, struct netconfig *);
    152  1.22      fvdl int print_rup_data(const char *, statstime *);
    153  1.22      fvdl void onehost(char *);
    154  1.22      fvdl void allhosts(void);
    155  1.22      fvdl int main(int, char *[]);
    156  1.22      fvdl void usage(void);
    157  1.14  drochner 
    158  1.14  drochner int
    159  1.21      fvdl compare(struct rup_data *d1, struct rup_data *d2)
    160   1.9       jtc {
    161   1.9       jtc 	switch(sort_type) {
    162   1.9       jtc 	case SORT_HOST:
    163   1.9       jtc 		return strcmp(d1->host, d2->host);
    164   1.9       jtc 	case SORT_LDAV:
    165   1.9       jtc 		return d1->statstime.avenrun[0]
    166   1.9       jtc 			- d2->statstime.avenrun[0];
    167   1.9       jtc 	case SORT_UPTIME:
    168   1.9       jtc 		return d1->statstime.boottime.tv_sec
    169   1.9       jtc 			- d2->statstime.boottime.tv_sec;
    170   1.9       jtc 	default:
    171   1.9       jtc 		/* something's really wrong here */
    172   1.9       jtc 		abort();
    173   1.9       jtc 	}
    174   1.9       jtc }
    175   1.9       jtc 
    176   1.9       jtc void
    177  1.21      fvdl remember_rup_data(const char *host, struct statstime *st)
    178   1.9       jtc {
    179   1.9       jtc         if (rup_data_idx >= rup_data_max) {
    180   1.9       jtc                 rup_data_max += 16;
    181   1.9       jtc                 rup_data = realloc (rup_data,
    182   1.9       jtc 				rup_data_max * sizeof(struct rup_data));
    183   1.9       jtc                 if (rup_data == NULL) {
    184  1.14  drochner                         err (1, "realloc");
    185   1.9       jtc 			/* NOTREACHED */
    186   1.9       jtc                 }
    187   1.9       jtc         }
    188   1.9       jtc 
    189   1.9       jtc 	rup_data[rup_data_idx].host = strdup(host);
    190   1.9       jtc 	rup_data[rup_data_idx].statstime = *st;
    191   1.9       jtc 	rup_data_idx++;
    192   1.9       jtc }
    193   1.9       jtc 
    194   1.9       jtc 
    195   1.9       jtc int
    196  1.21      fvdl rstat_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf)
    197   1.1    brezak {
    198  1.21      fvdl 	char host[NI_MAXHOST];
    199   1.9       jtc 	statstime *host_stat = (statstime *)replyp;
    200  1.21      fvdl 	struct sockaddr *sa = raddrp->buf;
    201   1.9       jtc 
    202  1.21      fvdl 	if (!search_host(sa)) {
    203  1.21      fvdl 		if (getnameinfo(sa, sa->sa_len, host, sizeof host, NULL, 0, 0))
    204  1.21      fvdl 			return 0;
    205   1.9       jtc 
    206  1.21      fvdl 		remember_host(sa);
    207   1.9       jtc 
    208   1.9       jtc 		if (sort_type != SORT_NONE) {
    209   1.9       jtc 			remember_rup_data(host, host_stat);
    210   1.9       jtc 		} else {
    211   1.9       jtc 			print_rup_data(host, host_stat);
    212   1.9       jtc 		}
    213   1.9       jtc 	}
    214   1.9       jtc 
    215   1.9       jtc 	return (0);
    216   1.9       jtc }
    217   1.9       jtc 
    218   1.9       jtc 
    219   1.9       jtc int
    220  1.21      fvdl print_rup_data(const char *host, statstime *host_stat)
    221   1.9       jtc {
    222   1.1    brezak 	struct tm *tmp_time;
    223   1.1    brezak 	struct tm host_time;
    224  1.13     perry 	unsigned ups=0,upm=0,uph=0,upd=0;
    225  1.13     perry 
    226   1.1    brezak 	char days_buf[16];
    227   1.1    brezak 	char hours_buf[16];
    228   1.1    brezak 
    229  1.18   hubertf 	if (printtime)
    230  1.18   hubertf 		printf("%-*.*s", HOST_WIDTH-4, HOST_WIDTH-4, host);
    231  1.18   hubertf 	else
    232  1.18   hubertf 		printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
    233   1.1    brezak 
    234   1.1    brezak 	tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
    235   1.1    brezak 	host_time = *tmp_time;
    236   1.1    brezak 
    237   1.1    brezak 	host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
    238   1.1    brezak 
    239  1.13     perry 	ups=host_stat->curtime.tv_sec;
    240  1.13     perry 	upd=ups/(3600*24);
    241  1.13     perry 	ups-=upd*3600*24;
    242  1.13     perry 	uph=ups/3600;
    243  1.13     perry 	ups-=uph*3600;
    244  1.13     perry 	upm=ups/60;
    245  1.13     perry 
    246  1.13     perry 	if (upd != 0)
    247  1.13     perry 		sprintf(days_buf, "%3u day%s, ", upd,
    248  1.13     perry 			(upd > 1) ? "s" : "");
    249   1.1    brezak 	else
    250   1.1    brezak 		days_buf[0] = '\0';
    251   1.1    brezak 
    252  1.13     perry 	if (uph != 0)
    253  1.13     perry 		sprintf(hours_buf, "%2u:%02u, ",
    254  1.13     perry 			uph, upm);
    255   1.1    brezak 	else
    256  1.13     perry 		if (upm != 0)
    257  1.17       abs 			sprintf(hours_buf, "%2u min%s ", upm,
    258  1.17       abs 			    (upm == 1) ? ", " : "s,");
    259   1.1    brezak 		else
    260   1.1    brezak 			hours_buf[0] = '\0';
    261   1.7   deraadt 	if (printtime)
    262  1.11   thorpej 		printf(" %2d:%02d%cm",
    263  1.11   thorpej 		    (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
    264  1.11   thorpej 		    host_time.tm_min, (host_time.tm_hour >= 12) ? 'p' : 'a');
    265   1.7   deraadt 
    266   1.7   deraadt 	printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
    267   1.7   deraadt 		days_buf, hours_buf,
    268   1.6       jtc 		(double)host_stat->avenrun[0]/FSCALE,
    269   1.6       jtc 		(double)host_stat->avenrun[1]/FSCALE,
    270   1.6       jtc 		(double)host_stat->avenrun[2]/FSCALE);
    271   1.1    brezak 
    272   1.1    brezak 	return(0);
    273   1.1    brezak }
    274   1.1    brezak 
    275   1.9       jtc 
    276   1.9       jtc void
    277  1.21      fvdl onehost(char *host)
    278   1.1    brezak {
    279   1.1    brezak 	CLIENT *rstat_clnt;
    280   1.1    brezak 	statstime host_stat;
    281  1.10        pk 	static struct timeval timeout = {25, 0};
    282   1.1    brezak 
    283   1.1    brezak 	rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
    284   1.1    brezak 	if (rstat_clnt == NULL) {
    285   1.9       jtc 		warnx("%s", clnt_spcreateerror(host));
    286   1.9       jtc 		return;
    287   1.1    brezak 	}
    288   1.1    brezak 
    289  1.15     lukem 	memset((char *)&host_stat, 0, sizeof(host_stat));
    290  1.10        pk 	if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
    291   1.9       jtc 		warnx("%s",  clnt_sperror(rstat_clnt, host));
    292   1.9       jtc 		return;
    293   1.1    brezak 	}
    294   1.1    brezak 
    295   1.9       jtc 	print_rup_data(host, &host_stat);
    296   1.9       jtc 	clnt_destroy(rstat_clnt);
    297   1.1    brezak }
    298   1.1    brezak 
    299   1.9       jtc void
    300   1.1    brezak allhosts()
    301   1.1    brezak {
    302   1.1    brezak 	statstime host_stat;
    303   1.1    brezak 	enum clnt_stat clnt_stat;
    304   1.9       jtc 	size_t i;
    305   1.9       jtc 
    306   1.9       jtc 	if (sort_type != SORT_NONE) {
    307   1.9       jtc 		printf("collecting responses...");
    308   1.9       jtc 		fflush(stdout);
    309   1.9       jtc 	}
    310   1.1    brezak 
    311  1.21      fvdl 	clnt_stat = rpc_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
    312   1.1    brezak 				   xdr_void, NULL,
    313  1.21      fvdl 				   xdr_statstime, (char*)&host_stat,
    314  1.21      fvdl 				   (resultproc_t)rstat_reply, "udp");
    315   1.1    brezak 	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
    316   1.9       jtc 		warnx("%s", clnt_sperrno(clnt_stat));
    317   1.1    brezak 		exit(1);
    318   1.1    brezak 	}
    319   1.9       jtc 
    320   1.9       jtc 	if (sort_type != SORT_NONE) {
    321   1.9       jtc 		putchar('\n');
    322  1.14  drochner 		qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
    323  1.14  drochner 		      (int (*)(const void*, const void*))compare);
    324   1.9       jtc 
    325   1.9       jtc 		for (i = 0; i < rup_data_idx; i++) {
    326   1.9       jtc 			print_rup_data(rup_data[i].host, &rup_data[i].statstime);
    327   1.9       jtc 		}
    328   1.9       jtc 	}
    329   1.1    brezak }
    330   1.1    brezak 
    331  1.14  drochner int
    332  1.21      fvdl main(int argc, char *argv[])
    333   1.1    brezak {
    334   1.1    brezak 	int ch;
    335   1.1    brezak 
    336   1.9       jtc 	sort_type = SORT_NONE;
    337   1.9       jtc 	while ((ch = getopt(argc, argv, "dhlt")) != -1)
    338   1.1    brezak 		switch (ch) {
    339   1.9       jtc 		case 'd':
    340   1.9       jtc 			printtime = 1;
    341   1.9       jtc 			break;
    342   1.9       jtc 		case 'h':
    343   1.9       jtc 			sort_type = SORT_HOST;
    344   1.9       jtc 			break;
    345   1.9       jtc 		case 'l':
    346   1.9       jtc 			sort_type = SORT_LDAV;
    347   1.9       jtc 			break;
    348   1.7   deraadt 		case 't':
    349   1.9       jtc 			sort_type = SORT_UPTIME;
    350   1.7   deraadt 			break;
    351   1.1    brezak 		default:
    352   1.1    brezak 			usage();
    353   1.1    brezak 			/*NOTREACHED*/
    354   1.1    brezak 		}
    355   1.1    brezak 
    356   1.4    brezak 	setlinebuf(stdout);
    357   1.9       jtc 
    358   1.1    brezak 	if (argc == optind)
    359   1.1    brezak 		allhosts();
    360   1.1    brezak 	else {
    361   1.1    brezak 		for (; optind < argc; optind++)
    362   1.9       jtc 			onehost(argv[optind]);
    363   1.1    brezak 	}
    364   1.9       jtc 
    365   1.1    brezak 	exit(0);
    366   1.9       jtc }
    367   1.9       jtc 
    368  1.14  drochner void
    369   1.9       jtc usage()
    370   1.9       jtc {
    371   1.9       jtc 	fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n");
    372   1.9       jtc 	exit(1);
    373   1.1    brezak }
    374