19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1993, 1994 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 35e88b3b4fSPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993, 1994\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 41a827060aSMark Murray static const char sccsid[] = "@(#)ruptime.c 8.2 (Berkeley) 4/5/94"; 429b50d902SRodney W. Grimes #endif /* not lint */ 439b50d902SRodney W. Grimes 44a827060aSMark Murray #include <sys/cdefs.h> 45a827060aSMark Murray __FBSDID("$FreeBSD$"); 46a827060aSMark Murray 479b50d902SRodney W. Grimes #include <sys/param.h> 489b50d902SRodney W. Grimes 499b50d902SRodney W. Grimes #include <protocols/rwhod.h> 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes #include <dirent.h> 529b50d902SRodney W. Grimes #include <err.h> 539b50d902SRodney W. Grimes #include <errno.h> 549b50d902SRodney W. Grimes #include <fcntl.h> 559b50d902SRodney W. Grimes #include <stdio.h> 569b50d902SRodney W. Grimes #include <stdlib.h> 579b50d902SRodney W. Grimes #include <string.h> 589b50d902SRodney W. Grimes #include <time.h> 599b50d902SRodney W. Grimes #include <unistd.h> 609b50d902SRodney W. Grimes 619b50d902SRodney W. Grimes struct hs { 629b50d902SRodney W. Grimes struct whod *hs_wd; 639b50d902SRodney W. Grimes int hs_nusers; 649b50d902SRodney W. Grimes } *hs; 659b50d902SRodney W. Grimes struct whod awhod; 665d53f683SMarc G. Fournier #define LEFTEARTH(h) (now - (h)->hs_wd->wd_recvtime > 4*24*60*60) 679b50d902SRodney W. Grimes #define ISDOWN(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60) 689b50d902SRodney W. Grimes #define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we)) 699b50d902SRodney W. Grimes 709b50d902SRodney W. Grimes size_t nhosts; 719b50d902SRodney W. Grimes time_t now; 729b50d902SRodney W. Grimes int rflg = 1; 739b50d902SRodney W. Grimes 74d3cb5dedSWarner Losh int hscmp(const void *, const void *); 75a827060aSMark Murray char *interval(time_t, const char *); 76d3cb5dedSWarner Losh int lcmp(const void *, const void *); 77d3cb5dedSWarner Losh void morehosts(void); 78d3cb5dedSWarner Losh int tcmp(const void *, const void *); 79d3cb5dedSWarner Losh int ucmp(const void *, const void *); 80d3cb5dedSWarner Losh void usage(void); 819b50d902SRodney W. Grimes 829b50d902SRodney W. Grimes int 83a827060aSMark Murray main(int argc, char *argv[]) 849b50d902SRodney W. Grimes { 859b50d902SRodney W. Grimes struct dirent *dp; 869b50d902SRodney W. Grimes struct hs *hsp; 879b50d902SRodney W. Grimes struct whod *wd; 889b50d902SRodney W. Grimes struct whoent *we; 899b50d902SRodney W. Grimes DIR *dirp; 909b50d902SRodney W. Grimes size_t hspace; 91a827060aSMark Murray int aflg, ch, fd, i, maxloadav; 929b50d902SRodney W. Grimes char buf[sizeof(struct whod)]; 93d3cb5dedSWarner Losh int (*cmp)(const void *, const void *); 94a827060aSMark Murray u_int cc; 959b50d902SRodney W. Grimes 969b50d902SRodney W. Grimes aflg = 0; 979b50d902SRodney W. Grimes cmp = hscmp; 981c8af878SWarner Losh while ((ch = getopt(argc, argv, "alrut")) != -1) 999b50d902SRodney W. Grimes switch (ch) { 1009b50d902SRodney W. Grimes case 'a': 1019b50d902SRodney W. Grimes aflg = 1; 1029b50d902SRodney W. Grimes break; 1039b50d902SRodney W. Grimes case 'l': 1049b50d902SRodney W. Grimes cmp = lcmp; 1059b50d902SRodney W. Grimes break; 1069b50d902SRodney W. Grimes case 'r': 1079b50d902SRodney W. Grimes rflg = -1; 1089b50d902SRodney W. Grimes break; 1099b50d902SRodney W. Grimes case 't': 1109b50d902SRodney W. Grimes cmp = tcmp; 1119b50d902SRodney W. Grimes break; 1129b50d902SRodney W. Grimes case 'u': 1139b50d902SRodney W. Grimes cmp = ucmp; 1149b50d902SRodney W. Grimes break; 1159b50d902SRodney W. Grimes default: 1169b50d902SRodney W. Grimes usage(); 1179b50d902SRodney W. Grimes } 1189b50d902SRodney W. Grimes argc -= optind; 1199b50d902SRodney W. Grimes argv += optind; 1209b50d902SRodney W. Grimes 1219b50d902SRodney W. Grimes if (argc != 0) 1229b50d902SRodney W. Grimes usage(); 1239b50d902SRodney W. Grimes 1249b50d902SRodney W. Grimes if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) 1259b50d902SRodney W. Grimes err(1, "%s", _PATH_RWHODIR); 1269b50d902SRodney W. Grimes 1279b50d902SRodney W. Grimes maxloadav = -1; 1289b50d902SRodney W. Grimes for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) { 1299b50d902SRodney W. Grimes if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5)) 1309b50d902SRodney W. Grimes continue; 1319b50d902SRodney W. Grimes if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) { 1329b50d902SRodney W. Grimes warn("%s", dp->d_name); 1339b50d902SRodney W. Grimes continue; 1349b50d902SRodney W. Grimes } 1359b50d902SRodney W. Grimes cc = read(fd, buf, sizeof(struct whod)); 1369b50d902SRodney W. Grimes (void)close(fd); 1379b50d902SRodney W. Grimes 1389b50d902SRodney W. Grimes if (cc < WHDRSIZE) 1399b50d902SRodney W. Grimes continue; 1409b50d902SRodney W. Grimes if (nhosts == hspace) { 1419b50d902SRodney W. Grimes if ((hs = 1429b50d902SRodney W. Grimes realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL) 1439b50d902SRodney W. Grimes err(1, NULL); 1449b50d902SRodney W. Grimes hsp = hs + nhosts; 1459b50d902SRodney W. Grimes } 1469b50d902SRodney W. Grimes 1479b50d902SRodney W. Grimes if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL) 1489b50d902SRodney W. Grimes err(1, NULL); 1499b50d902SRodney W. Grimes memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE); 1509b50d902SRodney W. Grimes 1519b50d902SRodney W. Grimes for (wd = (struct whod *)buf, i = 0; i < 2; ++i) 1529b50d902SRodney W. Grimes if (wd->wd_loadav[i] > maxloadav) 1539b50d902SRodney W. Grimes maxloadav = wd->wd_loadav[i]; 1549b50d902SRodney W. Grimes 1559b50d902SRodney W. Grimes for (hsp->hs_nusers = 0, 1569b50d902SRodney W. Grimes we = (struct whoent *)(buf + cc); --we >= wd->wd_we;) 1579b50d902SRodney W. Grimes if (aflg || we->we_idle < 3600) 1589b50d902SRodney W. Grimes ++hsp->hs_nusers; 1599b50d902SRodney W. Grimes ++hsp; 1609b50d902SRodney W. Grimes ++nhosts; 1619b50d902SRodney W. Grimes } 1629b50d902SRodney W. Grimes if (nhosts == 0) 163e88b3b4fSPhilippe Charnier errx(1, "no hosts in %s", _PATH_RWHODIR); 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes (void)time(&now); 1669b50d902SRodney W. Grimes qsort(hs, nhosts, sizeof(hs[0]), cmp); 167a827060aSMark Murray for (i = 0; i < (int)nhosts; i++) { 1689b50d902SRodney W. Grimes hsp = &hs[i]; 1695d53f683SMarc G. Fournier if (LEFTEARTH(hsp)) 1705d53f683SMarc G. Fournier continue; 1719b50d902SRodney W. Grimes if (ISDOWN(hsp)) { 1729b50d902SRodney W. Grimes (void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname, 1739b50d902SRodney W. Grimes interval(now - hsp->hs_wd->wd_recvtime, "down")); 1749b50d902SRodney W. Grimes continue; 1759b50d902SRodney W. Grimes } 1769b50d902SRodney W. Grimes (void)printf( 1779b50d902SRodney W. Grimes "%-12.12s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n", 1789b50d902SRodney W. Grimes hsp->hs_wd->wd_hostname, 1799b50d902SRodney W. Grimes interval((time_t)hsp->hs_wd->wd_sendtime - 1809b50d902SRodney W. Grimes (time_t)hsp->hs_wd->wd_boottime, " up"), 1819b50d902SRodney W. Grimes hsp->hs_nusers, 1829b50d902SRodney W. Grimes hsp->hs_nusers == 1 ? ", " : "s,", 1839b50d902SRodney W. Grimes maxloadav >= 1000 ? 5 : 4, 1849b50d902SRodney W. Grimes hsp->hs_wd->wd_loadav[0] / 100.0, 1859b50d902SRodney W. Grimes maxloadav >= 1000 ? 5 : 4, 1869b50d902SRodney W. Grimes hsp->hs_wd->wd_loadav[1] / 100.0, 1879b50d902SRodney W. Grimes maxloadav >= 1000 ? 5 : 4, 1889b50d902SRodney W. Grimes hsp->hs_wd->wd_loadav[2] / 100.0); 1899b50d902SRodney W. Grimes } 1909b50d902SRodney W. Grimes exit(0); 1919b50d902SRodney W. Grimes } 1929b50d902SRodney W. Grimes 1939b50d902SRodney W. Grimes char * 194a827060aSMark Murray interval(time_t tval, const char *updown) 1959b50d902SRodney W. Grimes { 1969b50d902SRodney W. Grimes static char resbuf[32]; 1979b50d902SRodney W. Grimes int days, hours, minutes; 1989b50d902SRodney W. Grimes 199fdb7c7dcSJoerg Wunsch if (tval < 0) { 2009b50d902SRodney W. Grimes (void)snprintf(resbuf, sizeof(resbuf), " %s ??:??", updown); 2019b50d902SRodney W. Grimes return (resbuf); 2029b50d902SRodney W. Grimes } 2039b50d902SRodney W. Grimes /* round to minutes. */ 204656dcd43SGarrett Wollman minutes = (tval + (60 - 1)) / 60; 205656dcd43SGarrett Wollman hours = minutes / 60; 206656dcd43SGarrett Wollman minutes %= 60; 207656dcd43SGarrett Wollman days = hours / 24; 208656dcd43SGarrett Wollman hours %= 24; 2099b50d902SRodney W. Grimes if (days) 2109b50d902SRodney W. Grimes (void)snprintf(resbuf, sizeof(resbuf), 21126dce0e6SMarc G. Fournier "%s %3d+%02d:%02d", updown, days, hours, minutes); 2129b50d902SRodney W. Grimes else 2139b50d902SRodney W. Grimes (void)snprintf(resbuf, sizeof(resbuf), 2149b50d902SRodney W. Grimes "%s %2d:%02d", updown, hours, minutes); 2159b50d902SRodney W. Grimes return (resbuf); 2169b50d902SRodney W. Grimes } 2179b50d902SRodney W. Grimes 218a827060aSMark Murray #define HS(a) ((const struct hs *)(a)) 2199b50d902SRodney W. Grimes 2209b50d902SRodney W. Grimes /* Alphabetical comparison. */ 2219b50d902SRodney W. Grimes int 222a827060aSMark Murray hscmp(const void *a1, const void *a2) 2239b50d902SRodney W. Grimes { 2249b50d902SRodney W. Grimes return (rflg * 2259b50d902SRodney W. Grimes strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname)); 2269b50d902SRodney W. Grimes } 2279b50d902SRodney W. Grimes 2289b50d902SRodney W. Grimes /* Load average comparison. */ 2299b50d902SRodney W. Grimes int 230a827060aSMark Murray lcmp(const void *a1, const void *a2) 2319b50d902SRodney W. Grimes { 2329b50d902SRodney W. Grimes if (ISDOWN(HS(a1))) 2339b50d902SRodney W. Grimes if (ISDOWN(HS(a2))) 2349b50d902SRodney W. Grimes return (tcmp(a1, a2)); 2359b50d902SRodney W. Grimes else 2369b50d902SRodney W. Grimes return (rflg); 2379b50d902SRodney W. Grimes else if (ISDOWN(HS(a2))) 2389b50d902SRodney W. Grimes return (-rflg); 2399b50d902SRodney W. Grimes else 2409b50d902SRodney W. Grimes return (rflg * 2419b50d902SRodney W. Grimes (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0])); 2429b50d902SRodney W. Grimes } 2439b50d902SRodney W. Grimes 2449b50d902SRodney W. Grimes /* Number of users comparison. */ 2459b50d902SRodney W. Grimes int 246a827060aSMark Murray ucmp(const void *a1, const void *a2) 2479b50d902SRodney W. Grimes { 2489b50d902SRodney W. Grimes if (ISDOWN(HS(a1))) 2499b50d902SRodney W. Grimes if (ISDOWN(HS(a2))) 2509b50d902SRodney W. Grimes return (tcmp(a1, a2)); 2519b50d902SRodney W. Grimes else 2529b50d902SRodney W. Grimes return (rflg); 2539b50d902SRodney W. Grimes else if (ISDOWN(HS(a2))) 2549b50d902SRodney W. Grimes return (-rflg); 2559b50d902SRodney W. Grimes else 2569b50d902SRodney W. Grimes return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers)); 2579b50d902SRodney W. Grimes } 2589b50d902SRodney W. Grimes 2599b50d902SRodney W. Grimes /* Uptime comparison. */ 2609b50d902SRodney W. Grimes int 261a827060aSMark Murray tcmp(const void *a1, const void *a2) 2629b50d902SRodney W. Grimes { 2639b50d902SRodney W. Grimes return (rflg * ( 2649b50d902SRodney W. Grimes (ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now 2659b50d902SRodney W. Grimes : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime) 2669b50d902SRodney W. Grimes - 2679b50d902SRodney W. Grimes (ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now 2689b50d902SRodney W. Grimes : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime) 2699b50d902SRodney W. Grimes )); 2709b50d902SRodney W. Grimes } 2719b50d902SRodney W. Grimes 2729b50d902SRodney W. Grimes void 273a827060aSMark Murray usage(void) 2749b50d902SRodney W. Grimes { 2759b50d902SRodney W. Grimes (void)fprintf(stderr, "usage: ruptime [-alrut]\n"); 2769b50d902SRodney W. Grimes exit(1); 2779b50d902SRodney W. Grimes } 278