- 论坛徽章:
- 0
|
各位大哥:
小弟最近做通过SNMP实时获取变量的值(比如:系统资源利用率)的一个东东。
使用工具为net-snmp。
做了一个简单的实验,结果Agent编译后通过snmpget总是返回一个固定的值,实际上这个值是在不断变化的。
我贴出我的程序请牛人帮忙分析下,万分感激。。。
首先写了一个MIB(NET-SNMP-TEST,想得到其中testAgentCpu1的值),
通过mib2c -c int_watch.conf testAgentCpu1命令自动生成 testAgentCpu1.c 和.h,现贴出经我修改后的.c
/*
* Note: this file originally auto-generated by mib2c using
* : mib2c.int_watch.conf,v 1.3 2005/05/03 14:38:11 dts12 Exp $
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "testAgentCpu1.h"
/* 我加的部分begin */
#include "rdm-test3.c" /* 这个是我自己写的,将内存利用率写入共享内存并返回值 */
/* 我加的部分end */
long testAgentCpu1 = 1; /* XXX: set default value */
/*
* Our initialization routine, called automatically by the agent
* (Note that the function name must match init_FILENAME())
*/
void
init_testAgentCpu1(void)
{
int i,j,k;
static oid testAgentCpu1_oid[] = { 1,3,6,1,4,1,8072,2,4,1,1,1 };
/* 我加的部分begin */
al_share_mem(&testAgentCpu1); /* 为rdm-test3.c中的函数,通过读共享内存,
将cpu的利用率百分数赋给testAgentCpu1,
我写了他的测试程序,确实可以动态返回,
每次运行返回值都不同 */
/* 我加的部分end */
DEBUGMSGTL(("testAgentCpu1",
"Initializing testAgentCpu1 scalar integer. Default value = %d\n",
testAgentCpu1));
netsnmp_register_int_instance("testAgentCpu1",
testAgentCpu1_oid,
OID_LENGTH(testAgentCpu1_oid),
&testAgentCpu1, NULL);
DEBUGMSGTL(("testAgentCpu1",
"Done initalizing testAgentCpu1 module\n"));
}//file ended
将此文件编入Agent后启动snmpd,执行snmpget localhost -c public -v 1 testAgentCpu1.0
每次都返回相同的值,郁闷。
如果重新启动snmpd,则又得到新的值,且又保持不变了。。。。
不知道怎么样才能获取到实时的值。请指点。 |
|