Object Modeling Technique (OMT) view of
the SNMP++ Snmp Class
|
The SNMP++ Pdu class is a
C++ encapsulation of the SMI Protocol Data Unit (PDU). PDUs
are the basic means of
SNMP communication between a manager and an agent. SNMP++ makes
working with PDUs easy and safe through use of the Pdu class. The Pdu class allows
easy construction, destruction, Vb object loading and
unloading of Pdu objects. Since SNMP++ is a bilingual
API, the Pdu class is abstract and does not contain
SNMP version 1 or version 2 specific information. A
single Pdu object can be used for all Snmp class request member functions. The Pdu class is used to interface with the Snmp
class for SNMP requests and is also used as a callback parameter for
asynchronous requests and notification reception. Note, Pdu
objects are zero based in regard to storage of Vbs ( the first vb in the Pdu is Vb #0 ).
For the most part, all Pdu
objects are the same in SNMP++. That is, all Pdu
objects have identical attributes. The only exception is when using the Pdu objects for sending notifications ,
traps and informs. For notifications, three additional Pdu
Class member functions may be used to set the identity, timestamp and
enterprise of a Pdu object.
Pdu Class Member Functions |
Description
|
Constructors |
|
Pdu::Pdu(
void); |
Construct an empty Pdu. |
Pdu::Pdu(
Vb* pvbs, const int pvb_count); |
Construct a Pdu with an
array of Vbs and size. |
Pdu::Pdu(
const Pdu &pdu); |
Construct a Pdu with
another Pdu. |
Member
Functions |
|
int
get_vblist( Vb* pvbs, const int pvb_count); |
Copies Vbs into caller
parameters. |
int
set_vblist( Vb* pvbs, const int pvb_count); |
Sets callers Vbs into the
Pdu. |
int
get_vb( Vb &vb, const int index); |
Get a particular Vb out
of a Pdu. |
int
set_vb( Vb &vb, const int index); |
Set a particular Vb into
a Vb. |
int
get_vb_count(); |
Get the Vb count from a Pdu. |
int
get_error_status(); |
Get the error status from a Pdu. |
int
get_error_index(); |
Get the error index from a Pdu. |
unsigned long get_request_id(); |
Get the request id from a Pdu. |
unsigned short get_type(); |
Get the Pdu type. |
int
valid(); |
Is the Pdu valid? |
Int
delete_vb( const int
position); |
Removes Vb at specified
position from Pdu. |
int
trim(const int i=1); |
Trim off the last Vb in
the Pdu,.default 1. |
Member
Functions For Inform and Trap Usage |
|
void set_notify_timestamp( const TimeTicks
& timestamp); |
Set timestamp on a trap or inform Pdu. |
void get_notify_timestamp( TimeTicks
& timestamp); |
Get the timestamp from a trap or inform Pdu object. |
void set_notify_id( const Oid id); |
Set the ID on a trap or inform Pdu. |
void get_notify_id( Oid &id); |
Get the ID from a trap or inform Pdu. |
void set_notify_enterprise( const Oid
&enterprise); |
Set the enterprise ID on a trap or inform Pdu. |
void get_notify_enterprise( Oid
& enterprise); |
Get the enterprise ID on a trap or inform Pdu. |
Overloaded
Operators |
|
Pdu&
operator=( const Pdu &pdu); |
Assign one Pdu to
another. |
Pdu&
operator+=( Vb &vb); |
Append a Vb to a Pdu; |
There are a variety of ways to construct Pdu objects including with and without construction
parameters.
//
constructor, no args Pdu::Pdu( void); //
constructor with Vbs and count Pdu::Pdu( Vb
*vbs, const int vb_count); //
constructor with another Pdu instance Pdu::Pdu( const Pdu
&pdu); //
destructor Pdu::~Pdu(); |
The Pdu class
supports a variety of member functions for getting and setting Pdu member variables. Included are ways to get and set the
variable bindings, error information , request
identification, and type information.
//
extract all the Vbs from a Pdu int
Pdu::get_vblist( Vb* vbs, const int vb_count); //
deposit Vbs to a Pdu int
Pdu::set_vblist( Vb* vbs, const int vb_count); //
get a particular vb //
where index 0 is the 1st vb int
Pdu::get_vb( Vb &vb, const int index); //
set a particular Vb //
where index 0 is the 1st Vb int
Pdu::set_vb( Vb &vb, const int index); //
return the number of Vbs int
Pdu::get_vb_count(); //
return the error index int
Pdu::get_error_index(); //
get the error status int
Pdu::get_error_status(); //
return the request id unsigned long Pdu::get_request_id(); //
get the Pdu type unsigned short Pdu::get_type(); //
return the validity of a Pdu int
Pdu::valid(); |
The Pdu class supports
overloaded operators for assignment and concatenation of Vb
objects to the Pdu.
//
assignment operator for assigning one Pdu to
another Pdu& operator=( const Pdu &pdu); //
append a Vb object to the Pdu’s
var bind list Pdy& operator+=( Vb vb); |
// set notify timestamp void
Pdu::set_notify_timestamp( const TimeTicks & timestamp); // get notify timestamp void
Pdu::get_notify_timestamp( TimeTicks
& timestamp); // set the notify id void
Pdu::set_notify_id( const Oid
id); // get the notify id void
Pdu::get_notify_id( Oid
&id); // set the notify enterprise void
Pdu::set_notify_enterprise( const Oid &enterprise); // get the notify enterprise void
Pdu::get_notify_enterprise( Oid
& enterprise); |
When
working with notifications, traps and informs, SNMP++ provides member functions
for getting and setting notification specific values. For usage on these member
functions, please refer to the section on sending traps and informs.
In order to use a Pdu
object in a management application, the variable binding list must be loaded into
the Pdu instance. This can be done in a variety of
manners depending on your needs. Loading a Pdu is
typically done before making a SNMP request.
//
example of how to load a Pdu object void load_pdu_examples() {
Pdu pdu;
// create a Pdu
object Vb vb;
// create a Vb
object
vb.set_oid( SYSDECR); //
set the oid portion of the Vb
to System Descriptor pdu += vb;
// loads the Vb to the Pdu
Pdu my_pdu;
// create another Pdu
object
Vb vbs[5];
// create 5 vbs
pdu.set_vblist( vbs,5);
// load all 5 to the pdu } |
After picking up a response Pdu
either from a blocked or asynchronous request, it is necessary to unload the Vbs so that the SMI values may then be extracted.
//
example of how to unload a Pdu void unload_pdu_example(
Pdu &pdu) { ins staus; Pdu pdu;
// create a Pdu
object Vb vb;
// create a Vb
object vb.set_oid( SYSDECR); //
set the oid portion of the Vb
to System Descriptor pdu
+= vb;
// loads the Vb to the Pdu char message[100];
// for the system descriptor printable
octet Snmp snmp( status); if ( status != SNMP_CLASS_SUCCESS) { cout < “SNMP++ error = “
<< snmp.error_msg( status) ; return;
} pdu.get_vb( vb,0);
//
unload the vb vb.get_value(
message);
// pull the message out of the vb cout << message;
// print it out }; |