<< Back to man.lupaworld.com

24.         Snmp Class Examples 

In addition to the examples in this section, please refer to the files in the table below for complete programs which can be used as useful command line utilities.

Program Name and Description

File Name

SnmpGet, performs SNMP++ get to v1 and v2 agents.

snmpget.cpp

SnmpNext, peforms SNMP++ getNext to v1 and v2 agents.

snmpnext.cpp

SnmpBulk, performs SNMP++ getBulk to v1 and v2 agents.

snmpbulk.cpp

SnmpSet, performs SNMP++ set to v1 and v2 agents.

snmpset.cpp

SnmpTrap, sends v1 or v2 trap to a manager.

snmptrap.cpp

SnmpWalk, walks an agent's MIB using v1 or v2 via GetBulk.

snmpwalk.cpp

 

24.1.  Getting a Single MIB Variable Example

 

#includesnmp_pp.h

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

void get_system_descriptor()

{

   int status;                                                                         // return status      

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

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

   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

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

      cout << snmp.error_msg( status);

   else {

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

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

 

}; 

 

24.2. Getting Multiple MIB Variables Example

#includesnmp_pp.h

#define SYSDESCR “1.3.6.1.2.1.1.1.0”                        // Object ID for system descriptor

#define SYSOBJECTID "1.3.6.1.2.1.1.2.0"                // Object ID for system object ID

#define SYSUPTIME  "1.3.6.1.2.1.1.3.0"               // Object ID for system up time

#define SYSCONTACT "1.3.6.1.2.1.1.4.0"                 // Object ID for system contact

#define SYSNAME "1.3.6.1.2.1.1.5.0"                         // Object ID for system name

#define SYSLOCATION "1.3.6.1.2.1.1.6.0"                 // Object ID for system location

#define SYSSERVICES "1.3.6.1.2.1.1.7.0"                 // Object ID for system services

void get_system_group()

{

   int status;                                                                         // return status      

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

   Vb vb[7];                                                                    // a vb for each object to get

   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;  }

 

  //-------[ build up the vbs to get]-----------------------------------------------------------------

  vb[0].set_oid( SYSDESCR);

  vb[1].set_oid( SYSOBJECTID);

  vb[2].set_oid( SYSUPTIME);

  vb[3].set_oid(SYSCONTACT);

  vb[4].set_oid( SYSNAME);

  vb[5].set_oid( SYSLOCATION);

  vb[6].set_oid( SYSSERVICES);

 

  //----[ append all the vbs to the pdu ]-----------------------------------------------------

  for ( int z=0;z<7;z++)

      pdu += vb[z];

 

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

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

      cout << snmp.error_msg( status);

   else {

      pdu.get_vbs( vb,7);                                      // extract the variable bindings

      for ( int w=0;w<7;w++)

          cout <<  vb[w].get_printable_value() << “\n”;  }  // print out the value

 

}; 

 


24.3. Setting a Single MIB Variable Example

#includesnmp_pp.h

#define SYSLOCATION “1.3.6.1.2.1.1.6.0”                  // Object ID for System location

void set_system_location()

{

   int status;                                                                             // return status

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

   Vb vb( SYSLOCATION);                                              // SNMP++ Variable Binding

   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++ Set ]-------------------------------------------------------

   vb.set_value(“Upstairs Mezzanine”);                            // add location string to vb

   pdu += vb;                                                                      //  add the variable binding

   status = snmp.set(  pdu, ctarget);

   cout << snmp.error_msg(status);

}

 


 

 

 

 

24.4. Setting Multiple MIB Variables Example

#includesnmp_pp.h

 #define SYSCONTACT "1.3.6.1.2.1.1.4.0"                // Object ID for system contact

#define SYSNAME "1.3.6.1.2.1.1.5.0"                         // Object ID for system name

#define SYSLOCATION "1.3.6.1.2.1.1.6.0"                 // Object ID for system location

void multi_set()

{

   int status;                                                        // return status      

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

   Vb vb[3];                                                                   // a vb for each object to get

   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;  }

 

  //-------[ build up the vbs to get]-----------------------------------------------------------------

  vb[0].set_oid( SYSCONTACT);

  vb[0].set_value(“Alan Turing”);

  vb[1].set_oid( SYSNAME);

  vb[1].set_value(“ The Turing Machine”);

  vb[2].set_oid( SYSLOCATION );

  vb[2].set_value(“ Cambridge, UK”);

 

  //----[ append all the vbs to the pdu ]-----------------------------------------------------

  for ( int z=0;z<3;z++)

      pdu += vb[z];

 

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

   status = snmp.set(  pdu, ctarget);

   cout << snmp.error_msg( status);

}

 


24.5. Walking a MIB using Get-Next Example

#include snmp_pp.h                                                     // include snmp++ header file

void mib_walk()

{

   int status;                                                            // return status      

   CTarget target( (IpAddress) “10.4.8.5”);      // SNMP++ v1 target

   Vb vb;                                                                   // a SNMP++ vb

   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;  }

 

  //-------[ set up the first vb ]---------------------------------------------------------------

  vb.set_oid(“1”);                                              // get next starting seed

  pdu += vb;                                                         // add vb to the pdu

 

  status = SNMP_CLASS_SUCCESS;

  while ( status == SNMP_CLASS_SUCCESS)

  {

        if (  (status = snmp.get_next(  pdu, ctarget)) == SNMP_CLASS_SUCCESS) {

        pdu.get_vb( vb,0);                               // extract the vb

        cout << “Mib Object = “ << vb.get_printable_oid() << “\n”;

        cout <<  Mib Value = “ << vb.get_printable_value() << “\n”;

        pdu.set_vb( vb,0);                               // use last vb as the next one

     }

     else

        cout << “SNMP++ Error = “ << snmp.error_msg( status);

  }

};


24.6. Sending a Trap Example

#include snmp_pp.h

void send_trap()

{

   int status;                                                                                // return status      

   CTarget target( (IpAddress) “10.4.8.5”);                       // SNMP++ v1 target

   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;  }

 

   status = snmp.trap( pdu, target,coldStart);

   cout << “ Trap Send Status = “ << snmp.error_msg( status);

};


24.7. Receiving Traps Example

#include “snmp_pp.h

//-----------------[ trap callback function definition ]-------------------------------------------------------------------

void my_trap_callback ( int reason,                                      // reason

                                       Snmp* session,                           // session handle

                                       Pdu & pdu,                                    // trap pdu

                                       TimeTicks &timestamp,            // timestamp

                                       SnmpTarget &target,                   // source of the trap

                                       void * cbd)                                     // optional callback data

{

   Address *address;

   unsigned char get_cummunity[80], set_community[80];

   unsigned long timeout;

   int retry;

 

   if ( reason == SNMP_CLASS_TRAP) {

       target.resolve_to_C( get_community,             // get community

                                         set_community,                  // set community

                                         &address,                            // address object

                                         timeout,                              // timeout

                                         retry);                                      // retry

       cout << “Trap Received from << address->get_printable() << “Trap Id = “ << trapid.get_printable();

   }

   else

      cout << “Trap Receive Error = “ << session->error_msg( reason);

 

};

 

//---------------[ trap receive register ]---------------------------------------------------------------------

Snmp *snmp;                                                                  // dynamic Snmp object

void trap_register()

{

   //----------------[ instantiate an Snmp object, delete when no longer receiving traps ]------------

   int status;

   snmp = new Snmp( status);

   if (( snmp == NULL) || ( status != SNMP_CLASS_SUCCESS))

     cout << “Error constructing Snmp Object\n”;

   else

   {

      //-------[ set up two empty collections, empty denotes receive all ]-------------------------------

      TargetCollection targets;    

      OidCollection trapids;

      //------[ invoke the regsiter ]----------------------------------------------------------------------------

      if ( status = snmp->notify_register(  trapids, targets, & my_trap_callback)) != SNMP_CLASS_SUCCESS)

         cout << “ Snmp Trap Register Error “ << snmp->error_msg( status);

   }

};