1、参考链接
1)Net-snmp添加子代理示例
2、操作步骤
1)网络拷贝下面的文件
http://www.net-snmp.org/tutorial/tutorial-5/toolkit/mib_module/NET-SNMP-TUTORIAL-MIB.txt
http://www.net-snmp.org/tutorial/tutorial-5/toolkit/demon/nstAgentSubagentObject.c
http://www.net-snmp.org/tutorial/tutorial-5/toolkit/demon/nstAgentSubagentObject.h
2)使用net-snmp-config 生成中间代码,然后使用gcc来编译它
./net-snmp-config --compile-subagent mysubagent nstAgentSubagentObject.c
报错:
root@sun-PC:~/share/work/tools/snmpd/mibs# ./net-snmp-config --compile-subagent mysubagent nstAgentSubagentObject.c generating the temporary code file: netsnmptmp.3261.c void init_nstAgentSubagentObject(void); checking for init_nstAgentSubagentObject in nstAgentSubagentObject.c init_nstAgentSubagentObject(void) checking for shutdown_nstAgentSubagentObject in nstAgentSubagentObject.c running: arm-marvell-linux-gnueabi-gcc -fno-strict-aliasing -g -O2 -Ulinux -Dlinux=linux -I. -I/usr/local/include -o mysubagent netsnmptmp.3261.c nstAgentSubagentObject.c -L/usr/local/lib -lnetsnmpmibs -lnetsnmpagent -lnetsnmp -lnetsnmpmibs -ldl -lnetsnmpagent -lnetsnmp -lrt netsnmptmp.3261.c:2:38: error: net-snmp/net-snmp-config.h: No such file or directory netsnmptmp.3261.c:6:40: error: net-snmp/net-snmp-includes.h: No such file or directory netsnmptmp.3261.c:7:52: error: net-snmp/agent/net-snmp-agent-includes.h: No such file or directory netsnmptmp.3261.c:21: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'stop_server' netsnmptmp.3261.c: In function 'usage': netsnmptmp.3261.c:37: warning: incompatible implicit declaration of built-in function 'fprintf' netsnmptmp.3261.c:37: error: 'stderr' undeclared (first use in this function) netsnmptmp.3261.c:37: error: (Each undeclared identifier is reported only once
已经安装了net-snmp-devel,/usr/include/net-snmp/ 路径下也有相关的头文件。
我们重新定义头文件路径:
export C_INCLUDE_PATH="/home/sun/share/work/tools/snmpd/net-snmp-5.7.3/include" export LDFLAGS="/home/sun/share/work/tools/snmpd/net-snmp-5.7.3/include/net-snmp/library"
3)再次编译依然报错
generating the temporary code file: netsnmptmp.32646.c void init_nstAgentSubagentObject(void); checking for init_nstAgentSubagentObject in ./nstAgentSubagentObject.c init_nstAgentSubagentObject(void) checking for shutdown_nstAgentSubagentObject in ./nstAgentSubagentObject.c running: arm-marvell-linux-gnueabi-gcc -fno-strict-aliasing -g -O2 -Ulinux -Dlinux=linux -I. -I/usr/local/include -o mysubagent netsnmptmp.32646.c ./nstAgentSubagentObject.c -L/usr/local/lib -lnetsnmpmibs -lnetsnmpagent -lnetsnmp -lnetsnmpmibs -ldl -lnetsnmpagent -lnetsnmp -lrt /home/sun/share/work/tools/arm-linux-gcc/armv7-marvell-linux-gnueabi-softfp/bin/../lib/gcc/arm-marvell-linux-gnueabi/4.4.5/../../../../arm-marvell-linux-gnueabi/bin/ld: cannot find -lnetsnmpmibs /home/sun/share/work/tools/arm-linux-gcc/armv7-marvell-linux-gnueabi-softfp/bin/../lib/gcc/arm-marvell-linux-gnueabi/4.4.5/../../../../arm-marvell-linux-gnueabi/bin/ld: cannot find -lnetsnmpagent /home/sun/share/work/tools/arm-linux-gcc/armv7-marvell-linux-gnueabi-softfp/bin/../lib/gcc/arm-marvell-linux-gnueabi/4.4.5/../../../../arm-marvell-linux-gnueabi/bin/ld: cannot find -lnetsnmp /home/sun/share/work/tools/arm-linux-gcc/armv7-marvell-linux-gnueabi-softfp/bin/../lib/gcc/arm-marvell-linux-gnueabi/4.4.5/../../../../arm-marvell-linux-gnueabi/bin/ld: cannot find -lnetsnmpmibs /home/sun/share/work/tools/arm-linux-gcc/armv7-marvell-linux-gnueabi-softfp/bin/../lib/gcc/arm-marvell-linux-gnueabi/4.4.5/../../../../arm-marvell-linux-gnueabi/bin/ld: cannot find -lnetsnmpagent /home/sun/share/work/tools/arm-linux-gcc/armv7-marvell-linux-gnueabi-softfp/bin/../lib/gcc/arm-marvell-linux-gnueabi/4.4.5/../../../../arm-marvell-linux-gnueabi/bin/ld: cannot find -lnetsnmp collect2: ld returned 1 exit status
6)上述报错信息是缺少动态 .so库文件;
7)net-snmp5.7.3 路径下没有发现 .so文件。可能是没有编译 net-snmp-devel 这个的原因,下载编译,找不到源码包。
8)按照Ubuntu下的net-snmp-devel 工具编译,查找库文件路径: /usr/lib/x86_64-linux-gnu
libsnmp.so -> libsnmp.so.30.0.3 libsnmp.so.30 -> libsnmp.so.30.0.3 libnetsnmpagent.so -> libnetsnmpagent.so.30.0.3 libnetsnmpagent.so.30 -> libnetsnmpagent.so.30.0.3
9)编译snmpd的时候加上了 --prefix 选项
./configure --host=arm-marvell-linux-gnueabi --with-cc=arm-marvell-linux-gnueabi-gcc --with-ar=arm-marvell-linux-gnueabi-ar --disable-shared --disable-scripts --with-endianness=little --disable-ipv6 --disable-manuals --disable-ucd-snmp-compatibility --enable-as-needed --disable-embedded-perl --prefix=/home/sun/share/work/tools/bin/snmp
在安装的路径下面重新执行 net-snmp-devel 操作,编译完成;
10)执行以下测试命令
snmpd -f -Le -d -c /etc/snmpd/snmpd.conf -M /etc/snmpd/mibs --rwcommunity=public --master=agentx --rwcommunity=public --master=agentx &
./mysubagent -f -Lo &
本地运行mysubagent 会报以下错误信息
Warning: Failed to connect to the agentx master agent ([NIL]):
11)snmpd.conf增加以下内容(和前面命令中的--master=agentx应该是一个作用)
master agentx
master 是说该snmpd以主代理方式运行,目前主代理snmpd只支持agentx类型,而我们要开发的程序是一种子代理(subagent),是需要连snmpd的master agent的。
12)本地运行 snmpget
./snmpget -v 2c -M /etc/snmpd/mibs -c public localhost NET-S NMP-TUTORIAL-MIB::nstAgentSubagentObject.0 snmpget: Unknown host (localhost) (Resource temporarily unavailable)
服务器运行 snmpget/snmpwalk
snmpget -v2c -c public 10.13.113.205 NET-SNMP-TUTORIAL-MIB::nstAgentSubagentObject.0 NET-SNMP-TUTORIAL-MIB::nstAgentSubagentObject.0: Unknown Object Identifier
本地运行snmpget 报错的原因应该是没有运行snmp服务;
远端snmpd服务器获取模块失败的原因是没有NET-SNMP-TUTORIAL-MIB 文件;
13)将上面的NET-SNMP-TUTORIAL-MIB.txt 文件拷贝到 /home/sun/.snmp/mibs 目录中,Ubuntu中执行命令成功
root@# snmpget -v 2c -c public 10.13.113.205 NET-SNMP-TUTORIAL-MIB::nstAgentSubagentObject.0 Bad operator (html): At line 128 in /home/sun/.snmp/mibs/NET-SNMP-TUTORIAL-MIB.txt NET-SNMP-TUTORIAL-MIB::nstAgentSubagentObject.0 = INTEGER: 2
3、整理一下子代理模式snmpd需要的程序和配置文件完整收录
1)需要的程序和文件
snmpd 程序、snmpd.conf配置文件、编译的子代理程序 xx;
新增子代理接口文件:XX-MIB.txt。放在snmp服务器侧;
2)运行脚本和命令
agent 端(注意:使用子代理模式,snmpd程序不能编译 mini-agent选项)
snmpd -f -Le -d -c /etc/snmpd/snmpd.conf & ./mysubagent -f -Lo & snmpd -f -c /etc/snmpd/snmpd.conf & ##不指定mibs文件会报错误,但是不影响 ./mysubagent -f &
服务器端
snmpget -v 2c -c public 10.13.113.205 NET-SNMP-TUTORIAL-MIB::nstAgentSubagentObject.0
3)尝试压缩snmpd的体积(去除MIB接口,保留子代理模式)。