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

📄 vmstat_linux.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 2 页
字号:
	    sscanf(b, "pgpgin %u", pin);	else {	    if (first)		snmp_log(LOG_ERR, "No pgpgin line in %s\n", VMSTAT_FILE);	    *pin = 0;	}	b = strstr(vmbuff, "pgpgout ");	if (b)	    sscanf(b, "pgpgout %u", pout);	else {	    if (first)		snmp_log(LOG_ERR, "No pgpgout line in %s\n", VMSTAT_FILE);	    *pout = 0;	}	b = strstr(vmbuff, "pswpin ");	if (b)	    sscanf(b, "pswpin %u", swpin);	else {	    if (first)		snmp_log(LOG_ERR, "No pswpin line in %s\n", VMSTAT_FILE);	    *swpin = 0;	}	b = strstr(vmbuff, "pswpout ");	if (b)	    sscanf(b, "pswpout %u", swpout);	else {	    if (first)		snmp_log(LOG_ERR, "No pswpout line in %s\n", VMSTAT_FILE);	    *swpout = 0;	}    }    else {	b = strstr(buff, "page ");	if (b)	    sscanf(b, "page %u %u", pin, pout);	else {	    if (first)		snmp_log(LOG_ERR, "No page line in %s\n", STAT_FILE);	    *pin = *pout = 0;	}	b = strstr(buff, "swap ");	if (b)	    sscanf(b, "swap %u %u", swpin, swpout);	else {	    if (first)		snmp_log(LOG_ERR, "No swap line in %s\n", STAT_FILE);	    *swpin = *swpout = 0;	}    }    b = strstr(buff, "intr ");    if (b) {	sscanf(b, "intr %llu %llu", &itotll, &i1ll);        *itot = (unsigned)itotll;        *i1 = (unsigned)i1ll;    }    else {	if (first)	    snmp_log(LOG_ERR, "No intr line in %s\n", STAT_FILE);	*itot = 0;    }    b = strstr(buff, "ctxt ");    if (b) {	sscanf(b, "ctxt %llu", &ctll);        *ct = (unsigned long)ctll;    }    else {	if (first)	    snmp_log(LOG_ERR, "No ctxt line in %s\n", STAT_FILE);	*ct = 0;    }    first = 0;}enum vmstat_index { swapin = 0, swapout,    rawswapin, rawswapout,    iosent, ioreceive,    rawiosent, rawioreceive,    sysinterrupts, syscontext,    cpuuser, cpusystem, cpuidle,    cpurawuser, cpurawnice,    cpurawsystem, cpurawidle,    cpurawinter, cpurawsoft, cpurawwait,    rawinterrupts, rawcontext};static unsignedvmstat(int iindex){    double          duse, dsys, didl, ddiv, divo2;    double          druse, drnic, drsys, dridl;    unsigned int    hertz;    double          ddiv2;    netsnmp_cpu_info *cpu;    netsnmp_cpu_load();    cpu = netsnmp_cpu_get_byIdx( -1, 0 );    duse = cpu->user_ticks + cpu->nice_ticks;    dsys = cpu->sys_ticks;    didl = cpu->idle_ticks;    ddiv = duse + dsys + didl;    hertz = sysconf(_SC_CLK_TCK);  /* get ticks/s from system */    divo2 = ddiv / 2;    druse = cpu->user_ticks;    drnic = cpu->nice_ticks;    drsys = cpu->sys_ticks;    dridl = cpu->idle_ticks;    ddiv2 = ddiv + cpu->wait_ticks                 + cpu->intrpt_ticks                 + cpu->sirq_ticks;    if (cpu->history) {        duse  -= (cpu->history[0].user_hist + cpu->history[0].nice_hist);        dsys  -=  cpu->history[0].sys_hist;        didl  -=  cpu->history[0].idle_hist;        ddiv2 -=  cpu->history[0].total_hist;    }    if (!ddiv) ddiv=1;   /* Protect against division-by-0 */     switch (iindex) {    case swapin:        return (cpu->swapIn  * 4 * hertz + divo2) / ddiv;    case swapout:        return (cpu->swapOut * 4 * hertz + divo2) / ddiv;    case iosent:        return (cpu->pageIn      * hertz + divo2) / ddiv;    case ioreceive:        return (cpu->pageOut     * hertz + divo2) / ddiv;    case sysinterrupts:        return (cpu->nInterrupts  * hertz + divo2) / ddiv;    case syscontext:        return (cpu->nCtxSwitches * hertz + divo2) / ddiv;    case cpuuser:        return (ddiv2 ? 100 * duse / ddiv2 : 0);    case cpusystem:        return (ddiv2 ? 100 * dsys / ddiv2 : 0);    case cpuidle:        return (ddiv2 ? 100 * didl / ddiv2 : 0);    case cpurawuser:        return druse;    case cpurawnice:        return drnic;    case cpurawsystem:        return drsys;    case cpurawidle:        return dridl;    case rawinterrupts:        return cpu->nInterrupts;    case rawcontext:        return cpu->nCtxSwitches;    case cpurawwait:	return cpu->wait_ticks;    case cpurawinter:	return cpu->intrpt_ticks;    case cpurawsoft:	return cpu->sirq_ticks;    case rawiosent:	return cpu->pageOut*2;    case rawioreceive:	return cpu->pageIn*2;    case rawswapin:	return cpu->swapIn;    case rawswapout:	return cpu->swapOut;    default:        return -1;    }}unsigned char  *var_extensible_vmstat(struct variable *vp,                      oid * name,                      size_t * length,                      int exact,                      size_t * var_len, WriteMethod ** write_method){    static long     long_ret;    static char     errmsg[300];    long_ret = 0;               /* set to 0 as default */    if (header_generic(vp, name, length, exact, var_len, write_method))        return (NULL);    switch (vp->magic) {    case MIBINDEX:        long_ret = 1;        return ((u_char *) (&long_ret));    case ERRORNAME:            /* dummy name */        sprintf(errmsg, "systemStats");        *var_len = strlen(errmsg);        return ((u_char *) (errmsg));    case SWAPIN:        long_ret = vmstat(swapin);        return ((u_char *) (&long_ret));    case SWAPOUT:        long_ret = vmstat(swapout);        return ((u_char *) (&long_ret));    case RAWSWAPIN:        long_ret = vmstat(rawswapin);        return ((u_char *) (&long_ret));    case RAWSWAPOUT:        long_ret = vmstat(rawswapout);        return ((u_char *) (&long_ret));    case IOSENT:        long_ret = vmstat(iosent);        return ((u_char *) (&long_ret));    case IORECEIVE:        long_ret = vmstat(ioreceive);        return ((u_char *) (&long_ret));    case IORAWSENT:        long_ret = vmstat(rawiosent);        return ((u_char *) (&long_ret));    case IORAWRECEIVE:        long_ret = vmstat(rawioreceive);        return ((u_char *) (&long_ret));    case SYSINTERRUPTS:        long_ret = vmstat(sysinterrupts);        return ((u_char *) (&long_ret));    case SYSCONTEXT:        long_ret = vmstat(syscontext);        return ((u_char *) (&long_ret));    case CPUUSER:        long_ret = vmstat(cpuuser);        return ((u_char *) (&long_ret));    case CPUSYSTEM:        long_ret = vmstat(cpusystem);        return ((u_char *) (&long_ret));    case CPUIDLE:        long_ret = vmstat(cpuidle);        return ((u_char *) (&long_ret));    case CPURAWUSER:        long_ret = vmstat(cpurawuser);        return ((u_char *) (&long_ret));    case CPURAWNICE:        long_ret = vmstat(cpurawnice);        return ((u_char *) (&long_ret));    case CPURAWSYSTEM:        long_ret = vmstat(cpurawsystem)+vmstat(cpurawinter)+vmstat(cpurawsoft);        return ((u_char *) (&long_ret));    case CPURAWKERNEL:        long_ret = vmstat(cpurawsystem);        return ((u_char *) (&long_ret));    case CPURAWIDLE:        long_ret = vmstat(cpurawidle);        return ((u_char *) (&long_ret));    case SYSRAWINTERRUPTS:	long_ret = vmstat(rawinterrupts);	return (u_char *)&long_ret;    case SYSRAWCONTEXT:	long_ret = vmstat(rawcontext);	return (u_char *)&long_ret;    case CPURAWWAIT:	if (!has_cpu_26) return NULL;        long_ret = vmstat(cpurawwait);        return ((u_char *) (&long_ret));    case CPURAWINTR:	if (!has_cpu_26) return NULL;        long_ret = vmstat(cpurawinter);        return ((u_char *) (&long_ret));    case CPURAWSOFTIRQ:	if (!has_cpu_26) return NULL;        long_ret = vmstat(cpurawsoft);        return ((u_char *) (&long_ret));		        /*         * reserved for future use          */        /*         * case ERRORFLAG:         * return((u_char *) (&long_ret));         * case ERRORMSG:         * return((u_char *) (&long_ret));         */    default:	snmp_log(LOG_ERR, "vmstat.c: don't know how to handle %d request\n",		vp->magic);    }    return NULL;}

⌨️ 快捷键说明

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