home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 8 Other
/
08-Other.zip
/
lnsnmp.zip
/
LNVSNMP.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-14
|
10KB
|
283 lines
/*************************************************************************/
/* Program Name: LNVSNMP.CPP Title: Main Module for SNMP LAN NetView */
/* OS/2 Developer Magazine, Issue: March-April '94 */
/* Article Title: Network Management: Using SNMP for LAN NetView */
/* Author: Stacey Taylor CompuServe ID: 73670,3235 */
/* Phone: (818) 878-7300 Fax: (818) 878-7313 */
/* Description: */
/* This simple program demonstrates the programming techniques required */
/* when retrieving data from an SNMP agent via the LAN NetView platform. */
/* Output from this program (i.e. tracing/debugging data), is written to */
/* a file called, SNMPDATA. */
/* */
/* Program Requirements: */
/* C/Set++ */
/* LAN NetView, Manage Module with Development Toolkit */
/*************************************************************************/
//---------------------------------------------------------
// System Include Files
//---------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <xom.h> // from LAN NetView include directory
#include <xmp.h> // from LAN NetView include directory
#include <xmp_snmp.h> // from LAN NetView include directory
// Prototype functions in the LNV-DATA.CPP module
void DecomposeSNMPStruct ( OM_workspace, OM_public_object );
int GetSNMPData ( OM_workspace, OM_public_object, char *, char * );
//---------------------------------------------------------
// Define the LAN NetView/SNMP interprocess communication
// structures:
// - Create XOM variables for use in the following structures
// - Features List Structure
// - SNMP Get Request Structure
//---------------------------------------------------------
//---------------------------------------------------------
// Create XOM variables. The OM_EXPORT macro execution
// is required for most structures
//---------------------------------------------------------
OM_EXPORT ( MP_C_COMMUNITY_NAME )
OM_EXPORT ( MP_C_NETWORK_ADDRESS )
OM_EXPORT ( MP_C_SNMP_GET_ARGUMENT )
//---------------------------------------------------------
// The FeatureList structure identifies the LAN NetView
// services required by our application.
//---------------------------------------------------------
static MP_feature FeatureList[] =
{
{ OM_STRING(OMP_O_OM_OM), OM_TRUE }, // Required
{ OM_STRING(OMP_O_MP_COMMON_PKG), OM_TRUE }, // Required
{ OM_STRING(OMP_O_MP_SNMP_PKG), OM_TRUE }, // Required for SNMP only
{ {0, NULL}, OM_FALSE} // Terminator
};
//---------------------------------------------------------
// The SNMPGetRequest structure defines who is to answer
// the request and what answers it is to provide. This
// structure requires 2 other structures: IPAddress and
// CommunityName. The IPAddress structure will contain the
// TCP/IP address of the remote 'ED' to receive the SNMP
// request. The CommunityName structure contains the
// SNMP community name (i.e. password). The IP address
// (in the 2nd entry of IPAddress) will be completed
// later in the program. The SNMPGetRequest will ask the
// remote SNMP agent for the values associated with these
// variables:
// - sysContact ( ID of "\x2B\x06\x01\x02\x01\x01\x04" )
// - sysLocation ( ID of "\x2B\x06\x01\x02\x01\x01\x06" )
// - sysName ( ID of "\x2B\x06\x01\x02\x01\x01\x05" )
// - ifInOctets ( ID of "\x2B\x06\x01\x02\x01\x02\x02\x01\x10" )
//---------------------------------------------------------
static OM_descriptor IPAddress[] =
{
OM_OID_DESC(OM_CLASS, MP_C_NETWORK_ADDRESS), // Structure ID
{ MP_IP_ADDRESS, OM_S_OCTET_STRING, {4, "0000"} }, // IP Address
OM_NULL_DESCRIPTOR // Terminator
};
static OM_descriptor CommunityName[] =
{
OM_OID_DESC(OM_CLASS, MP_C_COMMUNITY_NAME), // Structure ID
{ MP_COMMUNITY, OM_S_OCTET_STRING, {6, "public"} }, // SNMP password
OM_NULL_DESCRIPTOR // Terminator
};
static OM_descriptor SNMPGetRequest[] =
{
OM_OID_DESC(OM_CLASS, MP_C_SNMP_GET_ARGUMENT), // Structure ID
{ MP_RESPONDER_IP_ADDRESS, OM_S_OBJECT, {0, IPAddress} }, // struct ptr
{ MP_VAR_ID_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // sysContact
{ MP_VAR_ID_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // ifInOctets
{ MP_VAR_ID_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // sysNameOK
// { MP_VAR_BIND_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // sysName
{ MP_VAR_ID_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // sysLocation
{ MP_ACCESS_CONTROL, OM_S_OBJECT, {0, CommunityName} }, // struct ptr
OM_NULL_DESCRIPTOR // Terminator
};
// ------------------------------ Main --------------------------------------
int main(int argc, char **argv)
{
OM_workspace Workspace; // XOM/XMP workspace handle
MP_status Ret; // XOM/XMP function returns
OM_object Session; // handle to XOM/XMP session
OM_sint32 InvokeID; // handle to SNMP Get Request
int i; // looping variable
char IPTemp[20]; // TCP/IP string address
char IPRemote[5]; // TCP/IP binary address
char Data[128]; // returned SNMP Data
OM_value_position Total; // number of items in object
OM_private_object PrivateGetResponse; // pointer to returned data
OM_public_object SNMPGetResponse; // pointer to useable returned data
// validate that a TCP/IP address was entered (a validation routine
// needs to be added here!)
if (argc != 2)
{
printf("\nPlease enter the TCP/IP address when running this program\n");
printf("For example: C:>LNVSNMP 198.202.254.1\n");
return 1;
}
// Create and initialize an XOM/XMP work area
Workspace = mp_initialize ();
if ( Workspace == NULL )
{
printf ("Workspace initialization error");
return 1;
}
// Define the LAN NetView services we want
Ret = mp_version ( Workspace, FeatureList );
if ( Ret != MP_SUCCESS )
{
printf ( "Version initialization failed");
return 1;
}
// Verify all requested features are available
for ( i = 0; FeatureList[i].feature.length; i++ )
{
if ( FeatureList[i].activated == 0 )
{
printf ( "Feature(s) not available" );
return 1;
}
}
// Create a session with the workspace
Ret = mp_bind ( MP_DEFAULT_SESSION, Workspace, &Session );
if ( Ret != MP_SUCCESS )
{
printf ( "Cannot create session with workspace" );
return 1;
}
// Convert and set the remote 'ED' IP Address into the
// IPAddress structure. See X/Open definition for more
// information on the construction of the IPAddress
// structure.
strcpy ( IPTemp, argv[1] ); // Remote's TCP/IP Address
strcat ( IPTemp, "." ); // attach trailing period
IPRemote[0] = (char) atoi ( strtok ( IPTemp, "." ) ); // convert 1st part
for ( i = 1; i < 4; i++ )
{
IPRemote[i] = (char) atoi ( strtok ( NULL, "." ) ); // convert parts 2-4
}
IPAddress[1].value.string.length = 4; // set into structure
IPAddress[1].value.string.elements = IPRemote; // set into structure
// Place the ID(s) of the information we want returned into
// the SNMPGetRequest structure
at_str_to_oid ( "1.3.6.1.2.1.1.4.0", &SNMPGetRequest[2].value.string );
at_str_to_oid ( "1.3.6.1.2.1.2.2.1.10.1", &SNMPGetRequest[3].value.string );
at_str_to_oid ( "1.3.6.1.2.1.1.5.0", &SNMPGetRequest[4].value.string );
at_str_to_oid ( "1.3.6.1.2.1.1.6.0", &SNMPGetRequest[5].value.string );
// creates SNMPDATA file with a decomposition of our SNMP request
DecomposeSNMPStruct ( Workspace, SNMPGetRequest );
// Issue the SNMP Get Request. This is in synchronous mode.
// The returned data is basically unreadable and must be
// processed though om_get before accessing.
Ret = mp_get_req ( Session, MP_DEFAULT_CONTEXT, SNMPGetRequest,
&PrivateGetResponse, &InvokeID );
if (Ret != MP_SUCCESS)
{
unsigned char ErrorMsg[256];
mp_error_message ( Ret, sizeof ( ErrorMsg ), ErrorMsg );
printf ( "SNMP Get Request Failed: %s", ErrorMsg );
return 1;
}
// Process the data through om_get to make it accessable.
om_get ( PrivateGetResponse, 0, NULL, OM_FALSE, 0,
0,
&SNMPGetResponse, &Total );
// adds to SNMPDATA file with a decomposition of our SNMP response
DecomposeSNMPStruct ( Workspace, SNMPGetResponse );
// Display the returned information from SNMPGetResponse
GetSNMPData ( Workspace, SNMPGetResponse, "1.3.6.1.2.1.1.4.0", Data );
printf ("Contact: %s\n", Data );
GetSNMPData ( Workspace, SNMPGetResponse, "1.3.6.1.2.1.1.5.0", Data );
printf ("Name: %s\n", Data );
GetSNMPData ( Workspace, SNMPGetResponse, "1.3.6.1.2.1.1.6.0", Data );
printf ("Location: %s\n", Data );
GetSNMPData ( Workspace, SNMPGetResponse, "1.3.6.1.2.1.2.2.1.10.1", Data );
printf ("Bytes Received by Interface 1: %s\n", Data );
// clean up memory allocations
om_delete ( PrivateGetResponse );
om_delete ( SNMPGetResponse );
// Delete session with workspace
Ret = mp_unbind ( Session );
if ( Ret != MP_SUCCESS )
{
printf ( "Cannot delete session with workspace" );
return 1;
}
// Shutdown the workspace
Ret = mp_shutdown ( Workspace );
if ( Ret != MP_SUCCESS )
{
printf ( "Cannot shutdown the workspace" );
return 1;
}
return 0;
}