Netsnmp 使用dlmod安装oid节点库

1,引入

        netsnmp-5.9.1使用snmpd.conf中可以使用dlmod去动态加载我们自己的oid节点库,这看起来很通用和灵活,这次就在官网上找了个例子简单的验证了一下这个功能。

2,netsnmp-5.9.1编译

编译选项:
    ./configure --prefix=/home/net-snmp-5.9.1/installs --enable-shared --disable-static --with-defaults --enable-applications --disable-ipv6 --without-libwrap --without-rpm --without-zlib --disable-embedded-perl --disable-as-needed --without-perl-modules --with-mib-modules=ucd_snmp

注:
    必须的选项:--enable-shared  --with-mib-modules=ucd_snmp (ucd支持)


make && make install

3,在官网获取example文件

TUT:Writing a Dynamically Loadable Object - Net-SNMP Wiki

 mib库也在其中下载:NET-SNMP-TUTORIAL-MIB.txt

 nstAgentPluginObject.h

/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.int_watch.conf,v 5.0 2002/04/20 07:30:13 hardaker Exp $
 */
#ifndef NSTAGENTPLUGINOBJECT_H
#define NSTAGENTPLUGINOBJECT_H

/*
 * function declarations 
 */
void            init_nstAgentPluginObject(void);

#endif                          /* NSTAGENTPLUGINOBJECT_H */

  nstAgentPluginObject.c

/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.int_watch.conf,v 5.0 2002/04/20 07:30:13 hardaker 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 "nstAgentPluginObject.h"

/*
 * the variable we want to tie an OID to.  The agent will handle all
 * * GET and SET requests to this variable changing it's value as needed.
 */

static int      nstAgentPluginObject = 3;
static oid      nstAgentPluginObject_oid[] =
    { 1, 3, 6, 1, 4, 1, 8072, 2, 4, 1, 1, 3, 0 };

/*
 * our initialization routine, automatically called by the agent 
 * (to get called, the function name must match init_FILENAME()) 
 */
void
init_nstAgentPluginObject(void)
{

    /*
     * a debugging statement.  Run the agent with -DnstAgentPluginObject to see
     * the output of this debugging statement. 
     */
    DEBUGMSGTL(("nstAgentPluginObject",
                "Initializing the nstAgentPluginObject module\n"));


    /*
     * the line below registers our variables defined above as
     * accessible and makes it writable.  A read only version of any
     * of these registration would merely call
     * register_read_only_int_instance() instead.  The functions
     * called below should be consistent with your MIB, however.
     * 
     * If we wanted a callback when the value was retrieved or set
     * (even though the details of doing this are handled for you),
     * you could change the NULL pointer below to a valid handler
     * function. 
     */
    DEBUGMSGTL(("nstAgentPluginObject",
                "Initalizing nstAgentPluginObject scalar integer.  Default value = %d\n",
                nstAgentPluginObject));

    netsnmp_register_int_instance("nstAgentPluginObject",
                                  nstAgentPluginObject_oid,
                                  OID_LENGTH(nstAgentPluginObject_oid),
                                  &nstAgentPluginObject, NULL);

    DEBUGMSGTL(("nstAgentPluginObject",
                "Done initalizing nstAgentPluginObject module\n"));
}

void
deinit_nstAgentPluginObject(void)
{
    unregister_mib(nstAgentPluginObject_oid,OID_LENGTH(nstAgentPluginObject_oid));
}

如何编译这个些文件?

        把 这个文件放在netsnmp-5.9.1/下,然后如下:

网上提供的编译方法:

cc `net-snmp-config --cflags` -fPIC -shared -g -O0 -o nstAgentPluginObject.so nstAgentPluginObject.c `net-snmp-config --libs`

注:
    这个编译方法需要你的netsnmp库放在/usr/local下

 这个方法编译可以指定链接的libnetsnmp库路径:

gcc `net-snmp-config --cflags` -fPIC -shared -g -O0 -o nstAgentPluginObject.so nstAgentPluginObject.c nstAgentPluginObject.h `net-snmp-config --libdir` /home/net-snmp-5.9.1/installs/lib/libnetsnmp.so.40 

 遇到的坑:

        使用网上的方法,编译的库总是在snmpd加载时报错:

通过objdump -p snmpd 和 objdump -p nstAgentPluginObject.so 发现:

        snmpd : libnetsnmp.so.40 ,而我的 nstAgentPluginObject.so依赖的是:  libnetsnmp.so.30

后面我使用 locate  libnetsnmp.so.30 ,发现我/usr/local下有 libnetsnmp.so.30,(因为之前编译过netsnmp-5.7.3,然后给他安装在了/usr/local下),所以问题就知道怎么解决了:

        ① 要么重新编译netsnmp-5.9.1,将其安装到/usr/local下(前提需要删除 libnetsnmp.ao.30)

        ② 要么看看编译nstxxx这个库时,可以指定连接的库吗? 查看net-snmp-config其中有--libdir选项。

        以上,成功解决!!!

4,进行测试

        snmpd.conf 配置:

        dlmod  <Module Name> </path/name>

agentAddress udp:161
rwcommunity public
dlmod nstAgentPluginObject /home/net-snmp-5.9.1/nstAgentPluginObject.so 
sysLocation    Sitting on the Dock of the Bay
sysContact     Meter<me@example.org>
sysServices    72

运行snmpd (我没有安装在/usr/local下):

        ./snmpd -f -L -DnstAgentPluginObject,dlmod -Cc ./snmpd.conf

 snmpget:

//测试snmpd有正常跑起来
./snmpget -v2c -c public 192.168.100.10:161 1.3.6.1.2.1.1.1.0

//测试snmpd安装的动态节点是否生效(最后是其文件中的oid标量节点)
./snmpget -v2c -c public 192.168.100.10:161 1.3.6.1.4.1.8072.2.4.1.1.3.0

测试成功!!!。

snmpset: 失败了?

5,总结

        这次简单看了一下netsnmp的动态库加载,这也挺灵活的,本次主要做了简单的测试和一下问题的解决,后期还好继续研究一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值