📄 vmstat_hpux.c
字号:
/* * vmstat_hpux.c * UCD SNMP module for systemStats section of UCD-SNMP-MIB for HPUX 10.x/11.x *//* * To make lint skip the debug code and stop complaining */#ifdef __lint#define SNMP_NO_DEBUGGING 1#endif/* * Includes start here *//* * UCD-SNMP config details */#include <net-snmp/net-snmp-config.h>/* * Standard includes */#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/time.h>#include <string.h>/* * pstat and sysinfo structs */#include <sys/pstat.h>#include <sys/dk.h>/* * Includes needed for all modules */#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include "mibdefs.h"/* * Utility functions for UCD-SNMP */#include "util_funcs.h"/* * Header file for this module */#include "vmstat.h"#include "vmstat_hpux.h"/* * Includes end here *//* * Global structures start here *//* * A structure to save data gathered from the kernel pstat interface to. * CPUSTATES are defined as (see sys/dk.h): * #define CPUSTATES 9 -- number of CPU states * #define CP_USER 0 -- user mode of USER process * #define CP_NICE 1 -- user mode of USER process at nice priority * #define CP_SYS 2 -- kernel mode of USER process * #define CP_IDLE 3 -- IDLE mode * #define CP_WAIT 4 * #define CP_BLOCK 5 -- time blocked on a spinlock * #define CP_SWAIT 6 -- time blocked on the kernel semaphore * #define CP_INTR 7 -- INTERRUPT mode * #define CP_SSYS 8 -- kernel mode of KERNEL process */struct cpu_stat_snapshot { time_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[CPUSTATES];};/* * Define a structure to hold kernel static information */struct pst_static pst;/* * Global structures end here *//* * Global variables start here *//* * Variables for the calculated values, filled in update_stats * Need to be global since we need them in more than one function */static unsigned long swapin;static unsigned long swapout;static unsigned long blocks_read;static unsigned long blocks_write;static unsigned long interrupts;static unsigned long context_sw;/* * Since MIB wants CPU_SYSTEM, which is CP_SYS + CP_WAIT (see sys/dk.h) */static long cpu_perc[CPUSTATES + 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_hpux 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_hpux(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}}, {CPURAWNICE, ASN_COUNTER, RONLY, var_extensible_vmstat, 1, {CPURAWNICE}}, {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); /* * 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_hpux (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_hpux (init): snmp_alarm_register failed, cannot service requests.\n"); }} /* init_vmstat_hpux 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 */ struct pst_dynamic psd; struct pst_processor *psp; struct pst_vminfo psv; /* * time counter */ time_t current_time; /* * The usual stuff to count on, err, by */ int i; /* * Variables end here */ /* * Function starts here */ /* * Get time */ time(¤t_time); /* * 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 pstat 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 + 2)) { /* * Make sure we clean up before we put new data into snapshot */ memset(css, 0, sizeof *css); /* * Update timer */ css->css_time = current_time; if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) != -1) { css->css_cpus = psd.psd_proc_cnt; DEBUGMSGTL(("take_snapshot", "*** Number of CPUs: %d\n", css->css_cpus)); /* * We need a for-loop for the CPU values */ for (i = 0; i < CPUSTATES; i++) { css->css_cpu[i] = (unsigned long long) psd.psd_cpu_time[i]; DEBUGMSGTL(("take_snapshot", "*** Time for CPU state %d: %d\n", i, psd.psd_cpu_time[i])); } psp = (struct pst_processor *) malloc(css->css_cpus * sizeof(*psp)); if (pstat_getprocessor(psp, sizeof(*psp), css->css_cpus, 0) != -1) { int i; for (i = 0; i < css->css_cpus; i++) { css->css_blocks_read = psp[i].psp_phread; css->css_blocks_write = psp[i].psp_phwrite; } } else snmp_log(LOG_ERR, "vmstat_hpux (take_snapshot): pstat_getprocessor failed!\n"); } else snmp_log(LOG_ERR, "vmstat_hpux (take_snapshot): pstat_getdynamic failed!\n"); if (pstat_getvminfo(&psv, sizeof(psv), (size_t) 1, 0) != -1) { css->css_swapin = psv.psv_sswpin; css->css_swapout = psv.psv_sswpout; css->css_interrupts = psv.psv_sintr; css->css_context_sw = psv.psv_sswtch; } else snmp_log(LOG_ERR, "vmstat_hpux (take_snapshot): pstat_getvminfo failed!\n"); } /* * All engines running at warp speed, no problems (if there are any engines, that is) */ return (css->css_cpus > 0 ? 0 : -1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -