(一)编写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