net-snmp添加自定义MIB

管理信息库(MIB,Management Information Base)是TCP/IP网络管理协议标准框架的内容之一,MIB定义了受管设备必须保存的数据项、允许对每个数据项进行的操作及其含义,即管理系统可访问的受管设备的控制和状态信息等数据变量都保存在MIB中。

下面介绍如何自定义添加自定义MIB。

由于笔者水平有限,难免有错误的地方,轻喷!并欢迎指正!

首先得下载net-snmp的源码

这里有链接:
net-snmp-5.4.1.tar.gz源码:
http://download.csdn.net/detail/liang_baikai/9657550
net-snmp-5.7.1.tar.gz源码:
http://download.csdn.net/detail/liang_baikai/9666368
net-snmp-5.7.3.tar.gz源码:
http://download.csdn.net/detail/liang_baikai/9682366
笔者这里用的是net-snmp-5.7.3版本的。

这里以监控开发板的系统时间和接口流量为例

首先编写MIB

下面是我的MIB文件(语法可以百度稍微了解下,很简单,基本照抄就行了)

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"
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 }
GetNetSpeed OBJECT-TYPE
SYNTAX DisplayString
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"get internet speed"
::= { Time 2 }
END 

将这个mib放到源码目录的mibs路径下。

用mib2c工具生成.c和.h文件

env MIBS=”+/net-snmp-5.7.3/mibs/Test-SLK-MIB.TXT ” mib2c Test
(说明一下,前面是你的mib的路径,后面是要生成的文件名)
出现这个选项:选2

1) ucd-snmp style code
2) Net-SNMP style code
Select your choice : 2

出现这个选项:选1

 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 scalar(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

然后查看mibs目录下有一个Test.c和一个Test.h文件

修改下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"
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*  netspeedfunction   */

int speed()
{
    int i = 0,j = 0,h = 0;
    int fd,size;
    char buffer[80];
   // system("touch aaa.txt");

    system("ifconfig | grep \"RX bytes\" > /mnt/abc.txt");
    fd = open("/mnt/abc.txt",O_RDONLY);
    size = read(fd,buffer,sizeof(buffer));
    for(i = 0;i < 80;i ++)
    {
        if(buffer[i] == ':')
        {
            j = i + 1;
        }
        if(buffer[i] == ' ')
        {
            buffer[i] = '\0';
        }
    }
    close(fd);
    h = atoi(&buffer[j]);
    memset(buffer,0,sizeof(buffer));
    system("rm -f /mnt/abc.txt");

    return h;

}


/** Initializes the Test module */
void
init_Test(void)
{
    const oid       GetTime_oid[] = { 1, 3, 6, 1, 4, 1, 745352, 1, 1 };
    const oid       GetNetSpeed_oid[] = { 1, 3, 6, 1, 4, 1, 745352, 1, 2 };

    DEBUGMSGTL(("Test", "Initializing\n"));

    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("GetTime", handle_GetTime, GetTime_oid,
                             OID_LENGTH(GetTime_oid), HANDLER_CAN_RONLY));
    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("GetNetSpeed", handle_GetNetSpeed,
                             GetNetSpeed_oid, OID_LENGTH(GetNetSpeed_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;
}

int
handle_GetNetSpeed(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. 
     */

    char buf[80];
    switch (reqinfo->mode) {

    case MODE_GET:
            speed();
            memset(buf,0,sizeof(buf));
            snprintf(buf,80,"%d",speed());

   //     system("touch /mnt/aaaa.txt");
        snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                 /*
                                  * XXX: a pointer to the scalar's data 
                                  */ buf,
                                 /*
                                  * XXX: the length of the data in bytes 
                                  */ strlen(buf));
        break;


    default:
        /*
         * we should never get here, so this is a really bad error 
         */
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetNetSpeed\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}

配置编译

把Test.c和Test.h复制到/net-snmp-5.7.3/agent/mibgroups/路径下

# ./configure --host=arm-linux --target=arm-linux --build=i386-linux  --with-cc=/opt/hisi-linux/x86-arm/arm-hisiv200-linux/bin/arm-hisiv200-linux-gnueabi-gcc --with-ar=/opt/hisi-linux/x86-arm/arm-hisiv200-linux/bin/arm-hisiv200-linux-gnueabi-ar --prefix=/home/cj/snmp/ --disable-shared --disable-scripts --with-endianness=little -enable-mini-agent --disable-ipv6 --disable-manuals  --disable-ucd-snmp-compatibility --enable-as-needed --with-mib-modules=Test

(因为笔者这里需要移植到板子上运行,所以这样配置。不需要移植的直接 ./configure –with-mib-modules=Test 就好了)
(关于net-snmp的移植问题可以参考这里:http://blog.csdn.net/liang_baikai/article/details/53320335

然后make下就好了。

将agent路径下的snmpd文件拷贝到板子上运行就可以了。

./snmpd -c snmpd.conf

笔者这里已经移到板子上并运行了。下面看看是否能成功获取数据。
笔者这里有zabbix监控软件,直接用zabbix监控自定义的那个OID就好了。
这里写图片描述

这是获取时间的那个

这里写图片描述
这是获取接口流量的那个
成功获取到数据

自定义MIB成功!

  • 9
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值