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