📄 simplesnmp.java
字号:
/* * $Id: SimpleSnmp.java 4069 2007-07-06 14:00:47Z mortenv $ * * Copyright 2002-2004 Norwegian University of Science and Technology * Copyright 2007 UNINETT AS * * This file is part of Network Administration Visualized (NAV) * * NAV is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * NAV is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NAV; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package no.ntnu.nav.SimpleSnmp;import java.io.*;import java.util.*;import java.net.*;//import uk.co.westhawk.snmp.stack.*;//import uk.co.westhawk.snmp.pdu.*;import snmp.*;/** * <p> Class for quering devices via SNMP. The aim of this class is to * provide a very simple API for doing basic SNMP walking. </p> * * <p> To use first call setBaseOid(), setCs_ro() and setHost() * methods (or use setParams() ), then call the getAll() (or getNext() * ) method to retrieve values. </p> * * @version $LastChangedRevision: 4069 $ $LastChangedDate: 2007-07-06 16:00:47 +0200 (fre, 06 jul 2007) $ * @author Kristian Eide <kreide@online.no> */public class SimpleSnmp{ public static final int IFINDEX_OID = 0; public static final int IFINDEX_VALUE = 1; public static final int IFINDEX_BOTH = 2; public static final int IFINDEX_NONE = 3; public static final int IFINDEX_DEFAULT = IFINDEX_OID; private static final int DEFAULT_RETRIES = 4; private int retries = DEFAULT_RETRIES; private int backoff = 2; private String host = "127.0.0.1"; private String cs_ro = "community"; private String baseOid = "1.3"; private int timeoutCnt = 0; private boolean gotTimeout = false; private long getNextDelay = 0; private int socketTimeout = 1000; // milliseconds private int snmpVersion = 0; private SNMPv1CommunicationInterface comInterface = null; private boolean valid = false; //private SnmpContext context = null; private Map cache = new HashMap(); protected Set ignoredModules = new HashSet(); /** * Construct an empty SimpleSnmp class. */ protected SimpleSnmp() { } /** * Construct a SimpleSnmp class and set initial parameters. */ protected SimpleSnmp(String host, String cs_ro, String baseOid) { setParams(host, cs_ro, baseOid); } public static SimpleSnmp simpleSnmpFactory() { return simpleSnmpFactory(null, null); } public static SimpleSnmp simpleSnmpFactory(String host, String cs_ro, String baseOid) { return simpleSnmpFactory(null, null, host, cs_ro, baseOid); } public static SimpleSnmp simpleSnmpFactory(String vendor, String type) { if ("hp".equals(vendor)) return new SimpleSnmpHP(); return new SimpleSnmp(); } public static SimpleSnmp simpleSnmpFactory(String vendor, String type, String host, String cs_ro, String baseOid) { if ("hp".equals(vendor)) return new SimpleSnmpHP(host, cs_ro, baseOid); return new SimpleSnmp(host, cs_ro, baseOid); } public void setHost(String host) { if (!this.host.equals(host)) valid=false; this.host = host; } public void setCs_ro(String cs_ro) { if (!this.cs_ro.equals(cs_ro)) valid=false; this.cs_ro = cs_ro; } protected String getCs_ro() { return cs_ro; } public void setBaseOid(String baseOid) { this.baseOid = baseOid; } public void setParams(String host, String cs_ro, String baseOid) { setHost(host); setCs_ro(cs_ro); setBaseOid(baseOid); } /** * Set the SNMP version to use. * @param version 1 = SNMPv1, 2 = SNMPv2c. */ public void setSnmpVersion(int version) { if (version < 1 || version > 2) throw new RuntimeException("Invalid SNMP version: " + version); version--; if (snmpVersion != version) { valid = false; snmpVersion = version; } } /** * Return the SNMP version used for communication by this instance. * @return 1 for SNMP v1 and 2 for SNMP v2c */ public int getSnmpVersion() { return snmpVersion+1; } /** * Set the delay, in ms, between getNext requests. */ public void setGetNextDelay(long delay) { getNextDelay = delay; } /** * Set how many times the device can time out before a TimeoutException is thrown. * * @deprecated Use the more aptly named {@link #setRetries(int)} method. */ public void setTimeoutLimit(int limit) { setRetries(limit); } /** * Set the timeout limit to the default value (currently 4). * * @deprecated Use the more aptly named {@link #setDefaultRetries()} method. */ public void setDefaultTimeoutLimit() { setDefaultRetries(); } /** * Set the number of times the device may time out before a TimeoutException is thrown. */ public void setRetries(int limit) { retries = Math.max(1,limit); } /** * Set the number of retries to the default value, as defined by the static value DEFAULT_RETRIES */ public void setDefaultRetries() { retries = DEFAULT_RETRIES; } /** * Set the UDP socket timeout for the following requests. * @param socketTimeout A value in milliseconds. */ public void setSocketTimeout(int socketTimeout) { this.socketTimeout = socketTimeout; } /** * Use this method to specify that only the module with the given * module number should be asked for data. Note that this * information is only respected if this class is subclassed; the * default is to do nothing. * * @param module The number of the module to ask; use null to again ask all modules */ public void onlyAskModule(String module) { } /** * Use this method to specify that the given module should be * ignored and not collected data for. This is useful if the * module is down and would only give timeout exceptions. Note * that this information is only respected if this class is * subclassed; the default is to do nothing. * * @param module The number of the module to ignore. */ public void ignoreModule(String module) { ignoredModules.add(module); } /** * Specify if the ifindex is contained in the OID, the value, * both, or there is no ifindex. This is important for certain * types, e.g. HP, which need to treat the ifindex special due to * unit stacking. */ public void setIfindexIs(int ifindexIs) { } /* public boolean resetGotTimeout() { boolean b = gotTimeout; gotTimeout = false; return b; } */ /** * <p> Snmpwalk the given OID and return a maximum of cnt entries * from the subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @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 * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getNext(int getCnt, boolean decodeHex) throws TimeoutException { return getAll(baseOid, getCnt, decodeHex, true, 0); } /** * <p> Snmpwalk the given OID and return a maximum of cnt entries * from the subtree. </p> * * <p> Note: the baseOid prefix will be removed from any returned * OIDs. </p> * * @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 * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getNext(int getCnt, boolean decodeHex, boolean getNext) throws TimeoutException { return getAll(baseOid, getCnt, decodeHex, getNext, 0); } /** * <p> Snmpwalk the given OID and return a maximum of cnt entries * from the subtree. </p> * * @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 * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getNext(int getCnt, boolean decodeHex, boolean getNext, boolean stripPrefix) throws TimeoutException { return getAll(baseOid, getCnt, decodeHex, getNext, stripPrefix, 0); } /** * <p> Snmpwalk the given OID and return a maximum of cnt entries * from the 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 * @return an ArrayList containing String arrays of two elements; OID and value * @throws TimeoutException if the hosts times out */ public ArrayList getNext(String baseOid, int getCnt, boolean decodeHex, boolean getNext) throws TimeoutException { return getAll(baseOid, getCnt, decodeHex, getNext, 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 * @return a Map which maps the OIDs to their corresponding values * @throws TimeoutException if the hosts times out */ public Map getAllMap(String baseOid) throws TimeoutException { return getAllMap(baseOid, false, 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> *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -