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

📄 vmstat_solaris2.c

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  vmstat_solaris2.c *  UCD SNMP module for systemStats section of UCD-SNMP-MIB for SunOS/Solaris *  Jochen Kmietsch <kmietsch@jochen.de> *  with fixes and additions from the UCD-SNMP community *  Uses some ideas from xosview and top *  Some comments paraphrased from the SUN man pages  *  Version 0.1 initial release (Dec 1999) *  Version 0.2 added support for multiprocessor machines (Jan 2000) *  Version 0.3 some reliability enhancements and compile time fixes (Feb 2000) *  Version 0.4 portability issue and raw cpu value support (Jun 2000) *  Version 0.5 64-bit Solaris support and new data gathering routine (Aug 2000) *  Version 0.6 Memory savings, overroll precautions and lint checks (Aug 2000) *  Version 0.7 More raw counters and some cosmetic changes (Jan 2001) * *//* * To make lint skip the debug code and stop complaining  */#ifdef __lint#define SNMP_NO_DEBUGGING 1#endif/* * Includes start here  *//* * Standard includes  */#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/time.h>#include <string.h>/* * kstat and sysinfo structs  */#include <kstat.h>#include <sys/sysinfo.h>#include <net-snmp/net-snmp-config.h>#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include "mibdefs.h"#include "util_funcs.h"/* * Header file for this module  */#include "vmstat.h"#include "vmstat_solaris2.h"/* * Includes end here  *//* * Global structures start here  *//* * A structure to save data gathered from the kernel kstat interface to.   * We used to have the sys/sysinfo.h cpu_stat_t here but we did not need  * all of it, some in a different size and some additional ones so we build  * our own  */struct cpu_stat_snapshot {    hrtime_t        css_time;    unsigned int    css_cpus;    unsigned long long css_swapin;    unsigned long long css_swapout;    unsigned long long css_blocks_read;    unsigned long long css_blocks_write;    unsigned long long css_interrupts;    unsigned long long css_context_sw;    unsigned long long css_cpu[CPU_STATES];};/* * Global structures end here  *//* * Global variables start here  *//* * From kstat.h:  * Provides access to the kernel statistics library by  * initializing a kstat control structure and returning a pointer  * to this structure.  This pointer must be used as the kc argument in   * following function calls from libkstat (here kc is called kstat_fd).  * Pointer to structure to be opened with kstat_open in main procedure.  * We share this one with memory_solaris2 and kernel_sunos5, where it's  * defined.  */extern kstat_ctl_t *kstat_fd;/* * Variables for the calculated values, filled in update_stats     * Need to be global since we need them in more than one function  */static ulong    swapin;static ulong    swapout;static ulong    blocks_read;static ulong    blocks_write;static ulong    interrupts;static ulong    context_sw;/* * Since MIB wants CPU_SYSTEM, which is CPU_KERNEL + CPU_WAIT  */static long     cpu_perc[CPU_STATES + 1];/* * How many snapshots we have already taken, needed for the first  * POLL_INTERVAL * POLL_VALUES seconds of agent running  */static unsigned int number_of_snapshots;/* * The place to store the snapshots of system data in  */static struct cpu_stat_snapshot snapshot[POLL_VALUES + 1];/* * And one for the raw counters, which we fill when the raw values are  * requested, as opposed to the absolute values, which are taken every  * POLL_INTERVAL seconds and calculated over POLL_INTERVAL * POLL_VALUES time  */static struct cpu_stat_snapshot raw_values;/* * Global variables end here  *//* * Functions start here  *//* * Function prototype  */static void     update_stats(unsigned int registrationNumber,                             void *clientarg);static int      take_snapshot(struct cpu_stat_snapshot *css);/* * init_vmstat_solaris2 starts here  * Init function for this module, from prototype  * Defines variables handled by this module, defines root OID for  * this module and registers it with the agent  */FindVarMethod var_extensible_vmstat;voidinit_vmstat_solaris2(void){    /*     * Which variables do we service ?      */    struct variable2 extensible_vmstat_variables[] = {        {MIBINDEX, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,         {MIBINDEX}},        {ERRORNAME, ASN_OCTET_STR, RONLY, var_extensible_vmstat, 1,         {ERRORNAME}},        {SWAPIN, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {SWAPIN}},        {SWAPOUT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {SWAPOUT}},        {IOSENT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {IOSENT}},        {IORECEIVE, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,         {IORECEIVE}},        {SYSINTERRUPTS, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,         {SYSINTERRUPTS}},        {SYSCONTEXT, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,         {SYSCONTEXT}},        {CPUUSER, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {CPUUSER}},        {CPUSYSTEM, ASN_INTEGER, RONLY, var_extensible_vmstat, 1,         {CPUSYSTEM}},        {CPUIDLE, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {CPUIDLE}},        {CPURAWUSER, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,         {CPURAWUSER}},        {CPURAWSYSTEM, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,         {CPURAWSYSTEM}},        {CPURAWIDLE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,         {CPURAWIDLE}},        {CPURAWWAIT, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,         {CPURAWWAIT}},        {CPURAWKERNEL, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,         {CPURAWKERNEL}},        {IORAWSENT, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,         {IORAWSENT}},        {IORAWRECEIVE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,         {IORAWRECEIVE}},        /*         * Future use:          * {ERRORFLAG, ASN_INTEGER, RONLY, var_extensible_vmstat, 1, {ERRORFLAG }},         * {ERRORMSG, ASN_OCTET_STR, RONLY, var_extensible_vmstat, 1, {ERRORMSG }}         */    };    /*     * Define the OID pointer to the top of the mib tree that we're      * registering underneath      */    oid             vmstat_variables_oid[] = { UCDAVIS_MIB, 11 };    /*     * register ourselves with the agent to handle our mib tree      * LINTED Trust me, I know what I'm doing      */    REGISTER_MIB("ucd-snmp/vmstat", extensible_vmstat_variables, variable2,                 vmstat_variables_oid);    /*     * First check whether shared kstat contol is NULL, if so, try to open our      * own.      */    if (kstat_fd == NULL) {        kstat_fd = kstat_open();    }    /*     * Then check whether either shared kstat was found or we succeeded in      * opening our own.      */    if (kstat_fd == NULL) {        snmp_log(LOG_ERR,                 "vmstat_solaris2 (init): kstat_open() failed and no shared kstat control found.\n");    }    /*     * Start with some useful data      */    update_stats(0, NULL);    /*     * update_stats is run every POLL_INTERVAL seconds using this routine      * (see 'man snmp_alarm')      * This is only executed once to get some useful data in the beginning      */    if (snmp_alarm_register(5, NULL, update_stats, NULL) == 0) {        snmp_log(LOG_WARNING,                 "vmstat_solaris2 (init): snmp_alarm_register failed.\n");    }    /*     * This is the one that runs update_stats every POLL_INTERVAL seconds      */    if (snmp_alarm_register(POLL_INTERVAL, SA_REPEAT, update_stats, NULL)        == 0) {        snmp_log(LOG_ERR,                 "vmstat_solaris2 (init): snmp_alarm_register failed, cannot service requests.\n");    }}                               /* init_vmstat_solaris2 ends here *//* * Data collection function take_snapshot starts here  * Get data from kernel and save into the snapshot strutcs  * Argument is the snapshot struct to save to. Global anyway, but looks nicer  */static inttake_snapshot(struct cpu_stat_snapshot *css){    /*     * Variables start here      */    /*     * From sys/kstat.h (included from kstat.h):      * Pointer to current kstat      */    kstat_t        *ksp;    /*     * Counters      */    unsigned int    cpu_num = 0;    /*     * High resolution time counter      */    hrtime_t        current_time;    /*     * see sys/sysinfo.h, holds CPU data      */    cpu_stat_t      cs;    /*     * The usual stuff to count on, err, by      */    int             i;    /*     * Variables end here      */    /*     * Function starts here      */    /*     * Get time      */    current_time = gethrtime();    /*     * If we have just gotten the data, return the values from last run (skip if-clause)      * This happens on a snmpwalk request.  No need to read the kstat again      * if we just did it less than 2 seconds ago      * Jumps into if-clause either when snapshot is empty or when too old      */    if ((css->css_time == 0)        || (current_time > css->css_time + 2000000000)) {        /*         * Make sure we clean up before we put new data into snapshot          */        memset(css, 0, sizeof *css);        /*         * Update timer          */        css->css_time = current_time;        /*         * Look thru all the cpu slots on the machine whether they holds a CPU          * and if so, get the data from that CPU          * We walk through the whole kstat chain and sum up all the found cpu_stat kstats,          * there's one for every CPU in a machine          */        for (ksp = kstat_fd->kc_chain; ksp != NULL; ksp = ksp->ks_next) {            /*             * If we encounter an invalid kstat, skip it and continue with next one              */            if (ksp->ks_flags & KSTAT_FLAG_INVALID) {                continue;            }            if (strcmp(ksp->ks_module, "cpu_stat") == 0) {                /*                 * Yeah, we found a CPU.                  */                cpu_num++;                /*                 * Read data from kstat into cs structure                  * kstat_fd is the control structure, ksp the kstat we are reading                  * and cs the buffer we are writing to.                  */                if ((ksp->ks_type != KSTAT_TYPE_RAW) ||                    (ksp->ks_data_size != sizeof cs) ||                    (kstat_read(kstat_fd, ksp, &cs) == -1)) {                    snmp_log(LOG_ERR,                             "vmstat_solaris2 (take_snapshot): could not read cs structure.\n");                    return (-1);                }                /*                 * Get the data from the cs structure and sum it up in our own structure                  */                css->css_swapin +=                    (unsigned long long) cs.cpu_vminfo.swapin;                css->css_swapout +=                    (unsigned long long) cs.cpu_vminfo.swapout;                css->css_blocks_read +=                    (unsigned long long) cs.cpu_sysinfo.bread;                css->css_blocks_write +=                    (unsigned long long) cs.cpu_sysinfo.bwrite;                css->css_interrupts +=                    (unsigned long long) cs.cpu_sysinfo.intr;                css->css_context_sw +=                    (unsigned long long) cs.cpu_sysinfo.pswitch;                /*                 * We need a for-loop for the CPU values                  */		cs.cpu_sysinfo.cpu[CPU_WAIT] = cs.cpu_sysinfo.wait[W_IO] +		                               cs.cpu_sysinfo.wait[W_PIO];

⌨️ 快捷键说明

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