首先,编写含有table结构的MIB文件,检查好语法,TC-TABLE-MIB.txt内容如下:
http://blog.sina.com.cn/s/blog_8f3de3250100xhao.html
-- TC-TABLE-MIB.my
-- MIB generated by MG-SOFT Visual MIB Builder Version 6.0 Build 88
-- Monday, October 17, 2011 at 17:39:34
--
TC-TABLE-MIB DEFINITIONS ::= BEGIN
IMPORTS
DisplayString, TEXTUAL-CONVENTION
FROM SNMPv2-TC
OBJECT-TYPE, Integer32, Gauge32, MODULE-IDENTITY,enterprises
FROM SNMPv2-SMI
OBJECT-TYPE
FROM RFC-1212;
--- Node definitions
--
tc OBJECT IDENTIFIER ::= { enterprises 1500 }
-- 1.5
tcTable OBJECT-TYPE
SYNTAX SEQUENCE OF
TcEntry
MAX-ACCESS not-accessible
STATUS current
::= { tc 5 }
-- 1.5.1
tcEntry OBJECT-TYPE
SYNTAX TcEntry
MAX-ACCESS not-accessible
STATUS current
INDEX { tcindex }
::= { tcTable 1 }
TcEntry::=
SEQUENCE {
tcindex
INTEGER,
tcnode
INTEGER
}
-- 1.5.1.1
tcindex OBJECT-TYPE
SYNTAX INTEGER (1..100)
MAX-ACCESS read-only
STATUS current
::= { tcEntry 1 }
-- 1.5.1.2
tcnode OBJECT-TYPE
SYNTAX INTEGER
MAX-ACCESS read-write
STATUS current
::= { tcEntry 2 }
tcinfo OBJECT IDENTIFIER ::= { tc 10 }
tcName OBJECT-TYPE
SYNTAX DisplayString (SIZE (1..64))
MAX-ACCESS read-write
STATUS current
::= { tcinfo 1 }
tcLocation OBJECT-TYPE
SYNTAX DisplayString (SIZE (1..64))
MAX-ACCESS read-write
STATUS current
::= { tcinfo 2 }
tcSize OBJECT-TYPE
SYNTAX INTEGER
MAX-ACCESS read-write
STATUS current
::= { tcinfo 3 }
END
把生成的TC-TABLE-MIB.txt放到安装目录的mibs文件里。修改snmp.conf文件,没有的创建,从第一行开始添加mibs+TC-TABLE-MIB,保存在/usr/local/share/snmp/目录下
查看MIB是否添加成功:yang@ubuntu:~$ snmptranslate -Tp -IRtc
出现MIB Tree后证明添加成功。
此MIB文件含有table和scalar变量,scalar变量用mib2c.scalar.conf生成.c和.h文件,table表用mib2c.iterate.conf生成.c和 .h文件
yang@ubuntu:~$ mib2c -c mib2c.iterate.conftcTable
修改tcTable.c文件,在头文件下声明一下tcTable_createEntry()函数,可自定义所带参数,在initialize_table_tcTable()函数末尾初始化表的值。根据需求修改好函数后,把.c和.h文件拷贝到源码的/agent/mibgroup文件夹下,在/work/net-snmp-5.5.1/下configure:
./configure --prefix=/usr/local --enable-mfd-rewrites--with-default-snmp-version="2"--with-sys-contact="[email protected]" --with-sys-location="China"--with-logfile="/var/log/snmpd.log"--with-persistent-directory="/var/net-snmp" --with-mib-modules="tcTable"
make
sudo make install
kill掉之前的snmpd进程:yang@ubuntu:~$ sudo killall -9snmpd
重新运行snmpd,并读取配置文件:
yang@ubuntu:~$ sudo /usr/local/sbin/snmpd-c /usr/local/share/snmp/snmpd.conf -c/usr/local/share/snmp/snmp.conf
用walk获取数值,查看是否成功
yang@ubuntu:~$ snmpwalk -v2c -c demo 192.168.8.15tcTable
TC-TABLE-MIB::tcindex.1 = INTEGER: 1
TC-TABLE-MIB::tcindex.3 = INTEGER: 3
TC-TABLE-MIB::tcindex.5 = INTEGER: 5
TC-TABLE-MIB::tcnode.1 = INTEGER: 10
TC-TABLE-MIB::tcnode.3 = INTEGER: 20
TC-TABLE-MIB::tcnode.5 = INTEGER: 30
如果分支即含有表,又含有scalar,则可以分别生成.c和.h,
然后在/agent/mibgroup/下新建一个文件夹tcMIB,把所有要编译的模块的代码拷贝到里面,
在/mibgroup/下建立一个tc.h文件,添加所要编译的模块的目录:
config_require(tcMIB/tcTable);
config_require(tcMIB/xxx);
...
configure时,用--with-mib-modules="tc",就可以编译所有模块了
注意:
编写MIB中的table,entry的大小写,OBJECT-TYPE的变量名必须以小写开头。
不同的net-snmp版本调用的函数可能不同,5.2.6版本用snmp_set_var_value( idx,entry->mcCardInfoIndex,sizeof(entry->mcCardInfoIndex));函数给索引赋值,会导致程序崩溃,无法创建索引和表格,换用5.5.1版本后,用mib2c生成的模板调用snmp_set_var_typed_integer(idx, ASN_INTEGER, entry->tcindex);赋值索引,能够正常生成表格