home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / lnsnmp.zip / LNVSNMP.CPP < prev    next >
C/C++ Source or Header  |  1994-03-14  |  10KB  |  283 lines

  1. /*************************************************************************/
  2. /* Program Name: LNVSNMP.CPP  Title: Main Module for SNMP LAN NetView    */
  3. /* OS/2 Developer Magazine, Issue: March-April '94                       */
  4. /* Article Title: Network Management: Using SNMP for LAN NetView         */
  5. /* Author: Stacey Taylor   CompuServe ID: 73670,3235                     */
  6. /*                         Phone: (818) 878-7300 Fax: (818) 878-7313     */
  7. /* Description:                                                          */
  8. /*  This simple program demonstrates the programming techniques required */
  9. /* when retrieving data from an SNMP agent via the LAN NetView platform. */
  10. /* Output from this program (i.e. tracing/debugging data), is written to */
  11. /* a file called, SNMPDATA.                                              */
  12. /*                                                                       */
  13. /* Program Requirements:                                                 */
  14. /*   C/Set++                                                             */
  15. /*   LAN NetView, Manage Module with Development Toolkit                 */
  16. /*************************************************************************/
  17.  
  18.  
  19. //---------------------------------------------------------
  20. // System Include Files
  21. //---------------------------------------------------------
  22. #include <stdlib.h>
  23. #include <stdio.h> 
  24. #include <string.h>
  25. #include <xom.h>        // from LAN NetView include directory
  26. #include <xmp.h>        // from LAN NetView include directory
  27. #include <xmp_snmp.h>   // from LAN NetView include directory
  28.  
  29.  
  30. // Prototype functions in the LNV-DATA.CPP module
  31.  
  32. void DecomposeSNMPStruct ( OM_workspace, OM_public_object );
  33. int GetSNMPData ( OM_workspace, OM_public_object, char *, char * );
  34.  
  35.  
  36.  
  37. //---------------------------------------------------------
  38. // Define the LAN NetView/SNMP interprocess communication 
  39. // structures:
  40. //  - Create XOM variables for use in the following structures
  41. //  - Features List Structure
  42. //  - SNMP Get Request Structure
  43. //---------------------------------------------------------
  44.  
  45.  
  46. //---------------------------------------------------------
  47. // Create XOM variables.  The OM_EXPORT macro execution
  48. // is required for most structures
  49. //---------------------------------------------------------
  50. OM_EXPORT ( MP_C_COMMUNITY_NAME )
  51. OM_EXPORT ( MP_C_NETWORK_ADDRESS )
  52. OM_EXPORT ( MP_C_SNMP_GET_ARGUMENT )
  53.  
  54.  
  55. //---------------------------------------------------------
  56. // The FeatureList structure identifies the LAN NetView 
  57. // services required by our application.
  58. //---------------------------------------------------------
  59. static MP_feature FeatureList[] = 
  60. {
  61.    { OM_STRING(OMP_O_OM_OM),         OM_TRUE },   // Required
  62.    { OM_STRING(OMP_O_MP_COMMON_PKG), OM_TRUE },   // Required
  63.    { OM_STRING(OMP_O_MP_SNMP_PKG),   OM_TRUE },   // Required for SNMP only
  64.    { {0, NULL}, OM_FALSE}                         // Terminator
  65. };
  66.  
  67.  
  68. //---------------------------------------------------------
  69. // The SNMPGetRequest structure defines who is to answer
  70. // the request and what answers it is to provide. This 
  71. // structure requires 2 other structures: IPAddress and
  72. // CommunityName. The IPAddress structure will contain the
  73. // TCP/IP address of the remote 'ED' to receive the SNMP
  74. // request.  The CommunityName structure contains the 
  75. // SNMP community name (i.e. password). The IP address 
  76. // (in the 2nd entry of IPAddress) will be completed 
  77. // later in the program. The SNMPGetRequest will ask the
  78. // remote SNMP agent for the values associated with these
  79. // variables:
  80. //    - sysContact  ( ID of "\x2B\x06\x01\x02\x01\x01\x04" )
  81. //    - sysLocation ( ID of "\x2B\x06\x01\x02\x01\x01\x06" )
  82. //    - sysName     ( ID of "\x2B\x06\x01\x02\x01\x01\x05" )
  83. //    - ifInOctets  ( ID of "\x2B\x06\x01\x02\x01\x02\x02\x01\x10" )
  84. //---------------------------------------------------------
  85. static OM_descriptor IPAddress[] =
  86. {
  87.    OM_OID_DESC(OM_CLASS, MP_C_NETWORK_ADDRESS),        // Structure ID  
  88.    { MP_IP_ADDRESS, OM_S_OCTET_STRING, {4, "0000"} },  // IP Address
  89.    OM_NULL_DESCRIPTOR                                  // Terminator
  90. };
  91.  
  92. static OM_descriptor CommunityName[] =
  93. {
  94.    OM_OID_DESC(OM_CLASS, MP_C_COMMUNITY_NAME),         // Structure ID  
  95.    { MP_COMMUNITY, OM_S_OCTET_STRING, {6, "public"} }, // SNMP password
  96.    OM_NULL_DESCRIPTOR                                  // Terminator
  97. };
  98.  
  99. static OM_descriptor SNMPGetRequest[] =
  100. {
  101.    OM_OID_DESC(OM_CLASS, MP_C_SNMP_GET_ARGUMENT),      // Structure ID  
  102.    { MP_RESPONDER_IP_ADDRESS, OM_S_OBJECT, {0, IPAddress} }, // struct ptr
  103.    { MP_VAR_ID_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // sysContact
  104.    { MP_VAR_ID_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // ifInOctets
  105.    { MP_VAR_ID_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // sysNameOK
  106. //   { MP_VAR_BIND_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // sysName
  107.    { MP_VAR_ID_LIST, OM_S_OBJECT_IDENTIFIER_STRING, {1, "0"} }, // sysLocation
  108.    { MP_ACCESS_CONTROL, OM_S_OBJECT, {0, CommunityName} },   // struct ptr
  109.    OM_NULL_DESCRIPTOR                                 // Terminator
  110. };
  111.  
  112.  
  113.  
  114. // ------------------------------ Main --------------------------------------
  115.  
  116. int main(int argc, char **argv)
  117. {
  118.    OM_workspace Workspace;       // XOM/XMP workspace handle
  119.    MP_status    Ret;             // XOM/XMP function returns
  120.    OM_object    Session;         // handle to XOM/XMP session
  121.    OM_sint32    InvokeID;        // handle to SNMP Get Request
  122.    int          i;               // looping variable
  123.    char         IPTemp[20];      // TCP/IP string address
  124.    char         IPRemote[5];     // TCP/IP binary address 
  125.    char         Data[128];       // returned SNMP Data
  126.    OM_value_position Total;              // number of items in object
  127.    OM_private_object PrivateGetResponse; // pointer to returned data
  128.    OM_public_object  SNMPGetResponse;    // pointer to useable returned data
  129.  
  130.  
  131.    // validate that a TCP/IP address was entered (a validation routine
  132.    // needs to be added here!)
  133.  
  134.    if (argc != 2)
  135.    {
  136.       printf("\nPlease enter the TCP/IP address when running this program\n");
  137.       printf("For example:  C:>LNVSNMP 198.202.254.1\n");
  138.       return 1;
  139.    }
  140.  
  141.    //  Create and initialize an XOM/XMP work area
  142.  
  143.    Workspace = mp_initialize ();
  144.    if ( Workspace == NULL )
  145.    {
  146.       printf ("Workspace initialization error");
  147.       return 1;
  148.    }
  149.  
  150.  
  151.    // Define the LAN NetView services we want
  152.  
  153.    Ret = mp_version ( Workspace, FeatureList );
  154.    if ( Ret != MP_SUCCESS )
  155.    {
  156.       printf ( "Version initialization failed");
  157.       return 1;
  158.    }
  159.  
  160.  
  161.    // Verify all requested features are available
  162.  
  163.    for ( i = 0; FeatureList[i].feature.length; i++ )
  164.    {
  165.       if ( FeatureList[i].activated == 0 )
  166.       {
  167.          printf ( "Feature(s) not available" );
  168.          return 1;
  169.       }
  170.    }
  171.  
  172.  
  173.    // Create a session with the workspace
  174.  
  175.    Ret = mp_bind ( MP_DEFAULT_SESSION, Workspace, &Session );
  176.    if ( Ret != MP_SUCCESS )
  177.    {
  178.       printf ( "Cannot create session with workspace" );
  179.       return 1;
  180.    }
  181.  
  182.  
  183.    // Convert and set the remote 'ED' IP Address into the
  184.    // IPAddress structure.  See X/Open definition for more
  185.    // information on the construction of the IPAddress
  186.    // structure.
  187.  
  188.    strcpy ( IPTemp, argv[1] );    // Remote's TCP/IP Address
  189.    strcat ( IPTemp, "." );              // attach trailing period
  190.    IPRemote[0] = (char) atoi ( strtok ( IPTemp, "." ) ); // convert 1st part
  191.    for ( i = 1; i < 4; i++ )
  192.    {
  193.       IPRemote[i] = (char) atoi ( strtok ( NULL, "." ) ); // convert parts 2-4
  194.    }
  195.    IPAddress[1].value.string.length   = 4; // set into structure
  196.    IPAddress[1].value.string.elements = IPRemote; // set into structure
  197.  
  198.  
  199.    // Place the ID(s) of the information we want returned into
  200.    // the SNMPGetRequest structure
  201.  
  202.    at_str_to_oid ( "1.3.6.1.2.1.1.4.0", &SNMPGetRequest[2].value.string );
  203.    at_str_to_oid ( "1.3.6.1.2.1.2.2.1.10.1", &SNMPGetRequest[3].value.string );
  204.    at_str_to_oid ( "1.3.6.1.2.1.1.5.0", &SNMPGetRequest[4].value.string );
  205.    at_str_to_oid ( "1.3.6.1.2.1.1.6.0", &SNMPGetRequest[5].value.string );
  206.  
  207.  
  208.    // creates SNMPDATA file with a decomposition of our SNMP request
  209.  
  210.    DecomposeSNMPStruct ( Workspace, SNMPGetRequest );
  211.  
  212.  
  213.    // Issue the SNMP Get Request.  This is in synchronous mode.
  214.    // The returned data is basically unreadable and must be
  215.    // processed though om_get before accessing.
  216.  
  217.    Ret = mp_get_req ( Session, MP_DEFAULT_CONTEXT, SNMPGetRequest,
  218.                       &PrivateGetResponse, &InvokeID );
  219.    if (Ret != MP_SUCCESS)
  220.    {
  221.       unsigned char ErrorMsg[256];
  222.       mp_error_message ( Ret, sizeof ( ErrorMsg ), ErrorMsg );
  223.       printf ( "SNMP Get Request Failed: %s", ErrorMsg );
  224.       return 1;
  225.    }
  226.  
  227.  
  228.  
  229.    // Process the data through om_get to make it accessable.
  230.  
  231.    om_get ( PrivateGetResponse, 0, NULL, OM_FALSE, 0,
  232.                     0, 
  233.                     &SNMPGetResponse, &Total );
  234.  
  235.  
  236.    // adds to SNMPDATA file with a decomposition of our SNMP response
  237.  
  238.    DecomposeSNMPStruct ( Workspace, SNMPGetResponse );
  239.  
  240.  
  241.    // Display the returned information from SNMPGetResponse
  242.  
  243.    GetSNMPData ( Workspace, SNMPGetResponse, "1.3.6.1.2.1.1.4.0", Data );
  244.    printf ("Contact: %s\n", Data );
  245.    GetSNMPData ( Workspace, SNMPGetResponse, "1.3.6.1.2.1.1.5.0", Data );
  246.    printf ("Name:    %s\n", Data );
  247.    GetSNMPData ( Workspace, SNMPGetResponse, "1.3.6.1.2.1.1.6.0", Data );
  248.    printf ("Location: %s\n", Data );
  249.    GetSNMPData ( Workspace, SNMPGetResponse, "1.3.6.1.2.1.2.2.1.10.1", Data );
  250.    printf ("Bytes Received by Interface 1: %s\n", Data );
  251.  
  252.  
  253.    // clean up memory allocations
  254.  
  255.    om_delete ( PrivateGetResponse );
  256.    om_delete ( SNMPGetResponse );
  257.  
  258.  
  259.  
  260.    // Delete session with workspace
  261.  
  262.    Ret = mp_unbind ( Session );
  263.    if ( Ret != MP_SUCCESS )
  264.    {
  265.       printf ( "Cannot delete session with workspace" );
  266.       return 1;
  267.    }
  268.  
  269.  
  270.    // Shutdown the workspace
  271.  
  272.    Ret = mp_shutdown ( Workspace );
  273.    if ( Ret != MP_SUCCESS )
  274.    {
  275.       printf ( "Cannot shutdown the workspace" );
  276.       return 1;
  277.    }
  278.  
  279.  
  280.    return 0;
  281. }
  282.  
  283.