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