当前位置:  技术问答>linux和unix

新手求助:NET-SNMP中表变量的实现问题

    来源: 互联网  发布时间:2017-03-20

    本文导语:  本帖最后由 asite123 于 2010-08-10 12:00:17 编辑 在实现表对象的过程中,转换后的C代码如下: #include  #include  #include  #include "ExampleTable.h" struct ExampleTable_entry {        long UserIndex;     char UserStatus[30];     u_long...

本帖最后由 asite123 于 2010-08-10 12:00:17 编辑
在实现表对象的过程中,转换后的C代码如下:
#include 
#include 
#include 
#include "ExampleTable.h"

struct ExampleTable_entry {
       long UserIndex;
    char UserStatus[30];
    u_long CheckTime;
    long MonSet;
    long Old_MonSet;
    int   valid;
    struct ExampleTable_entry *next;
};
struct ExampleTable_entry  *ExampleTable_head;
struct ExampleTable_entry * ExampleTable_createEntry(long  UserIndex,char* UserStatus,u_long CheckTime,long MonSet) 
{
    struct ExampleTable_entry *entry;
    entry = SNMP_MALLOC_TYPEDEF(struct ExampleTable_entry);
    if (!entry)
        return NULL;
    entry->UserIndex = UserIndex;
    strcpy(entry->UserStatus,UserStatus);
    entry->CheckTime=CheckTime;
    entry->MonSet=MonSet;
    entry->next = ExampleTable_head;
    ExampleTable_head = entry;
    return entry;
}
void init_ExampleTable(void)
{
  /* here we initialize all the tables we're planning on supporting */
    initialize_table_ExampleTable();
}
 // # Determine the first/last column names
void initialize_table_ExampleTable(void)
{
    static oid ExampleTable_oid[] = {1,3,6,1,4,1,310,3};
    size_t ExampleTable_oid_len   = OID_LENGTH(ExampleTable_oid);
    netsnmp_handler_registration    *reg;
    netsnmp_iterator_info           *iinfo;
    netsnmp_table_registration_info *table_info;
    reg = netsnmp_create_handler_registration(
              "ExampleTable",     ExampleTable_handler,
              ExampleTable_oid, ExampleTable_oid_len,
              HANDLER_CAN_RWRITE
              );
    table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
    netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER,  /* index: UserIndex */ 0);
    table_info->min_column = 2;
    table_info->max_column = 10;    
    iinfo = SNMP_MALLOC_TYPEDEF( netsnmp_iterator_info );
    iinfo->get_first_data_point = ExampleTable_get_first_data_point;
    iinfo->get_next_data_point  = ExampleTable_get_next_data_point;
    iinfo->table_reginfo        = table_info;    
    netsnmp_register_table_iterator( reg, iinfo );
    ExampleTable_createEntry(1, "mytest1ggg", 1000, 1);
    ExampleTable_createEntry(12, "mytest133", 2000, 3);
    ExampleTable_createEntry(122, "mytest1ww", 3000, 5);
    ExampleTable_createEntry(132, "mytest1qq", 4000, 45);
    ExampleTable_createEntry(134, "mytest1wer", 5000, 56);
    ExampleTable_createEntry(145, "mytest1wesdfr", 6000, 61);
}
void ExampleTable_removeEntry( struct ExampleTable_entry *entry )
 {
    struct ExampleTable_entry *ptr, *prev;

    if (!entry)
        return;    /* Nothing to remove */
    for ( ptr  = ExampleTable_head, prev = NULL;
          ptr != NULL;
          prev = ptr, ptr = ptr->next ) {
        if ( ptr == entry )
            break;
    }
    if ( !ptr )
        return;    
    if ( prev == NULL )
        ExampleTable_head = ptr->next;
    else
        prev->next = ptr->next;

    SNMP_FREE( entry ); 
}
netsnmp_variable_list * ExampleTable_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 = ExampleTable_head;
    return ExampleTable_get_next_data_point(my_loop_context, my_data_context, put_index_data,  mydata );
}

netsnmp_variable_list * ExampleTable_get_next_data_point(void **my_loop_context,
                          void **my_data_context,
                          netsnmp_variable_list *put_index_data,
                          netsnmp_iterator_info *mydata)
{
    struct ExampleTable_entry *entry = (struct ExampleTable_entry *)*my_loop_context;
    netsnmp_variable_list *idx = put_index_data;

    if ( entry ) {
        snmp_set_var_value( idx, (u_char *)& entry->UserIndex,  sizeof(entry->UserIndex) );
        idx = idx->next_variable;
        *my_data_context = (void *)entry;
        *my_loop_context = (void *)entry->next;
return put_index_data;
    }     
    else {
        return NULL;
    }
}
int ExampleTable_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 ExampleTable_entry  *table_entry;
    switch (reqinfo->mode) {     
    case MODE_GET:
        for (request=requests; request; request=request->next) {
            table_entry = (struct ExampleTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  = netsnmp_extract_table_info(request);
    
            switch (table_info->colnum) {
            case COLUMN_USERINDEX:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb,ASN_INTEGER,(u_char *)& table_entry->UserIndex,sizeof(table_entry->UserIndex));
                break;
            case COLUMN_USERSTATUS:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb,ASN_OCTET_STR,(u_char *)& table_entry->UserStatus,strlen(table_entry->UserStatus));
                break;
            case COLUMN_CHECKTIME:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb,ASN_TIMETICKS,(u_char *)& table_entry->CheckTime,sizeof(table_entry->CheckTime));
                break;
            case COLUMN_MONSET:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb,ASN_INTEGER,(u_char *)& table_entry->MonSet,sizeof(table_entry->MonSet));
                break;
            default:
                netsnmp_set_request_error(reqinfo, request,
                                          SNMP_NOSUCHOBJECT);
                break;
            }
        }
        break;
    case MODE_SET_RESERVE1:
        for (request=requests; request; request=request->next) {
            table_entry = (struct ExampleTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(request);    
            switch (table_info->colnum) {
            case COLUMN_MONSET:
                if ( request->requestvb->type != ASN_INTEGER ) {
                    netsnmp_set_request_error( reqinfo, request,
                                               SNMP_ERR_WRONGTYPE );
                    return SNMP_ERR_NOERROR;
                }
                break;
            default:
                netsnmp_set_request_error( reqinfo, request,
                                           SNMP_ERR_NOTWRITABLE );
                return SNMP_ERR_NOERROR;
            }
        }
        break;
    case MODE_SET_RESERVE2:
        break;
    case MODE_SET_FREE:
        break;
    case MODE_SET_ACTION:
        for (request=requests; request; request=request->next) {
            table_entry = (struct ExampleTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);    
            switch (table_info->colnum) {
            case COLUMN_MONSET:
                table_entry->Old_MonSet = table_entry->MonSet;
                table_entry->MonSet     = *((long *)request->requestvb->val.integer);
                break;
            }
        }
        break;
    case MODE_SET_UNDO:
        for (request=requests; request; request=request->next) {
            table_entry = (struct ExampleTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);    
            switch (table_info->colnum) {
            case COLUMN_MONSET:
                table_entry->MonSet = table_entry->Old_MonSet;
                break;
            }
        }
        break;
    case MODE_SET_COMMIT:
        break;
    }
    return SNMP_ERR_NOERROR;
}
但进行编译后,使用snmptable -v 2c -c public localhost .1.3.6.1.4.1.310.3命令的时候每次都返回
Was that a table? SNMPv2-SMI::enterprises.310.3
而用使用snmpwalk -v 2c -c public localhost .1.3.6.1.4.1.310命令,则可以返回ExampleTable中的类容:
SNMPv2-SMI::enterprises.310.3.1.2.1 = STRING: "mytest1ggg"
SNMPv2-SMI::enterprises.310.3.1.2.12 = STRING: "mytest133"
SNMPv2-SMI::enterprises.310.3.1.2.122 = STRING: "mytest1ww"
………………
也就是说,现在只能以遍历的形式访问节点下的内容,而不能以表的形式访问节点下的内容。
请问这是什么原因呢?
本人是个菜鸟,这是我的第一个项目,虽然从来没有接触过,但真的想把它做好,希望前辈们给给我指教,先在这里谢谢了。
备注:操作系统UBUNTU10.4,NET-SNMP 版本5.3.3
实在是不好意思,由于分数有限,只能给这么多分了

|
呵呵 同道中人啊 我也是做这个的  你那个snmpwalk 并没有打印出索引 最小行应设为1吧
我也是刚接触这个东西 下面是我的代码 希望能给你帮助

#include 
#include 
#include 
#include "xXObjects.h"


 # define MAX 1024
 struct xXTable_entry {
    
unsigned int          wanXingIndex;
   unsigned int          wanXingPwd;
   unsigned char         wanXingName[MAX];
   int                   valid;
   struct xXTable_entry *next;
};

struct xXTable_entry *xXTable_head;

struct xXTable_entry *
xXTable_createEntry(int wanXingIndex, int wanXingPwd,char *wanXingName)
{
    struct xXTable_entry *entry;

    entry = SNMP_MALLOC_TYPEDEF(struct xXTable_entry);
    if (!entry)
        return NULL;

    entry->wanXingIndex = wanXingIndex;
snmp_log(LOG_ERR,"entry->wanXingIndex( %d)n",  entry->wanXingIndex);
    entry->wanXingPwd = wanXingPwd;
snmp_log(LOG_ERR,"entry->wanXingPwd( %d)n",  entry->wanXingPwd);
    strcpy(entry -> wanXingName,wanXingName);
snmp_log(LOG_ERR,"entry->wanXingName( %s)n", entry -> wanXingName);

    entry->next = xXTable_head;
    xXTable_head = entry;
    return entry;
}
void
xXTable_removeEntry(struct xXTable_entry *entry)
{
    struct xXTable_entry *ptr, *prev;

    if (!entry)
        return;                 

    for (ptr = xXTable_head, prev = NULL;
         ptr != NULL; prev = ptr, ptr = ptr->next) {
        if (ptr == entry)
            break;
    }
    if (!ptr)
        return;               
    if (prev == NULL)
        xXTable_head = ptr->next;
    else
        prev->next = ptr->next;

    SNMP_FREE(entry);          


void  /* initial the table*/
init_xXObjects(void)
{
  
    initialize_table_xXTable();
}

void       
initialize_table_xXTable(void)
{
    static oid      xXTable_oid[] =
        { 1, 3, 6, 1, 4, 1, 8072, 2, 211, 1, 1 };

    size_t   xXTable_oid_len =OID_LENGTH(xXTable_oid);
    netsnmp_handler_registration *reg;
    netsnmp_iterator_info *iinfo;
    netsnmp_table_registration_info *table_info;

    reg =
        netsnmp_create_handler_registration("xXTable",
                                            xXTable_handler,
                                            xXTable_oid,
                                            xXTable_oid_len,
                                            HANDLER_CAN_RWRITE);

    table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
    netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, 0);
    table_info->min_column =1;
    table_info->max_column = 3;

    iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
    iinfo->get_first_data_point = xXTable_get_first_data_point;
    iinfo->get_next_data_point = xXTable_get_next_data_point;
    iinfo->table_reginfo = table_info;

    netsnmp_register_table_iterator(reg, iinfo);

      xXTable_createEntry(1, 222,"happy one!");
  xXTable_createEntry(2, 123,"happy two!");
  xXTable_createEntry(3, 456,"happy three!");
 
}

netsnmp_variable_list *
xXTable_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 = xXTable_head;
    return xXTable_get_next_data_point(my_loop_context,
                                                  my_data_context,
                                                  put_index_data, mydata);
}
netsnmp_variable_list *
xXTable_get_next_data_point(void **my_loop_context,
                                       void **my_data_context,
                                       netsnmp_variable_list *
                                       put_index_data,
                                       netsnmp_iterator_info *mydata)
{
    struct xXTable_entry *entry =
        (struct xXTable_entry *) *my_loop_context;
    netsnmp_variable_list *idx = put_index_data;

    if (entry) {
        snmp_set_var_value(idx, (const u_char *)&entry->wanXingIndex,
                           sizeof(entry->wanXingIndex));
  snmp_log(LOG_ERR,"idx( %d)n", idx);

        idx = idx->next_variable;
        *my_data_context = (void *) entry;
        *my_loop_context = (void *) entry->next;
  return   put_index_data;             

else {
        return NULL;
              }
}

int
xXTable_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 xXTable_entry *table_entry;

    switch (reqinfo->mode) {
           case MODE_GET:
        for (request = requests; request; request = request->next)
 {
            table_entry = (struct xXTable_entry *)
                netsnmp_extract_iterator_context(request);
            table_info = netsnmp_extract_table_info(request);

            switch (table_info->colnum) {
            case COLUMN_WANXINGINDEX:
                if (!table_entry) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value(request->requestvb, ASN_INTEGER,
                                         (const u_char *)&table_entry->wanXingIndex,
                                         sizeof(table_entry->
                                                wanXingIndex));
     snmp_log(LOG_ERR,"handler-table_entry->wanXingIndex( %d)n",table_entry->wanXingIndex);
                break;
            case COLUMN_WANXINGPWD:
                if (!table_entry) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value(request->requestvb, ASN_INTEGER,
                                         (const u_char *)&table_entry->wanXingPwd,
                                         sizeof(table_entry->wanXingPwd));
                break;
            case COLUMN_WANXINGNAME:
                if (!table_entry) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
                                         (const u_char *)table_entry->wanXingName,
                                         strlen(table_entry->wanXingName));
                break;
            default:
                netsnmp_set_request_error(reqinfo, request,
                                          SNMP_NOSUCHOBJECT);
                break;
            }
        }
        break;

            case MODE_SET_RESERVE1:
        for (request = requests; request; request = request->next) {
            table_entry = (struct xXTable_entry *)
                netsnmp_extract_iterator_context(request);
            table_info = netsnmp_extract_table_info(request);

            switch (table_info->colnum) {
            case COLUMN_WANXINGINDEX:
                if (request->requestvb->type != ASN_INTEGER) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_ERR_WRONGTYPE);
                    return SNMP_ERR_NOERROR;
                }
                                break;
            case COLUMN_WANXINGPWD:
                if (request->requestvb->type != ASN_INTEGER) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_ERR_WRONGTYPE);
                    return SNMP_ERR_NOERROR;
                }
                                break;
            case COLUMN_WANXINGNAME:
                if (request->requestvb->type != ASN_OCTET_STR) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_ERR_WRONGTYPE);
                    return SNMP_ERR_NOERROR;
                }
                               break;
            default:
                netsnmp_set_request_error(reqinfo, request,
                                          SNMP_ERR_NOTWRITABLE);
                return SNMP_ERR_NOERROR;
            }
        }
        break;

    case MODE_SET_RESERVE2:
        break;

    case MODE_SET_FREE:
        break;

    case MODE_SET_ACTION:
        for (request = requests; request; request = request->next) {
            table_entry = (struct xXTable_entry *)
                netsnmp_extract_iterator_context(request);
            table_info = netsnmp_extract_table_info(request);

            switch (table_info->colnum) {
            case COLUMN_WANXINGINDEX:
                           
                table_entry->wanXingIndex =*( request->requestvb->val.integer);
                break;
            case COLUMN_WANXINGPWD:
                               
                table_entry->wanXingPwd =*( request->requestvb->val.integer);
                break;
            case COLUMN_WANXINGNAME:
                              
             strcpy(table_entry->wanXingName, request->requestvb->val.string);
                break;
            }
        }
        break;

    case MODE_SET_UNDO:
        
        break;

    case MODE_SET_COMMIT:
        break;
    }
    return SNMP_ERR_NOERROR;

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 新手如何加入开源项目 有什么好的c++,java 开源适合新手 请指点!
  • JDBC的问题,新手高手都来看看吧,说不定能帮帮我这个新手呢,谢谢!
  • 想找共同学习Linux的新手,一起学习,共同成长
  • 新手关于驱动开发的疑问
  • 新手有关linux的问题!
  • 新手急求~~~~~~~~~~~~~~~~~~~
  • 新手学习该用哪个版本
  • 面向新手的终端辅助工具 Clicompanion
  • 我是个新手,请各位老兄给介绍基本好书?
  • 新手请教啦~~能不能帮忙推荐几本书
  • 我是新手
  • 新手:用WEB页面修改数据库中的表?
  • Java新手上路之问题
  • 我是一名新手,请问如何学java
  • (新手)这里怎么看到300篇以外的所有文章?(不是搜索)
  • 新手求助!
  • 新手上路,怎样判断网卡是否安装成功?
  • !!新手求救!!
  • 新手在此多谢了:SUN的宠物商店
  • 新手请教,我用VI时按ESC键,总是回嘟嘟嘟嘟响,请问如何去掉!谢谢!


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3