<< Back to man.ChinaUnix.net

4.      An Introductory Example

Rather than begin by describing SNMP++ and all of its features, here is a simple example that illustrates its power and simplicity.  This example obtains a SNMP MIB System Descriptor object from the specified agent. Included are all code needed to create a SNMP++ session, get the system descriptor, and print it out.  Retries and time-outs are managed automatically. The SNMP++ code is in bold font.



4.1.     A Simple SNMP++ Example


#define SYSDESCR “”              // Object ID for System Descriptor

void get_system_descriptor()


   int status;                                      // return status      

   CTarget ctarget( (IpAddress) “”);        // SNMP++ community target

   Vb vb( SYSDESCR);                             // SNMP++ Variable Binding Object

   Pdu pdu;                                                 // SNMP++ PDU


    //-------[ Construct a SNMP++ SNMP Object ]---------------------------------------

   Snmp snmp( status);                             // Create a SNMP++ session

   if ( status != SNMP_CLASS_SUCCESS) {          // check creation status

      cout << snmp.error_msg( status);                   //  if fail, print error string

      return;  }


   //-------[ Invoke a SNMP++ Get ]-------------------------------------------------------

   pdu += vb;                                    //  add the variable binding to the PDU

   if (  (status = snmp.get(  pdu, ctarget)) != SNMP_CLASS_SUCCESS)

      cout << snmp.error_msg( status);

   else {

      pdu.get_vb( vb,0);                               // extract the variable binding from PDU

      cout << “System Descriptor = ”<< vb.get_printable_value();  }  // print out the value


};  // Thats all!



Explanation of Introductory Example

The actual SNMP++ calls are made up of ten lines of code. A CTarget object is created using the IP address of the agent. A variable binding (Vb) object is then created using the object identifier of the MIB object to retrieve (System Descriptor). The Vb object is then attached to a Pdu object. An Snmp object is used to invoke an SNMP get. Once retrieved, the response message is printed out. All error handling code is included.