Net-snmp Add User-Defined MIB and net-snmp User-Defined mib

Source: Internet
Author: User
Tags snmp

Net-snmp Add User-Defined MIB and net-snmp User-Defined mib
I know three methods to add a user-defined MIB. static Load. c and. add the H file to the corresponding location and re-compile the snmp library. The advantage is that you do not need to modify the configuration file. The disadvantage is that you have to re-compile each addition. 2. dynamically load the generated. c and. re-compile the H file. so library, modify snmpd. conf configuration file. The advantage is that you do not need to re-compile each addition. The disadvantage is that you must support the dlmod command. 3. child proxy extension, which will generate. c and. the H file can be compiled into an executable program and run the program and snmpd. The advantage is that the operation is simple, and the disadvantage is that you need to run two programs. The first steps of the three methods are the same. The MIB are compiled to generate. c and. H files and complete the. c files. 1. For details about how to compile the MIB syntax, see callback. The following is my MIB file. -- Test-SLK-MIB.txt Test-SLK-MIB DEFINITIONS: = begin imports object-GROUP, MODULE-COMPLIANCE, NOTIFICATION-group from SNMPv2-CONF enterprises, Integer32, Unsigned32, OBJECT-TYPE, MODULE-IDENTITY, NOTIFICATION-type from SNMPv2-SMI DisplayString FROM SNMPv2-TC; Test MODULE-identity last-UPDATED "201601221450Z" -- ORGANIZATION "" CONTACT-INFO "" DESCRIPTION "Video's Server MIB must end with Z. ": = {consumer I Ses 745352} Time object identifier: = {Test 1} GetTime OBJECT-type syntax DisplayString MAX-ACCESS read-only STATUS current DESCRIPTION "Example: 2016/1/22 ":: = {Time 1} END -- Test-SLK-MIB.txt this MIB file is very simple, only one OID 1.3.6.1.4.1.745352.1.1, put this MIB in the MIBS folder, my location is/usr/local/snmp/share/snmp/mibs. 2. generate. c and. h file Run Command mib2c Test-SLK-MIB: Test appears options choose 2 and 1. [root @ localhost mibs] # env MIBS = "+/etc/snmp/mibs/Test-MIB.my" mib2c Testwriting to-mib2c has multiple configuration files depending on the type ofcode you need to write. you must pick one depending on your need. you requested mib2c to be run on the following part of the MIB tree: OID: Test numeric translation :. 1.3.6.1.4.1.16535 number of scal Ars within: 1 number of tables within: 0 number of communications within: 0 First, do you want to generate code that is compatible with the ucd-snmp 4.X line of code, or code for the newer Net-SNMP 5.X codebase (which provides a much greater choice of APIs to pick from): 1) ucd-snmp style code 2) net-SNMP style code Select your choice: 2 *************************************** ********************* * ********* Generating code for scalar objects: **************************************** * **************************** It looks like you have some scalars in the mib you requested, so I will now generate code for them if you wish. you have two choices for scalar API styles currently. pick between them, or choose not to generate any code for the scalars: 1) If you're writing code for some generic sca Lars (by hand use: "mib2c-c mib2c. scalar. conf Test ") 2) If you want to magically" tie "integer variables to integer scalars (by hand use:" mib2c-c mib2c.int _ watch. conf Test ") 3) Don't generate any code for the scalars Select your choice: 1 using the mib2c. scalar. conf configuration file to generate your code. writing to Test. hwriting to Test. c *************************************** *************** * **************** Note well: The code generated by mib2c is only a template. * YOU *** must fill in the code before it'll work most of the time. in between ** cases, spots that MUST be edited within the files are marked with **/* XXX */or/* TODO */comments. **************************************** * ****************************** running indent on Test. hrunning indent on Test. c-generated Test. c file:/** Note: This file originally auto-generated by mib2c using * $ */# include <net-snmp/net-snmp-config.h> # include <net-snmp/net-snmp-includes.h> # include <net-snmp/agent/ net-snmp-agent-includes.h> # include "Test. h "/** Initializes the Test module */voidinit_Test (void) {const oid GetTime_oid [] = {1, 3, 6, 1, 4, 1, 745352, 1, 1}; DEBUGMSGTL ("Test", "Initializing \ n"); netsnmp_register_scalar (netsnmp_create _ Handler_registration ("GetTime", handle_GetTime, GetTime_oid, OID_LENGTH (GetTime_oid), HANDLER_CAN_RONLY);} Handle (handler * handler, handler * reginfo, handler * reqinfo, handler * requests) {/** We are never called for a GETNEXT if it's registered as a * "instance", as it's "magically" handled for us. * // ** a instance ha Ndler also only hands us one request at a time, so * we don't need to loop over a list of requests; we'll only get one. */switch (reqinfo-> mode) {case MODE_GET: snmp_set_var_typed_value (requests-> requestvb, ASN_OCTET_STR,/** XXX: a pointer to the scalar's data */, /** XXX: the length of the data in bytes */); break; default:/** we shoshould never get here, so this is a really bad error */sn Mp_log (LOG_ERR, "unknown mode (% d) in handle_GetTime \ n", reqinfo-> mode); return SNMP_ERR_GENERR;} return SNMP_ERR_NOERROR;} 3. complete Test. c. Add the corresponding value at XXX in the code, which is described in the Code. XXX: a pointer to the scalar's data. We need to fill in a pointer, XXX: the length of the data in bytes requires us to fill in the data size, of course, we must first define and then get it. After completion/** Note: this file originally auto-generated by mib2c using * $ */# include <net-snmp/net-snmp-config.h> # include <net-snmp/net-snmp-includes.h> # include <net-snmp/agent/ net-snmp-agent-includes.h> # include "Test. h "# include <time. h>/** Initializes the Test module */voidinit_Test (void) {const oid GetTime_oid [] = {1, 3, 6, 1, 4, 1, 745352, 1, 1}; DEBUGMSGTL ("Test", "Initializing \ n"); netsnmp_r Egister_scalar (hour ("GetTime", handle_GetTime, GetTime_oid, OID_LENGTH (GetTime_oid), interval);} hour (hour * handler, interval * reginfo, interval * reqinfo, interval * requests) {/** We are never called for a GETNEXT if it's registered as a * "instance", as it's "magically" handled For us. * // ** a instance handler also only hands us one request at a time, so * we don't need to loop over a list of requests; we'll only get one. */time_t t; switch (reqinfo-> mode) {case MODE_GET: time (& t); char szTime [100]; snprintf (szTime, 100, "% s ", ctime (& t); snmp_set_var_typed_value (requests-> requestvb, ASN_OCTET_STR,/** XXX: a pointer to the scalar's data */szTime,/* XXX: the lengt H of the data in bytes */strlen (szTime); break; default:/** we shoshould never get here, so this is a really bad error */snmp_log (LOG_ERR, "unknown mode (% d) in handle_GetTime \ n", reqinfo-> mode); return SNMP_ERR_GENERR;} return SNMP_ERR_NOERROR;} The procedure varies depending on the method. I. Static links copy Test. c and Test. h to/net-snmp-5.7.3/agent/mibgroups, here is the net-snmp source code. Compile./configure -- prefix =/usr/local/snmp -- with-mib-modules = Test, make & make install. Static Loading successful 2. Dynamic Loading and writing of makefile files CC = gccFLAGS =-I. 'Net-snmp-config -- cflags '-gDLFLAGS =-shared-fPIC-g Test. so: Test. c $ (CC) $ (CFLAGS) $ (DLFLAGS)-c-o Test. o Test. c $ (CC) $ (CFLAGS) $ (DLFLAGS)-o Test. so Test. o. PHONY: cleanclean: rm-f *. so *. o Compile and generate. so library. Modify snmpd. conf configuration file, add dlmod Test $ {Test. so absolute path}/Test. so start snmpd,/usr/local/snmpd-f-L-DTest, dlmod-c/usr/local/snmp/etc/snmpd. conf Dynamic Loading complete 3. The sub-proxy extension generates the Test program: net-snmp-config -- compile-subagent Test. c start snmpd, Test/usr/local/snmpd-c/usr/local/snmp/etc/snmpd. conf. /Test completed

Related Article

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.