⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplesnmp.java

📁 Network Administration Visualized 网络管理可视化源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * SimpleSnmp *  * $LastChangedRevision: 3963 $ * * $LastChangedDate: 2007-04-12 10:58:37 +0200 (tor, 12 apr 2007) $ * * Copyright 2002-2004 Norwegian University of Science and Technology *  * 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: 3963 $ $LastChangedDate: 2007-04-12 10:58:37 +0200 (tor, 12 apr 2007) $ * @author Kristian Eide &lt;kreide@online.no&gt; */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 final int DEFAULT_TIMEOUT_LIMIT = 4;	private int timeoutLimit = 4;	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 = 0;	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. 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;		}	}	/**	 * 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.	 */	public void setTimeoutLimit(int limit)	{		timeoutLimit = Math.max(1,limit);	}	/**	 * Set the timeout limit to the default value (currently 4).	 */	public void setDefaultTimeoutLimit()	{		timeoutLimit = DEFAULT_TIMEOUT_LIMIT;	}	public void setSocketTimeout(int socketTimeout)	{		this.socketTimeout = socketTimeout;		if (comInterface != null) {			try {				comInterface.setSocketTimeout(socketTimeout);			} catch (SocketException e) {				e.printStackTrace(System.err);			}		}	}	/**	 * 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>	 *	 * <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>	 *	 * @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;	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -