应用场景:
在管理Agent设备的配置文件的时候,难免会遇到增删查改的操作。一般的snmpget对应查,snmpset对应改,但是增删就不是简单的直接操作了,就需要用到今天的主角——RowStatus这个属性。
小坑一个:RowStatus一定是和Table一起用的。
传送门:官方文档,喜欢阅读官方文档的朋友不容错过,不过是全英文的。
环境:
系统:Ubuntu14.04LTS
snmp版本:net-snmp-5.7.3
step1.编写MIB文件
在上一篇文章中,已经注释解释了教为通用的部分,今天这个MIB加入了Table,稍有不同:
QQMIB DEFINITIONS ::= BEGIN
IMPORTS
OBJECT-GROUP, MODULE-COMPLIANCE, NOTIFICATION-GROUP
FROM SNMPv2-CONF
enterprises, Integer32, Unsigned32, OBJECT-TYPE, MODULE-IDENTITY,NOTIFICATION-TYPE
FROM SNMPv2-SMI
DisplayString, RowStatus
FROM SNMPv2-TC;
--.1.3.6.1.4.1.73691
QQMIB OBJECT IDENTIFIER ::= { enterprises 73691 }
--.1.3.6.1.4.1.73691.1
qosTableRoot OBJECT IDENTIFIER ::= { QQMIB 1 }
--创建一个table,必须以xxxTable这样的格式命名
--注意MAX-ACCESS的设置
--.1.3.6.1.4.1.73691.1.1
qosTable OBJECT-TYPE
SYNTAX SEQUENCE OF QosEntry
MAX-ACCESS read-create
STATUS current
DESCRIPTION
"专用承载列表"
::= { qosTableRoot 1 }
--创建Entry对象,理解为Row,格式为xxxEntry,注意和定义的Entry对象名区别开
--注意INDEX一定要的
--.1.3.6.1.4.1.73691.1.1.1
qosEntry OBJECT-TYPE
SYNTAX QosEntry
MAX-ACCESS read-create
STATUS current
DESCRIPTION
""
INDEX { qosIndex }
::= { qosTable 1 }
--定义Entry对象,格式为xxxEntry,注意和创建Entry对象名区别开
--这里定义Index,RowStatus是必须的,其他的就按照需求自定义了
QosEntry::= SEQUENCE {
qosIndex Integer32,
WANDevice Integer32,
DLMaxRate Unsigned32,
qosRowStatus RowStatus
}
--.1.3.6.1.4.1.73691.1.1.1.1
qosIndex OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-create
STATUS current
DESCRIPTION
"index"
::= { qosEntry 1 }
--.1.3.6.1.4.1.73691.1.1.1.2
WANDevice OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-create
STATUS current
DESCRIPTION
"参数解释:广域网侧设备。取值范围:4 - 4 ,默认值:4"
::= { qosEntry 2 }
--.1.3.6.1.4.1.73691.1.1.1.3
DLMaxRate OBJECT-TYPE
SYNTAX Unsigned32
MAX-ACCESS read-create
STATUS current
DESCRIPTION
"参数解释:下行最大速率。取值范围:0 - 262144,默认值:262144"
::= { qosEntry 3 }
--注意SYNTAX类型
--.1.3.6.1.4.1.73691.1.1.1.4
qosRowStatus OBJECT-TYPE
SYNTAX RowStatus
MAX-ACCESS read-create
STATUS current
DESCRIPTION
"rowStatus"
::= { qosEntry 4 }
END
需要注意的地方已经在上面的MIB文件中注释了,可别看漏了哦。毕竟漏一些东西,也可以正常生成c文件,最后运行时候命令效果不对,根本不知道哪里出了问题。
step2.使用mib2c工具生成c文件
和上篇文章一样,不多赘述。输入指令,依据需求选择:
zzj@sed:~/net-snmp/net-snmp-5.7.3/local$ env MIBS="+/home/zzj/.snmp/mibs/QQ-MIB.txt" mib2c qosTableRoot
writing to -
mib2c has multiple configuration files depending on the type of
code you need to write. You must pick one depending on your need.
You requested mib2c to be run on the following part of the MIB tree:
OID: qosTableRoot
numeric translation: .1.3.6.1.4.1.73691.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 : 2
**********************************************************************
GENERATING CODE FOR TABLES:
**********************************************************************
The Net-SNMP agent API is extremely extensive and, in fact, lets
each programmer write agent code according to the style that works
best for them based on their experience and their preference. We're
going to ask you a serious of questions that will help mib2c
generate code that best suits *your* needs, as the programmer that
will be responsible for taking the code and further refining it. If
you don't like how the results look, you are always welcome to
re-run mib2c and select a different set of options.
There are essentially two tasks involved in processing requests
for OIDs within a MIB table - firstly identifying the relevant row
of the table for a given request, and then returning (or updating)
the appropriate column value within that row. Many MIB tables model
the state of some external system (the kernel, a device, processes,
etc), and the MIB implementation module (the code we're about to
produce a template for) acts as an interface between this underlying
system