summaryrefslogtreecommitdiff
path: root/usr.bin/ruptime/ruptime.c
blob: 03549640b47038423bb95d5707b54c7afec052d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*	$OpenBSD: ruptime.c,v 1.13 2004/09/14 22:24:07 deraadt Exp $	*/

/*
 * Copyright (c) 1983 The Regents of the University of California.
 * All rights reserved.
 *
 * 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. 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.
 */

#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
 All rights reserved.\n";
#endif /* not lint */

#ifndef lint
/*static char sccsid[] = "from: @(#)ruptime.c	5.8 (Berkeley) 7/21/90";*/
static char rcsid[] = "$OpenBSD: ruptime.c,v 1.13 2004/09/14 22:24:07 deraadt Exp $";
#endif /* not lint */

#include <sys/param.h>
#include <sys/file.h>
#include <dirent.h>
#include <protocols/rwhod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

size_t	nhosts, hspace = 20;
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))

time_t now;
int rflg = 1;
int	hscmp(const void *, const void *);
int	ucmp(const void *, const void *);
int	lcmp(const void *, const void *);
int	tcmp(const void *, const void *);
char	*interval(time_t, char *);

void morehosts(void);

int
main(int argc, char *argv[])
{
	struct hs *hsp;
	struct whod *wd;
	struct whoent *we;
	DIR *dirp;
	struct dirent *dp;
	int aflg, cc, ch, f, i, maxloadav;
	char buf[sizeof(struct whod)];
	int (*cmp)(const void *, const void *) = hscmp;

	aflg = 0;
	while ((ch = getopt(argc, argv, "alrut")) != -1)
		switch((char)ch) {
		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: 
			(void)fprintf(stderr, "usage: ruptime [-alrut]\n");
			exit(1);
		}

	if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
		(void)fprintf(stderr, "ruptime: %s: %s.\n",
		    _PATH_RWHODIR, strerror(errno));
		exit(1);
	}
	morehosts();
	hsp = hs;
	maxloadav = -1;
	while ((dp = readdir(dirp))) {
		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
			continue;
		if ((f = open(dp->d_name, O_RDONLY, 0)) < 0) {
			(void)fprintf(stderr, "ruptime: %s: %s\n",
			    dp->d_name, strerror(errno));
			continue;
		}
		cc = read(f, buf, sizeof(struct whod));
		(void)close(f);
		if (cc < WHDRSIZE)
			continue;
		if (nhosts == hspace) {
			morehosts();
			hsp = hs + nhosts;
		}
		/* NOSTRICT */
		hsp->hs_wd = malloc((size_t)WHDRSIZE);
		wd = (struct whod *)buf;
		bcopy((char *)wd, (char *)hsp->hs_wd, (size_t)WHDRSIZE);
		hsp->hs_nusers = 0;
		for (i = 0; i < 2; i++)
			if (wd->wd_loadav[i] > maxloadav)
				maxloadav = wd->wd_loadav[i];
		we = (struct whoent *)(buf+cc);
		while (--we >= wd->wd_we)
			if (aflg || we->we_idle < 3600)
				hsp->hs_nusers++;
		nhosts++;
		hsp++;
	}
	if (!nhosts) {
		(void)printf("ruptime: no hosts in %s.\n", _PATH_RWHODIR);
		exit(1);
	}
	(void)time(&now);
	qsort((char *)hs, nhosts, sizeof (hs[0]), cmp);
	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);
		free((void *)hsp->hs_wd);
	}
	exit(0);
}

char *
interval(time_t tval, char *updown)
{
	static char resbuf[32];
	int days, hours, minutes;

	if (tval < 0 || tval > 999*24*60*60) {
		(void)snprintf(resbuf, sizeof resbuf, "%s     ??:??", updown);
		return(resbuf);
	}
	minutes = (tval + 59) / 60;		/* round to minutes */
	hours = minutes / 60; minutes %= 60;
	days = hours / 24; hours %= 24;
	if (days)
		(void)snprintf(resbuf, sizeof resbuf, "%s %3d+%02d:%02d",
		    updown, days, hours, minutes);
	else
		(void)snprintf(resbuf, sizeof resbuf, "%s     %2d:%02d",
		    updown, hours, minutes);
	return(resbuf);
}

/* alphabetical comparison */
int
hscmp(const void *a1, const void *a2)
{
	const struct hs *h1 = a1, *h2 = a2;

	return(rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname));
}

/* load average comparison */
int
lcmp(const void *a1, const void *a2)
{
	const struct hs *h1 = a1, *h2 = a2;

	if (ISDOWN(h1))
		if (ISDOWN(h2))
			return(tcmp(a1, a2));
		else
			return(rflg);
	else if (ISDOWN(h2))
		return(-rflg);
	else
		return(rflg *
			(h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0]));
}

/* number of users comparison */
int
ucmp(const void *a1, const void *a2)
{
	const struct hs *h1 = a1, *h2 = a2;

	if (ISDOWN(h1))
		if (ISDOWN(h2))
			return(tcmp(a1, a2));
		else
			return(rflg);
	else if (ISDOWN(h2))
		return(-rflg);
	else
		return(rflg * (h2->hs_nusers - h1->hs_nusers));
}

/* uptime comparison */
int
tcmp(const void *a1, const void *a2)
{
	const struct hs *h1 = a1, *h2 = a2;

	return(rflg * (
		(ISDOWN(h2) ? h2->hs_wd->wd_recvtime - now
			  : h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime)
		-
		(ISDOWN(h1) ? h1->hs_wd->wd_recvtime - now
			  : h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime)
	));
}

void
morehosts(void)
{
	hs = realloc((char *)hs, (hspace *= 2) * sizeof(*hs));
	if (hs == NULL) {
		(void)fprintf(stderr, "ruptime: %s.\n", strerror(ENOMEM));
		exit(1);
	}
}