Jump to content

    
Sign in to follow this  
Oops

NET-SNMP кодирование, помогите с реализацией таблицы.

Recommended Posts

Здрасти, здрасти!!!

 

Имеется ОС Linux на которой поднят SNMP демон ( snmpd ). Я пишу библиотеку, которую подключает snmpd при старте и используя которую реагирует на команды. Библиотеку пишу на обычном С, пользуюсь помощью тут: http://www.net-snmp.org/wiki/index.php/TUT...oding_Tutorials .

Понадобилось написать SNMP таблицу в кторой надо менять переменные.

Работать с таблицей состоящей из одной строки - просто, вот пример:

   static oid ipConfTable_registration_oid[] = { 1, 3, 6, 1, 4, 1, 1958, 7, 2, 2, 3 };    // прикрепляем к таблици OID.
   int ipConfTable_handler (netsnmp_mib_handler *handler,netsnmp_handler_registration *reginfo,netsnmp_agent_request_info *reqinfo,netsnmp_request_info *requests);  // объявляем функцию-обработчик обытий таблицы

/// TABLE    ///

       {
            netsnmp_table_data_set *table_set;
            netsnmp_table_row *row;

            table_set = netsnmp_create_table_data_set("Table");

            table_set->allow_creation = 1;

            netsnmp_table_dataset_add_index(table_set, ASN_OCTET_STR);

            netsnmp_table_set_multi_add_default_row(table_set,  // таблица из 5 колонок

                                                    1, ASN_IPADDRESS, 1, NULL, 0,
                                                    2, ASN_INTEGER, 1, NULL, 0,

                                                    3, ASN_IPADDRESS, 1, NULL, 0,
                                                    4, ASN_OCTET_STR, 1, NULL, 0,
                                                    5, ASN_IPADDRESS, 1, NULL, 0,
                                                    0  );

            netsnmp_register_table_data_set(netsnmp_create_handler_registration
                                            ("Table",//NULL,
                                             ipConfTable_handler // прикрепляем фукцию обработчик,
                                             ipConfTable_registration_oid,
                                             OID_LENGTH(ipConfTable_registration_oid),
                                             HANDLER_CAN_RWRITE), table_set, NULL);


            row = netsnmp_create_table_data_row();


            int index;
            index = 0;
            netsnmp_table_row_add_index(row, ASN_INTEGER, &index, sizeof(index));

            ipConfAddress = 0x00000000;
            netsnmp_set_row_column(row, 1, ASN_IPADDRESS,(char*)&ipConfAddress, sizeof(ipConfAddress));
            netsnmp_mark_row_column_writable(row, 1, 1);

            unsigned int num = 1;
            netsnmp_set_row_column(row, 2, ASN_INTEGER,(char*)&num, sizeof(num));
            netsnmp_mark_row_column_writable(row, 2, 1);       

            u_long ipConfNetMask = 0x00000000;
            netsnmp_set_row_column(row, 3, ASN_IPADDRESS, (char*)&ipConfNetMask,sizeof(ipConfNetMask));
            netsnmp_mark_row_column_writable(row, 3, 1);        

            netsnmp_set_row_column(row, 4, ASN_OCTET_STR, "eth0",strlen("eth0"));
            netsnmp_mark_row_column_writable(row, 4, 1);        

            u_long gateway = 0x00000000;
            netsnmp_set_row_column(row, 5, ASN_IPADDRESS, (char*)&gateway,sizeof(gateway));
            netsnmp_mark_row_column_writable(row, 5, 1);        

            netsnmp_table_dataset_add_row(table_set, row);

            netsnmp_register_auto_data_table(table_set, NULL);

       }

 

Далее в функции обработчике производим запись и чтение значений таблицы.

 

int ipConfTable_handler(netsnmp_mib_handler *handler,netsnmp_handler_registration *reginfo,netsnmp_agent_request_info *reqinfo,netsnmp_request_info *requests)
{
        
        u_long         *ipConfAddress_cache = NULL;
        u_long         *ipConfNetMask_cache = NULL;
        u_long         *gateway_cache = NULL;

        netsnmp_variable_list *var;

        long int ret;
        netsnmp_request_info       *request;
        netsnmp_table_request_info *table_info;
        long int  variable;
        char i;

        for (request=requests; request; request=request->next) {
        
        var = request->requestvb;
        
        netsnmp_extract_iterator_context(request);
        table_info = netsnmp_extract_table_info(request);

        switch (reqinfo->mode) {
        
            case MODE_GET:

                    if(key == 0){   readConfigEthernet(); key = 1;}

                        switch (table_info->colnum) {
                        case 1:
                            snmp_set_var_typed_value(var, ASN_IPADDRESS,(u_char *) &ipConfAddress,sizeof( TYPE ));
                            break;
                        case 2:
                            break;
                        case 3:
                            snmp_set_var_typed_value(var, ASN_IPADDRESS,(u_char *) &ipConfNetMask,sizeof( TYPE ));
                            break;
                        case 4:
                            break;
                        case 5:
                            snmp_set_var_typed_value(var, ASN_IPADDRESS,(u_char *) &gateway,sizeof( TYPE ));
                            break;
                        default:
                            break;
                        }

                    break;

                 case MODE_SET_RESERVE1:

                        switch (table_info->colnum) {
                        case 1:
                            if (requests->requestvb->type != ASN_IPADDRESS) {

                    netsnmp_set_request_error(reqinfo, requests,SNMP_ERR_WRONGTYPE);

                        return SNMP_ERR_WRONGTYPE;
                            }
                            break;
                        case 3:
                           if (requests->requestvb->type != ASN_IPADDRESS) {

                    netsnmp_set_request_error(reqinfo, requests,SNMP_ERR_WRONGTYPE);

                        return SNMP_ERR_WRONGTYPE;
                            }
                            break;
                        case 5:
                       
                            if (requests->requestvb->type != ASN_IPADDRESS) {

                    netsnmp_set_request_error(reqinfo, requests,SNMP_ERR_WRONGTYPE);

                        return SNMP_ERR_WRONGTYPE;
                            }
                            break;
                        default:
                       
                            break;
                        }
     

                    break;

    case MODE_SET_RESERVE2:

                       switch (table_info->colnum) {
                        case 1:
                            memdup((u_char **) & ipConfAddress_cache, (u_char *) & ipConfAddress, sizeof(ipConfAddress));

                            if (ipConfAddress_cache == NULL) {
                                netsnmp_set_request_error(reqinfo, requests,SNMP_ERR_RESOURCEUNAVAILABLE);
                return SNMP_ERR_RESOURCEUNAVAILABLE;
                            }

                            netsnmp_request_add_list_data(requests,netsnmp_create_data_list(DELAYED_INSTANCE_SET_NAME,ipConfAddress_cache, free));
           
                            break;
                        case 3:
                            memdup((u_char **) & ipConfNetMask_cache, (u_char *) & ipConfNetMask, sizeof(ipConfNetMask));

                            if (ipConfNetMask_cache == NULL) {
                                netsnmp_set_request_error(reqinfo, requests,SNMP_ERR_RESOURCEUNAVAILABLE);
                return SNMP_ERR_RESOURCEUNAVAILABLE;
                            }

                            netsnmp_request_add_list_data(requests,netsnmp_create_data_list(DELAYED_INSTANCE_SET_NAME,ipConfNetMask_cache, free));
                            break;
                        case 5:
                            memdup((u_char **) & gateway_cache, (u_char *) & gateway, sizeof(gateway));

                            if (gateway_cache == NULL) {
                                netsnmp_set_request_error(reqinfo, requests,SNMP_ERR_RESOURCEUNAVAILABLE);
                return SNMP_ERR_RESOURCEUNAVAILABLE;
                            }

                            netsnmp_request_add_list_data(requests,netsnmp_create_data_list(DELAYED_INSTANCE_SET_NAME,gateway_cache, free));
                            break;
                        default:
                            break;
                        }
                   break;

        case MODE_SET_ACTION:

                              
                    variable = 0;

                        switch (table_info->colnum) {
                        case 1:

                            ipConfAddress = *(requests->requestvb->val.integer);

                            break;
                        case 3:
                            ipConfNetMask = *(requests->requestvb->val.integer);

                            break;
                        case 5:
                            gateway = *(requests->requestvb->val.integer);
                            break;
                        default:
                            break;
                      }
                    
                break;

    case MODE_SET_UNDO:

                        switch (table_info->colnum) {
                        case 1:
                            ipConfAddress = *((u_long *) netsnmp_request_get_list_data(requests,DELAYED_INSTANCE_SET_NAME));
                            break;
                        case 3:
                            ipConfNetMask = *((u_long *) netsnmp_request_get_list_data(requests,DELAYED_INSTANCE_SET_NAME));
                            break;
                        case 5:
                            gateway = *((u_long *) netsnmp_request_get_list_data(requests,DELAYED_INSTANCE_SET_NAME));
                            break;
                        default:
    
                            break;
     
                    }

                break;


        case MODE_SET_COMMIT:
                    
        case MODE_SET_FREE:

            break;

    }
        }
    return SNMP_ERR_NOERROR;
}

 

Подскажите как создать таблицу из нескольких строк?

Как определить индекс строки к которой обратились из вне?

Как создавать и удалять строки в таблице?

 

Буду благодарен за простой понятный для новичка код на С.

Помогите пожалуйста...

Edited by Oops

Share this post


Link to post
Share on other sites
..Относительно таблиц вроде бы понятно все изложено.

 

подождали бы ещё 4 месяца... была бы круглая дата бы

 

(круглый)

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this