2000-04-14 10:11:07 +04:00
|
|
|
/* $NetBSD: rup.c,v 1.20 2000/04/14 06:11:09 simonb Exp $ */
|
1996-09-27 05:46:08 +04:00
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993, John Brezak
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-08-24 14:53:16 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-08-02 21:48:44 +04:00
|
|
|
#ifndef lint
|
2000-04-14 10:11:07 +04:00
|
|
|
__RCSID("$NetBSD: rup.c,v 1.20 2000/04/14 06:11:09 simonb Exp $");
|
1993-08-02 21:48:44 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1998-02-03 07:04:58 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <rpc/rpc.h>
|
1993-06-03 23:32:26 +04:00
|
|
|
#include <stdio.h>
|
1993-09-23 22:37:28 +04:00
|
|
|
#include <stdlib.h>
|
1993-06-03 23:32:26 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
1998-02-03 07:04:58 +03:00
|
|
|
#include <unistd.h>
|
1993-09-23 22:37:28 +04:00
|
|
|
|
|
|
|
#undef FSHIFT /* Use protocol's shift and scale values */
|
|
|
|
#undef FSCALE
|
1993-06-03 23:32:26 +04:00
|
|
|
#include <rpcsvc/rstat.h>
|
|
|
|
|
1993-11-10 06:52:18 +03:00
|
|
|
#define HOST_WIDTH 24
|
1993-06-03 23:32:26 +04:00
|
|
|
|
1993-11-10 06:52:18 +03:00
|
|
|
int printtime; /* print the remote host(s)'s time */
|
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
struct host_list {
|
|
|
|
struct host_list *next;
|
|
|
|
struct in_addr addr;
|
|
|
|
} *hosts;
|
|
|
|
|
1997-08-24 14:53:16 +04:00
|
|
|
int search_host __P((struct in_addr));
|
|
|
|
void remember_host __P((struct in_addr));
|
|
|
|
|
1993-11-10 06:52:18 +03:00
|
|
|
int
|
|
|
|
search_host(addr)
|
|
|
|
struct in_addr addr;
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
struct host_list *hp;
|
|
|
|
|
|
|
|
if (!hosts)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
for (hp = hosts; hp != NULL; hp = hp->next) {
|
|
|
|
if (hp->addr.s_addr == addr.s_addr)
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1993-11-10 06:52:18 +03:00
|
|
|
void
|
|
|
|
remember_host(addr)
|
|
|
|
struct in_addr addr;
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
struct host_list *hp;
|
|
|
|
|
|
|
|
if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
|
1997-08-24 14:53:16 +04:00
|
|
|
err(1, "malloc");
|
1993-12-10 02:58:02 +03:00
|
|
|
/* NOTREACHED */
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
hp->addr.s_addr = addr.s_addr;
|
|
|
|
hp->next = hosts;
|
|
|
|
hosts = hp;
|
|
|
|
}
|
|
|
|
|
1993-12-10 02:58:02 +03:00
|
|
|
struct rup_data {
|
2000-03-08 15:10:28 +03:00
|
|
|
const char *host;
|
1993-12-10 02:58:02 +03:00
|
|
|
struct statstime statstime;
|
|
|
|
};
|
|
|
|
struct rup_data *rup_data;
|
|
|
|
int rup_data_idx = 0;
|
|
|
|
int rup_data_max = 0;
|
|
|
|
|
|
|
|
enum sort_type {
|
|
|
|
SORT_NONE,
|
|
|
|
SORT_HOST,
|
|
|
|
SORT_LDAV,
|
|
|
|
SORT_UPTIME
|
|
|
|
};
|
|
|
|
enum sort_type sort_type;
|
|
|
|
|
1997-08-24 14:53:16 +04:00
|
|
|
int compare __P((struct rup_data *, struct rup_data *));
|
2000-03-08 15:10:28 +03:00
|
|
|
void remember_rup_data __P((const char *, struct statstime *));
|
1997-08-24 14:53:16 +04:00
|
|
|
int rstat_reply __P((char *, struct sockaddr_in *));
|
2000-03-08 15:10:28 +03:00
|
|
|
int print_rup_data __P((const char *, statstime *));
|
1997-08-24 14:53:16 +04:00
|
|
|
void onehost __P((char *));
|
|
|
|
void allhosts __P((void));
|
|
|
|
int main __P((int, char *[]));
|
|
|
|
void usage __P((void));
|
|
|
|
|
|
|
|
int
|
1993-12-10 02:58:02 +03:00
|
|
|
compare(d1, d2)
|
|
|
|
struct rup_data *d1;
|
|
|
|
struct rup_data *d2;
|
|
|
|
{
|
|
|
|
switch(sort_type) {
|
|
|
|
case SORT_HOST:
|
|
|
|
return strcmp(d1->host, d2->host);
|
|
|
|
case SORT_LDAV:
|
|
|
|
return d1->statstime.avenrun[0]
|
|
|
|
- d2->statstime.avenrun[0];
|
|
|
|
case SORT_UPTIME:
|
|
|
|
return d1->statstime.boottime.tv_sec
|
|
|
|
- d2->statstime.boottime.tv_sec;
|
|
|
|
default:
|
|
|
|
/* something's really wrong here */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
remember_rup_data(host, st)
|
2000-03-08 15:10:28 +03:00
|
|
|
const char *host;
|
1993-12-10 02:58:02 +03:00
|
|
|
struct statstime *st;
|
|
|
|
{
|
|
|
|
if (rup_data_idx >= rup_data_max) {
|
|
|
|
rup_data_max += 16;
|
|
|
|
rup_data = realloc (rup_data,
|
|
|
|
rup_data_max * sizeof(struct rup_data));
|
|
|
|
if (rup_data == NULL) {
|
1997-08-24 14:53:16 +04:00
|
|
|
err (1, "realloc");
|
1993-12-10 02:58:02 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rup_data[rup_data_idx].host = strdup(host);
|
|
|
|
rup_data[rup_data_idx].statstime = *st;
|
|
|
|
rup_data_idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1993-11-10 06:52:18 +03:00
|
|
|
rstat_reply(replyp, raddrp)
|
|
|
|
char *replyp;
|
|
|
|
struct sockaddr_in *raddrp;
|
1993-12-10 02:58:02 +03:00
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
char *host;
|
|
|
|
statstime *host_stat = (statstime *)replyp;
|
|
|
|
|
|
|
|
if (!search_host(raddrp->sin_addr)) {
|
|
|
|
hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
|
|
|
|
sizeof(struct in_addr), AF_INET);
|
|
|
|
|
|
|
|
if (hp)
|
|
|
|
host = hp->h_name;
|
|
|
|
else
|
|
|
|
host = inet_ntoa(raddrp->sin_addr);
|
|
|
|
|
|
|
|
remember_host(raddrp->sin_addr);
|
|
|
|
|
|
|
|
if (sort_type != SORT_NONE) {
|
|
|
|
remember_rup_data(host, host_stat);
|
|
|
|
} else {
|
|
|
|
print_rup_data(host, host_stat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
print_rup_data(host, host_stat)
|
2000-03-08 15:10:28 +03:00
|
|
|
const char *host;
|
1993-12-10 02:58:02 +03:00
|
|
|
statstime *host_stat;
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
struct tm *tmp_time;
|
|
|
|
struct tm host_time;
|
1997-01-17 01:18:16 +03:00
|
|
|
unsigned ups=0,upm=0,uph=0,upd=0;
|
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
char days_buf[16];
|
|
|
|
char hours_buf[16];
|
|
|
|
|
1999-07-30 05:29:30 +04:00
|
|
|
if (printtime)
|
|
|
|
printf("%-*.*s", HOST_WIDTH-4, HOST_WIDTH-4, host);
|
|
|
|
else
|
|
|
|
printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
|
1993-06-03 23:32:26 +04:00
|
|
|
|
|
|
|
tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
|
|
|
|
host_time = *tmp_time;
|
|
|
|
|
|
|
|
host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
|
|
|
|
|
1997-01-17 01:18:16 +03:00
|
|
|
ups=host_stat->curtime.tv_sec;
|
|
|
|
upd=ups/(3600*24);
|
|
|
|
ups-=upd*3600*24;
|
|
|
|
uph=ups/3600;
|
|
|
|
ups-=uph*3600;
|
|
|
|
upm=ups/60;
|
1993-06-03 23:32:26 +04:00
|
|
|
|
1997-01-17 01:18:16 +03:00
|
|
|
if (upd != 0)
|
|
|
|
sprintf(days_buf, "%3u day%s, ", upd,
|
|
|
|
(upd > 1) ? "s" : "");
|
1993-06-03 23:32:26 +04:00
|
|
|
else
|
|
|
|
days_buf[0] = '\0';
|
|
|
|
|
1997-01-17 01:18:16 +03:00
|
|
|
if (uph != 0)
|
|
|
|
sprintf(hours_buf, "%2u:%02u, ",
|
|
|
|
uph, upm);
|
1993-06-03 23:32:26 +04:00
|
|
|
else
|
1997-01-17 01:18:16 +03:00
|
|
|
if (upm != 0)
|
1999-01-10 11:19:23 +03:00
|
|
|
sprintf(hours_buf, "%2u min%s ", upm,
|
|
|
|
(upm == 1) ? ", " : "s,");
|
1993-06-03 23:32:26 +04:00
|
|
|
else
|
|
|
|
hours_buf[0] = '\0';
|
1993-11-10 06:52:18 +03:00
|
|
|
if (printtime)
|
1996-09-27 05:44:58 +04:00
|
|
|
printf(" %2d:%02d%cm",
|
|
|
|
(host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
|
|
|
|
host_time.tm_min, (host_time.tm_hour >= 12) ? 'p' : 'a');
|
1993-11-10 06:52:18 +03:00
|
|
|
|
|
|
|
printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
|
|
|
|
days_buf, hours_buf,
|
1993-09-23 22:37:28 +04:00
|
|
|
(double)host_stat->avenrun[0]/FSCALE,
|
|
|
|
(double)host_stat->avenrun[1]/FSCALE,
|
|
|
|
(double)host_stat->avenrun[2]/FSCALE);
|
1993-06-03 23:32:26 +04:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1993-12-10 02:58:02 +03:00
|
|
|
|
|
|
|
void
|
1993-11-10 06:52:18 +03:00
|
|
|
onehost(host)
|
|
|
|
char *host;
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
CLIENT *rstat_clnt;
|
|
|
|
statstime host_stat;
|
1994-02-05 17:58:14 +03:00
|
|
|
static struct timeval timeout = {25, 0};
|
1993-06-03 23:32:26 +04:00
|
|
|
|
|
|
|
rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
|
|
|
|
if (rstat_clnt == NULL) {
|
1993-12-10 02:58:02 +03:00
|
|
|
warnx("%s", clnt_spcreateerror(host));
|
|
|
|
return;
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
|
1997-10-19 18:25:00 +04:00
|
|
|
memset((char *)&host_stat, 0, sizeof(host_stat));
|
1994-02-05 17:58:14 +03:00
|
|
|
if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
|
1993-12-10 02:58:02 +03:00
|
|
|
warnx("%s", clnt_sperror(rstat_clnt, host));
|
|
|
|
return;
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
|
1993-12-10 02:58:02 +03:00
|
|
|
print_rup_data(host, &host_stat);
|
|
|
|
clnt_destroy(rstat_clnt);
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
|
1993-12-10 02:58:02 +03:00
|
|
|
void
|
1993-06-03 23:32:26 +04:00
|
|
|
allhosts()
|
|
|
|
{
|
|
|
|
statstime host_stat;
|
|
|
|
enum clnt_stat clnt_stat;
|
1993-12-10 02:58:02 +03:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (sort_type != SORT_NONE) {
|
|
|
|
printf("collecting responses...");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
1993-06-03 23:32:26 +04:00
|
|
|
|
|
|
|
clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
|
|
|
|
xdr_void, NULL,
|
1997-08-24 14:53:16 +04:00
|
|
|
xdr_statstime, (char*)&host_stat, rstat_reply);
|
1993-06-03 23:32:26 +04:00
|
|
|
if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
|
1993-12-10 02:58:02 +03:00
|
|
|
warnx("%s", clnt_sperrno(clnt_stat));
|
1993-06-03 23:32:26 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1993-12-10 02:58:02 +03:00
|
|
|
if (sort_type != SORT_NONE) {
|
|
|
|
putchar('\n');
|
1997-08-24 14:53:16 +04:00
|
|
|
qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
|
|
|
|
(int (*)(const void*, const void*))compare);
|
1993-12-10 02:58:02 +03:00
|
|
|
|
|
|
|
for (i = 0; i < rup_data_idx; i++) {
|
|
|
|
print_rup_data(rup_data[i].host, &rup_data[i].statstime);
|
|
|
|
}
|
|
|
|
}
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
|
1997-08-24 14:53:16 +04:00
|
|
|
int
|
1993-11-10 06:52:18 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
1993-12-10 02:58:02 +03:00
|
|
|
sort_type = SORT_NONE;
|
|
|
|
while ((ch = getopt(argc, argv, "dhlt")) != -1)
|
1993-06-03 23:32:26 +04:00
|
|
|
switch (ch) {
|
1993-12-10 02:58:02 +03:00
|
|
|
case 'd':
|
|
|
|
printtime = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
sort_type = SORT_HOST;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
sort_type = SORT_LDAV;
|
|
|
|
break;
|
1993-11-10 06:52:18 +03:00
|
|
|
case 't':
|
1993-12-10 02:58:02 +03:00
|
|
|
sort_type = SORT_UPTIME;
|
1993-11-10 06:52:18 +03:00
|
|
|
break;
|
1993-06-03 23:32:26 +04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
1993-06-10 16:28:06 +04:00
|
|
|
setlinebuf(stdout);
|
1993-12-10 02:58:02 +03:00
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
if (argc == optind)
|
|
|
|
allhosts();
|
|
|
|
else {
|
|
|
|
for (; optind < argc; optind++)
|
1993-12-10 02:58:02 +03:00
|
|
|
onehost(argv[optind]);
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
1993-12-10 02:58:02 +03:00
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
exit(0);
|
|
|
|
}
|
1993-12-10 02:58:02 +03:00
|
|
|
|
1997-08-24 14:53:16 +04:00
|
|
|
void
|
1993-12-10 02:58:02 +03:00
|
|
|
usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|