Precondition
- Hardware platform: ATMEL SAM9X5 Series
- Cross-compile tool: denx eldk-4.2
CrossCompile
openssl
denx的eldk-4.2版本的arm编译器自带的openssl库版本为0.9.8b,在编译net-snmp的时候configure会出错:
checking for BIO_dgram_get_peer... configure: error: DTLS support requires a newer version of OpenSSL
所以需要使用高版本的openssl。
$ git clone git://git.openssl.org/openssl.git
由于net-snmp不支持1.1的版本,并且出于Heartblood的考虑,最终选用的是0.9.8y的版本。
openssl-0.9.8的LIST没有包含linxu-elf-arm或者linux-armv4,如果按照”Configure shared no-asm linux=arm-linux-gcc”编译,会出现:
You gave the option ‘shared’. Normally, that would give you shared libraries.
Unfortunately, the OpenSSL configuration doesn’t include shared library support
for this platform yet, so it will pretend you gave the option ‘no-shared’.
google了一下,0.9.8的openssl采用统一的linux-generic32的target来编译:
$ git check OpenSSL_0_9_8y
$ ./Configure linux-generic32 no-asm shared
$ make
$ make INSTALL_PREFIX=/tmp/package-root install
编译完成后可以把openssl拷贝到目标板上执行以验证正确性:
$ openssl genrsa 512
Generating RSA private key, 512 bit long modulus
.................................++++++++++++
..++++++++++++
e is 65537 (0x10001)
-----BEGIN RSA PRIVATE KEY-----
MIIBOQIBAAJBAME3jqRRlFt/wnVOHdqKr5/aB+yGrP9ErmQjbNOTIkf63kvalBu5
9fYCwq1ScLjZsRHUDrxLJDL21IgeWAXIvvECAwEAAQJAXGil4BDkU56W8mOaA5uw
w1MT1xvSQYQno90Nf2MvUhzkzE9Y81/+CcZqkNRdGcAD6WvSVQ5mkyHxv8mjrGPv
wQIhAOLr4i6CM6hqWfzp8nv9msnPv9ntDkGISG/Eg/5NjDaXAiEA2foBw0Y1iCP+
l9XwomtLNM3oVTXyCsXK+PCYGcS4L7cCIA88JwNYWn+EJGaGjnEkpa/G8up+r+ZW
cAaQ+wunyTIhAiAZV0JLNGdWy3B2sG2IRFb67ZE8DshzzijZpgbYwBD1owIgFbLR
kjlSMIoNpDV1SjYLi/sk3lcdInDGwltyR1TOCms=
-----END RSA PRIVATE KEY-----
由于第一次编译的时候没有验证,导致后来snmpd启动的时候出错。
net-snmp
在官网下载net-snmp的源码,本人下载的为目前的最新版:5.7.3
解压缩,编译:
$ ./configure --build=i386-linux --host=arm-linux --disable-ipv6 --with-endianness=little \
--disable-manuals --enable-as-needed --enable-embedded-perl \
--without-perl-modules --disable-snmptrapd-subagent --disable-scripts \
--without-rpm --prefix=/path_to_your/rootfs --with-security-modules=tsm \
--with-transports=TLSTCP,DTLSUDP --with-openssl=/data/utils/openssl.git
由于我们之前重新编译了openssl,而不实用系统自带的openssl库,所以需要使用”–with-openssl”指定路径。
$ make LDFLAGS="-static"
$ make install
这样可以不编译出动态库。编译完成后主目录下会生成一个脚本文件net-snmp-config,这个脚本可以用来编译采用代理机制编写的snmpd服务,也可以用来创建v3版本的用户名、密码,此功能等于于 net-snmp-create-v3-user脚本。
v2测试
我们采用V2来验证snmpd的功能。
将snmpd、snmpget、snmpwalk传至目标板;
将系统MIB库传至目标板的/path_to_your/rootfs/share/snmp/mibs目录;
具体路径可以通过”snmpd -h”查看。
snmpd.conf可以通过”snmpconf -g basic_setup”来产生。
或者只需在snmpd.conf中添加:
rwcommnunity public
然后运行snmpd:
# snmpd -f -Lo
NET-SNMP version 5.7.3
- -f:以前台形式启动
- -Lo:输出DEBUG信息到标准输出
另外开一个终端通过snmpget可以查看运行时间:
# snmpget -v2c -c public localhost sysUpTime.0
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (727) 0:00:07.27
如果测试自己写的Agentx则需要在snmpd.conf中添加:
master agentx
重启snmpd服务:
# snmpd -f -Lo
Turning on AgentX master support.
NET-SNMP version 5.7.3
可以看到,比之前多了一行”Turning on AgentX master support.”提示,然后运行我们的agent代理。
因为我们的代码在编译的时候实际上用snmp的代码套用我们自己产生的代码,所以也可以使用和snmpd一样的参数:
# myswitch -f -Lo
NET-SNMP version 5.7.3 AgentX subagent connected
可以看到,程序已经链接到Agnetx代理。
在snmp.conf中添加自己定义的MIB库(添加的MIB库名字需要和放置到mibs目录中的名字一致):
mibs +MYBOARD-MIB
然后就可以使用
# snmptranslate -Tp -IR mySwitch
+--mySwitch(1)
+-- -R-- String sysName(1)
+-- -R-- String sysTempratrue1(2)
+-- -R-- String sysTemprature2(3)
+-- -R-- String sysHumidity1(4)
+-- -R-- String sysHumidity2(5)
+-- -RW- String sysTime(6)
+-- -RW- String sysThresAlarm(7)
+-- -R-- String sysThresRated(8)
+-- -RW- String sysThresDanger(9)
+-- -R-- String sysIrms(10)
+-- -R-- String sysModuleNum(11)
+-- -RW- String sysCutoffPolicy(12)
+-- -RW- String sysReset(13)
+-- -R-- String sysUrms(14)
+-- -RW- String sysUrmsMinAlarm(15)
+-- -RW- String sysUrmsMaxAlarm(16)
+-- -R-- String sysHWVersion(17)
+-- -R-- String sysSWVersion(18)
+-- -RW- String sysNetwork(19)
+-- -RW- String sysSNMP(20)
|
+--myboardModuleTable(28)
| |
| +--myboardModuleEntry(1)
| | Index: moduleIndex
| |
| +-- -R-- String moduleName(1)
| +-- -RW- String moduleThresAlarm(2)
| +-- -RW- String moduleThresRated(3)
| +-- -RW- String moduleThresDanger(4)
| +-- -R-- String moduleIrms(5)
| +-- -R-- String moduleSocketNum(6)
| +-- -RW- String moduleSocketInterval(7)
| +-- ---- Integer32 moduleIndex(255)
| Range: 1..6
|
+--myboardSocketTable(30)
|
+--myboardSocketEntry(1)
| Index: socketIndex
|
+-- -R-- String socketName(1)
+-- -R-- String socketType(2)
+-- -RW- String socketLoadLevel(3)
+-- -RW- String socketAlarm(4)
+-- -R-- String socketRated(5)
+-- -RW- String socketOnOffStatus(6)
+-- -R-- String socketOnOff(7)
+-- -R-- String socketLED(8)
+-- -R-- String socketUrms(9)
+-- -R-- String socketIrms(10)
+-- -R-- String socketActivePower(11)
+-- -R-- String socketReactivePower(12)
+-- -R-- String socketPf(13)
+-- -R-- String socketMaxActivePower(14)
+-- -R-- String socketEnergy(15)
+-- ---- Integer32 socketIndex(255)
Range: 1..24
查看自己添加的MIB库的树形定义,或者使用:
snmpwalk -v2c -c public localhost mySwitch
遍历自定义的MIB信息。
v3测试
error finding server identity keys
TLSTCP: Failed to create a SSL BIO
References
- http://www.net-snmp.org/
- http://www.net-snmp.org/wiki/index.php/TUT:Using_TLS
- http://www.net-snmp.org/wiki/index.php/Using_DTLS
- http://www.net-snmp.org/wiki/index.php/DTLS_Implementation_Notes
- https://www.openssl.org/
- http://openssl.6102.n7.nabble.com/cross-compiling-openssl-for-arm-td30536.html