NetSNMP开源代码学习——mib扩展

时间:2023-03-09 06:25:32
NetSNMP开源代码学习——mib扩展

扩展MIB库
关于MIB库的扩展网络文章非常多,这里我主要参考了http://blog.****.net/qq_27204267/article/details/51595708,这篇文章介绍的比较简单,流程清晰,内容全面,没有太多的理论讲解。
闲言少叙,参考前人经验直接在我的环境里来个“傻瓜”操作并记录步骤,防止忘记。哎,年纪大了健忘啊^^^^^^同时为了后续的移植操作和深入分析做基础。
1. 编译MIB库文件

一. 编译MIB库文件

-- Test-MIB.my
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; -- October , at : GMT
-- 1.3.6.1.4.1.
Test MODULE-IDENTITY
LAST-UPDATED "200210091450Z" -- October , at : GMT
ORGANIZATION
""
CONTACT-INFO
""
DESCRIPTION
"Video's Server MIB."
::= { enterprises } -- Node definitions
-- This part will include all details about the Test.
-- 1.3.6.1.4.1.16535.1
Time OBJECT IDENTIFIER ::= { Test } -- 1.3.6.1.4.1.16535.1.
GetTime OBJECT-TYPE
SYNTAX DisplayString (SIZE (..))
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Example : 2013/4/11"
::= { Time }
END -- Test-MIB.my

二. 生成源代码并修改实现的函数

/*
* 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"
#include <time.h> /** Initializes the Test module */
void
init_Test(void)
{
const oid GetTime_oid[] = { , , , , , , , , }; 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[];
snprintf(szTime,,"%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;
}

三. 静态库编译安装

./configure --prefix=/usr/local/net-snmp --with-default-snmp-version=2c --with-sys-contact="xxx@163.com" --with-sys-location="China" --with-logfile="/var/log/snmpd.log" --with-persistent-directory=/var/net-snmp/ --enable-applications --enable-ipv6 --with-out-transports="TCP TCPv6 Unix" --with-transports="Callback UDP UDPIPv6"  --disable-nls --enable-shared --enable-static --disable-embedded-perl --with-mib-modules=Test

将.c 和.h文件移动到源码目录下,我的是/opt/net-snmp-5.7.3/agent/mibgroup。
运行configure命令,并且添加--with-mib-modules=Test,进行MIB库文件的配置。
运行make && make install编译安装
测试:
执行命令:snmpget -v2c -c public localhost Test-MIB:GetTime.0测试

四. 生成共享库编译
具体编译命令为:

gcc -I `net-snmp-config --cflags` -fPIC -shared -g -O0 -o Test.so Test.c  `net-snmp-config --libs`

修改snmpd.conf,添加一行:
dlmod Test /usr/local/net-snmp/share/snmp/mibs/Test.so
测试:
重新configure(不要--with-mib-modules=Test),make,make install,安装snmpd程序
执行命令:snmpget -v2c -c public localhost Test-MIB:GetTime.0测试