net-snmp 创建我自己的 MIB:此 OID 当前不存在此类实例

网络工程 snmp linux
2021-07-09 06:33:09

先说第一件事。我想创建我自己的 MIB

如果我问一个简单的问题,请原谅我,因为我是 SNMP 的新手,而且我在这方面苦苦挣扎了几个星期。我已经通过了TUT:mib2c

更新:我有以下名为 的 MIB SAMPLE-MIB,它编译良好,没有任何错误(此编译仅在代理系统上完成,不在服务器上完成)。

SAMPLE-MIB DEFINITIONS ::= BEGIN

IMPORTS
    MODULE-IDENTITY, OBJECT-TYPE, Integer32,
        NOTIFICATION-TYPE                       FROM SNMPv2-SMI
        SnmpAdminString                         FROM SNMP-FRAMEWORK-MIB
        netSnmp                                 FROM NET-SNMP-MIB
;



sample    MODULE-IDENTITY
    LAST-UPDATED "201709120200Z"
    ORGANIZATION "NCS"
    CONTACT-INFO
        "@ncs-in.com"        
    DESCRIPTION
        "A test MIB"
    REVISION    "201709120200Z"
    DESCRIPTION
        "initial Draft"
    ::= { iso org(3) dod(6) internet(1) private(4) enterprises(1) 39106 }

sampleResearch               OBJECT IDENTIFIER ::= { sample 2 }

sampleVar OBJECT-TYPE
    SYNTAX Integer32
    MAX-ACCESS  read-write
    STATUS  current
    DESCRIPTION
        "testing"
    ::= { sampleResearch 1}

END

我已经这样做了:

root@snmp-agent:# MIBS=+SAMPLE-MIB  
root@snmp-agent:# export MIBS    
root@snmp-agent:# MIBS=ALL

我的 MIB 文件位于此路径中:/usr/share/snmp/mibs这是默认搜索路径。我已经编译并成功生成了 .c 和 .h 文件,如下所示:

root@snmp-agent:# snmptranslate -On SAMPLE-MIB::sampleVar
root@snmp-agent:# snmptranslate -IR SAMPLE-MIB
root@snmp-agent:# mib2c -c mib2c.scalar.conf sampleVar

注意:这些 sampleVar.c 和 sampleVar.h 文件位于以下路径中:/home/snmp-agent/Desktop/project/net-snmp-5.7.3/agent/mibgroup/.

为了简单起见,我不会修改 中的任何内容sampleVar.h,这就是我的sampleVar.c文件的外观:

/*

 * Note: this file originally auto-generated by mib2c using

 *        $

 */



#include <net-snmp/net-snmp-config.h>

#include <net-snmp/net-snmp-includes.h>

#include <net-snmp/agent/net-snmp-agent-includes.h>

#include "sampleVar.h"







static int myvar=10;

/** Initializes the sampleVar module */

void

init_sampleVar(void)

{

    const oid sampleVar_oid[] = { 1,3,6,1,4,1,39106,2,1 };

    const size_t sampleVar_oid_len   = OID_LENGTH(sampleVar_oid);

    netsnmp_handler_registration    *reg;

    netsnmp_iterator_info           *iinfo;

    netsnmp_table_registration_info *table_info;



  DEBUGMSGTL(("sampleVar", "Initializing\n"));



   /* netsnmp_register_scalar(

        netsnmp_create_handler_registration("sampleVar", handle_sampleVar,

                               sampleVar_oid, OID_LENGTH(sampleVar_oid),

                               &,HANDLER_CAN_RWRITE   ));*/



 netsnmp_register_int_instance("sampleVar",

                                  sampleVar_oid,

                                  OID_LENGTH(sampleVar_oid),

                                  &myvar, NULL);





}



int

handle_sampleVar(netsnmp_mib_handler *handler,

                          netsnmp_handler_registration *reginfo,

                          netsnmp_agent_request_info   *reqinfo,

                          netsnmp_request_info         *requests)

{

    int ret = 120892;

    netsnmp_request_info       *request;

    netsnmp_table_request_info *table_info;

    struct poolTable_entry          *table_entry;

    /* We are never called for a GETNEXT if it's registered as a

       "instance", as it's "magically" handled for us.  */



    DEBUGMSGTL(("sampleVar", "Initializing\n"));



    /* a instance handler also only hands us one request at a time, so

       we don't need to loop over a list of requests; we'll only get one. */







    return ret;

}

现在我用 MIB 重新配置 net-snmp,如下所示:

./configure --with-mib-modules="sampleVar"
make
make install

在此之后,当我(在代理上)执行此操作时,我snmptranslate -IR sampleVar.0得到的输出为:

root@snmp-agent:# snmptranslate -IR sampleVar.0
SAMPLE-MIB::sampleVar.0 (expected output of the command)

使用命令snmptranslate -On objectName.0我得到的输出为:

root@snmp-agent:# snmptranslate -On SAMPLE-MIB::sampleVar.0
.1.3.6.1.4.1.39106.2.1 (expected output of the command)

当我snmpwalk在服务器上

snmp-server:# snmpwalk -v 2c -c ncs -m SAMPLE-MIB 192.168.5.22 .1.3.6.1.4.1.39106.2.1
SAMPLE-MIB::sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): STRING: "/bin/echo"
SAMPLE-MIB::sampleVar.3.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): ""
SAMPLE-MIB::sampleVar.4.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): ""
SAMPLE-MIB::sampleVar.5.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 5
SAMPLE-MIB::sampleVar.6.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 1
SAMPLE-MIB::sampleVar.7.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 1
SAMPLE-MIB::sampleVar.20.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 4
SAMPLE-MIB::sampleVar.21.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 1

但是,当我snmpget从我的服务器上做时,我得到:

root@snmp-server:# snmpget 192.168.5.22 sampleVar.0
SAMPLE-MIB::sampleVar = No Such Object available on this agent at this OID
root@snmp-server:# snmpget -v 2c -c ncs 192.168.5.22 -m SAMPLE-MIB .1.3.6.1.4.1.39106.2.1
SAMPLE-MIB::sampleResearch.3.1.2 = No Such Object available on this agent at this OID

我确实sysUpTime.0从我的服务器得到了预期的输出

root@snmp-server:# snmpget 192.168.5.22 sysUpTime.0
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (721781) 2:00:17.81 (OUTPUT)

我已经用谷歌搜索了它并仍在搜索但没有运气。所以谁能告诉我下一步该怎么做?我应该如何snmpget在我自己的 MIB 上使用我的服务器?

任何想法或建议都受到高度赞赏。谢谢你。

更新 2:输出snmpgetnext

root@snmp-server:~# snmpgetnext -v 2c -c ncs 192.168.5.22 -m SAMPLE-MIB .1.3.6.1.4.1.39106.2.1
SAMPLE-MIB::sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): STRING: "/bin/echo"
root@snmp-server:~# snmpgetnext -v 2c -c ncs 192.168.5.22 -m SAMPLE-MIB .1.3.6.1.4.1.39106.2.2
SAMPLE-MIB::sample.3.1.1.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = ""

正如您所说:snmpget 用于定位 SNMP 中的特定 OID,因此,当我指定完整的 OID 时,我会得到正确的输出:

root@snmp-server:~# snmpget -v 2c -c ncs -m SAMPLE-MIB 192.168.5.22 sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54
SAMPLE-MIB::sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): STRING: "/bin/echo"

但这是什么:sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54这里有什么问题?我不明白!我没有指定这个东西!!!!

1个回答

我觉得你的问题是从如何SNMP数据(OID)数据库,构成一个简单的误解来临,如何snmpwalksnmpget以及snmpgetnext互动吧。

SNMP 将 OID 组织成相关的嵌套树(或表),MIB 定义这些树和其中的 OID。

在进入之前了解如何snmpgetsnmpgetnext工作很重要snmpwalk

  1. snmpget

snmpget 用于以SNMP 中特定OID为目标这假设我知道我想要从中获取数据的确切 OID,例如,我想要 IF-MIB::ifName 表 (1.3.6.1.2.1.31.1.1.1.1.1.1) 中的第一个条目。我知道该条目是为事实而存在的,因此它将返回一个值。

[jhead@sonder ~]$ snmpget -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1.1
.1.3.6.1.2.1.31.1.1.1.1.1 = STRING: fxp0
  1. snmpgetnext

snmpgetnext用于当您不确切知道目标系统上的组织方式时。意思是,你知道系统有这个IF-MIB::ifName表,但你不知道第一个条目是什么。 snmpgetnext的工作是从字面上找到您指向的任何 OID 的下一个条目。

我知道1.3.6.1.2.1.31.1.1.1.1.2在我的目标系统上不存在的事实,让我们验证一下snmpget

[jhead@sonder ~]$ snmpget -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1.2
.1.3.6.1.2.1.31.1.1.1.1.2 = No Such Instance currently exists at this OID

现在让我们尝试一个snmpgetnext

[jhead@sonder ~]$ snmpgetnext -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1.2
.1.3.6.1.2.1.31.1.1.1.1.4 = STRING: lsi

看起来我的下一个条目是 .4,太棒了!

  1. snmpwalk

snmpwalk实际上是对您定位的任何 OID 值的递归搜索。 snmpwalk实际上执行多个snmpgetnext操作来确定表的内容,因为该表的大小可能会有所不同,或者会随着时间而变化。

例如,如果我想检索所有 OID 条目IF-MIB::ifName(我的特定 MIB 将其定义为1.3.6.1.2.1.31.1.1.1.1

[jhead@sonder ~]$ snmpwalk -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1
.1.3.6.1.2.1.31.1.1.1.1.1 = STRING: fxp0
.1.3.6.1.2.1.31.1.1.1.1.4 = STRING: lsi
.1.3.6.1.2.1.31.1.1.1.1.5 = STRING: dsc
.1.3.6.1.2.1.31.1.1.1.1.6 = STRING: lo0
.1.3.6.1.2.1.31.1.1.1.1.7 = STRING: tap
.1.3.6.1.2.1.31.1.1.1.1.8 = STRING: gre
.1.3.6.1.2.1.31.1.1.1.1.9 = STRING: ipip

如您所见,我检索了该 OID 表中的所有条目。

snmpwalk如果表 A 包含表 B、C 和 D,也会递归通过表 A。DISMAN-PING-MIB例如您的示例中的整个表,它也会递归这些表。

  1. 你原来的问题。

现在,让我们看看为什么不能像示例中那样使用 snmpget 来定位表:

[jhead@sonder ~]$ snmpget -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1
.1.3.6.1.2.1.31.1.1.1.1 = No Such Instance currently exists at this OID

它失败是因为 OID 是一个表,没有与 .1.3.6.1.2.1.31.1.1.1.1 相关联的特定信息,它只是其余信息的容器

有不止我提到的 3 种方法,likesnmpbulkwalk和其他。您使用哪种方法可能会因您要完成的任务而异,但我怀疑snmpwalk会正常工作。您可以在此处查看示例