1. 平台信息

Linux:VMware? Workstation 12 Pro + Ubuntu 12.04 64bit

ARM:ZLG EPC-M6G2C 工控主板

涉及工具:SecureCRT(串口终端)、SugarNMSTool 智和网管软件

交叉工具链:arm-linux-gnueabihf-gcc (gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux)

SNMP源码版本:net-snmp-5.7.3

2. SNMP协议

SNMP:“简单网络管理协议”,用于网络管理的协议。SNMP用于网络设备的管理。SNMP的工作方式:管理员需要向设备获取数据,所以SNMP提供了“读”操作;管理员需要向设备执行设置操作,所以SNMP提供了“写”操作;设备需要在重要状况改变的时候,向管理员通报事件的发生,所以SNMP提供了“Trap”操作。

更多:https://blog.csdn.net/bbwangj/article/details/80981098

3. 移植和交叉编译SNMP

3.1 下载源码

官网下载地址:http://www.net-snmp.org/download.html

本次下载的是5.7.3版本:https://sourceforge.net/projects/net-snmp/files/net-snmp/5.7.3/

选择下载压缩包:net-snmp-5.7.3.tar.gz

在指定目录下,解压缩源码包:

1

root@Linux-host:~/hu/SNMP# tar -xzvf net-snmp-5.7.3.tar.gz

3.2 配置configure,生成.config文件

snmp的configure配置,可以使用./configure --help查看一下配置选项,具体如下表所示:

选项 说明
–host=arm-linux 运行平台
–target=arm-linux 目标平台
–build=i686-linux 编译平台
–with-cc=arm-linux-gcc 交叉编译工具
–with-ar=arm-linux-ar 使用的打包工具
–prefix=/usr/local/net-snmp 安装目录
–disable-shared 不编译共享库
–disable-scripts 不要安装mib2c等脚本
–with-endianness=little 指定小端模式
-enable-mini-agent 最小化构建agent
--disable-deprecated 不编译弃用的功能
--without-logfile 指定snmpd不输出日志文件(可以使用--with-logfile指定默认日志文件位置)
--disable-minimalist 删除所有非基本的代码功能
–enable-debugging 打开调试信息
--disable-testing-code 不使用测试代码(某些代码不被使用)
–with-openssl=/opt/hardhat openssl库路径(用于支持加密等)
–disable-ipv6 不使用IPv6
–disable-manuals 不安装manpage说明页
–disable-ucd-snmp-compatibility 不需要兼容ucd-snmp
–disable-snmptrapd-subagent 不用支持snmptrapd的子代理
–disable-embedded-perl 在SNMP代理和snmptrapd禁用嵌入式Perl。默认启用
–disable-applications 是否关闭snmpget等功能,根据自己的需要选择
--with-default-snmp-version="3" 指定默认协议版本
--enable-as-needed 仅链接需要库,不链接不必要的库(如果只使用其他方法行不通,链接libperl针对应用而非Net-SNMP库。)

对于交叉编译,我的配置如下:

1
2
3
4
5

root@Linux-host:~/hu/SNMP/net-snmp-5.7.3# ./configure --host=arm-linux \
--build=i686-linux --with-cc=arm-linux-gnueabihf-gcc \
--with-ar=arm-linux-gnueabihf-ar --with-endianness=little \
--disable-ipv6 --disable-embedded-perl --disable-shared  \
--prefix=/root/hu/SNMP/arm-snmp

其中

--with-cc=arm-linux-gnueabihf-gcc需要自己根据情况而定,

--disable-ipv6 取消ipv6的支持

--disable-embedded-perl 不加这一配置,会报错找不到perl库,如下提示:

1
2
3

/usr/bin/ld: cannot find -lperl
collect2: error: ld returned 1 exit status
make[1]: *** [libnetsnmpagent.la] 错误 1

也可以安装相关库:

1

sudo apt-get install libperl-dev

没有出错时,最后提示信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

...

---------------------------------------------------------
            Net-SNMP configuration summary:
---------------------------------------------------------

  SNMP Versions Supported:    1 2c 3
  Building for:               linux
  Net-SNMP Version:           5.7.3
  Network transport support:  Callback Unix Alias TCP UDP IPv4Base SocketBase TCPBase UDPIPv4Base UDPBase
  SNMPv3 Security Modules:     usm
  Agent MIB code:            default_modules =>  snmpv3mibs mibII ucd_snmp notification notification-log-mib target agent_mibs agentx disman/event disman/schedule utilities host
  MYSQL Trap Logging:         unavailable
  Embedded Perl support:      disabled
  SNMP Perl modules:          disabled
  SNMP Python modules:        disabled
  Crypto support from:        internal
  Authentication support:     MD5 SHA1
  Encryption support:         DES AES
  Local DNSSEC validation:    disabled

---------------------------------------------------------

3.3 编译安装

编译:

1
2
3
4
5

root@Linux-host:~/hu/SNMP/net-snmp-5.7.3# make

...
chmod a+x net-snmp-config
touch net-snmp-config-x

用arm-linux-gnueabihf-strip工具可以对生成的snmpd进行瘦身:

1
2
3
4
5
6
7

root@Linux-host:~/hu/SNMP/net-snmp-5.7.3# ll agent/snmpd
-rwxr-xr-x 1 root root 6528914  7月  2 15:07 agent/snmpd*

root@Linux-host:~/hu/SNMP/net-snmp-5.7.3# arm-linux-gnueabihf-strip agent/snmpd

root@Linux-host:~/hu/SNMP/net-snmp-5.7.3# ll agent/snmpd
-rwxr-xr-x 1 root root 1336548  7月  2 15:08 agent/snmpd*

由上可以看出,snmpd由6M多变为1.3M

安装:

1

root@Linux-host:~/hu/SNMP/net-snmp-5.7.3# make install

之后可以在指定的目录下得到安装后的文件。

1
2
3

root@Linux-host:~/hu/SNMP/net-snmp-5.7.3# cd ../arm-snmp/
root@Linux-host:~/hu/SNMP/arm-snmp# ls
bin  include  lib  sbin  share

3.4 配置文件snmp.conf的修改

先将示例的配置文件拷贝到安装目录下:

root@Linux-host:~/hu/SNMP/net-snmp-5.7.3# cp EXAMPLE.conf ../arm-snmp/share/snmp/snmpd.conf

后进行修改:

root@Linux-host:~/hu/SNMP/arm-snmp# vim share/snmp/snmpd.conf

修改1:打开对外udp:161端口

# Listen for connections from the local system only
#agentAddress udp:127.0.0.1:161 #注释掉
# Listen for connections on all interfaces (both IPv4 *and* IPv6)
#agentAddress udp:161,udp6:[::1]:161
agentAddress udp:161 #添加

修改2:选择SNMP协议版本

#
# ACTIVE MONITORING
#

# send SNMPv1 traps
#trapsink localhost public
# send SNMPv2c traps
trap2sink localhost public
# send SNMPv2c INFORMs
#informsink localhost public

修改3:设置访问权限

#
# ACCESS CONTROL
#

# system + hrSystem groups only
view systemonly included .1.3.6.1.2.1.1
view systemonly included .1.3.6.1.2.1.25.1

# Full access from the local host
#rocommunity public localhost
# Default access to basic system info
# rocommunity public default -V systemonly
rocommunity public default

4. 移植到目标板

4.1指令流程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

[root@EPC-M6G2C opt]# mkdir SNMP/
[root@EPC-M6G2C opt]# cd SNMP/
[root@EPC-M6G2C SNMP]# cp /media/mmcblk0p1/arm-net-snmp-5.7.3.tar ./
[root@EPC-M6G2C SNMP]# tar -xvf arm-net-snmp-5.7.3.tar

[root@EPC-M6G2C SNMP]# ls
arm-net-snmp-5.7.3/     arm-net-snmp-5.7.3.tar* readme.txt*
[root@EPC-M6G2C SNMP]# cd arm-net-snmp-5.7.3/
[root@EPC-M6G2C arm-net-snmp-5.7.3]# wr cp sbin/snmpd /usr/sbin/
[root@EPC-M6G2C arm-net-snmp-5.7.3]# wr cp -r share/ /usr/local/
[root@EPC-M6G2C arm-net-snmp-5.7.3]# ll /usr/sbin/snmpd
-rwxr-xr-x    1 root     root        1.3M Jul  2 07:36 /usr/sbin/snmpd*
[root@EPC-M6G2C arm-net-snmp-5.7.3]# ll /usr/local/share/snmp/
total 8
drwxr-xr-x    3 root     root         296 Jul  2 07:36 ./
drwxr-xr-x    3 root     root         224 Jul  2 07:36 ../
drwxr-xr-x    2 root     root        5.1K Jul  2 07:36 mibs/
-rw-r--r--    1 root     root        6.9K Jul  2 07:36 snmpd.conf

4.2 运行snmpd

snmpd命令选项:

1
2
3
4
5
6
7
8
9
10
11
12

注:snmpd命令的有用选项
-c FILE 指定文件为配置文件
-C 不读取默认的配置文件
-d dump接收和发送SNMP数据包
-D TOKEN 对于给定的TOKEN(标志)打开调试信息 ( -Dmib_init)
-I [-]INITLIST 对于要初始化的MIB列表显示
-M DIRLIST 指定MIB库的路径
-V 显示详细信息
-Le 把错误信息输出到日志中
-Lf FILE 把错误信息输出到指定文件中
-m MIBLIST use MIBLIST instead of the default MIB list
-f                    do not fork from the shell

依次可执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

[root@EPC-M6G2C arm-net-snmp-5.7.3]# snmpd -c /usr/local/share/snmp/snmpd.conf -
f -Le -d -M /usr/local/share/snmp/mibs/
Turning on AgentX master support.
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf
Failed to create the persistent directory for /var/net-snmp/snmpd.conf
read_config_store open failure on /var/net-snmp/snmpd.conf

Sending 44 bytes to UDP: [127.0.0.1]:162->[0.0.0.0]:0
0000: 30 2A 02 01  00 04 06 70  75 62 6C 69  63 A4 1D 06    0*.....public...
0016: 0A 2B 06 01  04 01 BF 08  03 02 0A 40  04 C0 A8 0A    .+.........@....
0032: 74 02 01 00  02 01 00 43  01 62 30 00                 t......C.b0.

NET-SNMP version 5.7.3

Sending 1 bytes to callback: 2 on fd 5
0000: 00                                                    .


Received 1 byte packet from callback: 1 on fd 3
0000: 00                                                    .


Sending 1 bytes to callback: 1 on fd 3
0000: 00                                                    .


Received 1 byte packet from callback: 2 on fd 5
0000: 00                                                    .


Sending 1 bytes to callback: 2 on fd 5
0000: 00                                                    .


Received 1 byte packet from callback: 1 on fd 3
0000: 00                                                    .


Sending 1 bytes to callback: 1 on fd 3
0000: 00                                                    .

可以看出运行正常,有发送和接受的udp数据包。

4.3 注意事项

4.3.1 iptables 防火墙对外开放udp:161端口访问权限

1

iptables -I INPUT -p udp --dport 161 -j ACCEPT

可用命令查看当前情况:

iptables -L

4.3.2 确保udp:161端口不被占用(161是snmp默认端口,一般情况下不会被其他占用)

可用命令查看端口情况:

netstat -an |grep 161

4.3.3 mibs文件路径可以不用指定,运行也没有问题,但是snmpd.conf配置文件路径必需要指定。

5. snmp测试

明天再写,先下班。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐