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

📄 vmstat_aix4.c

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * AIX4/5 cpu statistics module for net-snmp * * Version 0.1 - Initial release - 05/Jun/2003 * * Derived from vmstat_solaris2.c * Using libperfstat for statistics (Redbook SG24-6039) * * Ported to AIX by Michael Kukat <michael.kukat@to.com> * Thinking Objects Software GmbH * Lilienthalstra遝 2 * 70825 Stuttgart-Korntal * http://www.to.com/ * * Thanks go to Jochen Kmietsch for the solaris2 support and * to DaimlerChrysler AG Stuttgart for making this port possible *//* * 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>/* * libperfstat structs  */#include <libperfstat.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_aix4.h"/* * Includes end here  *//* * Global structures start here  *//* * A structure to save data gathered from the libperfstat. */struct cpu_stat_snapshot {	unsigned long long 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  *//* * 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];/* * 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_aix4 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_aix4(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}},		{SYSRAWINTERRUPTS, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,		 {SYSRAWINTERRUPTS}},		{SYSRAWCONTEXT, ASN_COUNTER, RONLY, var_extensible_vmstat, 1,		 {SYSRAWCONTEXT}},		/*		 * 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_aix4 (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_aix4 (init): snmp_alarm_register failed, cannot service requests.\n");	}}	/* init_vmstat_aix4 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 	 */	/*	 * High resolution time counter 	 */	struct timeval		tp;	unsigned long long	current_time;	/*	 * see libperfstat.h, holds CPU/memory data 	 */	perfstat_cpu_total_t	cs;	perfstat_memory_total_t	ms;	/*	 * The usual stuff to count on, err, by 	 */	int			i;	/*	 * Variables end here 	 */	/*	 * Function starts here 	 */	/*	 * Get time 	 */	gettimeofday(&tp, (struct timezone *)NULL);	current_time = tp.tv_sec * (unsigned long long)1000000 + tp.tv_usec;	/*	 * 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 perfstat 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 + 2000000)) {		/*		 * 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((perfstat_cpu_total((perfstat_id_t *)NULL, &cs, sizeof(perfstat_cpu_total_t), 1) > 0) &&			(perfstat_memory_total((perfstat_id_t *)NULL, &ms, sizeof(perfstat_memory_total_t), 1) > 0)) {			css->css_cpus = cs.ncpus;			css->css_swapin = ms.pgspins;			css->css_swapout = ms.pgspouts;			css->css_blocks_read = cs.sysread;			css->css_blocks_write = cs.syswrite;			css->css_interrupts = cs.devintrs + cs.softintrs;			css->css_context_sw = cs.pswitch;			css->css_cpu[CPU_USER] = cs.user;			css->css_cpu[CPU_SYSTEM] = cs.sys;			css->css_cpu[CPU_IDLE] = cs.idle;			css->css_cpu[CPU_WAIT] = cs.wait;		}	}	/*	 * All engines running at warp speed, no problems (if there are any engines, that is) 	 */	return (cs.ncpus > 0 ? 0 : -1);}	/* 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)  *//*

⌨️ 快捷键说明

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