[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.6

1.6     ! tholo       1: /*     $OpenBSD: rup.c,v 1.5 1996/12/10 19:00:03 deraadt 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.6     ! tholo      37: static char rcsid[] = "$OpenBSD: rup.c,v 1.5 1996/12/10 19:00:03 deraadt 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>
                     48: #include <arpa/inet.h>
                     49: #include <err.h>
                     50:
                     51: #undef FSHIFT                  /* Use protocol's shift and scale values */
                     52: #undef FSCALE
                     53: #include <rpcsvc/rstat.h>
                     54:
                     55: #define HOST_WIDTH 24
                     56:
                     57: int printtime;                 /* print the remote host(s)'s time */
                     58:
                     59: struct host_list {
                     60:        struct host_list *next;
                     61:        struct in_addr addr;
                     62: } *hosts;
                     63:
                     64: int
                     65: search_host(addr)
                     66:        struct in_addr addr;
                     67: {
                     68:        struct host_list *hp;
                     69:
                     70:        if (!hosts)
                     71:                return(0);
                     72:
                     73:        for (hp = hosts; hp != NULL; hp = hp->next) {
                     74:                if (hp->addr.s_addr == addr.s_addr)
                     75:                        return(1);
                     76:        }
                     77:        return(0);
                     78: }
                     79:
                     80: void
                     81: remember_host(addr)
                     82:        struct in_addr addr;
                     83: {
                     84:        struct host_list *hp;
                     85:
                     86:        if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
                     87:                err(1, NULL);
                     88:                /* NOTREACHED */
                     89:        }
                     90:        hp->addr.s_addr = addr.s_addr;
                     91:        hp->next = hosts;
                     92:        hosts = hp;
                     93: }
                     94:
                     95:
                     96: 
                     97: struct rup_data {
                     98:        char *host;
                     99:        struct statstime statstime;
                    100: };
                    101: struct rup_data *rup_data;
                    102: int rup_data_idx = 0;
                    103: int rup_data_max = 0;
                    104:
                    105: enum sort_type {
                    106:        SORT_NONE,
                    107:        SORT_HOST,
                    108:        SORT_LDAV,
                    109:        SORT_UPTIME
                    110: };
                    111: enum sort_type sort_type;
                    112:
                    113: compare(d1, d2)
                    114:        struct rup_data *d1;
                    115:        struct rup_data *d2;
                    116: {
                    117:        switch(sort_type) {
                    118:        case SORT_HOST:
                    119:                return strcmp(d1->host, d2->host);
                    120:        case SORT_LDAV:
1.3       deraadt   121:                return d1->statstime.avenrun[0]
1.1       deraadt   122:                        - d2->statstime.avenrun[0];
                    123:        case SORT_UPTIME:
                    124:                return d1->statstime.boottime.tv_sec
                    125:                        - d2->statstime.boottime.tv_sec;
                    126:        default:
                    127:                /* something's really wrong here */
                    128:                abort();
                    129:        }
                    130: }
                    131:
                    132: void
                    133: remember_rup_data(host, st)
                    134:        char *host;
                    135:        struct statstime *st;
                    136: {
                    137:         if (rup_data_idx >= rup_data_max) {
                    138:                 rup_data_max += 16;
                    139:                 rup_data = realloc (rup_data,
1.3       deraadt   140:                    rup_data_max * sizeof(struct rup_data));
1.1       deraadt   141:                 if (rup_data == NULL) {
                    142:                         err (1, NULL);
                    143:                        /* NOTREACHED */
                    144:                 }
                    145:         }
                    146:
                    147:        rup_data[rup_data_idx].host = strdup(host);
                    148:        rup_data[rup_data_idx].statstime = *st;
                    149:        rup_data_idx++;
                    150: }
                    151:
                    152:
                    153: int
                    154: rstat_reply(replyp, raddrp)
                    155:        char *replyp;
                    156:        struct sockaddr_in *raddrp;
                    157: {
                    158:        struct hostent *hp;
                    159:        char *host;
                    160:        statstime *host_stat = (statstime *)replyp;
                    161:
                    162:        if (!search_host(raddrp->sin_addr)) {
                    163:                hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
1.3       deraadt   164:                    sizeof(struct in_addr), AF_INET);
1.1       deraadt   165:                if (hp)
                    166:                        host = hp->h_name;
                    167:                else
                    168:                        host = inet_ntoa(raddrp->sin_addr);
                    169:
                    170:                remember_host(raddrp->sin_addr);
                    171:
1.3       deraadt   172:                if (sort_type != SORT_NONE)
1.1       deraadt   173:                        remember_rup_data(host, host_stat);
1.3       deraadt   174:                else
1.1       deraadt   175:                        print_rup_data(host, host_stat);
                    176:        }
                    177:
                    178:        return (0);
                    179: }
                    180:
                    181:
                    182: int
                    183: print_rup_data(host, host_stat)
                    184:        char *host;
                    185:        statstime *host_stat;
                    186: {
                    187:        struct tm *tmp_time;
                    188:        struct tm host_time;
1.6     ! tholo     189:        unsigned ups=0,upm=0,uph=0,upd=0;
        !           190:
1.1       deraadt   191:        char days_buf[16];
                    192:        char hours_buf[16];
                    193:
                    194:        printf("%-*.*s", HOST_WIDTH, HOST_WIDTH, host);
                    195:
                    196:        tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
                    197:        host_time = *tmp_time;
                    198:
                    199:        host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
                    200:
1.6     ! tholo     201:        ups=host_stat->curtime.tv_sec;
        !           202:        upd=ups/(3600*24);
        !           203:        ups-=upd*3600*24;
        !           204:        uph=ups/3600;
        !           205:        ups-=uph*3600;
        !           206:        upm=ups/60;
        !           207:
        !           208:        if (upd != 0)
        !           209:                sprintf(days_buf, "%3u day%s, ", upd,
        !           210:                        (upd > 1) ? "s" : "");
1.1       deraadt   211:        else
                    212:                days_buf[0] = '\0';
                    213:
1.6     ! tholo     214:        if (uph != 0)
        !           215:                sprintf(hours_buf, "%2u:%02u, ",
        !           216:                        uph, upm);
1.1       deraadt   217:        else
1.6     ! tholo     218:                if (upm != 0)
        !           219:                        sprintf(hours_buf, "%2u mins, ", upm);
1.1       deraadt   220:                else
                    221:                        hours_buf[0] = '\0';
                    222:
                    223:        if (printtime)
1.4       deraadt   224:                printf(" %2d:%02d%cm",
                    225:                    (host_time.tm_hour % 12) ? (host_time.tm_hour % 12) : 12,
1.3       deraadt   226:                    host_time.tm_min,
                    227:                    (host_time.tm_hour >= 12) ? 'p' : 'a');
1.1       deraadt   228:
                    229:        printf(" up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
1.3       deraadt   230:            days_buf, hours_buf,
                    231:            (double)host_stat->avenrun[0]/FSCALE,
                    232:            (double)host_stat->avenrun[1]/FSCALE,
                    233:            (double)host_stat->avenrun[2]/FSCALE);
1.1       deraadt   234:
                    235:        return(0);
                    236: }
                    237:
                    238:
                    239: void
                    240: onehost(host)
                    241:        char *host;
                    242: {
                    243:        CLIENT *rstat_clnt;
                    244:        statstime host_stat;
                    245:        static struct timeval timeout = {25, 0};
1.5       deraadt   246:        extern char *__progname;
1.1       deraadt   247:
                    248:        rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
                    249:        if (rstat_clnt == NULL) {
1.5       deraadt   250:                fprintf(stderr, "%s: %s", __progname,
                    251:                    clnt_spcreateerror(host));
1.1       deraadt   252:                return;
                    253:        }
                    254:
                    255:        bzero((char *)&host_stat, sizeof(host_stat));
1.3       deraadt   256:        if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL,
                    257:            xdr_statstime, &host_stat, timeout) != RPC_SUCCESS) {
1.5       deraadt   258:                fprintf(stderr, "%s: %s", __progname,
                    259:                    clnt_sperror(rstat_clnt, host));
1.1       deraadt   260:                return;
                    261:        }
                    262:
                    263:        print_rup_data(host, &host_stat);
                    264:        clnt_destroy(rstat_clnt);
                    265: }
                    266:
                    267: void
                    268: allhosts()
                    269: {
                    270:        statstime host_stat;
                    271:        enum clnt_stat clnt_stat;
1.5       deraadt   272:        extern char *__progname;
1.1       deraadt   273:        size_t i;
                    274:
                    275:        if (sort_type != SORT_NONE) {
                    276:                printf("collecting responses...");
                    277:                fflush(stdout);
                    278:        }
                    279:
                    280:        clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
1.3       deraadt   281:            xdr_void, NULL, xdr_statstime, &host_stat, rstat_reply);
1.1       deraadt   282:        if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
1.5       deraadt   283:                fprintf(stderr, "%s: %s", __progname, clnt_sperrno(clnt_stat));
1.1       deraadt   284:                exit(1);
                    285:        }
                    286:
                    287:        if (sort_type != SORT_NONE) {
                    288:                putchar('\n');
1.3       deraadt   289:                qsort(rup_data, rup_data_idx, sizeof(struct rup_data),
                    290:                    compare);
1.1       deraadt   291:
                    292:                for (i = 0; i < rup_data_idx; i++) {
1.3       deraadt   293:                        print_rup_data(rup_data[i].host,
                    294:                            &rup_data[i].statstime);
1.1       deraadt   295:                }
                    296:        }
                    297: }
                    298:
                    299:
                    300: main(argc, argv)
                    301:        int argc;
                    302:        char *argv[];
                    303: {
                    304:        int ch;
                    305:        extern int optind;
                    306:
                    307:        sort_type = SORT_NONE;
                    308:        while ((ch = getopt(argc, argv, "dhlt")) != -1)
                    309:                switch (ch) {
                    310:                case 'd':
                    311:                        printtime = 1;
                    312:                        break;
                    313:                case 'h':
                    314:                        sort_type = SORT_HOST;
                    315:                        break;
                    316:                case 'l':
                    317:                        sort_type = SORT_LDAV;
                    318:                        break;
                    319:                case 't':
                    320:                        sort_type = SORT_UPTIME;
                    321:                        break;
                    322:                default:
                    323:                        usage();
                    324:                        /*NOTREACHED*/
                    325:                }
                    326:
                    327:        setlinebuf(stdout);
                    328:
                    329:        if (argc == optind)
                    330:                allhosts();
                    331:        else {
                    332:                for (; optind < argc; optind++)
                    333:                        onehost(argv[optind]);
                    334:        }
                    335:
                    336:        exit(0);
                    337: }
                    338:
                    339:
                    340: usage()
                    341: {
                    342:        fprintf(stderr, "Usage: rup [-dhlt] [hosts ...]\n");
                    343:        exit(1);
                    344: }