前奏參见例如以下:

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. 26. Intellij IDEA 启动项目ClassNotFoundException

    转自:https://blog.csdn.net/zhw0596/article/details/81388147 使用Intellij IDEA  的过程中,新创建的项目启动时报 严重: Error ...

  2. 4.graph.h

    #pragma once #include <stdio.h> #include <graphics.h> #include <mmsystem.h> #pragm ...

  3. Important Abstractions and Data Structures

    For Developers‎ > ‎Coding Style‎ > ‎ Important Abstractions and Data Structures 目录 1 TaskRunne ...

  4. Python正则表达式初识(四)

    今天继续给大家分享Python正则表达式基础知识,主要给大家介绍一下特殊字符“{}”的用法,具体的教程如下. 特殊字符“{}”实质上也是一个限定词的用法,其限定前面字符所出现的次数,其常用的模式有三种 ...

  5. javaScript call与apply学习笔记

    call和apply是借用他人的函数实现自己到功能,具体表现在改变this指向,借用他人方法 而不同的地方是call是把实参按照形参的个数传入,而apply传入的是一个数组(argument) 写一个 ...

  6. Maven学习总结(12)——eclipse中构建多模块maven项目

    摘要:本文要用Maven来构建一个多模块的web项目 项目结构如下: system-parent      |----pom.xml      |----system-domain          ...

  7. msp430在ccsv5下出现的问题总结

    一.内存问题 问题描写叙述,报错: program will not fit into available memory.  placement with alignment fails for se ...

  8. 版本号控制-搭建gitserver

    GitHub是一个免费托管开源码的Gitserver,假设我们不想公开项目的源码,又不想付费使用.那么我们能够自己搭建一台Gitserver. 以下我们就看看,怎样在Ubuntu上搭建Gitserve ...

  9. es64 const

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 用C#调用Lua脚本

    用C#调用Lua脚本 一.引言 学习Redis也有一段时间了,感触还是颇多的,但是自己很清楚,路还很长,还要继续.上一篇文章简要的介绍了如何在Linux环境下安装Lua,并介绍了在Linux环境下如何 ...