1、 参照网上到处能下到的《net-snmp安装配置手册》编译安装net-snmp5.4.2,并参照《net-snmp安装配置手册》进行相应配置。
2、 根据ASN.1语法编写MIB库BAMBOO-MIB.txt。内容如下:
BAMBOO-MIB DEFINITIONS ::= BEGIN
IMPORTS
TimeTicks FROM SNMPv2-SMI
enterprises FROM SNMPv2-SMI
OBJECT-GROUP,NOTIFICATION-GROUP FROM SNMPv2-CONF
OBJECT-TYPE,Integer32,Gauge32,MODULE-IDENTITY NOTIFICATION-TYPE FROM SNMPv2-SMI
TEXTUAL-CONVENTION FROM SNMPv2-TC;
cdut OBJECT IDENTIFIER ::= { enterprises 8888 }
ce OBJECT IDENTIFIER ::= { cdut 8 }
FY0404 OBJECT IDENTIFIER ::= { ce 4 }
FY04040019 OBJECT IDENTIFIER ::= { FY0404 19 }
myVersion OBJECT-TYPE
Syntax OCTET STRING (SIZE (0..64))
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"sofware version"
::= { FY04040019 1 }
mySysResetNo OBJECT-TYPE
Syntax Integer32
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"if mySysResetNo=1,restart system"
::= { FY04040019 2 }
END
3、 在/usr/local/net-snmp/share/snmp下创建snmp.conf,用于配置MIB库。
在snmp.conf中添加如下内容来加载要添加的MIB库: mib +BAMBOO-MIB
再次运行#snmpd –c snmpd.conf –c snmp.conf使配置文件生效。
查看MIB库是否加载:
# snmptranslate -IR -Tp cdut
+--cdut(8888)
|
+--ce(8)
|
+--FY0404(4)
|
+--FY04040019(19)
|
+-- -R-- String myVersion(1)
| Size: 0..64
+-- -RW- Integer32 mySysResetNo(2)
4、 下面利用mib2c来生成.c和.h文件(因为该MIB库中的变量含有string类型,所以选用mib2c.scalar.conf作为模版)
#mib2c –c mib2c.scalar.conf FY04040019
将生成FY04040019.c和FY04040019.h文件。
5、 修改FY04040019.c文件:
添加以下定义:
#define STRLENGTH 64
char myVersion[STRLENGTH] = "bamboo-1.00";
unsigned mySysResetNo = 0;
int handle_myVersion(……)
{
……
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb,ASN_OCTET_STR,
(u_char *)myVersion, //++ by Bamboo
sizeof(myVersion) //++ by Bamboo
);
break;
}
int handle_mySysResetNo(……)
{
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb,ASN_INTEGER,
(u_char *)&mySysResetNo, //++ by Bamboo
sizeof(mySysResetNo) //++ bu Bamboo
);
break;
case MODE_SET_RESERVE1:
/*
* or you could use netsnmp_check_vb_type_and_size instead
*/
ret = netsnmp_check_vb_type(requests->requestvb,ASN_INTEGER);
if (ret != SNMP_ERR_NOERROR) {
netsnmp_set_request_error(reqinfo,requests,ret);
}
break;
case MODE_SET_RESERVE2:
if (0) { //++by Bamboo
netsnmp_set_request_error(reqinfo,
SNMP_ERR_RESOURCEUNAVAILABLE);
}
break;
case MODE_SET_FREE:
break;
case MODE_SET_ACTION:
mySysResetNo = *(requests->requestvb->val.integer); //++ by Bamboo
if (mySysResetNo > 1) { //++ by Bamboo
netsnmp_set_request_error(reqinfo,0/* some error */
);
}
break;
case MODE_SET_COMMIT:
if (0 ) { //++ by Bamboo
netsnmp_set_request_error(reqinfo,
SNMP_ERR_COMMITFailed);
}
break;
case MODE_SET_UNDO:
if (0) { //++ by Bamboo
/*
* try _really_really_ hard to never get to this point
*/
netsnmp_set_request_error(reqinfo,
SNMP_ERR_UNDOFailed);
}
break;
……
}
6、 下面准备重新编译net-snmp-5.4.2:
将FY04040019.c和FY04040019.h文件拷贝到net-snmp-5.4.2/agent/mibgroup下。
然后重新配置configure文件:
(当前目录为net-snmp-5.4.2)
#./configure –with-mib-modules=”FY04040019” 其他略(其他配置参数参考《net-snmp安装配置手册》)
进行编译和安装(安装目录为net-snmp)#make && make install
再次启动snmp:#snmpd –c snmpd.conf –c snmp.conf
到此MIB库的扩展以成功了,下面进行验证。
7、 查看myVersion对象的实例(访问实例一定要在对象后面加“.0”)
# snmpget -v2c -c public 192.168.16.3 1.3.6.1.4.1.8888.8.4.19.1.0
BAMBOO-MIB::myVersion.0 = Hex-STRING: 62 61 6D 62 6F 6F 2D 31 2E 30 30 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
OK, get成功
设置mySysResetNo的值
# snmpset -v2c -c public localhost 1.3.6.1.4.1.8888.8.4.19.2.0 i 1 //设置为整形数1
BAMBOO-MIB::mySysResetNo.0 = INTEGER: 1
查看mySysResetNo的值
snmpget -v2c -c public localhost 1.3.6.1.4.1.8888.8.4.19.2.0
BAMBOO-MIB::mySysResetNo.0 = INTEGER: 1
目前存在有待解决的问题:
snmpget -v2c -c public 192.168.16.3 1.3.6.1.4.1.8888.8.4.19.2.0
BAMBOO-MIB::mySysResetNo.0 = INTEGER: 1 //用ip地址来get,是可以的
snmpset -v2c -c public 192.168.16.3 1.3.6.1.4.1.8888.8.4.19.2.0 i 1
Error in packet.
Reason: noAccess
Failed object: BAMBOO-MIB::mySysResetNo.0 //用ip地址来set就不行了