NET-SNMP学习(二)Table

(一)编写MIBS

TEST-DEVICE-MIB DEFINITIONS ::= BEGIN
 
IMPORTS
    MODULE-IDENTITY, OBJECT-TYPE, TimeTicks FROM SNMPv2-SMI
       DisplayString, FROM SNMPv2-TC
    enterprises         
        FROM RFC1155-SMI;
    test OBJECT IDENTIFIER ::= { enterprises 1000 }
 
    
    Device OBJECT IDENTIFIER ::= { test 1 }
           
    boardSlotTable OBJECT-TYPE
        SYNTAX SEQUENCE OF BoardSlotEntry
        MAX-ACCESS not-accessible
        STATUS current
        DESCRIPTION " Fan status description table "
        ::= { Device  2 }
 
 
    boardSlotEntry OBJECT-TYPE
        SYNTAX BoardSlotEntry
        MAX-ACCESS not-accessible
        STATUS current
        DESCRIPTION " Boad status description table entry "
        INDEX   { boardSlotID }
        ::= { boardSlotTable 1}
 
 
    BoardSlotEntry::=
       SEQUENCE {
                boardSlotID  INTEGER,
                boardType INTEGER,
                boardSeriesNumber DisplayString, 
               }
 
    boardSlotID  OBJECT-TYPE
        SYNTAX  INTEGER
        MAX-ACCESS read-only
        STATUS current
        DESCRIPTION " Slot ID "
        ::= { boardSlotEntry 1 }
 
    --add enum value
    boardType  OBJECT-TYPE
        SYNTAX  INTEGER
        MAX-ACCESS read-only
        STATUS current
        DESCRIPTION " Slot Number "
        ::= { boardSlotEntry 2 }
        
    boardSeriesNumber  OBJECT-TYPE
        SYNTAX  DisplayString (SIZE (0..128))
        MAX-ACCESS read-only
        STATUS current
        DESCRIPTION " Slot Series Number "
        ::= { boardSlotEntry 3 }
 
    boardWorkMode  OBJECT-TYPE
        SYNTAX  INTEGER
        {
            normal (1),
            abnormal (2),
            offline (3)
        }
        MAX-ACCESS read-only
        STATUS current
        DESCRIPTION " Board Status: normal (1), abnormal (2) and offline (3)"
        ::= { boardSlotEntry 4 }
 
END

写完之后将其加入到snmp.conf中(即在conf文件里加mibs +xxx,  xxx指的是自己定义的BEGIN名称),如这里的定义的名称是

TEST-DEVICE-MIB。

TEST-DEVICE-MIB DEFINITIONS ::= BEGIN

然后通过命令

snmptranslate -Tp -IR TEST-DEVICE-MIB::Device

Note:Device是自定义Module名称

验证是否成功添加MIB:

+--Device(1)
   |
   +--boardSlotTable(2)
      |
      +--boardSlotEntry(1)
         |  Index: boardSlotID
         |
         +-- -R-- INTEGER   boardSlotID(1)
         +-- -R-- INTEGER   boardType(2)
         +-- -R-- String    boardSeriesNumber(3)
         |        Textual Convention: DisplayString
         |        Size: 0..128
         +-- -R-- EnumVal   boardWorkMode(4)
                  Values: normal(1), abnormal(2), offline(3)

出现这个代表已经成功添加到我们的MIB库里。

使用mib2c工具编译。表格使用mib2c.iterate.conf或mib2c.iterate_access.conf。

mib2c mib2c.iterate.conf TEST-DEVICE-MIB::Device

成功会有提示选择编译选项:

You requested mib2c to be run on the following part of the MIB tree:
  OID:                       	    Device
  numeric translation:       	    .1.3.6.1.4.1.1000.1
  number of scalars within:         0
  number of tables within:          1
  number of notifications within:   0

First, do you want to generate code that is compatible with the
ucd-snmp 4.X line of code, or code for the newer Net-SNMP 5.X code
base (which provides a much greater choice of APIs to pick from):

  1) ucd-snmp style code
  2) Net-SNMP style code

Select your choice :

根据提示选择即可。

继而生成了对应的.c  .h文件。

(二)修改代码

生成的.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 "boardSlotTable.h"

/** Initializes the boardSlotTable module */
void
init_boardSlotTable(void)
{
  /* here we initialize all the tables we're planning on supporting */
    initialize_table_boardSlotTable();
}

  # Determine the first/last column names

/** Initialize the boardSlotTable table by defining its contents and how it's structured */
void
initialize_table_boardSlotTable(void)
{
    const oid boardSlotTable_oid[] = {1,3,6,1,4,1,1000,1,2};
    const size_t boardSlotTable_oid_len   = OID_LENGTH(boardSlotTable_oid);
    netsnmp_handler_registration    *reg;
    netsnmp_iterator_info           *iinfo;
    netsnmp_table_registration_info *table_info;

    DEBUGMSGTL(("boardSlotTable:init", "initializing table boardSlotTable\n"));

    reg = netsnmp_create_handler_registration(
              "boardSlotTable",     boardSlotTable_handler,
              boardSlotTable_oid, boardSlotTable_oid_len,
              HANDLER_CAN_RONLY
              );

    table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
    netsnmp_table_helper_add_indexes(table_info,
                           ASN_INTEG
  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

just_run丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值