net-snmp mib模块处理程序/回调示例?

时间:2015-05-14 20:13:50

标签: c ubuntu net-snmp

我正在寻找一个带有自定义回调的mib模块的示例实现,该回调处理Netsnmp_node_handler中的一些结构:

test_handler(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)

我希望看一个检查它是什么请求的例子,即获取/设置/监视请求。检查oid是否与班级中的oid匹配,获取oid值等。目前我在执行此操作时遇到问题。

当我做一个snmpset时,我的模块被调用两次,不知道为什么。

1 个答案:

答案 0 :(得分:-1)

您要求完整实施。问题太多了。只是为了好处,提出一些基本代码:

void init_InterfaceCountTable(void)
{
    oid my_registration_oid[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //Give complete OID of your table
    table_set = netsnmp_create_table_data_set("InterfaceCountTable");

    table_set->allow_creation = 1;
    if(FLAG != 1)
    {
        netsnmp_table_dataset_add_index(table_set, ASN_INTEGER);

        netsnmp_table_set_multi_add_default_row(table_set,
                COLUMN_INTERFACECOUNTRX, ASN_INTEGER, 1, NULL, 0,
                COLUMN_INTERFACECOUNTTX, ASN_INTEGER, 1, NULL, 0,
                0);

        int i =0;
        long int idx = 0;
        long int col2 = 0;
        long int col3 = 0;
        for (i=0; i<MAX_NUM_ROW; i++)
        {
            row[i] = netsnmp_create_table_data_row();
            idx = i;
            netsnmp_table_row_add_index(row[i], ASN_INTEGER, &idx, sizeof(idx));
            col2 = interfaceCountStruct[i].Rx;
            netsnmp_set_row_column(row[i], COLUMN_INTERFACECOUNTRX,
                    ASN_INTEGER, &col2, sizeof(col2));
            //netsnmp_mark_row_column_writable(row[i], 2, 1);
            col3 = interfaceCountStruct[i].Tx;
            netsnmp_set_row_column(row[i], COLUMN_INTERFACECOUNTTX,
                    ASN_INTEGER, &col3, sizeof(col3));
            //netsnmp_mark_row_column_writable(row[i], 3, 1);
            netsnmp_table_dataset_add_row(table_set, row[i]);
        }

        netsnmp_register_table_data_set(netsnmp_create_handler_registration
                ("InterfaceCountTable", netsnmp_table_array_helper_handler,
                 my_registration_oid,
                 OID_LENGTH(my_registration_oid),
                 HANDLER_CAN_RWRITE), table_set, NULL);
    }
}


netsnmp_table_array_helper_handler(netsnmp_mib_handler *handler,
                                   netsnmp_handler_registration *reginfo,
                                   netsnmp_agent_request_info *agtreq_info,
                                   netsnmp_request_info *requests)
{
    int rc = SNMP_ERR_NOERROR;
    int i =0;

    switch (agtreq_info->mode) {
        case MODE_GET:
        case MODE_GETBULK:
        case MODE_GETNEXT:
        {
            for (i=0; i<MAX_NUM_ROW; i++)
            {
                netsnmp_table_dataset_remove_and_delete_row(table_set, row[i]);
            }

            long int idx = 0;
            long int col2 = 0;
            long int col3 = 0;

            for (i=0; i<MAX_NUM_ROW; i++)
            {
                row[i] = netsnmp_create_table_data_row();
                idx = interfaceCountStruct[i].idx;
                netsnmp_table_row_add_index(row[i], ASN_INTEGER, &idx, sizeof(idx));
                col2 = interfaceCountStruct[i].Rx;
                netsnmp_set_row_column(row[i], COLUMN_INTERFACECOUNTRX,
                    ASN_INTEGER, &col2, sizeof(col2));
        //netsnmp_mark_row_column_writable(row[i], 2, 1);
                col3 = interfaceCountStruct[i].Tx;
                netsnmp_set_row_column(row[i], COLUMN_INTERFACECOUNTTX,
                    ASN_INTEGER, &col3, sizeof(col3));
        //netsnmp_mark_row_column_writable(row[i], 3, 1);
                netsnmp_table_dataset_add_row(table_set, row[i]);
            }

        }
        break;

        default:
            return SNMP_ERR_GENERR;
    }

    return rc;
}

如果你能做得更好,请告诉我。