snmp总结三:net-snmp

snmp总结三:net-snmp

本实例测试版本 net-snmp-5.8,开发主机ubuntu已安装 net-snmp。

1 交叉编译

交叉编译时对功能进行一定的裁剪,编译脚本示例如下所示:

#!/bin/sh

HOME=$(pwd)
PREFIX_DIR=$HOME/install
COMPILE_HEAD=/opt/imx6ull_toolchain/bin/arm-none-linux-gnueabi-

# 编译snmp
make_snmp()
{
	cd $HOME
	rm -rf net-snmp-5.8
	tar zxvf net-snmp-5.8.tar.gz
	cd net-snmp-5.8

	./configure --prefix=$PREFIX_DIR --host=arm-linux --target=arm-linux --build=i686-linux \
	--with-cc=${COMPILE_HEAD}gcc  --with-ar=${COMPILE_HEAD}ar --with-endianness=little --disable-embedded-perl \
	LDFLAGS=-L$PREFIX_DIR/lib CFLAGS=-I$PREFIX_DIR/include \
	--disable-perl-cc-checks --without-perl-modules --disable-applications --disable-manuals --disable-scripts --disable-ipv6 \
	--with-default-snmp-version="3" --with-sys-contact="fiber" --with-sys-location="china" --with-logfile="/var/log/snmpd.log" \
	--with-persistent-directory="/var/net-snmp"
	make clean
	make
if [ $? == 0 ]; then
    make install
fi

	cd $HOME
	rm -rf net-snmp-5.8
}

make_snmp
编译完成后将相关动态库、snmpd可执行程序拷贝到目标板。

snmpd -v

NET-SNMP version:  5.8
Web:               http://www.net-snmp.org/
Email:             net-snmp-coders@lists.sourceforge.net

2 snmpd.conf

SNMPv2c基础配置示例

# snmp 代理协议及端口
agentAddress udp:6161
view all included .1
# 读写公共体
rwcommunity public
# 动态加载自定义mib库
dlmod hybrid_scalar /home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.so
dlmod hybrid_table /home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.so
# SNMPv2c trap配置 
trap2sink 192.168.1.108:6162 public
trap2sink 192.168.1.109:6162 public
trap2sink 192.168.1.111:6162 public

SNMPv3基础配置示例

# snmp 代理协议及端口
agentAddress udp:6161
view all included .1
# 动态加载自定义mib库
dlmod hybrid_scalar /home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.so
dlmod hybrid_table /home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.so

# SNMPv3 配置
# 定义用户访问权限(读写) snmpuser
rwuser snmpuser 
# 创建 snmpuser 用户,认证方式MD5,加密算法DES
createUser snmpuser MD5 auth123456 DES encrypt123456
# SNMPv3 trap配置
trapsess -v 3 -u snmpuser -a MD5 -A auth123456 -x DES -X encrypt123456 -l authPriv 192.168.1.108:6162

3 动态支持拓展 MIB

使用 snmp 管理设备时通常厂家都会为设备自定义拓展 MIB。拓展 MIB 的支持有好几种方式,这里选用以动态库的方式进行拓展。

使用测试的 hybrid.mib。

HYBRID-MIB DEFINITIONS ::= BEGIN		--MIB文件开始

IMPORTS									--模块数据类型引用区域
	OBJECT-GROUP, NOTIFICATION-GROUP
		FROM SNMPv2-CONF
	enterprises, Integer32, OBJECT-TYPE, MODULE-IDENTITY, OBJECT-IDENTITY, Unsigned32,
	NOTIFICATION-TYPE
		FROM SNMPv2-SMI
	TEXTUAL-CONVENTION, DisplayString
		FROM SNMPv2-TC;
	-- PMU
	-- FROM FH-SMI;					
	-- 网能:电源系统监控单元


-- 1.3.6.1.4.1.9999.1 根节点
HybridPower MODULE-IDENTITY
	LAST-UPDATED "202208250000Z"
	ORGANIZATION "Fiberhome."
	CONTACT-INFO 
		"Tel: +86 186xxxxxxx
		E-mail: xxxxxxx@fiberhome.com"
	DESCRIPTION 
		"Hybrid power system example mib file."
	::= { enterprises 9999 }

--
-- 节点定义
--

-- 简单标量
-- 1.3.6.1.4.1.9999.1
exampleObject OBJECT-IDENTITY
	STATUS current
	DESCRIPTION 
		"Example Object"
	::= { HybridPower 1 }

-- 1.3.6.1.4.1.9999.1.1
exampleObject1 OBJECT-TYPE
	SYNTAX Integer32
	MAX-ACCESS read-only
	STATUS current
	DESCRIPTION
		"Example:Integer32 read-only."
	::= { exampleObject 1 }

-- 1.3.6.1.4.1.9999.1.2
exampleObject2 OBJECT-TYPE
	SYNTAX Integer32
	MAX-ACCESS read-write
	STATUS current
	DESCRIPTION
		"Example:Integer32 read-write."
	::= { exampleObject 2 }

-- 1.3.6.1.4.1.9999.1.3
exampleObject3 OBJECT-TYPE
	SYNTAX OCTET STRING
	MAX-ACCESS read-write
	STATUS current
	DESCRIPTION
		"Example:String."
	::= { exampleObject 3 }


-- 简单表类型,用户列表
-- 1.3.6.1.4.1.9999.2
userTable OBJECT-TYPE
	SYNTAX SEQUENCE OF UserEntry
	MAX-ACCESS read-create
	STATUS current
	DESCRIPTION
		"userTable"
	::= { HybridPower 2 }

-- 1.3.6.1.4.1.9999.2.1
userEntry OBJECT-TYPE
	SYNTAX UserEntry
	MAX-ACCESS read-create
	STATUS current
	DESCRIPTION
		"userEntry."
	INDEX { userIdx }
	::= { userTable 1 }

UserEntry ::= SEQUENCE {
	userIdx				Integer32,
	userName			OCTET STRING,
	userAge				Integer32
	}

-- 1.3.6.1.4.1.9999.2.1.1
userIdx OBJECT-TYPE
	SYNTAX Integer32 (1..100)
	MAX-ACCESS read-only
	STATUS current
	DESCRIPTION
		"User index."
	::= { userEntry 1 }


-- 1.3.6.1.4.1.9999.2.1.2
userName OBJECT-TYPE
	SYNTAX OCTET STRING
	MAX-ACCESS read-write
	STATUS current
	DESCRIPTION
		"User name."
	::= { userEntry 2 }

-- 1.3.6.1.4.1.9999.2.1.3
userAge OBJECT-TYPE
	SYNTAX Integer32
	MAX-ACCESS read-write
	STATUS current
	DESCRIPTION
		"User age."
	::= { userEntry 3 }



-- trap 测试
-- 1.3.6.1.4.1.9999.3
hibridNotifications OBJECT-IDENTITY
	STATUS current
	DESCRIPTION 
		"notification tree."
	::= { HybridPower 3 }

-- 1.3.6.1.4.1.9999.3.1
RecNotify NOTIFICATION-TYPE
	OBJECTS     { RecCur, RecVol }
	STATUS current
	DESCRIPTION 
		"Description."
	::= { hibridNotifications 1 }


-- trap 通知对象
-- 1.3.6.1.4.1.9999.4
notificationObjs OBJECT IDENTIFIER
		::= { HybridPower 4 }
		
-- 1.3.6.1.4.1.9999.4.1
RecCur OBJECT-TYPE
	SYNTAX Integer32
	MAX-ACCESS accessible-for-notify
	STATUS current
	DESCRIPTION
		"Rectifier current."
	::= { notificationObjs 1 }
	
-- 1.3.6.1.4.1.9999.4.2
RecVol OBJECT-TYPE
	SYNTAX Integer32
	MAX-ACCESS accessible-for-notify
	STATUS current
	DESCRIPTION
		"Rectifier voltage."
	::= { notificationObjs 2 }

END

3.1 mib2c 生成 c 程序

使用 mib2c 程序生成 c 程序,命令示例如下:

# 生成标量程序
env MIBS="+/home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.mib"  mib2c -c mib2c.scalar.conf -f hybrid_scalar .1.3.6.1.4.1.9999
# 生成trap程序
env MIBS="+/home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.mib"  mib2c -c mib2c.notify.conf -f hybrid_notify .1.3.6.1.4.1.9999
# 生成表格程序
env MIBS="+/home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.mib"  mib2c -c mib2c.iterate.conf -f hybrid_table .1.3.6.1.4.1.9999

3.2 动态库支持

编写 makefile 生成 hybrid.so

CC := gcc
CFLAGS := -I. `net-snmp-config --cflags` -g -std=gnu99
LDFLAGS := -shared -fPIC

TAGET_NAME = hybrid

all: $(TAGET_NAME).so
$(TAGET_NAME).so:
	$(CC) $(CFLAGS) $(LDFLAGS) -o $(TAGET_NAME).so hybrid_scalar.c hybrid_table.c hybrid_notify.c

.PHONY : clean
clean :
	rm -f *.so *.o

修改 snmpd.conf 配置文件,在末尾添加

# snmp 代理协议及端口
agentAddress udp:6161
view all included .1
# 读写公共体
rwcommunity public
# 动态加载自定义mib库
dlmod hybrid_scalar /home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.so
dlmod hybrid_table /home/wanghuan/wh_tools/snmp/hybrid-snmp/hybrid.so
# SNMPv2c trap配置 
trap2sink 192.168.1.108:6162 public

运行 snmpd

sudo snmpd -f -Lo --master=agentx -Dagentx,dlmod -c /home/wanghuan/wh_tools/snmp/snmpd.conf

4 snmp测试

代理端运行后,我们通过 MIB Browser 工具加载 hybrid.mib 对其进行简单的 get、set操作测试。

get测试

set测试

表格查询

posted @ 2025-06-19 17:32  silencehuan  阅读(200)  评论(0)    收藏  举报