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

📄 vmstat_hpux.c

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 C
📖 第 1 页 / 共 2 页
字号:
}                               /* take_snapshot ends here *//* * This gets called every POLL_INTERVAL seconds to update the snapshots.  It takes a new snapshot and  * drops the oldest one.  This way we move the time window so we always take the values over  * POLL_INTERVAL * POLL_VALUES seconds and update the data used every POLL_INTERVAL seconds  * The alarm timer is in the init function of this module (snmp_alarm_register)  *//* * ARGSUSED0  */static voidupdate_stats(unsigned int registrationNumber, void *clientarg){    /*     * The time between the samples we compare      */    time_t          time_diff;    /*     * Easier to use these than the snapshots, short hand pointers      */    struct cpu_stat_snapshot *css_old, *css_new;    /*     * The usual stuff to count on, err, by      */    int             i;    /*     * The sum of the CPU ticks that have passed on the different CPU states, so we can calculate      * the percentages of each state      */    unsigned long long cpu_sum = 0;    DEBUGMSGTL(("ucd-snmp/vmstat_hpux.c:update_stats",                "updating stats\n"));    /*     * Take the current snapshot      */    if (take_snapshot(&snapshot[0]) == -1) {        snmp_log(LOG_WARNING,                 "vmstat_hpux (update_stats): Something went wrong with take_snapshot.\n");        return;    }    /*     * Do we have some data we can use ?  An issue right after the start of the agent      */    if (number_of_snapshots > 0) {        /*         * Huh, the number of CPUs changed during run time.  That is indeed s.th. worth noting, we          * output a humorous (more or less) syslog message and need to retake the snapshots          */        if (snapshot[0].css_cpus != snapshot[1].css_cpus) {            if (snapshot[0].css_cpus > snapshot[1].css_cpus) {                snmp_log(LOG_NOTICE,                         "vmstat_hpux (update_stats): Cool ! Number of CPUs increased, must be hot-pluggable.\n");            } else {                snmp_log(LOG_NOTICE,                         "vmstat_hpux (update_stats): Lost at least one CPU, RIP.\n");            }            /*             * Make all snapshots but the current one invalid              */            number_of_snapshots = 1;            /*             * Move the current one in the "first" [1] slot              */            memmove(&snapshot[1], &snapshot[0], sizeof snapshot[0]);            /*             * Erase the current one              */            memset(&snapshot[0], 0, sizeof snapshot[0]);            /*             * Try to get a new snapshot in five seconds so we can return s.th. useful              */            if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) {                snmp_log(LOG_WARNING,                         "vmstat_hpux (update_stats): snmp_alarm_register failed.\n");            }            return;        }        /*         * Short hand pointers          */        css_new = &snapshot[0];        css_old = &snapshot[number_of_snapshots];        /*         * How much time has passed between the snapshots we get the values from ?          */        time_diff =            (snapshot[0].css_time -             snapshot[number_of_snapshots].css_time);        DEBUGMSGTL(("ucd-snmp/vmstat_hpux.c:update_stats",                    "time_diff: %lld\n", time_diff));        /*         * swapin and swapout are in pages, MIB wants kB/s,so we just need to get kB and seconds          * For the others we need to get value per second          * Retreive static information to obtain memory page_size          */        if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {            snmp_log(LOG_ERR, "vmstat_hpux: pstat_getstatic failed!\n");        }        /*         * LINTED cast needed, really          */        swapin =            (unsigned int) ((css_new->css_swapin - css_old->css_swapin) *                            pst.page_size / 1024 / time_diff);        /*         * LINTED cast needed, really          */        swapout =            (unsigned int) ((css_new->css_swapout - css_old->css_swapout) *                            pst.page_size / 1024 / time_diff);        /*         * LINTED cast needed, really          */        blocks_read =            (unsigned             int) ((css_new->css_blocks_read -                    css_old->css_blocks_read) / time_diff);        /*         * LINTED cast needed, really          */        blocks_write =            (unsigned             int) ((css_new->css_blocks_write -                    css_old->css_blocks_write) / time_diff);        /*         * LINTED cast needed, really          */        interrupts =            (unsigned             int) ((css_new->css_interrupts -                    css_old->css_interrupts) / time_diff);        /*         * LINTED cast needed, really          */        context_sw =            (unsigned             int) ((css_new->css_context_sw -                    css_old->css_context_sw) / time_diff);        /*         * Loop thru all the CPUSTATES and get the differences          */        for (i = 0; i < CPUSTATES; i++) {            cpu_sum += (css_new->css_cpu[i] - css_old->css_cpu[i]);        }        /*         * Now calculate the absolute percentage values          * Looks somewhat complicated sometimes but tries to get around using floats to increase speed          */        for (i = 0; i < CPUSTATES; i++) {            /*             * Since we don't return fractions we use + 0.5 to get between 99 and 101 percent adding the values              * together, otherwise we would get less than 100 most of the time              */            /*             * LINTED has to be 'long'              */            cpu_perc[i] =                (long) (((css_new->css_cpu[i] -                          css_old->css_cpu[i]) * 100 +                         (cpu_sum / 2)) / cpu_sum);        }        /*         * As said before, MIB wants CPU_SYSTEM which is CP_SYS + CP_WAIT          */        /*         * LINTED has to be 'long'          */        cpu_perc[CPU_SYSTEM] =            (long) ((((css_new->css_cpu[CP_SYS] - css_old->css_cpu[CP_SYS])                      + (css_new->css_cpu[CP_WAIT] -                         css_old->css_cpu[CP_WAIT]))                     * 100 + (cpu_sum / 2)) / cpu_sum);    }    /*     * Make the current one the first one and move the whole thing one place down      */    memmove(&snapshot[1], &snapshot[0],            (size_t) (((char *) &snapshot[POLL_VALUES]) -                      ((char *) &snapshot[0])));    /*     * Erase the current one      */    memset(&snapshot[0], 0, sizeof snapshot[0]);    /*     * Only important on start up, we keep track of how many snapshots we have taken so far      */    if (number_of_snapshots < POLL_VALUES) {        number_of_snapshots++;    }}                               /* update_stats ends here *//* * *var_extensible_vmstat starts here  * The guts of the module, this routine gets called to service a request  */unsigned char *var_extensible_vmstat(struct variable *vp,                      oid * name,                      size_t * length,                      int exact,                      size_t * var_len, WriteMethod ** write_method){    /*     * Needed for returning the values      */    static long     long_ret;    static char     errmsg[300];    /*     * set to 0 as default      */    long_ret = 0;    /*     * generic check whether the options passed make sense and whether the      * right variable is requested      */    if (header_generic(vp, name, length, exact, var_len, write_method) !=        MATCH_SUCCEEDED) {        return (NULL);    }    /*     * The function that actually returns s.th.      */    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:        return ((u_char *) (&swapin));    case SWAPOUT:        return ((u_char *) (&swapout));    case IOSENT:        return ((u_char *) (&blocks_write));    case IORECEIVE:        return ((u_char *) (&blocks_read));    case SYSINTERRUPTS:        return ((u_char *) (&interrupts));    case SYSCONTEXT:        return ((u_char *) (&context_sw));    case CPUUSER:        return ((u_char *) (&cpu_perc[CP_USER]));    case CPUSYSTEM:        return ((u_char *) (&cpu_perc[CPU_SYSTEM]));    case CPUIDLE:        return ((u_char *) (&cpu_perc[CP_IDLE]));    case CPURAWUSER:        take_snapshot(&raw_values);        /*         * LINTED has to be 'long'          */        long_ret =            (long) (raw_values.css_cpu[CP_USER] / raw_values.css_cpus);        return ((u_char *) (&long_ret));    case CPURAWNICE:        take_snapshot(&raw_values);        /*         * LINTED has to be 'long'          */        long_ret =            (long) (raw_values.css_cpu[CP_NICE] / raw_values.css_cpus);        return ((u_char *) (&long_ret));    case CPURAWSYSTEM:        take_snapshot(&raw_values);        /*         * LINTED has to be 'long'          */        long_ret =            (long) ((raw_values.css_cpu[CP_SYS] +                     raw_values.css_cpu[CP_WAIT]) / raw_values.css_cpus);        return ((u_char *) (&long_ret));    case CPURAWIDLE:        take_snapshot(&raw_values);        /*         * LINTED has to be 'long'          */        long_ret =            (long) (raw_values.css_cpu[CP_IDLE] / raw_values.css_cpus);        return ((u_char *) (&long_ret));    case CPURAWWAIT:        take_snapshot(&raw_values);        /*         * LINTED has to be 'long'          */        long_ret =            (long) (raw_values.css_cpu[CP_WAIT] / raw_values.css_cpus);        return ((u_char *) (&long_ret));    case CPURAWKERNEL:        take_snapshot(&raw_values);        /*         * LINTED has to be 'long'          */        long_ret =            (long) (raw_values.css_cpu[CP_SYS] / raw_values.css_cpus);        return ((u_char *) (&long_ret));    case IORAWSENT:        take_snapshot(&raw_values);        /*         * LINTED has to be 'long'          */        long_ret = (long) (raw_values.css_blocks_write);        return ((u_char *) (&long_ret));    case IORAWRECEIVE:        take_snapshot(&raw_values);        /*         * LINTED has to be 'long'          */        long_ret = (long) (raw_values.css_blocks_read);        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_hpux: Error in request, no match found.\n");    }    return (NULL);}                               /* *var_extensible_vmstat ends here *//* * Functions end here  *//* * Program ends here  */

⌨️ 快捷键说明

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