[BACK]Return to rup.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rup

Annotation of src/usr.bin/rup/rup.c, Revision 1.7

1.7     ! deraadt     1: /*     $OpenBSD: rup.c,v 1.6 1997/02/01 07:18:28 tholo Exp $   */
1.2       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1993, John Brezak
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
1.7     ! deraadt    37: static char rcsid[] = "$OpenBSD: rup.c,v 1.6 1997/02/01 07:18:28 tholo Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
                     40: #include <stdio.h>
                     41: #include <stdlib.h>
                     42: #include <string.h>
                     43: #include <time.h>
                     44: #include <sys/param.h>
                     45: #include <sys/socket.h>
                     46: #include <netdb.h>
                     47: #include <rpc/rpc.h>
1.7     ! deraadt    48: #include <rpc/pmap_clnt.h>
1.1       deraadt    49: #include <arpa/inet.h>
                     50: #include <err.h>
                     51:
                     52: #undef FSHIFT                  /* Use protocol's shift and scale values */
                     53: #undef FSCALE
                     54: #include <rpcsvc/rstat.h>
                     55:
                     56: #define HOST_WIDTH 24
                     57:
                     58: int printtime;                 /* print the remote host(s)'s time */
                     59:
                     60: struct host_list {
                     61:        struct host_list *next;
                     62:        struct in_addr addr;
                     63: } *hosts;
                     64:
1.7     ! deraadt    65: void usage __P((void));
        !            66: int print_rup_data __P((char *, statstime *host_stat));
        !            67:
1.1       deraadt    68: int
                     69: search_host(addr)
                     70:        struct in_addr addr;
                     71: {
                     72:        struct host_list *hp;
                     73:
                     74:        if (!hosts)
                     75:                return(0);
                     76:
                     77:        for (hp = hosts; hp != NULL; hp = hp->next) {
                     78:                if (hp->addr.s_addr == addr.s_addr)
                     79:                        return(1);
                     80:        }
                     81:        return(0);
                     82: }
                     83:
                     84: void
                     85: remember_host(addr)
                     86:        struct in_addr addr;
                     87: {
                     88:        struct host_list *hp;
                     89:
                     90:        if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
                     91:                err(1, NULL);
                     92:                /* NOTREACHED */
                     93:        }
                     94:        hp->addr.s_addr = addr.s_addr;
                     95:        hp->next = hosts;
                     96:        hosts = hp;
                     97: }
                     98:
                     99:
                    100: 
                    101: struct rup_data {
                    102:        char *host;
                    103:        struct statstime statstime;
                    104: };
                    105: struct rup_data *rup_data;
                    106: int rup_data_idx = 0;
                    107: int rup_data_max = 0;
                    108:
                    109: enum sort_type {
                    110:        SORT_NONE,
                    111:        SORT_HOST,
                    112:        SORT_LDAV,
                    113:        SORT_UPTIME
                    114: };
                    115: enum sort_type sort_type;
                    116:
1.7     ! deraadt   117: int
1.1       deraadt   118: compare(d1, d2)
                    119:        struct rup_data *d1;
                    120:        struct rup_data *d2;
                    121: {
                    122:        switch(sort_type) {
                    123:        case SORT_HOST:
                    124:                return strcmp(d1->host, d2->host);
                    125:        case SORT_LDAV:
1.3       deraadt   126:                return d1->statstime.avenrun[0]
1.1       deraadt   127:                        - d2->statstime.avenrun[0];
                    128:        case SORT_UPTIME:
                    129:                return d1->statstime.boottime.tv_sec
                    130:                        - d2->statstime.boottime.tv_sec;
                    131:        default:
                    132:                /* something's really wrong here */
                    133:                abort();
                    134:        }
                    135: }
                    136:
                    137: void
                    138: remember_rup_data(host, st)
                    139:        char *host;
                    140:        struct statstime *st;
                    141: {
                    142:         if (rup_data_idx >= rup_data_max) {
                    143:                 rup_data_max += 16;
                    144:                 rup_data = realloc (rup_data,
1.3       deraadt   145:                    rup_data_max * sizeof(struct rup_data));
1.1       deraadt   146:                 if (rup_data == NULL) {
                    147:                         err (1, NULL);
                    148:                        /* NOTREACHED */
                    149:                 }
                    150:         }
                    151:
                    152:        rup_data[rup_data_idx].host = strdup(host);
                    153:        rup_data[rup_data_idx].statstime = *st;
                    154:        rup_data_idx++;
                    155: }
                    156:
                    157:
                    158: int
                    159: rstat_reply(replyp, raddrp)
                    160:        char *replyp;
                    161:        struct sockaddr_in *raddrp;
                    162: {
                    163:        struct hostent *hp;
                    164:        char *host;
                    165:        statstime *host_stat = (statstime *)replyp;
                    166:
                    167:        if (!search_host(raddrp->sin_addr)) {
                    168:                hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
1.3       deraadt   169:                    sizeof(struct in_addr), AF_INET);
1.1       deraadt   170:                if (hp)
                    171:                        host = hp->h_name;
                    172:                else
                    173:                        host = inet_ntoa(raddrp->sin_addr);
                    174:
                    175:                remember_host(raddrp->sin_addr);
                    176:
1.3       deraadt   177:                if (sort_type != SORT_NONE)
1.1       deraadt   178:                        remember_rup_data(host, host_stat);
1.3       deraadt   179:                else
1.1       deraadt   180:                        print_rup_data(host, host_stat);
                    181:        }
                    182:
                    183:        return (0);
                    184: }
                    185:
                    186:
                    187: int
                    188: print_rup_data(host, host_stat)
                    189:        char *host;
                    190:        statstime *host_stat;
                    191: {
                    192:        struct tm *tmp_time;
                    193:        struct tm host_time;
1.6       tholo     194:        unsigned ups=0,upm=0,uph=0,upd=0;
                    195:
1.1       deraadt   196:        char days_buf[16];
                    197:        char hours_buf[16];
                    198:
                    199:        printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
                    200:
                    201:        tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
                    202:        host_time = *tmp_time;
                    203:
                    204:        host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
                    205:
1.6       tholo     206:        ups=host_stat->curtime.tv_sec;
                    207:        upd=ups/(3600*24);
                    208:        ups-=upd*3600*24;
                    209:        uph=ups/3600;
                    210:        ups-=uph*3600;
                    211:        upm=ups/60;
                    212:
                    213:        if (upd != 0)
                    214:                sprintf(days_buf, "%3u day%s, ", upd,
                    215:                        (upd > 1) ? "s" : "");
1.1       deraadt   216:        else
                    217:                days_buf[0] = '\0';
                    218:
1.6       tholo     219:        if (uph != 0)
                    220:                sprintf(hours_buf, "%2u:%02u, ",
                    221:                        uph, upm);
1.1       deraadt   222:        else
1.6       tholo     223:                if (upm != 0)
                    224:                        sprintf(hours_buf, "%2u mins, ", upm);
1.1       deraadt   225:                else
                    226:                        hours_buf[0] = '\0';
                    227:
                    228:        if (printtime)
1.4       deraadt   229:                printf(" %2d:%02d%cm",
                    230:                    (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
1.3       deraadt   231:                    host_time.tm_min,
                    232:                    (host_time.tm_hour >= 12) ? 'p' : 'a');
1.1       deraadt   233:
                    234:        printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
1.3       deraadt   235:            days_buf, hours_buf,
                    236:            (double)host_stat->avenrun[0]/FSCALE,
                    237:            (double)host_stat->avenrun[1]/FSCALE,
                    238:            (double)host_stat->avenrun[2]/FSCALE);
1.1       deraadt   239:
                    240:        return(0);
                    241: }
                    242:
                    243:
                    244: void
                    245: onehost(host)
                    246:        char *host;
                    247: {
                    248:        CLIENT *rstat_clnt;
                    249:        statstime host_stat;
                    250:        static struct timeval timeout = {25, 0};
1.5       deraadt   251:        extern char *__progname;
1.1       deraadt   252:
                    253:        rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
                    254:        if (rstat_clnt == NULL) {
1.5       deraadt   255:                fprintf(stderr, "%s: %s", __progname,
                    256:                    clnt_spcreateerror(host));
1.1       deraadt   257:                return;
                    258:        }
                    259:
                    260:        bzero((char *)&host_stat, sizeof(host_stat));
1.3       deraadt   261:        if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL,
                    262:            xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
1.5       deraadt   263:                fprintf(stderr, "%s: %s", __progname,
                    264:                    clnt_sperror(rstat_clnt, host));
1.1       deraadt   265:                return;
                    266:        }
                    267:
                    268:        print_rup_data(host, &host_stat);
                    269:        clnt_destroy(rstat_clnt);
                    270: }
                    271:
                    272: void
                    273: allhosts()
                    274: {
                    275:        statstime host_stat;
                    276:        enum clnt_stat clnt_stat;
1.5       deraadt   277:        extern char *__progname;
1.1       deraadt   278:        size_t i;
                    279:
                    280:        if (sort_type != SORT_NONE) {
                    281:                printf("collecting responses...");
                    282:                fflush(stdout);
                    283:        }
                    284:
                    285:        clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
1.7     ! deraadt   286:            xdr_void, NULL, xdr_statstime, (char *)&host_stat, rstat_reply);
1.1       deraadt   287:        if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
1.5       deraadt   288:                fprintf(stderr, "%s: %s", __progname, clnt_sperrno(clnt_stat));
1.1       deraadt   289:                exit(1);
                    290:        }
                    291:
                    292:        if (sort_type != SORT_NONE) {
                    293:                putchar('\n');
1.3       deraadt   294:                qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
                    295:                    compare);
1.1       deraadt   296:
                    297:                for (i = 0; i < rup_data_idx; i++) {
1.3       deraadt   298:                        print_rup_data(rup_data[i].host,
                    299:                            &rup_data[i].statstime);
1.1       deraadt   300:                }
                    301:        }
                    302: }
                    303:
1.7     ! deraadt   304: int
1.1       deraadt   305: main(argc, argv)
                    306:        int argc;
                    307:        char *argv[];
                    308: {
                    309:        int ch;
                    310:        extern int optind;
                    311:
                    312:        sort_type = SORT_NONE;
                    313:        while ((ch = getopt(argc, argv, "dhlt")) != -1)
                    314:                switch (ch) {
                    315:                case 'd':
                    316:                        printtime = 1;
                    317:                        break;
                    318:                case 'h':
                    319:                        sort_type = SORT_HOST;
                    320:                        break;
                    321:                case 'l':
                    322:                        sort_type = SORT_LDAV;
                    323:                        break;
                    324:                case 't':
                    325:                        sort_type = SORT_UPTIME;
                    326:                        break;
                    327:                default:
                    328:                        usage();
                    329:                        /*NOTREACHED*/
                    330:                }
                    331:
                    332:        setlinebuf(stdout);
                    333:
                    334:        if (argc == optind)
                    335:                allhosts();
                    336:        else {
                    337:                for (; optind < argc; optind++)
                    338:                        onehost(argv[optind]);
                    339:        }
                    340:
                    341:        exit(0);
                    342: }
                    343:
                    344:
1.7     ! deraadt   345: void
1.1       deraadt   346: usage()
                    347: {
                    348:        fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n");
                    349:        exit(1);
                    350: }