Use net-snmp to develop the SNMP proxy client

Source: Internet
Author: User
Tags snmp
Conclusion: Using net-snmp to develop an SNMP proxy client

I have been working on the SNMP proxy for some time ago. From ignorance to project completion. Some summaries are written here and shared with those who need them.

 

1.Develop MIB files

The MIB file format is: start line; import; start from the root node, layer by layer, each layer can find dependencies on the upper layer. For specific implementation, see the mibs/NET-SNMP-EXAMPLES-MIB.txt in the installation package

Note: For tables that can be added or deleted, an additional row status item must be added to the table. Used to add, delete, and modify an ID row.

Copy the designed MIB file to/usr/local/share/snmp/mibs/

 

2.Use mib2c tool to generate *. C *. h file

 

Set environment variables and generate code using the corresponding configuration file. Be sure to use the root permission.

Export mibs = all;

Mib2c-C configure mibnode

Choose configure:

Use mib2c. iterate_access.conf or mib2c. iterate. conf

Use mib2c. scalar. conf for nodes

Use mib2c. ipvy. conf for trap

(If no file is generated, check whether the previous steps are completed correctly)

(Refer to the files under agent/mibgroup/example/in the installation package to complete development)

 

3.Modify and add the generated. c file to compile the source code.

 

A) node functions

Handle_xxx(Netsnmp_mib_handler * Handler,

Netsnmp_handler_registration * reginfo,

Netsnmp_agent_request_info * reqinfo,

Netsnmp_request_info * requests)

Mode_get is used for get operations;

Mode_set_commit is used for write operations. Obtain node data from requests-> requestvb-> Val. Integer/string.

 

B) Table Function Description (mib2c. iterate_access.conf ):

Initialize_table_xxxtable (void)

Module initialization

 

Xxxtable_handler

Table processing function handle

 

Xxxtable_createentry

Input table parameters, create a new structure, and assign parameters to each item in the structure in sequence. Add the structure to the front of xxxtable_head as the first node.

 

Xxxtable_removeentry

This function can be generated using the mib2c. iterate. conf file.

The entry indicates the table entry, which is set in the MIB file ). You can view the second parameter comment of the netsnmp_table_helper_add_indexes function.

 

Xxxtable_get_first_data_point

Create a linked list manually. Each node contains a table and the header node is assigned to xxxtable_head.

Grant xxxtable_head to * my_loop_context.

 

Xxxtable_get_next_data_point

You must enter the last two parameters of snmp_set_var_value (idx, (u_char *) XXX, sizeof (XXX). XXX is the index item.

 

Xxxtable_create_data_context (netsnmp_variable_list * index_data, int column)

Create a structure template. The entry is passed in by index_data, for example:

Entry-> iplineid = * (index_data-> Val. integer); specify other items as needed. If the rowstatus item exists, the rowstatus is initialized to 0.

Add the created struct to the front of the header node of xxxtable_head.

This function is called in the xxxtable_handler function. The second parameter in the default template is null, and an error occurs during compilation. Set this parameter to 1.

 

Xxxtable_commit_row (void ** my_data_context, int new_or_del)

You can add, modify, and delete rows in this function. Example:

Struct iplinetable_entry * entry = (ipl_t *) * my_data_context;

Switch (Entry-> rowstatus)

Different operations are differentiated based on the Entry-> rowstatus type.

 

Get_xxx (void * data_context, size_t * ret_len)

Get node data content

 

Set_xxx (void * data_context, long * val, size_t val_len)

Set node content

 

C) trap Function

There are two methods to send the trap: one is to use SNMPTRAP in the script, and the other is to use the function to send the trap in the program. Sending function: send_xxxtrap_trap ().

You need to fill in two snmp_varlist_add_variable functions.

Snmp_varlist_add_variable (& var_list, snmptrap_oid, oid_length (snmptrap_oid ),

Asn_object_id, (u_char *) xxxtrap_oid, sizeof (xxxtrap_oid ));

Snmp_varlist_add_variable (& var_list, yyy_oid, oid_length (yyy_oid ),

Asn_octet_str, String, strlen (string ));

Xxxtrap_oid is the trap node number, and yyy_oid is the associated node number. String is the string to be sent.

 

 

4.
Compile

Add extension using dynamic library

Generate a dynamic library:

Gcc-g-I/root/net-snmp-5.2.2/include/-c-o example. O example. c

Gcc-g-FPIC-shared-O example. So example. o

 

Load the dynamic library to the program Node

The configuration file must be added in/etc/snmp/snmpd. conf.

Dlmod example/root/snmpdll/example. So

Then restart the snmpd program.

I have been working on the SNMP proxy for some time ago. From ignorance to project completion. Some summaries are written here and shared with those who need them.

 

1.Develop MIB files

The MIB file format is: start line; import; start from the root node, layer by layer, each layer can find dependencies on the upper layer. For specific implementation, see the mibs/NET-SNMP-EXAMPLES-MIB.txt in the installation package

Note: For tables that can be added or deleted, an additional row status item must be added to the table. Used to add, delete, and modify an ID row.

Copy the designed MIB file to/usr/local/share/snmp/mibs/

 

2.Use mib2c tool to generate *. C *. h file

 

Set environment variables and generate code using the corresponding configuration file. Be sure to use the root permission.

Export mibs = all;

Mib2c-C configure mibnode

Choose configure:

Use mib2c. iterate_access.conf or mib2c. iterate. conf

Use mib2c. scalar. conf for nodes

Use mib2c. ipvy. conf for trap

(If no file is generated, check whether the previous steps are completed correctly)

(Refer to the files under agent/mibgroup/example/in the installation package to complete development)

 

3.Modify and add the generated. c file to compile the source code.

 

A) node functions

Handle_xxx(Netsnmp_mib_handler * Handler,

Netsnmp_handler_registration * reginfo,

Netsnmp_agent_request_info * reqinfo,

Netsnmp_request_info * requests)

Mode_get is used for get operations;

Mode_set_commit is used for write operations. Obtain node data from requests-> requestvb-> Val. Integer/string.

 

B) Table Function Description (mib2c. iterate_access.conf ):

Initialize_table_xxxtable (void)

Module initialization

 

Xxxtable_handler

Table processing function handle

 

Xxxtable_createentry

Input table parameters, create a new structure, and assign parameters to each item in the structure in sequence. Add the structure to the front of xxxtable_head as the first node.

 

Xxxtable_removeentry

This function can be generated using the mib2c. iterate. conf file.

The entry indicates the table entry, which is set in the MIB file ). You can view the second parameter comment of the netsnmp_table_helper_add_indexes function.

 

Xxxtable_get_first_data_point

Create a linked list manually. Each node contains a table and the header node is assigned to xxxtable_head.

Grant xxxtable_head to * my_loop_context.

 

Xxxtable_get_next_data_point

You must enter the last two parameters of snmp_set_var_value (idx, (u_char *) XXX, sizeof (XXX). XXX is the index item.

 

Xxxtable_create_data_context (netsnmp_variable_list * index_data, int column)

Create a structure template. The entry is passed in by index_data, for example:

Entry-> iplineid = * (index_data-> Val. integer); specify other items as needed. If the rowstatus item exists, the rowstatus is initialized to 0.

Add the created struct to the front of the header node of xxxtable_head.

This function is called in the xxxtable_handler function. The second parameter in the default template is null, and an error occurs during compilation. Set this parameter to 1.

 

Xxxtable_commit_row (void ** my_data_context, int new_or_del)

You can add, modify, and delete rows in this function. Example:

Struct iplinetable_entry * entry = (ipl_t *) * my_data_context;

Switch (Entry-> rowstatus)

Different operations are differentiated based on the Entry-> rowstatus type.

 

Get_xxx (void * data_context, size_t * ret_len)

Get node data content

 

Set_xxx (void * data_context, long * val, size_t val_len)

Set node content

 

C) trap Function

There are two methods to send the trap: one is to use SNMPTRAP in the script, and the other is to use the function to send the trap in the program. Sending function: send_xxxtrap_trap ().

You need to fill in two snmp_varlist_add_variable functions.

Snmp_varlist_add_variable (& var_list, snmptrap_oid, oid_length (snmptrap_oid ),

Asn_object_id, (u_char *) xxxtrap_oid, sizeof (xxxtrap_oid ));

Snmp_varlist_add_variable (& var_list, yyy_oid, oid_length (yyy_oid ),

Asn_octet_str, String, strlen (string ));

Xxxtrap_oid is the trap node number, and yyy_oid is the associated node number. String is the string to be sent.

 

 

4.
Compile

Add extension using dynamic library

Generate a dynamic library:

Gcc-g-I/root/net-snmp-5.2.2/include/-c-o example. O example. c

Gcc-g-FPIC-shared-O example. So example. o

 

Load the dynamic library to the program Node

The configuration file must be added in/etc/snmp/snmpd. conf.

Dlmod example/root/snmpdll/example. So

Then restart the snmpd program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.