NET-SNMP学习(二)Table

(一)编写MIBS

TEST-DEVICE-MIB DEFINITIONS ::= BEGINIMPORTSMODULE-IDENTITY, OBJECT-TYPE, TimeTicks FROM SNMPv2-SMIDisplayString, FROM SNMPv2-TCenterprises         FROM RFC1155-SMI;test OBJECT IDENTIFIER ::= { enterprises 1000 }Device OBJECT IDENTIFIER ::= { test 1 }boardSlotTable OBJECT-TYPESYNTAX SEQUENCE OF BoardSlotEntryMAX-ACCESS not-accessibleSTATUS currentDESCRIPTION " Fan status description table "::= { Device  2 }boardSlotEntry OBJECT-TYPESYNTAX BoardSlotEntryMAX-ACCESS not-accessibleSTATUS currentDESCRIPTION " Boad status description table entry "INDEX   { boardSlotID }::= { boardSlotTable 1}BoardSlotEntry::=SEQUENCE {boardSlotID  INTEGER,boardType INTEGER,boardSeriesNumber DisplayString, }boardSlotID  OBJECT-TYPESYNTAX  INTEGERMAX-ACCESS read-onlySTATUS currentDESCRIPTION " Slot ID "::= { boardSlotEntry 1 }--add enum valueboardType  OBJECT-TYPESYNTAX  INTEGERMAX-ACCESS read-onlySTATUS currentDESCRIPTION " Slot Number "::= { boardSlotEntry 2 }boardSeriesNumber  OBJECT-TYPESYNTAX  DisplayString (SIZE (0..128))MAX-ACCESS read-onlySTATUS currentDESCRIPTION " Slot Series Number "::= { boardSlotEntry 3 }boardWorkMode  OBJECT-TYPESYNTAX  INTEGER{normal (1),abnormal (2),offline (3)}MAX-ACCESS read-onlySTATUS currentDESCRIPTION " 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:                       	    Devicenumeric translation:       	    .1.3.6.1.4.1.1000.1number of scalars within:         0number of tables within:          1number of notifications within:   0First, 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 code2) Net-SNMP style codeSelect your choice :

根据提示选择即可。

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

(二)修改代码

生成的.c代码:

/** Note: this file originally auto-generated by mib2c using*  $*/#include 
#include 
#include 
#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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部