📄 simplesnmp.java
字号:
* @param baseOid Override the baseOid; if null a null value is returned * @param decodeHex try to decode returned hex to ASCII * @return a Map which maps the OIDs to their corresponding values * @throws TimeoutException if the hosts times out */ public Map getAllMap(String baseOid, boolean decodeHex) throws TimeoutException { return getAllMap(baseOid, decodeHex, 0); } /** * <p> Snmpwalk the given OID and return the entire subtree as a * Map. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @param decodeHex try to decode returned hex to ASCII * @param stripCnt Strip this many elements (separated by .) from the start of OIDs * @return a Map which maps the OIDs to their corresponding values * @throws TimeoutException if the hosts times out */ public Map getAllMap(String baseOid, boolean decodeHex, int stripCnt) throws TimeoutException { List l = getAll(baseOid, decodeHex); if (l == null) return null; Map m = new HashMap(); for (Iterator it = l.iterator(); it.hasNext();) { String[] s = (String[])it.next(); s[0] = strip(s[0], '.', stripCnt, true); m.put(s[0], s[1]); } return m; } /** * <p> Snmpwalk the given OID and return the entire subtree as a * Map. Includes the option to ask for a map from OID to module to * be included, but the default implementation ignores this. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @param decodeHex try to decode returned hex to ASCII * @param stripCnt Strip this many elements (separated by .) from the start of OIDs * @return a Map which maps the OIDs to their corresponding values * @throws TimeoutException if the hosts times out */ public Map getAllMap(String baseOid, boolean decodeHex, int stripCnt, boolean oidToModuleMap) throws TimeoutException { return getAllMap(baseOid, decodeHex, stripCnt); } /** * <p> Snmpwalk the given OID and return the entire subtree as a * Map; the OIDs are mapped to a {@link java.util.List List} of * values. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @return a Map which maps the OIDs to a List of corresponding values * @throws TimeoutException if the hosts times out */ public Map getAllMapList(String baseOid) throws TimeoutException { return getAllMapList(baseOid, 0); } /** * <p> Snmpwalk the given OID and return the entire subtree as a * Map; the OIDs are mapped to a {@link java.util.List List} of * values. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @param stripCnt Strip this many elements (separated by .) from the end OIDs * @return a Map which maps the OIDs to a List of corresponding values * @throws TimeoutException if the hosts times out */ public Map getAllMapList(String baseOid, int stripCnt) throws TimeoutException { List l = getAll(baseOid); return listToMapList(l, stripCnt); } /** * <p> Convert a list of two-element String arrays to a Map of * Lists, stripping the first stripCnt elements (separated by .) * from the first String in the array. </p> * * <p> If the String array contains 3 elements, the third will be * prepended to the first after stripping. </p> */ protected Map listToMapList(List l, int stripCnt) { if (l == null) return null; Map m = new HashMap(); for (Iterator it = l.iterator(); it.hasNext();) { String[] s = (String[])it.next(); s[0] = strip(s[0], '.', stripCnt, false); s[0] = convertToIfIndex(s); List vl; if ( (vl=(List)m.get(s[0])) == null) m.put(s[0], vl=new ArrayList()); vl.add(s[1]); } return m; } // Strip elements from string s protected String strip(String s, char sep, int cnt, boolean front) { if (cnt > 0) { int p = 0, k = 0; for (int i=0; i < cnt; i++) { k = s.indexOf(sep, p); if (k < 0) break; p = k+1; } if (front) { if (p > 0) s = s.substring(0, p-1); } else { s = s.substring(p, s.length()); } } return s; } /** * Get the ifIndex from the String array. Subclasses can override * this to do special processing; the default is just to return the * first element. */ protected String convertToIfIndex(String[] s) { return convertToIfIndex(s, 0); } /** * Get the ifIndex from the String array. Subclasses can override * this to do special processing; the default is just to return the * <i>i</i>th element. * * @param idx Index in string array of the ifindex to be converted */ protected String convertToIfIndex(String[] s, int idx) { return s[idx]; } /** * Extract the OID part of the ifindex; this value should be * suitable for adding to an OID for collecting data for a specific * ifindex. */ public String extractIfIndexOID(String ifindex) { return ifindex; } /** * Remove any module-specific parts from the sysname. */ public String extractSysname(String sysname, String module) { return sysname; } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll() throws TimeoutException { return getAll(false, true); } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll(String baseOid) throws TimeoutException { return getAll(baseOid, false, true); } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param decodeHex Try to decode returned hex to ASCII * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll(boolean decodeHex) throws TimeoutException { return getAll(decodeHex, true); } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @param decodeHex Try to decode returned hex to ASCII * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll(String baseOid, boolean decodeHex) throws TimeoutException { return getAll(baseOid, decodeHex, true); } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @param decodeHex Try to decode returned hex to ASCII * @param stripCnt Strip this many elements (separated by .) from the start of OIDs * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll(String baseOid, boolean decodeHex, int stripCnt) throws TimeoutException { return getAll(baseOid, 0, decodeHex, true, true, stripCnt); } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param decodeHex Try to decode returned hex to ASCII * @param getNext Send GETNEXT in first packet, this will not work if you specify an exact OID * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll(boolean decodeHex, boolean getNext) throws TimeoutException { return getAll(baseOid, decodeHex, getNext); } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @param decodeHex Try to decode returned hex to ASCII * @param getNext Send GETNEXT in first packet, this will not work if you specify an exact OID * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll(String baseOid, boolean decodeHex, boolean getNext) throws TimeoutException { return getAll(baseOid, 0, decodeHex, getNext, 0); } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @param getCnt The maximum number of OIDs to get; 0 or less means get as much as possible * @param decodeHex Try to decode returned hex to ASCII * @param getNext Send GETNEXT in first packet, this will not work if you specify an exact OID * @param stripCnt Strip this many elements (separated by .) from the start of OIDs * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll(String baseOid, int getCnt, boolean decodeHex, boolean getNext, int stripCnt) throws TimeoutException { return getAll(baseOid, getCnt, decodeHex, getNext, true, stripCnt); } /** * <p> Snmpwalk the given OID and return the entire subtree. </p> * * @param baseOid Override the baseOid; if null a null value is returned * @param getCnt The maximum number of OIDs to get; 0 or less means get as much as possible * @param decodeHex Try to decode returned hex to ASCII * @param getNext Send GETNEXT in first packet, this will not work if you specify an exact OID * @param stripPrefix Strip baseOid prefix from response OIDs if true * @param stripCnt Strip this many elements (separated by .) from the start of OIDs * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getAll(String baseOid, int getCnt, boolean decodeHex, boolean getNext, boolean stripPrefix, int stripCnt) throws TimeoutException { if (baseOid == null) return null; if (baseOid.charAt(0) == '.') baseOid = baseOid.substring(1, baseOid.length()); String cacheKey = host+":"+cs_ro+":"+baseOid+":"+decodeHex+":"+getNext+":"+stripPrefix+":"+stripCnt; if (cache.containsKey(cacheKey)) { return new ArrayList((Collection)cache.get(cacheKey)); } ArrayList l = getAllJavaSnmp(baseOid, getCnt, decodeHex, getNext, stripPrefix, stripCnt, socketTimeout); cache.put(cacheKey, l); return l; } private ArrayList getAllJavaSnmp(String baseOid, int getCnt, boolean decodeHex, boolean getNext, boolean stripPrefix, int stripCnt, int timeout) throws TimeoutException { ArrayList l = new ArrayList(); try { checkSnmpContext(); if (timeout < 1) timeout = 1000; // Prevent infinite waiting for response comInterface.setSocketTimeout(timeout); // Should we get the entire subtree? boolean getAll = getCnt == 0; SNMPVarBindList var = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -