The following example code, taken from the SNMPD-SMARTCTL extension for harddisk SMART Data, demonstrates the basic use of the snmpd-connector-lib library.
The first step toward using the snmpd-connector-lib library is to source it into your script.
As you can see the recommended method of including the snmpd-connector-lib library allows the location of the script file to be specified at run-time using the SNMPD_CONNECTOR_LIB variable. This may be useful if you wish to modify or extend the library yourself or if the library is installed in a non-standard location.
The second step is to define the MIB structure as a series of one or more Bash arrays and declare the BASE_MIB variable, as shown in the example below.
As the above example demonstrates the BASE_MIB variable should be set to the root element to which this extension agent should be bound. Each entry in the RTABLE (root table) array will appear directly beneath the element specified in BASE_MIB.
To declare another level of elements beneath the root entries (in this case corresponding to the smartCtlEntry node of the smartCtlTable table) another array may declared and entered into the parent table by prefixing the array name with a # symbol.
Finally, the FTABLE array in the above example provides an index function (declared by appending _INDEX to the table name) and a list of functions which may be called (with an index as a parameter) by the extension agent to obtain the data values represented by this MIB from the system.
When declaring a table, as opposed to a list of objects, an index function is required to enable the snmpd-connector-lib library firstly to detect that this is in fact a table and secondly so that the correct index values may be automatically returned when presented with an appropriate GETNEXT request. As you can see from the example below the function will usually be fairly trivial.
The index function may be called by the snmpd-connector-lib library in two distinct modes. In the first mode no index will be supplied indicating that the first index is requested. In the second mode an index value will be supplied indicating that the next index value should be returned. If the next index would be out of range then nothing should be returned. Any return value should be sent to stdout for capture.
The fourth, and most complex step, is to implement the MIB specific functions which we have named when we declared the MIB objects in step two above. These functions will be called automatically by the snmpd-connector-lib library (with the appropriate index parameter if they are part of a table) and are responsible for retrieving the data from the system and sending it to the requesting agent (usually using one of the helper functions provided in the snmpd-connector-lib library for this purpose).
The above example implements a simple function to return the index of the queried entry in a table. As you can see the function simply checks that the supplied index value is in range and, if it is, sends it back to the requesting agent using the send_integer function. Should the supplied index not be in range then this is indicated to the requesting agent using the send_none function.
Finally, the last step is to call the main entry point in the snmpd-connector-lib library.
As you can see from the above example this is easily accomplished with a single call to the_loop which will start the extension agent listening for requests.