| 1 | #ifndef lint |
| 2 | static char sccsid[] = "@(#)ruptime.c 4.4 82/05/09"; |
| 3 | #endif |
| 4 | |
| 5 | #include <sys/param.h> |
| 6 | #include <stdio.h> |
| 7 | #include <dir.h> |
| 8 | #include <utmp.h> |
| 9 | #include "rwhod.h" |
| 10 | |
| 11 | DIR *etc; |
| 12 | |
| 13 | #define NHOSTS 100 |
| 14 | int nhosts; |
| 15 | struct hs { |
| 16 | struct whod *hs_wd; |
| 17 | int hs_nusers; |
| 18 | } hs[NHOSTS]; |
| 19 | struct whod awhod; |
| 20 | int hscmp(); |
| 21 | |
| 22 | #define WHDRSIZE (sizeof (awhod) - sizeof (awhod.wd_we)) |
| 23 | |
| 24 | char *interval(); |
| 25 | int now; |
| 26 | char *malloc(), *sprintf(); |
| 27 | int aflg; |
| 28 | |
| 29 | main(argc, argv) |
| 30 | int argc; |
| 31 | char **argv; |
| 32 | { |
| 33 | struct direct *dp; |
| 34 | int f, i, t; |
| 35 | char buf[BUFSIZ]; int cc; |
| 36 | register struct hs *hsp = hs; |
| 37 | register struct whod *wd; |
| 38 | register struct whoent *we; |
| 39 | int maxloadav = 0; |
| 40 | |
| 41 | time(&t); |
| 42 | argc--, argv++; |
| 43 | again: |
| 44 | if (!strcmp(*argv, "-a")) { |
| 45 | aflg++; |
| 46 | argc--, argv++; |
| 47 | goto again; |
| 48 | } |
| 49 | if (chdir("/etc") < 0) { |
| 50 | perror("/etc"); |
| 51 | exit(1); |
| 52 | } |
| 53 | etc = opendir("."); |
| 54 | if (etc == NULL) { |
| 55 | perror("/etc"); |
| 56 | exit(1); |
| 57 | } |
| 58 | while (dp = readdir(etc)) { |
| 59 | if (dp->d_ino == 0) |
| 60 | continue; |
| 61 | if (strncmp(dp->d_name, "whod.", 5)) |
| 62 | continue; |
| 63 | if (nhosts == NHOSTS) { |
| 64 | fprintf(stderr, "too many hosts\n"); |
| 65 | exit(1); |
| 66 | } |
| 67 | f = open(dp->d_name, 0); |
| 68 | if (f > 0) { |
| 69 | cc = read(f, buf, BUFSIZ); |
| 70 | if (cc >= WHDRSIZE) { |
| 71 | hsp->hs_wd = (struct whod *)malloc(WHDRSIZE); |
| 72 | wd = (struct whod *)buf; |
| 73 | bcopy(buf, hsp->hs_wd, WHDRSIZE); |
| 74 | hsp->hs_nusers = 0; |
| 75 | for (i = 0; i < 2; i++) |
| 76 | if (wd->wd_loadav[i] > maxloadav) |
| 77 | maxloadav = wd->wd_loadav[i]; |
| 78 | we = (struct whoent *)(buf+cc); |
| 79 | while (--we >= wd->wd_we) |
| 80 | if (aflg || we->we_idle < 3600) |
| 81 | hsp->hs_nusers++; |
| 82 | nhosts++; hsp++; |
| 83 | } |
| 84 | } |
| 85 | (void) close(f); |
| 86 | } |
| 87 | qsort((char *)hs, nhosts, sizeof (hs[0]), hscmp); |
| 88 | (void) time(&now); |
| 89 | if (nhosts == 0) { |
| 90 | printf("no hosts!?!\n"); |
| 91 | exit(1); |
| 92 | } |
| 93 | for (i = 0; i < nhosts; i++) { |
| 94 | hsp = &hs[i]; |
| 95 | if (now - hsp->hs_wd->wd_recvtime > 5 * 60) { |
| 96 | printf("%-8.8s%s\n", hsp->hs_wd->wd_hostname, |
| 97 | interval(now - hsp->hs_wd->wd_recvtime, "down")); |
| 98 | continue; |
| 99 | } |
| 100 | printf("%-8.8s%s, %4d user%s load %*.2f, %*.2f, %*.2f\n", |
| 101 | hsp->hs_wd->wd_hostname, |
| 102 | interval(hsp->hs_wd->wd_sendtime - |
| 103 | hsp->hs_wd->wd_bootime, " up"), |
| 104 | hsp->hs_nusers, |
| 105 | hsp->hs_nusers == 1 ? ", " : "s,", |
| 106 | maxloadav >= 1000 ? 5 : 4, |
| 107 | hsp->hs_wd->wd_loadav[0] / 100.0, |
| 108 | maxloadav >= 1000 ? 5 : 4, |
| 109 | hsp->hs_wd->wd_loadav[1] / 100.0, |
| 110 | maxloadav >= 1000 ? 5 : 4, |
| 111 | hsp->hs_wd->wd_loadav[2] / 100.0); |
| 112 | cfree(hsp->hs_wd); |
| 113 | } |
| 114 | exit(0); |
| 115 | } |
| 116 | |
| 117 | char * |
| 118 | interval(time, updown) |
| 119 | int time; |
| 120 | char *updown; |
| 121 | { |
| 122 | static char resbuf[32]; |
| 123 | int days, hours, minutes; |
| 124 | |
| 125 | if (time < 0 || time > 3*30*24*60*60) { |
| 126 | (void) sprintf(resbuf, " %s ??:??", updown); |
| 127 | return (resbuf); |
| 128 | } |
| 129 | minutes = (time + 59) / 60; /* round to minutes */ |
| 130 | hours = minutes / 60; minutes %= 60; |
| 131 | days = hours / 24; hours %= 24; |
| 132 | if (days) |
| 133 | (void) sprintf(resbuf, "%s %2d+%02d:%02d", |
| 134 | updown, days, hours, minutes); |
| 135 | else |
| 136 | (void) sprintf(resbuf, " %s %2d:%02d", |
| 137 | updown, hours, minutes); |
| 138 | return (resbuf); |
| 139 | } |
| 140 | |
| 141 | hscmp(h1, h2) |
| 142 | struct hs *h1, *h2; |
| 143 | { |
| 144 | |
| 145 | return (strcmp(h1->hs_wd->wd_hostname, h2->hs_wd->wd_hostname)); |
| 146 | } |