Implemented sorting options found in Sun's rup.

This commit is contained in:
jtc 1993-12-09 23:58:02 +00:00
parent 3cf1d8505e
commit fe6e50dbb6
2 changed files with 151 additions and 54 deletions

View File

@ -31,7 +31,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Id: rup.1,v 1.7 1993/11/10 03:52:18 deraadt Exp $
.\" $Id: rup.1,v 1.8 1993/12/09 23:58:02 jtc Exp $
.\"
.Dd June 7, 1993
.Dt RUP 1
@ -40,8 +40,8 @@
.Nm rup
.Nd remote status display
.Sh SYNOPSIS
.Nm rup
.Op Fl t
.Nm rup
.Op Fl dhlt
.Op Ar host ...
.Sh DESCRIPTION
.Nm rup
@ -56,9 +56,15 @@ averaged over 1, 5 and 15 minutes.
.Pp
The following options are available:
.Bl -tag -width indent
.It Fl t
.It Fl d
For each host, report what it's local time is.
This is useful for checking time syncronization on a network.
.It Fl h
Sort the display alphabetically by host name.
.It Fl l
Sort the display by load average.
.It Fl t
Sort the display by up time.
.El
.Pp
The
@ -99,5 +105,3 @@ The
command
appeared in
.Tn SunOS .
.Sh BUGS
The sorting options are not implemented.

View File

@ -32,7 +32,7 @@
*/
#ifndef lint
static char rcsid[] = "$Id: rup.c,v 1.8 1993/12/03 23:34:10 deraadt Exp $";
static char rcsid[] = "$Id: rup.c,v 1.9 1993/12/09 23:58:04 jtc Exp $";
#endif /* not lint */
#include <stdio.h>
@ -44,6 +44,7 @@ static char rcsid[] = "$Id: rup.c,v 1.8 1993/12/03 23:34:10 deraadt Exp $";
#include <netdb.h>
#include <rpc/rpc.h>
#include <arpa/inet.h>
#include <err.h>
#undef FSHIFT /* Use protocol's shift and scale values */
#undef FSCALE
@ -51,8 +52,6 @@ static char rcsid[] = "$Id: rup.c,v 1.8 1993/12/03 23:34:10 deraadt Exp $";
#define HOST_WIDTH 24
char *argv0;
int printtime; /* print the remote host(s)'s time */
struct host_list {
@ -83,36 +82,113 @@ remember_host(addr)
struct host_list *hp;
if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
fprintf(stderr, "%s: no memory.\n", argv0);
exit(1);
err(1, NULL);
/* NOTREACHED */
}
hp->addr.s_addr = addr.s_addr;
hp->next = hosts;
hosts = hp;
}
struct rup_data {
char *host;
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;
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)
char *host;
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) {
err (1, NULL);
/* NOTREACHED */
}
}
rup_data[rup_data_idx].host = strdup(host);
rup_data[rup_data_idx].statstime = *st;
rup_data_idx++;
}
int
rstat_reply(replyp, raddrp)
char *replyp;
struct sockaddr_in *raddrp;
{
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)
char *host;
statstime *host_stat;
{
struct tm *tmp_time;
struct tm host_time;
struct tm host_uptime;
char days_buf[16];
char hours_buf[16];
struct hostent *hp;
char *host;
statstime *host_stat = (statstime *)replyp;
if (search_host(raddrp->sin_addr))
return(0);
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);
printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
@ -150,60 +226,63 @@ rstat_reply(replyp, raddrp)
(double)host_stat->avenrun[1]/FSCALE,
(double)host_stat->avenrun[2]/FSCALE);
remember_host(raddrp->sin_addr);
return(0);
}
void
onehost(host)
char *host;
{
CLIENT *rstat_clnt;
statstime host_stat;
struct sockaddr_in addr;
struct hostent *hp;
hp = gethostbyname(host);
if (hp == NULL) {
fprintf(stderr, "%s: unknown host \"%s\"\n",
argv0, host);
return(-1);
}
rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
if (rstat_clnt == NULL) {
fprintf(stderr, "%s: %s %s", argv0, host, clnt_spcreateerror(""));
return(-1);
warnx("%s", clnt_spcreateerror(host));
return;
}
bzero((char *)&host_stat, sizeof(host_stat));
if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, NULL) != RPC_SUCCESS) {
fprintf(stderr, "%s: %s: %s\n", argv0, host, clnt_sperror(rstat_clnt, host));
return(-1);
warnx("%s", clnt_sperror(rstat_clnt, host));
return;
}
addr.sin_addr.s_addr = *(int *)hp->h_addr;
rstat_reply((char *)&host_stat, &addr);
print_rup_data(host, &host_stat);
clnt_destroy(rstat_clnt);
}
void
allhosts()
{
statstime host_stat;
enum clnt_stat clnt_stat;
size_t i;
if (sort_type != SORT_NONE) {
printf("collecting responses...");
fflush(stdout);
}
clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
xdr_void, NULL,
xdr_statstime, &host_stat, rstat_reply);
if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
warnx("%s", clnt_sperrno(clnt_stat));
exit(1);
}
if (sort_type != SORT_NONE) {
putchar('\n');
qsort(rup_data, rup_data_idx, sizeof(struct rup_data), compare);
for (i = 0; i < rup_data_idx; i++) {
print_rup_data(rup_data[i].host, &rup_data[i].statstime);
}
}
}
usage()
{
fprintf(stderr, "Usage: %s [-t] [hosts ...]\n", argv0);
exit(1);
}
main(argc, argv)
int argc;
@ -212,15 +291,20 @@ main(argc, argv)
int ch;
extern int optind;
if (!(argv0 = rindex(argv[0], '/')))
argv0 = argv[0];
else
argv0++;
while ((ch = getopt(argc, argv, "?t")) != -1)
sort_type = SORT_NONE;
while ((ch = getopt(argc, argv, "dhlt")) != -1)
switch (ch) {
case 'd':
printtime = 1;
break;
case 'h':
sort_type = SORT_HOST;
break;
case 'l':
sort_type = SORT_LDAV;
break;
case 't':
printtime++;
sort_type = SORT_UPTIME;
break;
default:
usage();
@ -228,11 +312,20 @@ main(argc, argv)
}
setlinebuf(stdout);
if (argc == optind)
allhosts();
else {
for (; optind < argc; optind++)
(void) onehost(argv[optind]);
onehost(argv[optind]);
}
exit(0);
}
usage()
{
fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n");
exit(1);
}