准备工作
CentOS7
,
net-snmp-5.7.2
1.
snmp
安装及配置
snmp
安装snmp
- net-snmp: snmp服务端
- net-snmp-utils:snmp客户端工具集
- net-snmp-perl:snmp mib库编译工具
yum install -y net-snmp net-snmp-utils net-snmp-perl
配置snmp
...
#com2sec notConfigUser default public
# 配置允许访问agent的地址
com2sec notConfigUser 192.168.51.0/24 public
# 这两行注释
#view systemview included .1.3.6.1.2.1.1
#view systemview included .1.3.6.1.2.1.25.1.1
# 新增,表示可以查看所有的信息
view all included .1
view systemview included .1
# 修改read为all
access notConfigGroup "" any noauth exact all none none
...
启动并验证snmp
# 192.168.52.104
systemctl start snmpd
systemctl enable snmpd
# snmpwalk -v 2c -c public 192.168.51.104 sysName.0
SNMPv2-MIB::sysName.0 = STRING: work1
2.
mib
编写及加载
mib
自定义mib
# TEST-MIB.txt
-- TEST-MIB.txt
TEST-MIB DEFINITIONS ::= BEGIN
IMPORTS
OBJECT-GROUP, MODULE-COMPLIANCE, NOTIFICATION-GROUP
FROM SNMPv2-CONF
enterprises, Integer32, Unsigned32, OBJECT-TYPE, MODULE-IDENTITY,
NOTIFICATION-TYPE
FROM SNMPv2-SMI
DisplayString
FROM SNMPv2-TC;
Test MODULE-IDENTITY
LAST-UPDATED "202204061450Z" --必须以Z结尾
ORGANIZATION
""
CONTACT-INFO
""
DESCRIPTION
"Video's Server MIB."
::= { enterprises 30000 }
Time OBJECT IDENTIFIER ::= { Test 1 }
GetTime OBJECT-TYPE
SYNTAX DisplayString
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Example : 2022/4/6"
::= { Time 1 }
END
-- TEST-MIB.txt
移动TEST-MIB.txt到SNMP mibs
mv TEST-MIB.txt usr/share/snmp/mibs/
新建目录并将mib文件转化为c文件
mkdir mib_test
cd mib_test
env MIBS="+/usr/share/snmp/mibs/TEST-MIB.txt" mib2c Test
#执行完成会生成 Test.h Test.c
编译自定义mib
补全Test.c
/*
* Note: this file originally auto-generated by mib2c using
* $
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "Test.h"
/** Initializes the Test module */
void
init_Test(void)
{
const oid GetTime_oid[] = { 1,3,6,1,4,1,30000,1,1 };
DEBUGMSGTL(("Test", "Initializing\n"));
netsnmp_register_scalar(
netsnmp_create_handler_registration("GetTime", handle_GetTime,
GetTime_oid, OID_LENGTH(GetTime_oid),
HANDLER_CAN_RONLY
));
}
int
handle_GetTime(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
/* We are never called for a GETNEXT if it's registered as a
"instance", as it's "magically" handled for us. */
/* a instance handler also only hands us one request at a time, so
we don't need to loop over a list of requests; we'll only get one. */
time_t t;
switch(reqinfo->mode) {
case MODE_GET:
time(&t);
char szTime[100];
snprintf(szTime,100,"%s",ctime(&t));
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
/* XXX: a pointer to the scalar's data */
szTime,
/* XXX: the length of the data in bytes */
strlen(szTime));
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetTime\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
编写makefile
CC=gcc
FLAGS=-I. `net-snmp-config --cflags` -g
DLFLAGS=-shared -fPIC -g
Test.so: Test.c
$(CC) $(CFLAGS) $(DLFLAGS) -c -o Test.o Test.c
$(CC) $(CFLAGS) $(DLFLAGS) -o Test.so Test.o
.PHONY : clean
clean :
rm -f *.so *.o
执行编译
make makefile
# 执行完毕会生成Test.so
配置snmp加载Test.so
# 配置snmp /etc/snmp/snmpd.conf
# 最后添加
dlmod Test /root/aa/Test.so
重新启动snmp
systemctl restart snmpd
systemctl status snmpd #查看snmpd是否正常启动
3.验证自定义的
mib
是否生效
mib
snmpwalk -v 2c -c public localhost .1.3.6.1.4.1.30000.1.1
SNMPv2-SMI::enterprises.30000.1.1.0 = STRING: "Wed Apr 6 10:20:53 2022
"
4.
mib
的添加方式
mib
常见的snmp添加自定义mib库的方式有以下3种
静态加载
将生成的.c和.h文件加入到相应的位置,重新编译snmp库,优点是不需要修改配置文件,缺点是每次添加都得重新编译,方式如下。
# 把Test.c和Test.h复制到net-snmp源码中 net-snmp-5.7.2/agent/mibgroup,然后编译安装
./configure --prefix=/usr/local/snmp --with-mib-modules=Test,make && make install
动态加载
将生成的.c和.h文件再编译成.so库,修改snmpd.conf配置文件,优点是每次添加不需要重新编译,缺点是必须支持dlmod命令。
子代理扩展
将生成的.c和.h文件编译成可执行程序,运行该程序和snmpd即可,优点是操作简单,缺点是需要运行两个程序才行,方式如下。
# 生成Test程序:
net-snmp-config --compile-subagent Test Test.c
# 启动snmpd,Test
systemctl start snmpd
./Test
版权声明:本文为yiyu89原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。