用net-snmp获得cpu利用百分比值(转)

通过Agent获取变量的值(小弟写的是一个返回cpu利用百分数的函数 void al_share_mem(int * value),
包含在文件rdm-test3.c中,该函数可动态地返回值)

然后小弟将MIB文件经过mib2c命令编译成.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,则又得到新的值,且又保持不变了。。。。
不知道怎么样才能获取到实时的值。请指点。

问题解决了。
不过用比较笨的方法
就是直接映射共享内存的地址
加入两行程序
shmid=shmget(0x1000,SHMDATASIZE,0660);
shmdata=shmat(shmid,0,0);

最后将netsnmp_register_int_instance中的&testAgentCpu1替换为shmdata即可
不知道有没有更好的解决方法
期待中。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值