Home | History | Annotate | Line # | Download | only in ruptime
ruptime.c revision 1.10
      1  1.10       mjl /*	$NetBSD: ruptime.c,v 1.10 2001/01/05 07:14:46 mjl Exp $	*/
      2   1.5       tls 
      3   1.1       cgd /*
      4   1.5       tls  * Copyright (c) 1983, 1993, 1994
      5   1.5       tls  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.6     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.6     lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\n\
     39   1.6     lukem 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.5       tls /*static char sccsid[] = "from: @(#)ruptime.c	8.2 (Berkeley) 4/5/94";*/
     44  1.10       mjl __RCSID("$NetBSD: ruptime.c,v 1.10 2001/01/05 07:14:46 mjl Exp $");
     45   1.1       cgd #endif /* not lint */
     46   1.1       cgd 
     47   1.1       cgd #include <sys/param.h>
     48   1.5       tls 
     49   1.5       tls #include <protocols/rwhod.h>
     50   1.5       tls 
     51   1.3       jtc #include <dirent.h>
     52   1.5       tls #include <err.h>
     53   1.5       tls #include <errno.h>
     54   1.5       tls #include <fcntl.h>
     55   1.1       cgd #include <stdio.h>
     56   1.1       cgd #include <stdlib.h>
     57   1.1       cgd #include <string.h>
     58   1.5       tls #include <time.h>
     59   1.5       tls #include <tzfile.h>
     60   1.5       tls #include <unistd.h>
     61   1.1       cgd 
     62   1.1       cgd struct hs {
     63   1.1       cgd 	struct	whod *hs_wd;
     64   1.1       cgd 	int	hs_nusers;
     65   1.1       cgd } *hs;
     66   1.1       cgd struct	whod awhod;
     67   1.1       cgd 
     68   1.1       cgd #define	ISDOWN(h)		(now - (h)->hs_wd->wd_recvtime > 11 * 60)
     69   1.1       cgd #define	WHDRSIZE	(sizeof (awhod) - sizeof (awhod.wd_we))
     70   1.1       cgd 
     71   1.5       tls size_t nhosts;
     72   1.1       cgd time_t now;
     73   1.1       cgd int rflg = 1;
     74   1.1       cgd 
     75  1.10       mjl int	 hscmp(const void *, const void *);
     76  1.10       mjl char	*interval(time_t, char *);
     77  1.10       mjl int	 lcmp(const void *, const void *);
     78  1.10       mjl int	 main(int, char **);
     79  1.10       mjl void	 morehosts(void);
     80  1.10       mjl int	 tcmp(const void *, const void *);
     81  1.10       mjl int	 ucmp(const void *, const void *);
     82  1.10       mjl void	 usage(void);
     83   1.5       tls 
     84   1.5       tls int
     85  1.10       mjl main(int argc, char **argv)
     86   1.1       cgd {
     87   1.3       jtc 	struct dirent *dp;
     88   1.5       tls 	struct hs *hsp;
     89   1.5       tls 	struct whod *wd;
     90   1.5       tls 	struct whoent *we;
     91   1.5       tls 	DIR *dirp;
     92   1.5       tls 	size_t hspace;
     93   1.5       tls 	int aflg, cc, ch, fd, i, maxloadav;
     94   1.1       cgd 	char buf[sizeof(struct whod)];
     95  1.10       mjl 	int (*cmp)(const void *, const void *);
     96   1.1       cgd 
     97   1.6     lukem 	hsp = NULL;
     98   1.1       cgd 	aflg = 0;
     99   1.5       tls 	cmp = hscmp;
    100  1.10       mjl 	while ((ch = getopt(argc, argv, "alrtu")) != -1)
    101   1.5       tls 		switch (ch) {
    102   1.1       cgd 		case 'a':
    103   1.1       cgd 			aflg = 1;
    104   1.1       cgd 			break;
    105   1.1       cgd 		case 'l':
    106   1.1       cgd 			cmp = lcmp;
    107   1.1       cgd 			break;
    108   1.1       cgd 		case 'r':
    109   1.1       cgd 			rflg = -1;
    110   1.1       cgd 			break;
    111   1.1       cgd 		case 't':
    112   1.1       cgd 			cmp = tcmp;
    113   1.1       cgd 			break;
    114   1.1       cgd 		case 'u':
    115   1.1       cgd 			cmp = ucmp;
    116   1.1       cgd 			break;
    117   1.1       cgd 		default:
    118   1.5       tls 			usage();
    119   1.1       cgd 		}
    120   1.5       tls 	argc -= optind;
    121   1.5       tls 	argv += optind;
    122   1.5       tls 
    123   1.5       tls 	if (argc != 0)
    124   1.5       tls 		usage();
    125   1.5       tls 
    126   1.5       tls 	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
    127   1.5       tls 		err(1, "%s", _PATH_RWHODIR);
    128   1.1       cgd 
    129   1.1       cgd 	maxloadav = -1;
    130   1.5       tls 	for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
    131   1.1       cgd 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
    132   1.1       cgd 			continue;
    133   1.5       tls 		if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
    134   1.5       tls 			warn("%s", dp->d_name);
    135   1.1       cgd 			continue;
    136   1.1       cgd 		}
    137   1.5       tls 		cc = read(fd, buf, sizeof(struct whod));
    138   1.5       tls 		(void)close(fd);
    139   1.5       tls 
    140   1.1       cgd 		if (cc < WHDRSIZE)
    141   1.1       cgd 			continue;
    142   1.1       cgd 		if (nhosts == hspace) {
    143   1.5       tls 			if ((hs =
    144   1.5       tls 			    realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
    145   1.6     lukem 				err(1, "realloc");
    146   1.1       cgd 			hsp = hs + nhosts;
    147   1.1       cgd 		}
    148   1.5       tls 
    149   1.5       tls 		if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
    150   1.6     lukem 			err(1, "malloc");
    151   1.5       tls 		memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
    152   1.5       tls 
    153   1.5       tls 		for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
    154   1.1       cgd 			if (wd->wd_loadav[i] > maxloadav)
    155   1.1       cgd 				maxloadav = wd->wd_loadav[i];
    156   1.5       tls 
    157   1.5       tls 		for (hsp->hs_nusers = 0,
    158   1.5       tls 		    we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
    159   1.1       cgd 			if (aflg || we->we_idle < 3600)
    160   1.5       tls 				++hsp->hs_nusers;
    161   1.5       tls 		++hsp;
    162   1.5       tls 		++nhosts;
    163   1.1       cgd 	}
    164   1.5       tls 	if (nhosts == 0)
    165  1.10       mjl 		errx(0, "no hosts in %s", _PATH_RWHODIR);
    166   1.5       tls 
    167   1.4       cgd 	(void)time(&now);
    168   1.5       tls 	qsort(hs, nhosts, sizeof (hs[0]), cmp);
    169   1.1       cgd 	for (i = 0; i < nhosts; i++) {
    170   1.1       cgd 		hsp = &hs[i];
    171   1.1       cgd 		if (ISDOWN(hsp)) {
    172   1.1       cgd 			(void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
    173   1.1       cgd 			    interval(now - hsp->hs_wd->wd_recvtime, "down"));
    174   1.1       cgd 			continue;
    175   1.1       cgd 		}
    176   1.1       cgd 		(void)printf(
    177   1.1       cgd 		    "%-12.12s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
    178   1.1       cgd 		    hsp->hs_wd->wd_hostname,
    179   1.1       cgd 		    interval((time_t)hsp->hs_wd->wd_sendtime -
    180   1.1       cgd 			(time_t)hsp->hs_wd->wd_boottime, "  up"),
    181   1.1       cgd 		    hsp->hs_nusers,
    182   1.1       cgd 		    hsp->hs_nusers == 1 ? ", " : "s,",
    183   1.1       cgd 		    maxloadav >= 1000 ? 5 : 4,
    184   1.1       cgd 			hsp->hs_wd->wd_loadav[0] / 100.0,
    185   1.1       cgd 		    maxloadav >= 1000 ? 5 : 4,
    186   1.1       cgd 		        hsp->hs_wd->wd_loadav[1] / 100.0,
    187   1.1       cgd 		    maxloadav >= 1000 ? 5 : 4,
    188   1.1       cgd 		        hsp->hs_wd->wd_loadav[2] / 100.0);
    189   1.1       cgd 	}
    190   1.1       cgd 	exit(0);
    191   1.1       cgd }
    192   1.1       cgd 
    193   1.1       cgd char *
    194  1.10       mjl interval(time_t tval, char *updown)
    195   1.1       cgd {
    196   1.1       cgd 	static char resbuf[32];
    197   1.1       cgd 	int days, hours, minutes;
    198   1.1       cgd 
    199   1.7   hubertf 	if (tval < 0) {
    200   1.5       tls 		(void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
    201   1.5       tls 		return (resbuf);
    202   1.1       cgd 	}
    203   1.5       tls 						/* round to minutes. */
    204   1.5       tls 	minutes = (tval + (SECSPERMIN - 1)) / SECSPERMIN;
    205   1.5       tls 	hours = minutes / MINSPERHOUR;
    206   1.5       tls 	minutes %= MINSPERHOUR;
    207   1.5       tls 	days = hours / HOURSPERDAY;
    208   1.5       tls 	hours %= HOURSPERDAY;
    209   1.1       cgd 	if (days)
    210   1.5       tls 		(void)snprintf(resbuf, sizeof(resbuf),
    211   1.9        ad 		    "%s%4d+%02d:%02d", updown, days, hours, minutes);
    212   1.1       cgd 	else
    213   1.5       tls 		(void)snprintf(resbuf, sizeof(resbuf),
    214   1.9        ad 		    "%s     %2d:%02d", updown, hours, minutes);
    215   1.5       tls 	return (resbuf);
    216   1.1       cgd }
    217   1.1       cgd 
    218   1.5       tls #define	HS(a)	((struct hs *)(a))
    219   1.5       tls 
    220   1.5       tls /* Alphabetical comparison. */
    221   1.5       tls int
    222   1.1       cgd hscmp(a1, a2)
    223   1.5       tls 	const void *a1, *a2;
    224   1.1       cgd {
    225   1.5       tls 	return (rflg *
    226   1.5       tls 	    strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
    227   1.1       cgd }
    228   1.1       cgd 
    229   1.5       tls /* Load average comparison. */
    230   1.5       tls int
    231  1.10       mjl lcmp(const void *a1, const void *a2)
    232   1.1       cgd {
    233   1.8  christos 	if (ISDOWN(HS(a1))) {
    234   1.5       tls 		if (ISDOWN(HS(a2)))
    235   1.5       tls 			return (tcmp(a1, a2));
    236   1.1       cgd 		else
    237   1.5       tls 			return (rflg);
    238   1.8  christos 	} else if (ISDOWN(HS(a2))) {
    239   1.5       tls 		return (-rflg);
    240   1.8  christos 	} else
    241   1.5       tls 		return (rflg *
    242   1.5       tls 		   (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
    243   1.1       cgd }
    244   1.1       cgd 
    245   1.5       tls /* Number of users comparison. */
    246   1.5       tls int
    247  1.10       mjl ucmp(const void *a1, const void *a2)
    248   1.1       cgd {
    249   1.8  christos 	if (ISDOWN(HS(a1))) {
    250   1.5       tls 		if (ISDOWN(HS(a2)))
    251   1.5       tls 			return (tcmp(a1, a2));
    252   1.1       cgd 		else
    253   1.5       tls 			return (rflg);
    254   1.8  christos 	} else if (ISDOWN(HS(a2))) {
    255   1.5       tls 		return (-rflg);
    256   1.8  christos 	} else
    257   1.5       tls 		return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
    258   1.1       cgd }
    259   1.1       cgd 
    260   1.5       tls /* Uptime comparison. */
    261   1.5       tls int
    262  1.10       mjl tcmp(const void *a1, const void *a2)
    263   1.1       cgd {
    264   1.5       tls 	return (rflg * (
    265   1.5       tls 		(ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
    266   1.5       tls 		    : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
    267   1.1       cgd 		-
    268   1.5       tls 		(ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
    269   1.5       tls 		    : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
    270   1.1       cgd 	));
    271   1.1       cgd }
    272   1.1       cgd 
    273   1.5       tls void
    274  1.10       mjl usage(void)
    275   1.1       cgd {
    276  1.10       mjl 	(void)fprintf(stderr, "usage: ruptime [-alrtu]\n");
    277   1.5       tls 	exit(1);
    278   1.1       cgd }
    279