首先,,編寫含有table結(jié)構(gòu)的MIB文件,,檢查好語(yǔ)法,TC-TABLE-MIB.txt內(nèi)容如下:
--
-- TC-TABLE-MIB.my
-- MIB generated by MG-SOFT Visual MIB Builder Version 6.0
-- 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文件,,沒有的創(chuàng)建,,從第一行開始添加mibs
+TC-TABLE-MIB,保存在/usr/local/share/snmp/目錄下
查看MIB是否添加成功:yang@ubuntu:~$ snmptranslate -Tp -IR
tc
出現(xiàn)MIB Tree后證明添加成功,。
此MIB文件含有table和scalar變量,,scalar變量用mib2c.scalar.conf生成.c和.h文件,table表用mib2c.iterate.conf生成
.c和 .h文件
yang@ubuntu:~$ mib2c -c mib2c.iterate.conf
tcTable
修改tcTable.c文件,,在頭文件下聲明一下tcTable_createEntry()函數(shù),,可自定義所帶參數(shù),在
initialize_table_tcTable()函數(shù)末尾初始化表的值,。根據(jù)需求修改好函數(shù)后,,把.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進(jìn)程:yang@ubuntu:~$ sudo killall -9
snmpd
重新運(yùn)行snmpd,,并讀取配置文件:yang@ubuntu:~$ sudo /usr/local/sbin/snmpd
-c /usr/local/share/snmp/snmpd.conf -c
/usr/local/share/snmp/snmp.conf
用walk獲取數(shù)值,,查看是否成功
yang@ubuntu:~$ snmpwalk -v2c -c demo 192.168.8.15
tcTable
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/下新建一個(gè)文件夾tcMIB,把所有要編譯的模塊的代碼拷貝到里面,,在/mibgroup/下建立一個(gè)tc.h文件,,添加所要編譯的模塊的目錄:
config_require(tcMIB/tcTable);
config_require(tcMIB/xxx);
...
configure時(shí),用--with-mib-modules="tc",,就可以編譯所有模塊了
注意:
編寫MIB中的table,,entry的大小寫,OBJECT-TYPE的變量名必須以小寫開頭,。
不同的net-snmp版本調(diào)用的函數(shù)可能不同,,5.2.6版本用snmp_set_var_value( idx,
entry->mcCardInfoIndex,
sizeof(entry->mcCardInfoIndex)
);函數(shù)給索引賦值,會(huì)導(dǎo)致程序崩潰,,無(wú)法創(chuàng)建索引和表格,,換用5.5.1版本后,用mib2c生成的模板調(diào)用snmp_set_var_typed_integer(
idx, ASN_INTEGER, entry->tcindex
);賦值索引,,能夠正常生成表格
|
|
來(lái)自: 漢江秋月夜 > 《net_snmp》