免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3456 | 回复: 1
打印 上一主题 下一主题

关于Agent如何动态地取得变量的值。。。(net-snmp) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-10 13:41 |只看该作者 |倒序浏览
通过Agent获取变量的值(小弟写的是一个返回cpu利用百分数的函数 void al_share_mem(int * value),\r\n包含在文件rdm-test3.c中,该函数可动态地返回值)\r\n\r\n然后小弟将MIB文件经过mib2c命令编译成.c和.h文件,并修改.c文件如下:\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不知道怎么样才能获取到实时的值。请指点。

论坛徽章:
0
2 [报告]
发表于 2006-02-10 16:15 |只看该作者
问题解决了。\r\n不过用比较笨的方法\r\n就是直接映射共享内存的地址\r\n加入两行程序\r\nshmid=shmget(0x1000,SHMDATASIZE,0660);\r\nshmdata=shmat(shmid,0,0);\r\n\r\n最后将netsnmp_register_int_instance中的&testAgentCpu1替换为shmdata即可\r\n不知道有没有更好的解决方法\r\n期待中。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP