当前位置: 首页 > news >正文

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'

 

 

http://www.lryc.cn/news/59885.html

相关文章:

  • 一文说透安全沙箱技术
  • Java多线程基础面试总结(二)
  • NS32F407VGT6 NS32F407VET6软硬件通用STM32F407VGT6 407VET6
  • Openstack: network: ovs: dpif/show 实例分析:interface
  • 必要的项目管理软件因素
  • 大学刚毕业,用10000小时,走进字节跳动拿了offer
  • docker 安装 redis
  • Ceph常见问题
  • Android---Jetpack之Paging
  • gensim.models.word2vec() 参数详解
  • 光栅和矢量图像处理SDK:Graphics Mill 11.7Crack
  • 阿里云的客服 锻炼你心性的 一种方式 !!!
  • Linux常用的网络命令有哪些?快速入门!
  • PMP认证价值在哪?这个证书有什么用?
  • 一条更新语句的执行流程又是怎样的呢?
  • promise异步编程指南
  • 20230411----重返学习-网易云音乐首页案例-git远程仓库
  • Ansys Zemax | 模拟 AR 系统中的全息光波导:第二部分
  • 常用Git命令
  • 新手程序员被职场PUA的时候正确的化解姿势
  • LINUX_kali学习笔记
  • 第十天面试实战篇
  • YML是一种数据存储格式
  • 笔记:Java关于轻量级锁与重量级锁之间的问答
  • 有哪些通过PMP认证考试的心得值得分享?
  • 【unity learn】【Ruby 2D】角色发射飞弹
  • C++模板基础(九)
  • 【剑指 Offer】(1)
  • 每日一题 leetcode1026 2023-4-18
  • 【Python_Scrapy学习笔记(十二)】基于Scrapy框架实现POST请求爬虫