前奏參见例如以下:

http://blog.sina.com.cn/s/blog_8f3de3250100xhao.html

http://blog.csdn.net/hepeng597/article/details/8782868

http://blog.csdn.net/rheostat/article/details/8172580

问题解决:

http://bbs.csdn.net/topics/340248598

实现:

/*

 * 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 "dpiProject.h"





#define NNN    30

#define STRMAX    200

#define COLUMN_MIN    1

#define COLUMN_MAX    11





struct dpiTaskTable_entry *

dpiTaskTable_createEntry(u_long  gnTaskId, char *gnTaskName, size_t gnTaskName_len);





/** Initializes the dpiProject module */

void

init_dpiProject(void)

{

  /* here we initialize all the tables we're planning on supporting */

    initialize_table_dpiTaskTable();

}





/** Initialize the dpiTaskTable table by defining its contents and how it's structured */

void

initialize_table_dpiTaskTable(void)

{

    const oid dpiTaskTable_oid[] = {1,3,6,1,4,1,22222,0,1,0,0};

    const size_t dpiTaskTable_oid_len   = OID_LENGTH(dpiTaskTable_oid);

    netsnmp_handler_registration    *reg;

    netsnmp_iterator_info           *iinfo;

    netsnmp_table_registration_info *table_info;





    DEBUGMSGTL(("dpiProject:init", "initializing table dpiTaskTable\n"));





    reg = netsnmp_create_handler_registration(

              "dpiTaskTable",     dpiTaskTable_handler,

              dpiTaskTable_oid, dpiTaskTable_oid_len,

              HANDLER_CAN_RONLY

              );





    table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );

    netsnmp_table_helper_add_indexes(table_info,

                           ASN_UNSIGNED,  /* index: gnTaskId */

                           0);

    //定义最大最小列数

    table_info->min_column = COLUMN_MIN;

    table_info->max_column = COLUMN_MAX;

    

    iinfo = SNMP_MALLOC_TYPEDEF( netsnmp_iterator_info );

    iinfo->get_first_data_point = dpiTaskTable_get_first_data_point;

    iinfo->get_next_data_point  = dpiTaskTable_get_next_data_point;

    iinfo->table_reginfo        = table_info;

    

    netsnmp_register_table_iterator( reg, iinfo );

    netsnmp_inject_handler_before( reg, 

        netsnmp_get_cache_handler(DPITASKTABLE_TIMEOUT,

                                  dpiTaskTable_load, dpiTaskTable_free,

                                  dpiTaskTable_oid, dpiTaskTable_oid_len),

            TABLE_ITERATOR_NAME);





    /* Initialise the contents of the table here */

    //初始化表格

    dpiTaskTable_createEntry(1, "dpi agent1", strlen("dpi agent1"));

    dpiTaskTable_createEntry(2, "dpi agent1", strlen("dpi agent1"));

    dpiTaskTable_createEntry(3, "dpi agent1", strlen("dpi agent1"));

}





    /* Typical data structure for a row entry */

//总共11列

struct dpiTaskTable_entry {

    /* Index values */

    //u_long gnTaskId;





    /* Column values */

    u_long gnTaskId;

    char gnTaskName[NNN];

    size_t gnTaskName_len;

    //char gnTaskState[NNN];

    //size_t gnTaskState_len;

    //u_long gnTaskCpuUsage;

    //u_long gnTaskMemoryUsed;

    //char gnTaskStartTime[NNN];

    //size_t gnTaskStartTime_len;

    //char gnTaskUpTime[NNN];

    //size_t gnTaskUpTime_len;





    /* Illustrate using a simple linked list */

    int   valid;

    struct dpiTaskTable_entry *next;

};





struct dpiTaskTable_entry  *dpiTaskTable_head;





/* create a new row in the (unsorted) table */

struct dpiTaskTable_entry *

dpiTaskTable_createEntry(u_long  gnTaskId, char *gnTaskName, size_t gnTaskName_len) 

{

    struct dpiTaskTable_entry *entry;





    entry = SNMP_MALLOC_TYPEDEF(struct dpiTaskTable_entry);

    if (!entry)

        return NULL;





    entry->gnTaskId = gnTaskId;

    snmp_log(LOG_ERR,"entry->gnTaskId(%d)\n",  entry->gnTaskId);

    //实现创建表

    entry->gnTaskName_len = gnTaskName_len;

    strncpy(entry->gnTaskName, gnTaskName, gnTaskName_len);





    entry->next = dpiTaskTable_head;

    dpiTaskTable_head = entry;

    return entry;

}





/* remove a row from the table */

void

dpiTaskTable_removeEntry( struct dpiTaskTable_entry *entry ) {

    struct dpiTaskTable_entry *ptr, *prev;





    if (!entry)

        return;    /* Nothing to remove */





    for ( ptr  = dpiTaskTable_head, prev = NULL;

          ptr != NULL;

          prev = ptr, ptr = ptr->next ) {

        if ( ptr == entry )

            break;

    }

    if ( !ptr )

        return;    /* Can't find it */





    if ( prev == NULL )

        dpiTaskTable_head = ptr->next;

    else

        prev->next = ptr->next;





    SNMP_FREE( entry );   /* XXX - release any other internal resources */

}





/* Example cache handling - set up linked list from a suitable file */

int

dpiTaskTable_load( netsnmp_cache *cache, void *vmagic ) {

    FILE *fp;

    struct dpiTaskTable_entry *this;

    char buf[STRMAX];





    /* The basic load routine template assumes that the data to

       be reported is held in a file - with one row of the file

       for each row of the table.

          If your data is available via a different API, you

       should amend this initial block (and the control of the

       'while' loop) accordingly.

          'XXX' marks where the template is incomplete and

       code will definitely need to be added. */





    fp = fopen( "/data/for/dpiTaskTable", "r" );

    if ( !fp ) {

        return -1;

    }

    while ( fgets( buf, STRMAX, fp )) {

        this = SNMP_MALLOC_TYPEDEF( struct dpiTaskTable_entry );

        /* XXX - Unpick 'buf' to extract the individual field values

                 and then populate the 'this' data structure with them */





        this->next = dpiTaskTable_head;

        dpiTaskTable_head = this;    /* Iterate helper is fine with unordered lists! */

    }

    fclose(fp);

    return 0;  /* OK */

}





void

dpiTaskTable_free( netsnmp_cache *cache, void *vmagic ) {

    struct dpiTaskTable_entry *this, *that;





    for ( this = dpiTaskTable_head; this; this=that ) {

        that = this->next;

        SNMP_FREE( this );   /* XXX - release any other internal resources */

    }

    dpiTaskTable_head = NULL;

}





/* Example iterator hook routines - using 'get_next' to do most of the work */

netsnmp_variable_list *

dpiTaskTable_get_first_data_point(void **my_loop_context,

                          void **my_data_context,

                          netsnmp_variable_list *put_index_data,

                          netsnmp_iterator_info *mydata)

{

    *my_loop_context = dpiTaskTable_head;

    return dpiTaskTable_get_next_data_point(my_loop_context, my_data_context,

                                    put_index_data,  mydata );

}





netsnmp_variable_list *

dpiTaskTable_get_next_data_point(void **my_loop_context,

                          void **my_data_context,

                          netsnmp_variable_list *put_index_data,

                          netsnmp_iterator_info *mydata)

{

    struct dpiTaskTable_entry *entry = (struct dpiTaskTable_entry *)*my_loop_context;

    netsnmp_variable_list *idx = put_index_data;





    if ( entry ) {

        snmp_set_var_typed_integer( idx, ASN_UNSIGNED, entry->gnTaskId );

        idx = idx->next_variable;

        *my_data_context = (void *)entry;

        *my_loop_context = (void *)entry->next;

        return put_index_data;

    } else {

        return NULL;

    }

}









/** handles requests for the dpiTaskTable table */

int

dpiTaskTable_handler(

    netsnmp_mib_handler               *handler,

    netsnmp_handler_registration      *reginfo,

    netsnmp_agent_request_info        *reqinfo,

    netsnmp_request_info              *requests) {





    netsnmp_request_info       *request;

    netsnmp_table_request_info *table_info;

    struct dpiTaskTable_entry          *table_entry;





    DEBUGMSGTL(("dpiProject:handler", "Processing request (%d)\n", reqinfo->mode));





    switch (reqinfo->mode) {

        /*

         * Read-support (also covers GetNext requests)

         */

    case MODE_GET:

        for (request=requests; request; request=request->next) {

            table_entry = (struct dpiTaskTable_entry *)

                              netsnmp_extract_iterator_context(request);

            table_info  =     netsnmp_extract_table_info(      request);

    

            switch (table_info->colnum) {

            case COLUMN_GNTASKID:

                if ( !table_entry ) {

                    netsnmp_set_request_error(reqinfo, request,

                                              SNMP_NOSUCHINSTANCE);

                    continue;

                }

                snmp_set_var_typed_integer( request->requestvb, ASN_UNSIGNED,

                                            table_entry->gnTaskId);

                break;

            case COLUMN_GNTASKNAME:

                if ( !table_entry ) {

                    netsnmp_set_request_error(reqinfo, request,

                                              SNMP_NOSUCHINSTANCE);

                    continue;

                }

                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,

                                          table_entry->gnTaskName,

                                          table_entry->gnTaskName_len);

                break;

#if 0

            case COLUMN_GNTASKSTATE:

                if ( !table_entry ) {

                    netsnmp_set_request_error(reqinfo, request,

                                              SNMP_NOSUCHINSTANCE);

                    continue;

                }

                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,

                                          table_entry->gnTaskState,

                                          table_entry->gnTaskState_len);

                break;

            case COLUMN_GNTASKCPUUSAGE:

                if ( !table_entry ) {

                    netsnmp_set_request_error(reqinfo, request,

                                              SNMP_NOSUCHINSTANCE);

                    continue;

                }

                snmp_set_var_typed_integer( request->requestvb, ASN_UNSIGNED,

                                            table_entry->gnTaskCpuUsage);

                break;

            case COLUMN_GNTASKMEMORYUSED:

                if ( !table_entry ) {

                    netsnmp_set_request_error(reqinfo, request,

                                              SNMP_NOSUCHINSTANCE);

                    continue;

                }

                snmp_set_var_typed_integer( request->requestvb, ASN_UNSIGNED,

                                            table_entry->gnTaskMemoryUsed);

                break;

            case COLUMN_GNTASKSTARTTIME:

                if ( !table_entry ) {

                    netsnmp_set_request_error(reqinfo, request,

                                              SNMP_NOSUCHINSTANCE);

                    continue;

                }

                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,

                                          table_entry->gnTaskStartTime,

                                          table_entry->gnTaskStartTime_len);

                break;

            case COLUMN_GNTASKUPTIME:

                if ( !table_entry ) {

                    netsnmp_set_request_error(reqinfo, request,

                                              SNMP_NOSUCHINSTANCE);

                    continue;

                }

                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,

                                          table_entry->gnTaskUpTime,

                                          table_entry->gnTaskUpTime_len);

                break;

#endif

            default:

                netsnmp_set_request_error(reqinfo, request,

                                          SNMP_NOSUCHOBJECT);

                break;

            }

        }

        break;





    }

    return SNMP_ERR_NOERROR;

}

说明:

主要是通过m2c 工具生成.c.h文件





1.生成代码模板:变量:env MIBS="+/usr/local/share/snmp/mibs/XXX-MIB.txt" mib2c xxx(相应xxx.c,xxx.h)

                表格:env MIBS="+/usr/local/share/snmp/mibs/XXX-MIB.txt" mib2c -c mib2c.iterate.conf xxx(相应xxx.c,xxx.h)





2.改动模板代码。通过devInfo_handler模块提供的api

  模板代码流程:init_dpiProject-》initialize_table_xxxTable-》xxxTable_createEntry

  改动处:

  1.在initialize_table_xxxTable改动表格列数最小最大值

  2.initialize_table_xxxTable加人xxxTable_createEntry初始化

  3.在xxxTable_createEntry实现表格数据获取(过devInfo_handler模块提供的api)





有三种实现集成到net snmp其中的方式:静态、动态、子代理。本模块採用子代理方式


snmp agent 表格实现(子代理方式实现)的更多相关文章

  1. net-snmp子代理(SubAgent)编写详述

    net-snmp子代理(SubAgent)编写 net-snmp子代理(SubAgent)编写 Netsnmp_Node_Handler MIB/OID定义 1.头文件test.h的编写 2.test ...

  2. snmpd 子代理模式编译测试

    1.参考链接 1)Net-snmp添加子代理示例 https://blog.csdn.net/eyf0917/article/details/39546651   2.操作步骤 1)网络拷贝下面的文件 ...

  3. zabbix配置文件详解--服务(server)端、客户(agent)端、代理(proxy)端

    在zabbix服务(server)端.客户(agent)端.代理(proxy)端分别对应着一个配置文件,即:zabbix_server.conf,zabbix_agentd.conf,zabbix_p ...

  4. SNMP AGENT函数介绍

    http://wenku.baidu.com/view/6a7903a9d1f34693daef3e9f.html 一.  SNMP AGENT在SNMP框架中的位置 1.1 SNMP是被广泛接受并投 ...

  5. Atitit  代理与分销系统(1)  子代理 充值总额功能设计概览 sum() groubpy subagt

    Atitit  代理与分销系统(1)  子代理 充值总额功能设计概览 sum() groubpy subagt Keyword 分组与聚合操作. 一个for做分组...里面的做聚合... 数据g操作查 ...

  6. JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明

    1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...

  7. Atitit 动态调用webservice与客户端代理方式调用

    Atitit 动态调用webservice与客户端代理方式调用 方式1: 使用call.invoke  直接调用WSDL,缺点:麻烦,不推荐--特别是JAVA调用.NET的WS时,会有不少的问题需要解 ...

  8. .NET环境下导出Excel表格的两种方式和导入两种类型的Excel表格

    一.导出Excel表格的两种方式,其中两种方式指的是导出XML数据类型的Excel(即保存的时候可以只需要修改扩展名为.xls)和真正的Excel这两种. using System; using Sy ...

  9. 利用loadrunner代理方式,录制手机APP脚本

    利用loadrunner代理方式录制手机(iPhone.android)应用程序HTTP脚本 工具/原料 loadrunner 智能手机 方法/步骤   利用笔记本网卡或者类似360随身wifi,在安 ...

随机推荐

  1. .Net中字典的使用

    /// <summary> /// 获取用户市信息 /// </summary> /// <param name="CustomerId">&l ...

  2. Hue的三大特点、三大功能和架构

    不多说,直接上干货! Hue 是 Cloudera 的大数据 Web 工具 官方访问网站 : http://gethue.com/ GitHub : https://github.com/cloude ...

  3. 实现图片懒加载(lazyload)

    对页面加载速度影响最大的就是图片,一张普通的图片可以达到几M的大小,而代码也许就只有几十KB.当页面图片很多时,页面的加载速度缓慢,几S钟内页面没有加载完成,也许会失去很多的用户. 所以,对于图片过多 ...

  4. Huawei交换机VRP配置介绍

    一.命令视图• 用户视图<Huawei>• 系统视图 [Huawei]• 接口视图 [Huawei-GigabitEthernet0/0/1]• 协议视图 [Huawei-rip-1]• ...

  5. iOS Scheme 跳转主流实现方案

    iOS Scheme跳转主流实现方案主要是路由跳转,目前iOS常用路由框架是JLRouter.HHRouter.MGJRouter. 但是这些路由库都各有不足,首先是JLRouter,用不到的功能繁多 ...

  6. Android中Service的一个Demo例子

    Android中Service的一个Demo例子  Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding.  本文,主要贴代码,不对Servic ...

  7. SpringBoot与SpringCloud的区别

    1.Spring boot 是 Spring 的一套快速配置脚手架,可以基于spring boot 快速开发单个微服务:Spring Cloud是一个基于Spring Boot实现的云应用开发工具: ...

  8. android 动态设置TextView值,例:金额添加

    一说到动态递增设置TextView值,非常多人应该立即就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值可是效率不 ...

  9. 3.索引与string进行映射实现高效查找

    #include <iostream> #include <typeindex>//类型索引 #include <unordered_map>//红黑树 #incl ...

  10. java poi操作excel示例代码

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...