Solaris System Management Agent Developer's Guide

Scalar Objects

Scalar objects are used for singular variables that are not part of a table or an array. If your MIB contains scalar objects, you must run mib2c with a scalar-specific configuration file on the MIB nodes that contain the scalars. You should use the following command, where mibnode1 and mibnode2 are top-level nodes of scalar data for which you want to generate code:


mib2c -c mib2c.scalar.conf mibnode1 mibnode2 ...

You can specify as many nodes of scalar data as you want. This command generates two C code files that are named mibnode.c and mibnode.h for each MIB node that is specified in the command line. You must modify the mibnode1.c and mibnode2.c files to enable the agent to retrieve data from scalar objects. See the mib2c(1M) man page for more information about using the mib2c tool.

Now, compile the MIB and example code as described in demo_module_1 Code Example for Scalar Objects.

demo_module_1 Code Example for Scalar Objects

The demo_module_1 code example is provided to help you understand how to modify the code generated by the mib2c command to perform a scalar data retrieval. The demo_module_1 code example is located by default in the directory /usr/demo/sma_snmp/demo_module_1.

The README_demo_module_1 file contains instructions that describe how to perform the following tasks:

The demo_module_1 is set up to allow you to generate code templates me1LoadGroup.c and me1LoadGroup.h. You can then compare the generated files to the files demo_module_1.c and demo_module_1.h. The mib2c utility generates me1LoadGroup.c, which contains the init_me1LoadGroup() function. You should compare this function to the init_demo_module_1() function in the demo_module_1.c file.

The demo_module_1.c and demo_module_1.h files have been modified appropriately to retrieve scalar data. You can use these files as a model for learning how to work with scalar data in your own module. The instructions then explain how to compile the modified source files to create a functioning module.

Modifications for Scalar Data Retrieval

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:

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:

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.