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