The demo_module_1 example code, demo_module_1.c, provides the system load average for 1, 5 and 15 minutes, respectively.
The init_demo_module_1() function call defines the OIDs for the following three scalar objects:
me1SystemLoadAvg1min
me1SystemLoadAvg5min
me1SystemLoadAvg15min
These OIDs are set up in the demo_module_1.c source file, to reflect what is in the SDK-DEMO1-MIB.txt. The OIDs are defined as follows:
static oid me1SystemLoadAvg15min_oid[] = { 1,3,6,1,4,1,42,2,2,4,4,1,1,3, 0 }; static oid me1SystemLoadAvg1min_oid[] = { 1,3,6,1,4,1,42,2,2,4,4,1,1,1, 0 }; static oid me1SystemLoadAvg5min_oid[] = { 1,3,6,1,4,1,42,2,2,4,4,1,1,2, 0}; |
The mib2c command used the netsnmp_register_read_only_instance() function to register these handler functions:
get_me1SystemLoadAvg1min()
get_me1SystemLoadAvg5min()
get_me1SystemLoadAvg15min()
In this way, when a GET or GET_NEXT request is received, the corresponding handler function is called.
For example, for the 15 minute load average, you can manually register the get_me1SystemLoadAvg15min() handler function. The handler retrieves data on the me1SystemLoadAvg15min scalar. You must place the handler in the netsnmp_register_read_only_instance() function as follows:
netsnmp_register_read_only_instance (netsnmp_create_handler_registration ("me1SystemLoadAvg15min", get_me1SystemLoadAvg15min, me1SystemLoadAvg15min_oid, OID_LENGTH(me1SystemLoadAvg15min_oid), HANDLER_CAN_RONLY)); |
Alternatively, you can use the mib2c command to generate the function bodies of the handler functions for you. Replace /* XXX... in the generated code with your own data structure for returning the data to the requests. For instance, the following code must be modified:
case MODE_GET: snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR, (u_char *) /* XXX: a pointer to the scalar's data */, /* XXX: the length of the data in bytes */); break;
This code must be modified to include your own data structure for returning data to the requests. Replace the /* XXX... that is shown in the preceding code.
case MODE_GET: data = getLoadAvg(LOADAVG_1MIN); snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR, (u_char *) data , strlen(data)); free(data); break;
Note that the input MIB file contains the specification of a table as well as scalar data. When you run mib2c -c mib2c.scalar.conf scalar-node the template code is generated only for the scalar nodes in the MIB.