17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*740638c8Sbw * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <sys/param.h> 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <dirent.h> 457c478bd9Sstevel@tonic-gate #include <stdlib.h> 467c478bd9Sstevel@tonic-gate #include <strings.h> 477c478bd9Sstevel@tonic-gate #include <unistd.h> 487c478bd9Sstevel@tonic-gate #include <fcntl.h> 497c478bd9Sstevel@tonic-gate #include <protocols/rwhod.h> 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate static DIR *dirp; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #define HOSTLIM 100 547c478bd9Sstevel@tonic-gate static int hostslim = HOSTLIM; 557c478bd9Sstevel@tonic-gate static int nhosts; 567c478bd9Sstevel@tonic-gate struct hs { 577c478bd9Sstevel@tonic-gate struct whod *hs_wd; 587c478bd9Sstevel@tonic-gate int hs_nusers; 597c478bd9Sstevel@tonic-gate }; 607c478bd9Sstevel@tonic-gate static int hscmp(), ucmp(), lcmp(), tcmp(); 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #define RWHODIR "/var/spool/rwho" 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static char *interval(); 657c478bd9Sstevel@tonic-gate static time_t now; 667c478bd9Sstevel@tonic-gate static int aflg; 677c478bd9Sstevel@tonic-gate static int rflg = 1; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #define down(h) (now - (h)->hs_wd->wd_recvtime > 11 * 60) 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* ARGSUSED */ 72*740638c8Sbw int 737c478bd9Sstevel@tonic-gate main(int argc, char **argv) 747c478bd9Sstevel@tonic-gate { 757c478bd9Sstevel@tonic-gate struct dirent *dp; 767c478bd9Sstevel@tonic-gate int f, i; 777c478bd9Sstevel@tonic-gate struct whod *buf; 787c478bd9Sstevel@tonic-gate int cc; 797c478bd9Sstevel@tonic-gate char *name; 807c478bd9Sstevel@tonic-gate struct hs *hs; 817c478bd9Sstevel@tonic-gate struct hs *hsp; 827c478bd9Sstevel@tonic-gate struct whod *wd; 837c478bd9Sstevel@tonic-gate struct whoent *we; 847c478bd9Sstevel@tonic-gate int maxloadav = 0; 857c478bd9Sstevel@tonic-gate int (*cmp)() = hscmp; 867c478bd9Sstevel@tonic-gate ptrdiff_t hoff; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate name = *argv; 897c478bd9Sstevel@tonic-gate while (*++argv) 907c478bd9Sstevel@tonic-gate while (**argv) 917c478bd9Sstevel@tonic-gate switch (*(*argv)++) { 927c478bd9Sstevel@tonic-gate case 'a': 937c478bd9Sstevel@tonic-gate aflg++; 947c478bd9Sstevel@tonic-gate break; 957c478bd9Sstevel@tonic-gate case 'l': 967c478bd9Sstevel@tonic-gate cmp = lcmp; 977c478bd9Sstevel@tonic-gate break; 987c478bd9Sstevel@tonic-gate case 'u': 997c478bd9Sstevel@tonic-gate cmp = ucmp; 1007c478bd9Sstevel@tonic-gate break; 1017c478bd9Sstevel@tonic-gate case 't': 1027c478bd9Sstevel@tonic-gate cmp = tcmp; 1037c478bd9Sstevel@tonic-gate break; 1047c478bd9Sstevel@tonic-gate case 'r': 1057c478bd9Sstevel@tonic-gate rflg = -rflg; 1067c478bd9Sstevel@tonic-gate break; 1077c478bd9Sstevel@tonic-gate case '-': 1087c478bd9Sstevel@tonic-gate break; 1097c478bd9Sstevel@tonic-gate default: 1107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Usage: %s [ -alrtu ]" 1117c478bd9Sstevel@tonic-gate " (choose at most one of l, t, or u)\n", 1127c478bd9Sstevel@tonic-gate name); 1137c478bd9Sstevel@tonic-gate exit(1); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate if ((hs = malloc(hostslim * sizeof (struct hs))) == NULL) { 1177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "initial hs malloc failed\n"); 1187c478bd9Sstevel@tonic-gate exit(1); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate hsp = hs; 1217c478bd9Sstevel@tonic-gate if ((buf = malloc(sizeof (struct whod))) == NULL) { 1227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "initial buf malloc failed\n"); 1237c478bd9Sstevel@tonic-gate exit(1); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate if (chdir(RWHODIR) < 0) { 1277c478bd9Sstevel@tonic-gate perror(RWHODIR); 1287c478bd9Sstevel@tonic-gate exit(1); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate dirp = opendir("."); 1317c478bd9Sstevel@tonic-gate if (dirp == NULL) { 1327c478bd9Sstevel@tonic-gate perror(RWHODIR); 1337c478bd9Sstevel@tonic-gate exit(1); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate while (dp = readdir(dirp)) { 1367c478bd9Sstevel@tonic-gate if (dp->d_ino == 0) 1377c478bd9Sstevel@tonic-gate continue; 1387c478bd9Sstevel@tonic-gate if (strncmp(dp->d_name, "whod.", 5)) 1397c478bd9Sstevel@tonic-gate continue; 1407c478bd9Sstevel@tonic-gate if (nhosts == hostslim) { 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * We trust that the file system's limit on the number 1437c478bd9Sstevel@tonic-gate * of files in a directory will kick in long before 1447c478bd9Sstevel@tonic-gate * integer overflow. 1457c478bd9Sstevel@tonic-gate */ 1467c478bd9Sstevel@tonic-gate hostslim = hostslim << 1; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * hsp points into an area about to be moved, 1507c478bd9Sstevel@tonic-gate * so we first remember its offset into hs[], 1517c478bd9Sstevel@tonic-gate * then restore it after realloc() has moved 1527c478bd9Sstevel@tonic-gate * the data. 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate hoff = hsp - hs; 1557c478bd9Sstevel@tonic-gate hs = realloc(hs, hostslim * sizeof (struct hs)); 1567c478bd9Sstevel@tonic-gate if (hs == NULL) { 1577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "too many hosts\n"); 1587c478bd9Sstevel@tonic-gate exit(1); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate hsp = hs + hoff; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate f = open(dp->d_name, 0); 1637c478bd9Sstevel@tonic-gate if (f > 0) { 1647c478bd9Sstevel@tonic-gate int whdrsize = sizeof (*buf) - sizeof (buf->wd_we); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate cc = read(f, buf, sizeof (struct whod)); 1677c478bd9Sstevel@tonic-gate if (cc >= whdrsize) { 1687c478bd9Sstevel@tonic-gate hsp->hs_wd = malloc(whdrsize); 1697c478bd9Sstevel@tonic-gate wd = buf; 1707c478bd9Sstevel@tonic-gate bcopy((char *)buf, (char *)hsp->hs_wd, 1717c478bd9Sstevel@tonic-gate whdrsize); 1727c478bd9Sstevel@tonic-gate hsp->hs_nusers = 0; 1737c478bd9Sstevel@tonic-gate for (i = 0; i < 2; i++) 1747c478bd9Sstevel@tonic-gate if (wd->wd_loadav[i] > maxloadav) 1757c478bd9Sstevel@tonic-gate maxloadav = wd->wd_loadav[i]; 1767c478bd9Sstevel@tonic-gate /* LINTED: pointer alignment */ 1777c478bd9Sstevel@tonic-gate we = (struct whoent *)(((char *)buf)+cc); 1787c478bd9Sstevel@tonic-gate while (--we >= wd->wd_we) 1797c478bd9Sstevel@tonic-gate if (aflg || we->we_idle < 3600) 1807c478bd9Sstevel@tonic-gate hsp->hs_nusers++; 1817c478bd9Sstevel@tonic-gate nhosts++; hsp++; 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate (void) close(f); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate (void) time(&now); 1877c478bd9Sstevel@tonic-gate qsort((char *)hs, nhosts, sizeof (hs[0]), cmp); 1887c478bd9Sstevel@tonic-gate if (nhosts == 0) { 1897c478bd9Sstevel@tonic-gate (void) printf("no hosts!?!\n"); 1907c478bd9Sstevel@tonic-gate exit(1); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate for (i = 0; i < nhosts; i++) { 1937c478bd9Sstevel@tonic-gate hsp = &hs[i]; 1947c478bd9Sstevel@tonic-gate if (down(hsp)) { 1957c478bd9Sstevel@tonic-gate (void) printf("%-12s%s\n", hsp->hs_wd->wd_hostname, 1967c478bd9Sstevel@tonic-gate interval((int)(now - hsp->hs_wd->wd_recvtime), 1977c478bd9Sstevel@tonic-gate "down")); 1987c478bd9Sstevel@tonic-gate continue; 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate (void) printf("%-12s%s, %4d user%s load %*.2f," 2017c478bd9Sstevel@tonic-gate " %*.2f, %*.2f\n", 2027c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_hostname, 2037c478bd9Sstevel@tonic-gate interval(hsp->hs_wd->wd_sendtime - 2047c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_boottime, " up"), 2057c478bd9Sstevel@tonic-gate hsp->hs_nusers, 2067c478bd9Sstevel@tonic-gate hsp->hs_nusers == 1 ? ", " : "s,", 2077c478bd9Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4, 2087c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_loadav[0] / 100.0, 2097c478bd9Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4, 2107c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_loadav[1] / 100.0, 2117c478bd9Sstevel@tonic-gate maxloadav >= 1000 ? 5 : 4, 2127c478bd9Sstevel@tonic-gate hsp->hs_wd->wd_loadav[2] / 100.0); 2137c478bd9Sstevel@tonic-gate free(hsp->hs_wd); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate return (0); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate static char * 2207c478bd9Sstevel@tonic-gate interval(int time, char *updown) 2217c478bd9Sstevel@tonic-gate { 2227c478bd9Sstevel@tonic-gate static char resbuf[32]; 2237c478bd9Sstevel@tonic-gate int days, hours, minutes; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate if (time < 0 || time > 10*365*24*60*60) { 2267c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, " %s ??:??", updown); 2277c478bd9Sstevel@tonic-gate return (resbuf); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate minutes = (time + 59) / 60; /* round to minutes */ 2307c478bd9Sstevel@tonic-gate hours = minutes / 60; minutes %= 60; 2317c478bd9Sstevel@tonic-gate days = hours / 24; hours %= 24; 2327c478bd9Sstevel@tonic-gate if (days) 2337c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, "%s %2d+%02d:%02d", 2347c478bd9Sstevel@tonic-gate updown, days, hours, minutes); 2357c478bd9Sstevel@tonic-gate else 2367c478bd9Sstevel@tonic-gate (void) sprintf(resbuf, "%s %2d:%02d", 2377c478bd9Sstevel@tonic-gate updown, hours, minutes); 2387c478bd9Sstevel@tonic-gate return (resbuf); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate static int 2427c478bd9Sstevel@tonic-gate hscmp(struct hs *h1, struct hs *h2) 2437c478bd9Sstevel@tonic-gate { 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate return (rflg * strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname)); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * Compare according to load average. 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate static int 2527c478bd9Sstevel@tonic-gate lcmp(struct hs *h1, struct hs *h2) 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate if (down(h1)) 2567c478bd9Sstevel@tonic-gate if (down(h2)) 2577c478bd9Sstevel@tonic-gate return (tcmp(h1, h2)); 2587c478bd9Sstevel@tonic-gate else 2597c478bd9Sstevel@tonic-gate return (rflg); 2607c478bd9Sstevel@tonic-gate else if (down(h2)) 2617c478bd9Sstevel@tonic-gate return (-rflg); 2627c478bd9Sstevel@tonic-gate else 2637c478bd9Sstevel@tonic-gate return (rflg * 2647c478bd9Sstevel@tonic-gate (h2->hs_wd->wd_loadav[0] - h1->hs_wd->wd_loadav[0])); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate /* 2687c478bd9Sstevel@tonic-gate * Compare according to number of users. 2697c478bd9Sstevel@tonic-gate */ 2707c478bd9Sstevel@tonic-gate static int 2717c478bd9Sstevel@tonic-gate ucmp(struct hs *h1, struct hs *h2) 2727c478bd9Sstevel@tonic-gate { 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate if (down(h1)) 2757c478bd9Sstevel@tonic-gate if (down(h2)) 2767c478bd9Sstevel@tonic-gate return (tcmp(h1, h2)); 2777c478bd9Sstevel@tonic-gate else 2787c478bd9Sstevel@tonic-gate return (rflg); 2797c478bd9Sstevel@tonic-gate else if (down(h2)) 2807c478bd9Sstevel@tonic-gate return (-rflg); 2817c478bd9Sstevel@tonic-gate else 2827c478bd9Sstevel@tonic-gate return (rflg * (h2->hs_nusers - h1->hs_nusers)); 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate * Compare according to uptime. 2877c478bd9Sstevel@tonic-gate */ 2887c478bd9Sstevel@tonic-gate static int 2897c478bd9Sstevel@tonic-gate tcmp(struct hs *h1, struct hs *h2) 2907c478bd9Sstevel@tonic-gate { 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate return (rflg * ( 2937c478bd9Sstevel@tonic-gate (down(h2) ? h2->hs_wd->wd_recvtime - now : 2947c478bd9Sstevel@tonic-gate h2->hs_wd->wd_sendtime - h2->hs_wd->wd_boottime) 2957c478bd9Sstevel@tonic-gate - 2967c478bd9Sstevel@tonic-gate (down(h1) ? h1->hs_wd->wd_recvtime - now : 2977c478bd9Sstevel@tonic-gate h1->hs_wd->wd_sendtime - h1->hs_wd->wd_boottime))); 2987c478bd9Sstevel@tonic-gate } 299