📄 vmstat_dynix.c
字号:
/* * vmstat_dynix.c * UCD SNMP module for systemStats section of UCD-SNMP-MIB for Dynix * Patrick Hess <phess@phess.best.vwh.net> * * This is just a port of the vmstat_solaris2 code Version 0.7 * *//* * To make lint skip the debug code and stop complaining */#ifdef __lint#define SNMP_NO_DEBUGGING 1#endif#define __NO_ASM_MACRO 1/* * Includes start here */#include <net-snmp/net-snmp-config.h>/* * Standard includes */#include <sys/tmp_ctl.h>#include <sys/sysperf.h>#include <sys/vmmeter.h>#include <unistd.h>#include <string.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_dynix.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 { 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[V_CPU_STATES];};/* * 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 ulong swapin;static ulong swapout;static ulong blocks_read;static ulong blocks_write;static ulong interrupts;static ulong context_sw;/* * Since MIB wants V_CPU_SYSTEM, which is V_CPU_KERNEL + V_CPU_STREAM */static long cpu_perc[V_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_dynix 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_dynix(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); /* * 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_dynix (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_dynix (init): snmp_alarm_register failed, cannot service requests.\n"); }} /* init_vmstat_dynix 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 */ /* * Counters */ unsigned int cpu_num = 0; /* * Low resolution time counter */ time_t current_time; /* * see sys/sysperf.h, holds CPU data */ exp_vmmeter_t *cs, *origcs = 0; /* * size of the cs struct */ size_t vminfo_size; /* * The usual stuff to count on, err, by */ int i; int engnum = 0; /* * Variables end here */ /* * Function starts here */ /* * Get time */ current_time = time(0); /* * If we have just gotten the data, return the values from last run (skip if-clause) */ /* * This happens on a snmpwalk request. */ /* * 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); /* * Get the number of CPUs we gather data from */ if ((cpu_num = tmp_ctl(TMP_NENG, 0)) < 0) { snmp_log(LOG_ERR, "vmstat_dynix: (take snapshot) bad tmp_ctl return\n"); return (-1); } css->css_cpus = cpu_num; vminfo_size = cpu_num * sizeof(exp_vmmeter_t); if (!(cs = (exp_vmmeter_t *) malloc(vminfo_size))) { snmp_log(LOG_ERR, "vmstat_dynix: (take_snapshot) bad malloc return\n"); return (-1); } origcs = cs; /* * Update timer */ css->css_time = current_time; /* * Read data from kernel into cs structure */ /* * cs is the buffer we are writing to and */ /* * vminfo_size is the size of the cs struct */ if ((getkerndata(VMMETER_DATAID, cs, vminfo_size)) < 0) { snmp_log(LOG_ERR, "vmstat_dynix (take_snapshot): getkerndata failure."); return (-1); } /* * Get the data from each CPU */ /* * We walk through the whole vmmeter struct and sum up all the found stats, */ /* * there's one for every CPU in a machine */ /* * Okay... you can't laugh at this! I'm a C-hack, not a C-coder. :) */ while (engnum < cpu_num) { /* * Get the data from the cs structure and sum it up in our own structure */ css->css_swapin += (unsigned long long) cs->v_swpin; css->css_swapout += (unsigned long long) cs->v_swpout; css->css_blocks_read += (unsigned long long) cs->v_phread; css->css_blocks_write += (unsigned long long) cs->v_phwrite; css->css_interrupts += (unsigned long long) cs->v_intr; css->css_context_sw += (unsigned long long) cs->v_swtch; /* * We need a for-loop for the CPU STATE values */ for (i = 0; i < V_CPU_STATES; i++) { css->css_cpu[i] += (unsigned long long) cs->v_time[i]; } /* end for */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -