> 文章列表 > snmp 自定义子代理mib库

snmp 自定义子代理mib库

snmp 自定义子代理mib库

 测试环境:centos8

1、安装软件

yum install -y net-snmp net-snmp-utils 
yum install -y net-snmp-perl net-snmp-devel net-snmp-libs

2、创建用户

net-snmp-create-v3-user
输入用户名 soft
输入密码 123456
输入密码 654321

service snmpd restart

3、创建mib文件

cd /usr/local/share/snmp/mibs

vim TEST-MIB.txt

--开始
TEST-MIB DEFINITIONS ::= BEGIN--引入部分
IMPORTSDisplayString,TestAndIncr,PhysAddress,MacAddress,DateAndTime,RowStatus,TEXTUAL-CONVENTIONFROM SNMPv2-TCenterprisesFROM RFC1155-SMIMODULE-IDENTITY,OBJECT-TYPE,NOTIFICATION-TYPE,Integer32,IpAddress,TimeTicks,Unsigned32FROM SNMPv2-SMI;
--引用结束,用分号--定义节点
--enterprises的OID是1.3.6.1.4
testMib    OBJECT IDENTIFIER ::= { enterprises 20001 }system OBJECT IDENTIFIER ::= { testMib  1 }--set节点
systemAlarmSet OBJECT IDENTIFIER ::= { system 3 }setMemThreshold  OBJECT-TYPE
SYNTAX      TestAndIncr
MAX-ACCESS  read-write
STATUS      current
DESCRIPTION "set cpu Threshold"
::= { systemAlarmSet 1 }setCpuThreshold OBJECT-TYPE
SYNTAX      DisplayString
MAX-ACCESS  read-write
STATUS      current
DESCRIPTION "set cpu Threshold"
::= { systemAlarmSet 2}--结束定义
END

生成.c和.h文件

sudo env MIBS="+/usr/share/snmp/mibs/TEST-MIB.txt" mib2c testMib

选择
2
1
y

查看mib树

snmptranslate -Tp -IR TEST-MIB::testMib

修改.c文件

vim testMib.c

/ Note: this file originally auto-generated by mib2c* using mib2c.scalar.conf*/#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "testMib.h"long  value = 0;
char *value2 = 0;/ Initializes the testMib module */
void
init_testMib(void)
{const oid setMemThreshold_oid[] = { 1,3,6,1,4,1,20001,1,3,1 };const oid setCpuThreshold_oid[] = { 1,3,6,1,4,1,20001,1,3,2 };DEBUGMSGTL(("testMib", "Initializing\\n"));netsnmp_register_scalar(netsnmp_create_handler_registration("setMemThreshold", handle_setMemThreshold,setMemThreshold_oid, OID_LENGTH(setMemThreshold_oid),HANDLER_CAN_RWRITE));netsnmp_register_scalar(netsnmp_create_handler_registration("setCpuThreshold", handle_setCpuThreshold,setCpuThreshold_oid, OID_LENGTH(setCpuThreshold_oid),HANDLER_CAN_RWRITE));
}int
handle_setMemThreshold(netsnmp_mib_handler *handler,netsnmp_handler_registration *reginfo,netsnmp_agent_request_info   *reqinfo,netsnmp_request_info         *requests)
{int ret;/* 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, sowe don't need to loop over a list of requests; we'll only get one. */switch(reqinfo->mode) {case MODE_GET:snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,(u_char *) &value/* XXX: a pointer to the scalar's data */,sizeof(value)/* XXX: the length of the data in bytes */);break;/ SET REQUEST multiple states in the transaction.  See:* http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg*/case MODE_SET_RESERVE1:/* or you could use netsnmp_check_vb_type_and_size instead */ret = netsnmp_check_vb_type(requests->requestvb, ASN_INTEGER);if ( ret != SNMP_ERR_NOERROR ) {netsnmp_set_request_error(reqinfo, requests, ret );}break;case MODE_SET_RESERVE2:/* XXX malloc "undo" storage buffer */if (0/* XXX if malloc, or whatever, failed: */) {netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_RESOURCEUNAVAILABLE);}break;case MODE_SET_FREE:/* XXX: free resources allocated in RESERVE1 and/orRESERVE2.  Something failed somewhere, and the statesbelow won't be called. */break;case MODE_SET_ACTION:/* XXX: perform the value change here *///memcpy(&value, setValue, sizeof(setValue));value = *(requests->requestvb->val.integer);if (0/* XXX: error? */) {netsnmp_set_request_error(reqinfo, requests, 0/* some error */);}break;case MODE_SET_COMMIT:/* XXX: delete temporary storage */value = *(requests->requestvb->val.integer);if (0/* XXX: error? */) {/* try _really_really_ hard to never get to this point */netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_COMMITFAILED);}break;case MODE_SET_UNDO:/* XXX: UNDO and return to previous value for the object */if (0/* XXX: error? */) {/* try _really_really_ hard to never get to this point */netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_UNDOFAILED);}break;default:/* we should never get here, so this is a really bad error */snmp_log(LOG_ERR, "unknown mode (%d) in handle_setMemThreshold\\n", reqinfo->mode );return SNMP_ERR_GENERR;}return SNMP_ERR_NOERROR;
}
int
handle_setCpuThreshold(netsnmp_mib_handler *handler,netsnmp_handler_registration *reginfo,netsnmp_agent_request_info   *reqinfo,netsnmp_request_info         *requests)
{int ret;/* 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, sowe don't need to loop over a list of requests; we'll only get one. */switch(reqinfo->mode) {case MODE_GET:snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,(u_char *) value2/* XXX: a pointer to the scalar's data */,strlen(value2 + 1 )/* XXX: the length of the data in bytes */);break;/ SET REQUEST multiple states in the transaction.  See:* http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg*/case MODE_SET_RESERVE1:/* or you could use netsnmp_check_vb_type_and_size instead */ret = netsnmp_check_vb_type(requests->requestvb, ASN_INTEGER);if ( ret != SNMP_ERR_NOERROR ) {netsnmp_set_request_error(reqinfo, requests, ret );}break;case MODE_SET_RESERVE2:/* XXX malloc "undo" storage buffer */if (0/* XXX if malloc, or whatever, failed: */) {netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_RESOURCEUNAVAILABLE);}break;case MODE_SET_FREE:/* XXX: free resources allocated in RESERVE1 and/orRESERVE2.  Something failed somewhere, and the statesbelow won't be called. */break;case MODE_SET_ACTION:/* XXX: perform the value change here */if (0/* XXX: error? */) {netsnmp_set_request_error(reqinfo, requests, 0/* some error */);}break;case MODE_SET_COMMIT:/* XXX: delete temporary storage */free(value2);value2 = strdup((char*)requests->requestvb->val.string);if (0/* XXX: error? */) {/* try _really_really_ hard to never get to this point */netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_COMMITFAILED);}break;case MODE_SET_UNDO:/* XXX: UNDO and return to previous value for the object */if (0/* XXX: error? */) {/* try _really_really_ hard to never get to this point */netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_UNDOFAILED);}break;default:/* we should never get here, so this is a really bad error */snmp_log(LOG_ERR, "unknown mode (%d) in handle_setCpuThreshold\\n", reqinfo->mode );return SNMP_ERR_GENERR;}return SNMP_ERR_NOERROR;
}

修改配置

vim /etc/snmp/snmpd.conf

master agentx
rwcommunity public

代理加载
net-snmp-config --compile-subagent testMib testMib.c

./testMib

重启服务

service snmpd restart

测试

snmpwalk -v3 -u soft -A 123456 -a MD5 -l authPriv  -X 654321 -x DES localhost 1.3.6.1.4.1.20001.1.3.1.0

snmpset -v3 -u soft -A 123456 -a MD5 -l authPriv  -X 654321 -x DES localhost 1.3.6.1.4.1.20001.1.3.1.0 i 20

snmpwalk -v3 -u soft -A 123456 -a MD5 -l authPriv  -X 654321 -x DES localhost 1.3.6.1.4.1.20001.1.3.2.0

snmpset -v3 -u soft -A 123456 -a MD5 -l authPriv  -X 654321 -x DES localhost 1.3.6.1.4.1.20001.1.3.2.0 s 'hello'