SNMP is a powerful network management application development package. It provides all commands described in the SNMP NMS protocol and provides resolution of SMI data types. The MIB data contains normal data and table data. When extracting the table data, since the number of items and the OID are uid, the value cannot be obtained directly through a particular OID. Typically, the use of the getNext command will be described in the book of SNMP to implement the traversal. This method is relatively simple, and the principle of this algorithm is mainly discussed and how to use SNMP .
The MIB table is described by rows and columns. One of the list is the original OID of each entry, while the head head is index. This way, an OID and an index have only identified one of the tables. For example, in the interface table, IFDesCR (OID is
1.3.6
. 1.2.1.2.2.1.2) is a column, and the specific entry is a row for an interface. In this way, the OID of a specific entry is expressed as: the form of OID INDEX. The picture below describes a format of a table.
iFindex
1.3.6
17.2.1.2.1.1.1 IFDESCR
1.3.6
17.2.1.2.2.1.2 itype
1.3.6
17.2.1.2.1.3 IFMTU
1.3.6
17.2.1.2.2.1.4 iFSPEED
1.3.6
. 1.2.1.2.1.5 Index0 xxx xxx xxx xxx xxx index1 xxx xxx xxx xxx xxx index2 xxx xxx xxx xxx xxx
According to the protocol description, the most basic method is to obtain a table item by index. But in fact, Index itself is also a group, plus some of the tables need multiple index, and various index data types are different, such as INDEX to manually handle IP address types, this method is very difficult. Operationality. Therefore, in actual programming, some comparison methods can be taken.
From SNMP, the description of the getNext command can be seen that if the parameters of GetNext are the head OID of a list in a table, such as the front IFDESCR (
1.3.6
. 1.2.1.2.2.1.2), the resulting value is the first row element value of the column, and the OID of the value can be obtained. The value of the second row can be obtained using GetNext using GetNext again. In this way, if it comes to the last line of the column, then getnext will get the first row of the next column. If the last element of the table is reached, the next element value obtained by the MIB tree is obtained using GetNext. Obviously, in the case of the offshore, the front portion of the OID has been different from the OID of the header, so it can be judged by the obtained OID value.
The following is the main code:
/ * Extract a particular item from the agent, namely a column in the table * /
Void get_table (OID * ITEM_OID, CTARGET * TARGET)
{
Genaddress address;
Target-> get_address (address); Oid ful_oids [max_index]; // Used to save all OID
INT index_count = 0; // The number of rows of this column
BOOL TAG = true; // Sign Cycle ends
For (; tag == true;)
{
PDU PDU;
VB VB;
vb.set_oid (item_oid);
PDU = VB;
Int status;
IF ((status = snmp-> get_next (pdu, * target) == snmp_class_success) {
PDU.GET_VB (VB, 0);
Oid full_oid; // This item's OID
vb.get_oid (full_oid);
/ / Judgment is a cross-boundary, if so, end cycles
IF (item_oid -> ncompare (item_oid -> len (), full_oid) == 0)
{
vb.get_oid (full_oids [index_count]);
INDEX_COUNT ;
/ *
Data processing here
* /
}
Else
{
Tag = false;
}
}
Else
{
Tag = false;
}
}
}
The above algorithm is to extract a column from the table, so how to extract a line? It can be improved to the above algorithm to suit our needs. However, the getNext command is in column. When we want to use it to get a line or must first obtain at least one column information, that is, in the travel calendar algorithm, or contain the above code. This method is successful after practice, and only the algorithm is described here.
First, the above code is to be executed, but the resulting full OID must be saved when data processing. We know that the obtained OID is actually consisting of column OID Index, and the column OID is known, then if we will replace the column OID portion in front of the obtained OID, you can get the other column OID. Complete OID. In most cases, different column OIDs in the same table only differ only, so the replacement method is relatively simple. Here, we only need to replace one (other cases, you can only make changes), the algorithm is as follows:
// Extract table data
Oid row_oid [MAX_OID_NUM];
/ *
First include the previously extracted code here,
And save Full_OID in the data processing at the array row_oid
* /
/ / It is assumed that all of the items of the first column have been saved in the array row_oid.
// Cycle
For (int i = 0; i { Oid OID; / / Get the data of each column in this line For (OID = first column OID; OID { PDU PDU; VB VB; // Replace, if necessary, other methods can be used, and the simplest case is selected only by replacing one bit. Row_oid [I] [OID.LEN () - 1] = OID [OID.LEN () - 1]; vb.set_oid (row_oid [i]); PDU = VB; Int status; IF ((status = snmp-> get (PDU, * Target)) == snmp_class_success) { PDU.GET_VB (VB, 0); / * Data processing here * / } Else { COUT << snmp-> error_msg (status) << "/ n"; } } In fact, there are a lot of methods for SNMP extraction table data, which is the simplest and most basic way. And for different development kits, there is a better way to support the table, such as the ADVENTSNMP development package with direct table operation functions. Regardless of how the method is used, it is very helpful to understand the most basic working principle.