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

标签 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 时,我的模块被调用了两次,不知道为什么。

最佳答案

您要求的是完整的实现。是不是太过分了。只是为了方便,放一些基本代码:

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;
}

请让我知道您是否可以改进它。

关于c - net-snmp mib 模块处理程序/回调示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30246502/

相关文章:

c - 如何避免 if 上出现 "constant expression"?

c - 如何调用 lstat(2) 而不是 lstat(3)?

c - 在子进程中读取

Ubuntu 20.04 中 MSSQL 的 PHP 驱动程序

linux - 是否可以以编程方式更改 unix 进程的可执行文件名称 (ucmd)?

c - 系统调用的效率

ruby-on-rails - 我的 ubuntu 用户无法访问开发用户在 postgres for rails 中创建的数据库

ubuntu - 我正在使用 Ubuntu 12.04(精确)64 位,它使用 8GB RAM

c - net-snmp api 使用用户的凭据

snmp - 如何在 PEN 下构建 OID 树而不是 Net-SNMP 扩展?