我所知道的添加自定义MIB的方法有三种
 
1.静态加载,将生成的.c和.h文件加入到相应的位置,重新编译snmp库,优点是不需要修改配置文件,缺点是每次添加都得重新编译;
2.动态加载,将生成的.c和.h文件再编译成.so库,修改snmpd.conf配置文件。优点是每次添加不需要重新编译,缺点是必须支持dlmod命令;
3.子代理扩展,将生成的.c和.h文件编译成可执行程序,运行该程序和snmpd即可,优点是操作简单,缺点是需要运行两个程序才行。
 
三种方法的前几步是一样的,都是编写MIB,生成.c和.h文件,补全.c文件。
 
1.编写MIB
      MIB的语法见http://blog.csdn.net/shanzhizi/article/details/15340305,写得很清楚,很详细。
下面给出我自己的MIB文件。
-- Test-SLK-MIB.txt
     Test-SLK-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 "201601221450Z"       --必须以Z结尾
             ORGANIZATION
                 ""
             CONTACT-INFO
                 ""
             DESCRIPTION
                 "Video's Server MIB."
             ::= { enterprises 745352 }
 
         Time OBJECT IDENTIFIER ::= { Test 1 }
 
 
         GetTime OBJECT-TYPE
             SYNTAX DisplayString 
             MAX-ACCESS read-only
             STATUS current
             DESCRIPTION
                 "Example : 2016/1/22"
            ::= { Time 1 }
    END
-- Test-SLK-MIB.txt
 
这个MIB文件很简单,只有一个OID 1.3.6.1.4.1.745352.1.1,把这个MIB放入MIBS文件夹,我的位于/usr/local/snmp/share/snmp/mibs。
 
2.生成.c和.h文件
      运行命令mib2c Test-SLK-MIB::Test 出现的选项依次选2和1.
 
[root@localhost mibs]# env MIBS="+/etc/snmp/mibs/Test-MIB.my" mib2c Test
writing to -
mib2c has multiple configuration files depending on the type of
code you need to write.  You must pick one depending on your need.
 
You requested mib2c to be run on the following part of the MIB tree:
  OID:                              Test
  numeric translation:              .1.3.6.1.4.1.16535
  number of scalars within:         1   
  number of tables within:          0   
  number of notifications within:   0   
 
First, do you want to generate code that is compatible with the 
ucd-snmp 4.X line of code, or code for the newer Net-SNMP 5.X code
base (which provides a much greater choice of APIs to pick from):
 
  1) ucd-snmp style code
  2) Net-SNMP style code
 
Select your choice : 2 
 
**********************************************************************
         GENERATING CODE FOR SCALAR OBJECTS:
**********************************************************************
 
  It looks like you have some scalars in the mib you requested, so I
  will now generate code for them if you wish.  You have two choices
  for scalar API styles currently.  Pick between them, or choose not 
  to generate any code for the scalars:
 
  1) If you're writing code for some generic scalars
     (by hand use: "mib2c -c mib2c.scalar.conf Test")
 
  2) If you want to magically "tie" integer variables to integer
     scalars
     (by hand use: "mib2c -c mib2c.int_watch.conf Test")
 
  3) Don't generate any code for the scalars
 
Select your choice: 1
    using the mib2c.scalar.conf configuration file to generate your code.
writing to Test.h
writing to Test.c
 
 
 
**********************************************************************
* NOTE WELL: The code generated by mib2c is only a template.  *YOU*  *
* must fill in the code before it'll work most of the time.  In many *
* cases, spots that MUST be edited within the files are marked with  *
* /* XXX */ or /* TODO */ comments.                                  *
**********************************************************************
running indent on Test.h
running indent on Test.c
 
 
生成的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, 745352, 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. 
     */
 
    switch (reqinfo->mode) {
 
    case MODE_GET:
        snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                 /*
                                  * XXX: a pointer to the scalar's data 
                                  */ ,
                                 /*
                                  * XXX: the length of the data in bytes 
                                  */ );
        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;
}
 
3.补全Test.c
在代码中XXX处添加相应的值,代码中都有说明,XXX: a pointer to the scalar's data,这个要我们填一个指针,XXX: the length of the data in bytes 这个要我们填数据的大小,当然要先定义,然后去获取啊。
 
补全后
/*
 * 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[] = { 1, 3, 6, 1, 4, 1, 745352, 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;
}
 
接下来根据方法不同,步骤也不同。
一、静态链接
    把Test.c和Test.h复制到/net-snmp-5.7.3/agent/mibgroups,这里是说net-snmp源码里。
    编译./configure --prefix=/usr/local/snmp --with-mib-modules=Test,make && make install。
    静态加载成功
 
二、动态加载
    编写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
 
编译生成.so库。
修改snmpd.conf配置文件,在文件末尾加入dlmod Test ${Test.so所在绝对路径}/Test.so
启动snmpd,
     /usr/local/snmpd -f -L -DTest,dlmod -c /usr/local/snmp/etc/snmpd.conf
动态加载完成
 
三、子代理扩展
生成Test程序:
 net-snmp-config --compile-subagent Test Test.c
启动snmpd,Test
/usr/local/snmpd -c /usr/local/snmp/etc/snmpd.conf
./Test
完成

net-snmp添加自定义MIB的更多相关文章

  1. SNMP与MIB

    简单网络管理协议(SNMP:Simple Network Management Protocol)是一套网络管理协议,注意,SNMP是一个强大的网络管理协议,而不是"简单"的.利用 ...

  2. 关于SNMP的MIB文件的语法简述

    源地址:https://blog.csdn.net/carechere/article/details/51236184 SNMP协议的MIB文件的常见宏定义的描述: 对MIB文件中一些常见的宏定义的 ...

  3. SNMP 原理与实战详解

    原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

  4. Centos7 Cacti-0.8.8g安装及SNMP简介

    在官网可以看到关于cacti的下载说明http://www.cacti.net/download_cacti.php Download Cacti The latest stable version ...

  5. 浅议SNMP安全、SNMP协议、网络管理学习

    相关学习资料 tcp-ip详解卷1:协议.pdf(重点看25章SNMP部分) http://www.rfc-editor.org/rfc/rfc1213.txt http://www.rfc-edit ...

  6. 基于W5500的嵌入式SNMP代理端实现

     一 实验背景 近期一个做焊接设备的朋友想在焊机上加入监控的新功能,实时获取焊机的温度.功耗等參数,还可简单控制,实现对集群焊接设备的网络化管理.而这个朋友不想在开发管理系统上花太多精力,想找一个 ...

  7. SNMP概述–运维必知的协议基础

    一.什么是SNMP?   SNMP是  “Simple Network Management Protocol” 的缩写,中文意思是简单网络管理协议,它是由互联网工作小组在RFC1157中定义的应用层 ...

  8. H3C交换机SNMP配置

    1.启动/关闭SNMP Agent服务 在系统视图模式下: 启用:snmp-agent 关闭:undo snmp-agent 注:缺省情况下snmp agent是关闭的 2. 使能或禁止SNMP相应版 ...

  9. SNMP学习笔记之SNMP 原理与实战详解

    原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

随机推荐

  1. Scalaz(41)- Free :IO Monad-Free特定版本的FP语法

    我们不断地重申FP强调代码无副作用,这样才能实现编程纯代码.像通过键盘显示器进行交流.读写文件.数据库等这些IO操作都会产生副作用.那么我们是不是为了实现纯代码而放弃IO操作呢?没有IO的程序就是一段 ...

  2. 《第一行代码》学习笔记——第1章 开始启程,你的第一行Android代码

    1.3 创建你的第一个Android项目 1.3.1 创建HelloWorld项目 1.Application Name代表应用名称,手机上显示的就是它: 2.Project Name代表项目名称,其 ...

  3. ahjesus code simith 存储过程模板

    <%------------------------------------------------------------------------------------------ * Au ...

  4. 12款免费的响应式 WordPress 主题下载

    响应式和现代设计风格的多用途 WordPress 主题能够非常灵活的适应所有设备.而高级主题能够更轻松定制,您可以从主题选项中禁用/启用响应模式.多用途的响应式设计的主题是最适合杂志网站,博客网站,想 ...

  5. IKONS – 赞!264 款手工打造的免费矢量图标

    IKONS 是一套免费的矢量图标集,包含264款手工精心制作的可伸缩的矢量图标,分享给网页设计人员和开发人员.这些图标提供了 SVG.AI.ESP.PSD.CSH 和 PNG 格式.所有的图标都可以免 ...

  6. 经典网页设计:20个华丽的 iPhone 应用程序演示网站

    一个物品销售很好,重要的原因之一是它的包装,因为这是最重要的细节,可以把一个人转变成购买者.一个好的包装设计和良好的表现比产品本身更重要,因此被分配了大量的金钱和资源,以创造伟大的东西. 因此,为了销 ...

  7. CutJS – 用于 HTML5 游戏开发的 2D 渲染引擎

    CutJS 是轻量级的,快速的,基于 Canvas 开发的 HTML5  2D 渲染引擎,可以用于游戏开发.它是开源的,跨平台的,与现代的浏览器和移动设备兼容.CutJS 提供了一个类似 DOM 树的 ...

  8. 批量另存mxd

    在GIS数据处理中,批量操作是经常遇到的问题,Python脚本是解决问题的最好方法.现在需要将arcgis10.1的mxd另存为10.0,不仅数量较多,而且每个mxd要素和标注非常多,手动来操作确实慢 ...

  9. 操作系统开发系列—4.LDT

    一直以来,我们把所有的段描述符都放在GDT中,而不管它属于内核还是用户程序,为了有效地在任务之间实施隔离,处理器建议每个任务都应当具有自己的描述符表,称为局部描述符表LDT,并且把专属于自己的那些段放 ...

  10. subversion SVN

    subversion(简称svn)是近年来崛起的版本管理软件系统,是cvs的接班人.目前,绝大多数开源软件都使用svn作为代码版本管理软件. Subversion是一个版本控制系统,相对于的RCS.C ...