Package com.sun.management.snmp

Examples of com.sun.management.snmp.SnmpVarBindList


    int rc = 0;

    try {
      // Build the list of variables you want to query
      //
      final SnmpVarBindList list = new SnmpVarBindList("Get varbind list");

      // Write one specific OID
      //
      SnmpVarBind oid = new SnmpVarBind(oids);
      oid.setSnmpValue(new SnmpString("myValue"));
      list.addVarBind(oid);

      // Make the SNMP set request
      //
      System.out.println(
              "setRequest() of SNMPSet: Start SNMP V" + version +
              " GET request for SNMP agent on \"" + remoteHost +
              "\" at port \"" + port + "\".");

      // Set request
      //
      SnmpRequest request = session.snmpSetRequest(null, list);

      // Check for a timeout of the request
      //
      boolean completed = request.waitForCompletion((maxRetries + 1) * timeOut);
      if (completed == false) {
        System.out.println(
                "setRequest() of SNMPSet: Request timed out, " +
                "check reachability of agent.");

        // Print request
        //
        System.out.println(
                "setRequest() of SNMPSet: Request= " +
                request.toString() + ".");

        rc = 1;
      }

      if (rc == 0) {
        System.out.println(
                "getRequest() of SNMPGet: Finish SNMP V" +
                version + " GET request.");

        // Now we have a response. Check if the response contains an error
        //
        String errorStatus = SnmpRequest.snmpErrorToString(
                request.getErrorStatus());
        if (errorStatus.compareTo("noError") != 0) {
          System.out.println(
                  "getRequest() of SNMPGet: Error status= " +
                  errorStatus + ".");

          System.out.println(
                  "getRequest() of SNMPGet: Error index= " +
                  request.getErrorIndex() + ".");

          if (errorStatus.compareTo(connectStatus) == 0) {
            System.out.println(
                    "getRequest() of SNMPGet: Get request failed as " +
                    "expected with " + connectStatus + " status.");
          } else {
            System.out.println(
                    "getRequest() of SNMPGet: Get request should " +
                    "fail with " + connectStatus + " status.");

            rc = 1;
          }
        } else {
          // Now we shall display the content of the result
          //
          SnmpVarBindList resp = request.getResponseVarBindList();

          System.out.println("getRequest() of SNMPGet: Result=");

          for (int i = 0; i < resp.getVarBindCount(); i++) {
            System.out.println(resp.getVarBindAt(i));
          }

          if (connectStatus.compareTo("noError") != 0) {
            // Request should failed
            //
            System.out.println(
                    "getRequest() of SNMPGet: Get request should " +
                    "fail with " + connectStatus + " status.");

            rc = 1;
          } else {
            if (validOIDs) {
              // Check that we obtain correct values for the OIDs
              //
              if (resp.checkForValidValues()) {
                System.out.println(
                        "getRequest() of SNMPGet: Returned values for" +
                        " OIDs are correct.");
              } else {
                System.out.println(
                        "getRequest() of SNMPGet: Returned values for" +
                        " OIDs are not correct.");

                rc = 1;
              }
            } else {
              // Check that we obtain incorrect values for the OIDs
              //
              if (resp.checkForValidValues()) {
                System.out.println(
                        "getRequest() of SNMPGet: Returned values for" +
                        " OIDs should not be correct.");

                rc = 1;
View Full Code Here


    String previousOID = "";

    try {
      // Build the list of variables you want to query
      //
      final SnmpVarBindList list = new SnmpVarBindList("Get varbind list");

      // Read specific OIDs
      //
      if (walk) {
        // Walk request
        //
         list.addVarBind("0.0");
         previousOID = "0.0";
      } else {
        // Get request
        //
        list.addVarBind(oids);
      }

      // Make the SNMP get request
      //
      System.out.println(
              "getRequest() of SNMPGet: Start SNMP V" + version +
              " GET request for SNMP agent on \"" + remoteHost +
              "\" at port \"" + port + "\".");

      while (previousOID.compareTo("end") != 0) {
        SnmpRequest request = null;
        if (walk) {
          // Walk request
          //
          request = session.snmpGetNextRequest(null, list);
        } else {
          // Get request
          //
          request = session.snmpGetRequest(null, list);
        }

        // Check for a timeout of the request
        //
        boolean completed =
                request.waitForCompletion((maxRetries + 1) * timeOut);
        if (completed == false) {
          if (connectStatus.compareTo("reqTimeout") != 0) {
            System.out.println(
                    "getRequest() of SNMPGet: Request timed out, " +
                    "check reachability of agent.");

            // Print request
            //
            System.out.println(
                    "getRequest() of SNMPGet: Request= " +
                    request.toString() + ".");

            rc = 1;
          } else {
            System.out.println(
                    "getRequest() of SNMPGet: Request timed out as expected.");
          }
        }

        if (rc == 0 && completed) {
          System.out.println(
                  "getRequest() of SNMPGet: Finish SNMP V" +
                  version + " GET request.");

          // Now we have a response. Check if the response contains an error
          //
          String errorStatus = SnmpRequest.snmpErrorToString(
                  request.getErrorStatus());
          if (errorStatus.compareTo("noError") != 0) {
            System.out.println(
                    "getRequest() of SNMPGet: Error status= " +
                    errorStatus + ".");

            System.out.println(
                    "getRequest() of SNMPGet: Error index= " +
                    request.getErrorIndex() + ".");

            if (errorStatus.compareTo(connectStatus) == 0) {
              System.out.println(
                      "getRequest() of SNMPGet: Get request failed as " +
                      "expected with " + connectStatus + " status.");
            } else {
              if (walk && errorStatus.compareTo("noSuchName") == 0) {
                System.out.println(
                        "getRequest() of SNMPGet: Get request failed as " +
                        "expected with " + connectStatus + " status.");
              } else {
                System.out.println(
                        "getRequest() of SNMPGet: Get request should " +
                        "fail with " + connectStatus + " status.");

                rc = 1;
              }
            }

            previousOID = "end";
          } else {
            // Now we shall display the content of the result
            //
            SnmpVarBindList resp = request.getResponseVarBindList();

            System.out.println("getRequest() of SNMPGet: Result=");

            String tmpOID = "";
            String realOID = "";
            for (int i = 0; i < resp.getVarBindCount(); i++) {
              tmpOID = resp.getVarBindAt(i).getOid().toString();
              int endIndex = tmpOID.lastIndexOf(".");
              String indexOID = tmpOID.substring(endIndex, tmpOID.length());

              realOID = tmpOID.substring(0, endIndex);
              if (realOID.startsWith("1.3.6.1.2.1.66.2")) {
                endIndex = realOID.lastIndexOf(".");
                realOID = realOID.substring(0, endIndex);
              }

              String name = resp.getVarBindAt(i).resolveVarName(realOID).getName();
              String value = resp.getVarBindAt(i).getStringValue();
              System.out.println(name + indexOID + "=" + value);

              if (walk) {
                list.removeVarBind(previousOID);
                list.addVarBind(tmpOID);
                previousOID = tmpOID;
              } else {
                previousOID = "end";
              }
            }

            if (connectStatus.compareTo("noError") != 0) {
              // Request should failed
              //
              System.out.println(
                      "getRequest() of SNMPGet: Get request should " +
                      "fail with " + connectStatus + " status.");

              rc = 1;
            } else {
              if (validOIDs) {
                // Check that we obtain correct values for the OIDs
                //
                if (resp.checkForValidValues()) {
                  System.out.println(
                          "getRequest() of SNMPGet: Returned values for" +
                          " OIDs are correct.");
                } else {
                  System.out.println(
                          "getRequest() of SNMPGet: Returned values for" +
                          " OIDs are not correct.");

                  rc = 1;
                }
              } else {
                // Check that we obtain incorrect values for the OIDs
                //
                if (resp.checkForValidValues()) {
                  System.out.println(
                          "getRequest() of SNMPGet: Returned values for" +
                          " OIDs should not be correct.");

                  rc = 1;
View Full Code Here

            session.setDefaultPeer(agent);

            // Build the list of variables you want to query.
            // For debug purposes, you can associate a name to your list.
            //
            final SnmpVarBindList list =
                    new SnmpVarBindList("SyncManager varbind list");

            // We want to read the "dsServerDescription" variable.
            //
            // We will thus query "dsServerDescription"
            //
            list.addVarBind(attributeName);

            // Make the SNMP get request and wait for the result.
            //
            SnmpRequest request = session.snmpGetNextRequest(null, list);

            final boolean completed = request.waitForCompletion(0);

            // Check for a timeout of the request.
            //
            if (completed == false) {
                fail("SyncManager::main: Request timed out." +
                        " Check reachability of agent");
                return;
            }

            // Now we have a response. Check if the response contains
            // an error.
            //
            final int errorStatus = request.getErrorStatus();
            if (errorStatus != SnmpDefinitions.snmpRspNoError) {
                fail("Error status = " +
                        SnmpRequest.snmpErrorToString(errorStatus));
                fail("Error index = " + request.getErrorIndex());
                return;
            }

            // Now we shall display the content of the result.
            //
            final SnmpVarBindList result = request.getResponseVarBindList();
            assertNotNull(result);
            assertEquals(result.getVarBindCount(), 1);


            // Nicely stop the session
            //
            session.destroySession();
View Full Code Here

            session.setDefaultPeer(agent);

            // Build the list of variables you want to query.
            // For debug purposes, you can associate a name to your list.
            //
            final SnmpVarBindList list =
                    new SnmpVarBindList("SyncManager varbind list");

            // We want to read the "dsServerDescription" variable.
            //
            // We will thus query "dsServerDescription"
            //
            list.addVarBind("dsServerDescription");

            // Make the SNMP get request and wait for the result.
            //
            SnmpRequest request = session.snmpGetNextRequest(null, list);

            final boolean completed = request.waitForCompletion(0);

            // Check for a timeout of the request.
            //
            if (completed == false) {
                fail("SyncManager::main: Request timed out." +
                        " Check reachability of agent");
                return;
            }

            // Now we have a response. Check if the response contains
            // an error.
            //
            final int errorStatus = request.getErrorStatus();
            if (errorStatus != SnmpDefinitions.snmpRspNoError) {
                fail("Error status = " +
                        SnmpRequest.snmpErrorToString(errorStatus));
                fail("Error index = " + request.getErrorIndex());
                return;
            }

            // Now we shall display the content of the result.
            //
            final SnmpVarBindList result = request.getResponseVarBindList();
            assertNotNull(result);
            assertEquals(result.getVarBindCount(), 1);
            assertEquals(result.getVarBindAt(0).isValidValue(), expectedResult);


            // Nicely stop the session
            //
            session.destroySession();
View Full Code Here

TOP

Related Classes of com.sun.management.snmp.SnmpVarBindList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.