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