⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 if.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 2 页
字号:
        printf(" %*.*s",     -max_ip,    max_ip,    cur_if->ip);        if (oflag) {            printf(" %*s %*s", max_ibytes,  cur_if->s_ibytes,                               max_obytes,  cur_if->s_obytes);        } else {            printf(" %*s %*s", max_ipkts,   cur_if->s_ipkts,                               max_ierrs,   cur_if->s_ierrs);            if (bflag)                 printf(" %*s", max_ibytes,  cur_if->s_ibytes);                printf(" %*s %*s", max_opkts,   cur_if->s_opkts,                               max_oerrs,   cur_if->s_oerrs);            if (bflag)                 printf(" %*s", max_obytes,  cur_if->s_obytes);            printf(" %*s",     max_outq,    cur_if->s_outq);        }     /* if (tflag)            printf(" %4d", cur_if->???);      */        if (dflag)            printf(" %4d", cur_if->drops);        putchar('\n');    }        /*         * ... and tidy up.         */    for (cur_if = if_head; cur_if; cur_if=if_head) {        if_head=cur_if->next;        cur_if->next = NULL;        SNMP_FREE( cur_if );    }}#define	MAXIF	100struct	iftot {	char	ift_name[128];		/* interface name */        int     ifIndex;	u_long	ift_ip;			/* input packets */	u_long	ift_ib;			/* input bytes */	u_long	ift_ie;			/* input errors */	u_long	ift_op;			/* output packets */	u_long	ift_ob;			/* output bytes */	u_long	ift_oe;			/* output errors */	u_long	ift_co;			/* collisions */	u_long	ift_dr;			/* drops */};int signalled;	/* set if alarm goes off "early" *//* * Print a running summary of interface statistics. * Repeat display every interval seconds, showing statistics * collected over that interval.  Assumes that interval is non-zero. * First line printed at top of screen is always cumulative. */static voidsidewaysintpr(unsigned int interval){    /*     * As with the "one-shot" interface display, there are     *   two different possible output formats.  The Net/     *   Open-BSD style displays both information about a     *   single interface *and* the overall totals.     * The equivalent Free-BSD approach is to report on one     *   or the other (rather than both).  This is probably     *   more useful (IMO), and significantly more efficient.     *   So that's the style implemented here.     *     * Note that the 'ifcol' OID buffer can represent a full     *   instance (including ifIndex), rather than just a     *   column object OID, as with the one-shot code.     */    oid    ifcol_oid[]  = { 1,3,6,1,2,1,2,2,1,0,0 };    size_t ifcol_len    = OID_LENGTH( ifcol_oid );    netsnmp_variable_list *var, *vp;    struct iftot *ip  = NULL, *cur_if = NULL;    /* single I/F display */    struct iftot *sum = NULL, *total  = NULL;    /* overall summary    */    int    line;    int    first;    int    i;    var = NULL;    if ( intrface ) {        /*         * Locate the ifIndex of the interface to monitor,         *   by walking the ifDescr column of the ifTable         */        ifcol_oid[ ifcol_len-2 ] = 2;   /* ifDescr  */        snmp_varlist_add_variable( &var, ifcol_oid, ifcol_len-1,                                   ASN_NULL, NULL,  0);        i = strlen(intrface);        netsnmp_query_walk( var, ss );        for (vp=var; vp; vp=vp->next_variable) {            if (strncmp(intrface, (char *)vp->val.string, i) == 0 &&                i == vp->val_len)                break;  /* found requested interface */        }        /*         * XXX - Might be worth searching ifName/ifAlias as well         */        if (!vp) {            fprintf(stderr, "%s: unknown interface\n", intrface );            exit(1);        }        /*         *  Prepare the current and previous 'iftot' structures,         *    and set the ifIndex value in the OID buffer.         */        ip     = SNMP_MALLOC_TYPEDEF( struct iftot );        cur_if = SNMP_MALLOC_TYPEDEF( struct iftot );        if (!ip || !cur_if) {            fprintf(stderr, "internal error\n");            exit(1);        }        ifcol_oid[ ifcol_len-1 ] = vp->name[ ifcol_len-1 ];        snmp_free_var( var );        var = NULL;    } else {        /*         *  Prepare the current and previous 'iftot' structures.         *    (using different pointers, for consistency with *BSD code)         */        sum   = SNMP_MALLOC_TYPEDEF( struct iftot );        total = SNMP_MALLOC_TYPEDEF( struct iftot );        if (!sum || !total) {            fprintf(stderr, "internal error\n");            exit(1);        }    }    timerSet( interval );    first = 1;banner:    printf( "%17s %14s %16s", "input",        intrface ? intrface : "(Total)", "output");    putchar('\n');    printf( "%10s %5s %10s %10s %5s %10s %5s",        "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");    if (dflag)	printf(" %5.5s", "drops");    putchar('\n');    fflush(stdout);    line = 0;loop:    if ( intrface ) {#define ADD_IFVAR( x ) ifcol_oid[ ifcol_len-2 ] = x; \    snmp_varlist_add_variable( &var, ifcol_oid, ifcol_len, ASN_NULL, NULL,  0)    /*  if (bflag) { */            ADD_IFVAR( 10 );        /* ifInOctets     */            ADD_IFVAR( 16 );        /* ifOutOctets    */    /*  } */        ADD_IFVAR( 11 );            /* ifInUcastPkts  */        ADD_IFVAR( 12 );            /* ifInNUcastPkts */        ADD_IFVAR( 14 );            /* ifInErrors     */        ADD_IFVAR( 17 );            /* ifOutUcastPkts */        ADD_IFVAR( 18 );            /* ifOutNUcastPkts */        ADD_IFVAR( 20 );            /* ifOutErrors    */        ADD_IFVAR( 21 );            /* ifOutQLen      */        if (dflag) {            ADD_IFVAR( 19 );        /* ifOutDiscards  */        }#undef ADD_IFVAR        netsnmp_query_get( var, ss );   /* Or parallel walk ?? */        cur_if->ift_ip = 0;        cur_if->ift_ib = 0;        cur_if->ift_ie = 0;        cur_if->ift_op = 0;        cur_if->ift_ob = 0;        cur_if->ift_oe = 0;        cur_if->ift_co = 0;        cur_if->ift_dr = 0;        cur_if->ifIndex = var->name[ ifcol_len-1 ];        for (vp=var; vp; vp=vp->next_variable) {            if ( ! vp->val.integer )                continue;            switch (vp->name[ifcol_len-2]) {            case 10:    /* ifInOctets */                cur_if->ift_ib = *vp->val.integer;                break;            case 11:    /* ifInUcastPkts */                cur_if->ift_ip += *vp->val.integer;                break;            case 12:    /* ifInNUcastPkts */                cur_if->ift_ip += *vp->val.integer;                break;            case 14:    /* ifInErrors */                cur_if->ift_ie = *vp->val.integer;                break;            case 16:    /* ifOutOctets */                cur_if->ift_ob = *vp->val.integer;                break;            case 17:    /* ifOutUcastPkts */                cur_if->ift_op += *vp->val.integer;                break;            case 18:    /* ifOutNUcastPkts */                cur_if->ift_op += *vp->val.integer;                break;            case 19:    /* ifOutDiscards */                cur_if->ift_dr = *vp->val.integer;                break;            case 20:    /* ifOutErrors */                cur_if->ift_oe = *vp->val.integer;                break;            case 21:    /* ifOutQLen */                cur_if->ift_co = *vp->val.integer;                break;            }        }        snmp_free_varbind( var );        var = NULL;         if (!first) { 	    printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", 		cur_if->ift_ip - ip->ift_ip, 		cur_if->ift_ie - ip->ift_ie,		cur_if->ift_ib - ip->ift_ib,		cur_if->ift_op - ip->ift_op,		cur_if->ift_oe - ip->ift_oe,		cur_if->ift_ob - ip->ift_ob,		cur_if->ift_co - ip->ift_co);	    if (dflag)		printf(" %5lu", cur_if->ift_dr - ip->ift_dr);            putchar('\n');            fflush(stdout);	}	ip->ift_ip = cur_if->ift_ip;	ip->ift_ie = cur_if->ift_ie;	ip->ift_ib = cur_if->ift_ib;	ip->ift_op = cur_if->ift_op;	ip->ift_oe = cur_if->ift_oe;	ip->ift_ob = cur_if->ift_ob;	ip->ift_co = cur_if->ift_co;	ip->ift_dr = cur_if->ift_dr;    }  /* (single) interface */    else {	sum->ift_ip = 0;	sum->ift_ib = 0;	sum->ift_ie = 0;	sum->ift_op = 0;	sum->ift_ob = 0;	sum->ift_oe = 0;	sum->ift_co = 0;	sum->ift_dr = 0;#define ADD_IFVAR( x ) ifcol_oid[ ifcol_len-2 ] = x; \    snmp_varlist_add_variable( &var, ifcol_oid, ifcol_len-1, ASN_NULL, NULL,  0)        ADD_IFVAR( 11 );            /* ifInUcastPkts  */        ADD_IFVAR( 12 );            /* ifInNUcastPkts */        ADD_IFVAR( 14 );            /* ifInErrors     */        ADD_IFVAR( 17 );            /* ifOutUcastPkts */        ADD_IFVAR( 18 );            /* ifOutNUcastPkts */        ADD_IFVAR( 20 );            /* ifOutErrors    */        ADD_IFVAR( 21 );            /* ifOutQLen      */    /*  if (bflag) { */            ADD_IFVAR( 10 );        /* ifInOctets     */            ADD_IFVAR( 16 );        /* ifOutOctets    */    /*  } */        if (dflag) {            ADD_IFVAR( 19 );        /* ifOutDiscards  */        }#undef ADD_IFVAR        ifcol_oid[ ifcol_len-2 ] = 11;       /* ifInUcastPkts */        while ( 1 ) {            if (netsnmp_query_getnext( var, ss ) != SNMP_ERR_NOERROR)                break;            if ( snmp_oid_compare( ifcol_oid, ifcol_len-2,                                   var->name, ifcol_len-2) != 0 )                break;    /* End of Table */                        for ( vp=var; vp; vp=vp->next_variable ) {                if ( ! vp->val.integer )                    continue;                switch ( vp->name[ ifcol_len-2 ] ) {                case 10:    /* ifInOctets */                    sum->ift_ib += *vp->val.integer;                    break;                case 11:    /* ifInUcastPkts */                    sum->ift_ip += *vp->val.integer;                    break;                case 12:    /* ifInNUcastPkts */                    sum->ift_ip += *vp->val.integer;                    break;                case 14:    /* ifInErrors */                    sum->ift_ie += *vp->val.integer;                    break;                case 16:    /* ifOutOctets */                    sum->ift_ob += *vp->val.integer;                    break;                case 17:    /* ifOutUcastPkts */                    sum->ift_op += *vp->val.integer;                    break;                case 18:    /* ifOutNUcastPkts */                    sum->ift_op += *vp->val.integer;                    break;                case 19:    /* ifOutDiscards */                    sum->ift_dr += *vp->val.integer;                    break;                case 20:    /* ifOutErrors */                    sum->ift_oe += *vp->val.integer;                    break;                case 21:    /* ifOutQLen */                    sum->ift_co += *vp->val.integer;                    break;                }            }            /*             * Now loop to retrieve the next entry from the table.             */        }   /* while (1) */        snmp_free_varbind( var );        var = NULL;         if (!first) { 	    printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", 		sum->ift_ip - total->ift_ip, 		sum->ift_ie - total->ift_ie,		sum->ift_ib - total->ift_ib,		sum->ift_op - total->ift_op,		sum->ift_oe - total->ift_oe,		sum->ift_ob - total->ift_ob,		sum->ift_co - total->ift_co);	    if (dflag)		printf(" %5lu", sum->ift_dr - total->ift_dr);            putchar('\n');            fflush(stdout);	}	total->ift_ip = sum->ift_ip;	total->ift_ie = sum->ift_ie;	total->ift_ib = sum->ift_ib;	total->ift_op = sum->ift_op;	total->ift_oe = sum->ift_oe;	total->ift_ob = sum->ift_ob;	total->ift_co = sum->ift_co;	total->ift_dr = sum->ift_dr;    }  /* overall summary */    timerPause();    timerSet(interval);    line++;    first = 0;    if (line == 21)    	goto banner;    else    	goto loop;    /*NOTREACHED*/}/* * timerSet sets or resets the timer to fire in "interval" seconds. * timerPause waits only if the timer has not fired. * timing precision is not considered important. */#if (defined(WIN32) || defined(cygwin))static int      sav_int;static time_t   timezup;static voidtimerSet(int interval_seconds){    sav_int = interval_seconds;    timezup = time(0) + interval_seconds;}/* * you can do better than this !  */static voidtimerPause(void){    time_t          now;    while (time(&now) < timezup)#ifdef WIN32        Sleep(400);#else    {        struct timeval  tx;        tx.tv_sec = 0;        tx.tv_usec = 400 * 1000;        /* 400 milliseconds */        select(0, 0, 0, 0, &tx);    }#endif}#else/* * Called if an interval expires before sidewaysintpr has completed a loop. * Sets a flag to not wait for the alarm. */RETSIGTYPEcatchalarm(int sig){    signalled = YES;}static voidtimerSet(int interval_seconds){#ifdef HAVE_SIGSET    (void) sigset(SIGALRM, catchalarm);#else    (void) signal(SIGALRM, catchalarm);#endif    signalled = NO;    (void) alarm(interval_seconds);}static voidtimerPause(void){#ifdef HAVE_SIGHOLD    sighold(SIGALRM);    if (!signalled) {        sigpause(SIGALRM);    }#else    int             oldmask;    oldmask = sigblock(sigmask(SIGALRM));    if (!signalled) {        sigpause(0);    }    sigsetmask(oldmask);#endif}#endif                          /* !WIN32 && !cygwin */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -