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