📄 snmpwalkmv.java
字号:
} } else if(args[x].equals("-p")) { try { m_port = Integer.parseInt(args[++x]); } catch(NumberFormatException e) { throw new IllegalArgumentException("Malformed port number"); } } else if(args[x].equals("--")) { // // end of arguments // lastArg = x+1; break; } else { throw new IllegalArgumentException("Unknown Option " + args[x]); } lastArg = x+1; } } // end for // // Now the last two values should be the (host, oid) pair! // if((args.length - lastArg) == 1) // just the host! { m_host = args[lastArg++]; } else if((args.length - lastArg) == 2) { m_host = args[lastArg++]; m_startOid = ".1.3.6.1.2.1.2.2.1.1"; // hard code to ifTable } else { throw new IllegalArgumentException("Invalid number of arguments"); } } // end of parseOptions /** * Defined by the SnmpHandler interface. Used to process internal session * errors. * * @param session The SNMP session in error. * @param err The Error condition * @param pdu The pdu associated with this error condition * */ public void snmpInternalError(SnmpSession session, int err, SnmpSyntax pdu) { System.err.println("An unexpected error occured with the SNMP Session"); System.err.println("The error code is " + err); synchronized(session) { session.notify(); } } /** * This method is define by the SnmpHandler interface and invoked * if an agent fails to respond. * * @param session The SNMP session in error. * @param pdu The PDU that timedout. * */ public void snmpTimeoutError(SnmpSession session, SnmpSyntax pdu) { System.err.println("The session timed out trying to communicate with the remote host"); synchronized(session) { session.notify(); } } /** * This method is defined by the SnmpHandler interface and invoked * when the agent responds to the management application. * * @param session The session receiving the pdu. * @param cmd The command from the pdu. * @param pdu The received pdu. * * @see org.opennms.protocols.snmp.SnmpPduPacket#getCommand */ public void snmpReceivedPdu(SnmpSession session, int cmd, SnmpPduPacket pdu) { SnmpPduRequest req = null; if(pdu instanceof SnmpPduRequest) { req = (SnmpPduRequest)pdu; } if(pdu.getCommand() != SnmpPduPacket.RESPONSE) { System.err.println("Error: Received non-response command " + pdu.getCommand()); synchronized(session) { session.notify(); } return; } if(req.getErrorStatus() != 0) { System.out.println("End of mib reached"); synchronized(session) { session.notify(); } return; } // // The last variable in the list of elements // is always the first to run off the table, so // we only need to check that one. // SnmpVarBind[] vars = null; if(snmpwalkmv.ROOT.isRootOf(pdu.getVarBindAt(pdu.getLength()-1).getName())) { // // Create a new map of the interface entry // vars = pdu.toVarBindArray(); //Loop through and print varbind values for(int x = 0; x < ms_elemList.length; x++) { SnmpObjectId id = new SnmpObjectId(ms_elemList[x].getOid()); for(int y = 0; y < vars.length; y++) { if(id.isRootOf(vars[y].getName())) { try { // // Retrieve the class object of the expected SNMP data type for this element // Class classObj = ms_elemList[x].getTypeClass(); // // If the SnmpSyntax object matches the expected class // then store it in the map. Else, store a null pointer // in the map. // if (classObj.isInstance(vars[y].getValue())) { System.out.println(vars[y].getName() + ": " + vars[y].getValue()); } else { // do nothing } } catch (ClassNotFoundException e) { System.out.println("Failed retrieving SNMP type class for element: " + ms_elemList[x].getAlias()); System.out.println(e.getLocalizedMessage()); } catch (NullPointerException e) { System.out.println(e.getLocalizedMessage()); } break; } } } } else { System.out.println("End of mib reached"); synchronized(session) { session.notify(); } return; } // // next pdu // SnmpPduRequest nxt = new SnmpPduRequest(SnmpPduPacket.GETNEXT); for(int x = 0; x < vars.length; x++) { nxt.addVarBind(new SnmpVarBind(vars[x].getName())); } session.send(nxt, this); } /** * The main routine. */ public static void main(String[] args) { snmpwalkmv walker = new snmpwalkmv(); InetAddress remote = null; try { walker.parseOptions(args); remote = InetAddress.getByName(walker.m_host); } catch(IllegalArgumentException e) { System.err.println(e.getMessage()); System.exit(1); } catch(UnknownHostException e) { System.err.println("UnknownHostException: " + e.getMessage()); System.exit(1); } // // Initialize the peer // SnmpPeer peer = new SnmpPeer(remote); if(walker.m_port != -1) peer.setPort(walker.m_port); if(walker.m_timeout != -1) peer.setTimeout(walker.m_timeout); if(walker.m_retries != -1) peer.setRetries(walker.m_retries); SnmpParameters parms = peer.getParameters(); parms.setVersion(walker.m_version); if(walker.m_community != null) parms.setReadCommunity(walker.m_community); // // Now create the session, set the initial request // and walk the tree! // SnmpSession session = null; try { session = new SnmpSession(peer); } catch(SocketException e) { System.err.println("SocketException creating the SNMP session"); System.err.println("SocketException: " + e.getMessage()); System.exit(1); } session.setDefaultHandler(walker); // // set the stop point // SnmpObjectId id = new SnmpObjectId(walker.m_startOid); int[] ids = id.getIdentifiers(); ++ids[ids.length-1]; id.setIdentifiers(ids); walker.m_stopAt = id; // build the first request SnmpPduRequest pdu = new SnmpPduRequest(SnmpPduRequest.GETNEXT); for(int x = 0; x < ms_elemList.length; x++) { SnmpObjectId oid = new SnmpObjectId(ms_elemList[x].getOid()); pdu.addVarBind(new SnmpVarBind(oid)); } // // send the first request // try { synchronized(session) { session.send(pdu); session.wait(); } } catch(InterruptedException e) { // do nothing } finally { session.close(); } } // end main}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -