2001-01-05 10:14:46 +03:00
|
|
|
/* $NetBSD: ruptime.c,v 1.10 2001/01/05 07:14:46 mjl Exp $ */
|
1997-01-09 18:17:30 +03:00
|
|
|
|
1993-03-21 21:04:42 +03:00
|
|
|
/*
|
1997-01-09 18:17:30 +03:00
|
|
|
* Copyright (c) 1983, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 21:04:42 +03:00
|
|
|
*
|
|
|
|
* 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-10-19 18:26:26 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
#ifndef lint
|
1997-10-19 18:26:26 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 21:04:42 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-01-09 18:17:30 +03:00
|
|
|
/*static char sccsid[] = "from: @(#)ruptime.c 8.2 (Berkeley) 4/5/94";*/
|
2001-01-05 10:14:46 +03:00
|
|
|
__RCSID("$NetBSD: ruptime.c,v 1.10 2001/01/05 07:14:46 mjl Exp $");
|
1993-03-21 21:04:42 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1997-01-09 18:17:30 +03:00
|
|
|
|
1993-03-21 21:04:42 +03:00
|
|
|
#include <protocols/rwhod.h>
|
1997-01-09 18:17:30 +03:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-01-09 18:17:30 +03:00
|
|
|
#include <time.h>
|
|
|
|
#include <tzfile.h>
|
|
|
|
#include <unistd.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
struct hs {
|
|
|
|
struct whod *hs_wd;
|
|
|
|
int hs_nusers;
|
|
|
|
} *hs;
|
|
|
|
struct whod awhod;
|
|
|
|
|
|
|
|
#define ISDOWN(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60)
|
|
|
|
#define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we))
|
|
|
|
|
1997-01-09 18:17:30 +03:00
|
|
|
size_t nhosts;
|
1993-03-21 21:04:42 +03:00
|
|
|
time_t now;
|
|
|
|
int rflg = 1;
|
|
|
|
|
2001-01-05 10:14:46 +03:00
|
|
|
int hscmp(const void *, const void *);
|
|
|
|
char *interval(time_t, char *);
|
|
|
|
int lcmp(const void *, const void *);
|
|
|
|
int main(int, char **);
|
|
|
|
void morehosts(void);
|
|
|
|
int tcmp(const void *, const void *);
|
|
|
|
int ucmp(const void *, const void *);
|
|
|
|
void usage(void);
|
1997-01-09 18:17:30 +03:00
|
|
|
|
|
|
|
int
|
2001-01-05 10:14:46 +03:00
|
|
|
main(int argc, char **argv)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1993-12-04 04:49:05 +03:00
|
|
|
struct dirent *dp;
|
1997-01-09 18:17:30 +03:00
|
|
|
struct hs *hsp;
|
|
|
|
struct whod *wd;
|
|
|
|
struct whoent *we;
|
|
|
|
DIR *dirp;
|
|
|
|
size_t hspace;
|
|
|
|
int aflg, cc, ch, fd, i, maxloadav;
|
1993-03-21 21:04:42 +03:00
|
|
|
char buf[sizeof(struct whod)];
|
2001-01-05 10:14:46 +03:00
|
|
|
int (*cmp)(const void *, const void *);
|
1993-03-21 21:04:42 +03:00
|
|
|
|
1997-10-19 18:26:26 +04:00
|
|
|
hsp = NULL;
|
1993-03-21 21:04:42 +03:00
|
|
|
aflg = 0;
|
1997-01-09 18:17:30 +03:00
|
|
|
cmp = hscmp;
|
2001-01-05 10:14:46 +03:00
|
|
|
while ((ch = getopt(argc, argv, "alrtu")) != -1)
|
1997-01-09 18:17:30 +03:00
|
|
|
switch (ch) {
|
1993-03-21 21:04:42 +03:00
|
|
|
case 'a':
|
|
|
|
aflg = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
cmp = lcmp;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflg = -1;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
cmp = tcmp;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
cmp = ucmp;
|
|
|
|
break;
|
|
|
|
default:
|
1997-01-09 18:17:30 +03:00
|
|
|
usage();
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
1997-01-09 18:17:30 +03:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 0)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
|
|
|
|
err(1, "%s", _PATH_RWHODIR);
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
maxloadav = -1;
|
1997-01-09 18:17:30 +03:00
|
|
|
for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
|
1993-03-21 21:04:42 +03:00
|
|
|
if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
|
|
|
|
continue;
|
1997-01-09 18:17:30 +03:00
|
|
|
if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
|
|
|
|
warn("%s", dp->d_name);
|
1993-03-21 21:04:42 +03:00
|
|
|
continue;
|
|
|
|
}
|
1997-01-09 18:17:30 +03:00
|
|
|
cc = read(fd, buf, sizeof(struct whod));
|
|
|
|
(void)close(fd);
|
|
|
|
|
1993-03-21 21:04:42 +03:00
|
|
|
if (cc < WHDRSIZE)
|
|
|
|
continue;
|
|
|
|
if (nhosts == hspace) {
|
1997-01-09 18:17:30 +03:00
|
|
|
if ((hs =
|
|
|
|
realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
|
1997-10-19 18:26:26 +04:00
|
|
|
err(1, "realloc");
|
1993-03-21 21:04:42 +03:00
|
|
|
hsp = hs + nhosts;
|
|
|
|
}
|
1997-01-09 18:17:30 +03:00
|
|
|
|
|
|
|
if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
|
1997-10-19 18:26:26 +04:00
|
|
|
err(1, "malloc");
|
1997-01-09 18:17:30 +03:00
|
|
|
memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
|
|
|
|
|
|
|
|
for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
|
1993-03-21 21:04:42 +03:00
|
|
|
if (wd->wd_loadav[i] > maxloadav)
|
|
|
|
maxloadav = wd->wd_loadav[i];
|
1997-01-09 18:17:30 +03:00
|
|
|
|
|
|
|
for (hsp->hs_nusers = 0,
|
|
|
|
we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
|
1993-03-21 21:04:42 +03:00
|
|
|
if (aflg || we->we_idle < 3600)
|
1997-01-09 18:17:30 +03:00
|
|
|
++hsp->hs_nusers;
|
|
|
|
++hsp;
|
|
|
|
++nhosts;
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
1997-01-09 18:17:30 +03:00
|
|
|
if (nhosts == 0)
|
2001-01-05 10:14:46 +03:00
|
|
|
errx(0, "no hosts in %s", _PATH_RWHODIR);
|
1997-01-09 18:17:30 +03:00
|
|
|
|
1993-03-21 21:04:42 +03:00
|
|
|
(void)time(&now);
|
1997-01-09 18:17:30 +03:00
|
|
|
qsort(hs, nhosts, sizeof (hs[0]), cmp);
|
1993-03-21 21:04:42 +03:00
|
|
|
for (i = 0; i < nhosts; i++) {
|
|
|
|
hsp = &hs[i];
|
|
|
|
if (ISDOWN(hsp)) {
|
|
|
|
(void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
|
|
|
|
interval(now - hsp->hs_wd->wd_recvtime, "down"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void)printf(
|
|
|
|
"%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n",
|
|
|
|
hsp->hs_wd->wd_hostname,
|
|
|
|
interval((time_t)hsp->hs_wd->wd_sendtime -
|
|
|
|
(time_t)hsp->hs_wd->wd_boottime, " up"),
|
|
|
|
hsp->hs_nusers,
|
|
|
|
hsp->hs_nusers == 1 ? ", " : "s,",
|
|
|
|
maxloadav >= 1000 ? 5 : 4,
|
|
|
|
hsp->hs_wd->wd_loadav[0] / 100.0,
|
|
|
|
maxloadav >= 1000 ? 5 : 4,
|
|
|
|
hsp->hs_wd->wd_loadav[1] / 100.0,
|
|
|
|
maxloadav >= 1000 ? 5 : 4,
|
|
|
|
hsp->hs_wd->wd_loadav[2] / 100.0);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2001-01-05 10:14:46 +03:00
|
|
|
interval(time_t tval, char *updown)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
|
|
|
static char resbuf[32];
|
|
|
|
int days, hours, minutes;
|
|
|
|
|
1998-04-19 23:52:33 +04:00
|
|
|
if (tval < 0) {
|
1997-01-09 18:17:30 +03:00
|
|
|
(void)snprintf(resbuf, sizeof(resbuf), " %s ??:??", updown);
|
|
|
|
return (resbuf);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
1997-01-09 18:17:30 +03:00
|
|
|
/* round to minutes. */
|
|
|
|
minutes = (tval + (SECSPERMIN - 1)) / SECSPERMIN;
|
|
|
|
hours = minutes / MINSPERHOUR;
|
|
|
|
minutes %= MINSPERHOUR;
|
|
|
|
days = hours / HOURSPERDAY;
|
|
|
|
hours %= HOURSPERDAY;
|
1993-03-21 21:04:42 +03:00
|
|
|
if (days)
|
1997-01-09 18:17:30 +03:00
|
|
|
(void)snprintf(resbuf, sizeof(resbuf),
|
2000-12-31 20:35:20 +03:00
|
|
|
"%s%4d+%02d:%02d", updown, days, hours, minutes);
|
1993-03-21 21:04:42 +03:00
|
|
|
else
|
1997-01-09 18:17:30 +03:00
|
|
|
(void)snprintf(resbuf, sizeof(resbuf),
|
2000-12-31 20:35:20 +03:00
|
|
|
"%s %2d:%02d", updown, hours, minutes);
|
1997-01-09 18:17:30 +03:00
|
|
|
return (resbuf);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
1997-01-09 18:17:30 +03:00
|
|
|
#define HS(a) ((struct hs *)(a))
|
|
|
|
|
|
|
|
/* Alphabetical comparison. */
|
|
|
|
int
|
1993-03-21 21:04:42 +03:00
|
|
|
hscmp(a1, a2)
|
1997-01-09 18:17:30 +03:00
|
|
|
const void *a1, *a2;
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1997-01-09 18:17:30 +03:00
|
|
|
return (rflg *
|
|
|
|
strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
1997-01-09 18:17:30 +03:00
|
|
|
/* Load average comparison. */
|
|
|
|
int
|
2001-01-05 10:14:46 +03:00
|
|
|
lcmp(const void *a1, const void *a2)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1998-12-20 00:44:31 +03:00
|
|
|
if (ISDOWN(HS(a1))) {
|
1997-01-09 18:17:30 +03:00
|
|
|
if (ISDOWN(HS(a2)))
|
|
|
|
return (tcmp(a1, a2));
|
1993-03-21 21:04:42 +03:00
|
|
|
else
|
1997-01-09 18:17:30 +03:00
|
|
|
return (rflg);
|
1998-12-20 00:44:31 +03:00
|
|
|
} else if (ISDOWN(HS(a2))) {
|
1997-01-09 18:17:30 +03:00
|
|
|
return (-rflg);
|
1998-12-20 00:44:31 +03:00
|
|
|
} else
|
1997-01-09 18:17:30 +03:00
|
|
|
return (rflg *
|
|
|
|
(HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
1997-01-09 18:17:30 +03:00
|
|
|
/* Number of users comparison. */
|
|
|
|
int
|
2001-01-05 10:14:46 +03:00
|
|
|
ucmp(const void *a1, const void *a2)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1998-12-20 00:44:31 +03:00
|
|
|
if (ISDOWN(HS(a1))) {
|
1997-01-09 18:17:30 +03:00
|
|
|
if (ISDOWN(HS(a2)))
|
|
|
|
return (tcmp(a1, a2));
|
1993-03-21 21:04:42 +03:00
|
|
|
else
|
1997-01-09 18:17:30 +03:00
|
|
|
return (rflg);
|
1998-12-20 00:44:31 +03:00
|
|
|
} else if (ISDOWN(HS(a2))) {
|
1997-01-09 18:17:30 +03:00
|
|
|
return (-rflg);
|
1998-12-20 00:44:31 +03:00
|
|
|
} else
|
1997-01-09 18:17:30 +03:00
|
|
|
return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
1997-01-09 18:17:30 +03:00
|
|
|
/* Uptime comparison. */
|
|
|
|
int
|
2001-01-05 10:14:46 +03:00
|
|
|
tcmp(const void *a1, const void *a2)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1997-01-09 18:17:30 +03:00
|
|
|
return (rflg * (
|
|
|
|
(ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
|
|
|
|
: HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
|
1993-03-21 21:04:42 +03:00
|
|
|
-
|
1997-01-09 18:17:30 +03:00
|
|
|
(ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
|
|
|
|
: HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
|
1993-03-21 21:04:42 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
1997-01-09 18:17:30 +03:00
|
|
|
void
|
2001-01-05 10:14:46 +03:00
|
|
|
usage(void)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
2001-01-05 10:14:46 +03:00
|
|
|
(void)fprintf(stderr, "usage: ruptime [-alrtu]\n");
|
1997-01-09 18:17:30 +03:00
|
|
|
exit(1);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|