CiscoSNMP.java 11.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
package com.sitech.ismp.coll;

import java.util.Vector;

import com.adventnet.snmp.beans.SnmpTarget;
import com.adventnet.snmp.snmp2.SnmpOID;

public class CiscoSNMP extends CollBase{
	SnmpTarget target = null;

	public String ObjectIP = null;

	private int PORT = 161;

	private String VERSION = "v1";

	private String COMMUNITY = "public";

	private int RETRIES = 3;

	private int TIMEOUT = 4;

	private String MIBS = "5";

	private String USER_NAME = "";

	private String AUTH_PROTOCOL = new String("NO_AUTH");

	private String AUTH_PASSWORD = "";

	private String PRIV_PASSWORD = "";

	private String CONTEXT_NAME = "";

	private String CONTEXT_ID = "";

	public String[] ifType = new String[235];

	public String[] ifAdminStatus = new String[4];

	// public Vector KPISet = new Vector();
	private boolean DEBUG = false;

	public CiscoSNMP() {
		// init(routerip,kbp_id);
	}

	public void init(String routerip, int port, String comm) {
		COMMUNITY = comm;
		PORT = port; // 逻辑端口
		init(routerip);
	}

	public void init(String routerip, String comm) {
		COMMUNITY = comm;
		init(routerip);
	}

	public void init(String routerip) {
		this.ObjectIP = routerip;
		target = new SnmpTarget();
		//target.setLoadFromCompiledMibs(true);
		target.setDebug(DEBUG);
		target.setSnmpVersion(SnmpTarget.VERSION1);
		target.setTargetHost(ObjectIP); // set the agent hostname
		target.setCommunity(COMMUNITY);
		target.setTargetPort(PORT);
		target.setRetries(RETRIES);
		target.setTimeout(TIMEOUT);
		if (target.getSnmpVersion() == target.VERSION3) {
			target.create_v3_tables();
		}
		setIfType();
		setIfAdminStatus();

		KPISet.clear();

	}

	public void release() {
		// KPISet.clear();
		target.releaseResources();
	}

	public boolean getState() {
		return getSNMPState();
	}

	public boolean getSNMPState() {
		String value = getKPIInfo(".1.3.6.1.2.1.1.5.0");
		if (value == null) {
			return false;
		}
		return true;
	}

	public String getKPIInfo(String oid) {
		// System.out.println("Begin getKPIInfo");
		String oids[] = new String[1];

		oids[0] = "1.0";
		target.setObjectIDList(oids);
		target.oidList[0] = new com.adventnet.snmp.snmp2.SnmpOID(oid);
		String result[] = target.snmpGetList(); // do a get request
		// System.out.println("End getKPIInfo");
		if (result == null) {
			return null;
		} else {
			return result[0];
		}
	}

	public Vector walkKPIInfo(String oid) {
		Vector tmp_result = new Vector();
		Vector tmp_oid = new Vector();
		target.setObjectID(oid);
		target.oidList[0] = new com.adventnet.snmp.snmp2.SnmpOID(oid);// .1.3.6.1.4.1.11.2.3.1.2.2.1
		// Each
		// entry
		// contains
		// objects
		// for a
		// particular
		// file
		// system.(HP)

		int maxtry = 0;
		com.adventnet.snmp.snmp2.SnmpOID[] oidList = target.getSnmpOIDList();
		if (oidList == null) {
			return null;
		} else {
			SnmpOID rootoid = oidList[0];
			SnmpOID curroid = null;
			while (maxtry++ < 3000) { // limit the max getnexts to 5000
				String result1[] = target.snmpGetNextList();
				if (result1 == null) {
					target.oidList[0] = curroid;
					continue;
					// break;
				}
				if (!SnmpTarget.isInSubTree(rootoid, target.getSnmpOID()))
					break; // check first column

				for (int i = 0; i < result1.length; i++) { // print the values
					// System.out.println(target.getObjectID(i) + ": " +
					// result1[i]);
					tmp_result.add(result1[i]);
					tmp_oid.add(target.getObjectID(i));
				}
			}

			if (maxtry == 1) { // we did not get a valid row
				System.err
						.println("Request fed, timed out or no available data("
								+ oid + "). \n " + target.getErrorString());
			}
		}
		// System.out.println("End snmpWalkList");
		return tmp_result;
	}

	public Vector getRowKPIInfo(String rootoid, Vector suboid) {
		return null;
	}

	public Vector getColKPIInfo(String oid) {
		return null;
	}

	static public void main(String args[]) {
		SNMPTarget collection = new SNMPTarget();
		collection.init(args[0]);
	}

	public void setIfType() {

		ifType[1] = "other接口";
		ifType[2] = "regular1822接口";
		ifType[3] = "hdh1822接口";
		ifType[4] = "ddnX25接口";
		ifType[5] = "rfc877x25接口";
		ifType[6] = "ethernet接口";
		ifType[7] = "iso88023Csmacd接口";
		ifType[8] = "iso88024TokenBus接口";
		ifType[9] = "iso88025TokenRing接口";
		ifType[10] = "iso88026Man接口";
		ifType[11] = "starLan接口";
		ifType[12] = "proteon10Mbit接口";
		ifType[13] = "proteon80Mbit接口";
		ifType[14] = "hyperchannel接口";
		ifType[15] = "fddi接口";
		ifType[16] = "lapb接口";
		ifType[17] = "sdlc接口";
		ifType[18] = "ds1接口";
		ifType[19] = "e1接口";
		ifType[20] = "basicISDN接口";

		ifType[21] = "primaryISDN接口";
		ifType[22] = "propPointToPointSerial接口";
		ifType[23] = "ppp接口";
		ifType[24] = "softwareLoopback接口";
		ifType[25] = "eon接口";
		ifType[26] = "ethernet3Mbit接口";
		ifType[27] = "nsip接口";
		ifType[28] = "slip接口";
		ifType[29] = "ultra接口";
		ifType[30] = "ds3接口";
		ifType[31] = "sip接口";
		ifType[32] = "frameRelay接口";
		ifType[33] = "rs232接口";
		ifType[34] = "para接口";
		ifType[35] = "arcnet接口";
		ifType[36] = "arcnetPlus接口";
		ifType[37] = "atm接口";
		ifType[38] = "miox25接口";
		ifType[39] = "sonet接口";
		ifType[40] = "x25ple接口";

		ifType[41] = "iso88022llc接口";
		ifType[42] = "localTalk接口";
		ifType[43] = "smdsDxi接口";
		ifType[44] = "frameRelayService接口";
		ifType[45] = "v35接口";
		ifType[46] = "hssi接口";
		ifType[47] = "hippi接口";
		ifType[48] = "modem接口";
		ifType[49] = "aal5接口";

		ifType[50] = "sonetPath接口";
		ifType[51] = "sonetVT接口";
		ifType[52] = "smdsIcip接口";
		ifType[53] = "propVirtual接口";
		ifType[54] = "propMultiplexor接口";
		ifType[55] = "ieee80212接口";
		ifType[56] = "fibreChannel接口";
		ifType[57] = "hippiInterface接口";
		ifType[58] = "frameRelayInterconnect接口";
		ifType[59] = "aflane8023接口";

		ifType[60] = "aflane8025接口";
		ifType[61] = "cctEmul接口";
		ifType[62] = "fastEther接口";
		ifType[63] = "isdn接口";
		ifType[64] = "v11接口";
		ifType[65] = "v36接口";
		ifType[66] = "g703at64k接口";
		ifType[67] = "g703at2mb接口";
		ifType[68] = "qllc接口";
		ifType[69] = "fastEtherFX接口";

		ifType[70] = "channel接口";
		ifType[71] = "ieee80211接口";
		ifType[72] = "ibm370parChan接口";
		ifType[73] = "escon接口";
		ifType[74] = "dlsw接口";
		ifType[75] = "isdns接口";
		ifType[76] = "isdnu接口";
		ifType[77] = "lapd接口";
		ifType[78] = "ipSwitch接口";
		ifType[79] = "rsrb接口";

		ifType[80] = "atmLogical接口";
		ifType[81] = "ds0接口";
		ifType[82] = "ds0Bundle接口";
		ifType[83] = "bsc接口";
		ifType[84] = "async接口";
		ifType[85] = "cnr接口";
		ifType[86] = "iso88025Dtr接口";
		ifType[87] = "eplrs接口";
		ifType[88] = "arap接口";
		ifType[89] = "propCnls接口";

		ifType[90] = "hostPad接口";
		ifType[91] = "termPad接口";
		ifType[92] = "frameRelayMPI接口";
		ifType[93] = "x213接口";
		ifType[94] = "adsl接口";
		ifType[95] = "radsl接口";
		ifType[96] = "sdsl接口";
		ifType[97] = "vdsl接口";
		ifType[98] = "iso88025CRFPInt接口";
		ifType[99] = "myrinet接口";

		ifType[100] = "voiceEM接口";
		ifType[101] = "voiceFXO接口";
		ifType[102] = "voiceFXS接口";
		ifType[103] = "voiceEncap接口";
		ifType[104] = "voiceOverIp接口";
		ifType[105] = "atmDxi接口";
		ifType[106] = "atmFuni接口";
		ifType[107] = "atmIma接口";
		ifType[108] = "pppMultilinkBundle接口";
		ifType[109] = "ipOverCdlc接口";

		ifType[110] = "ipOverClaw接口";
		ifType[111] = "stackToStack接口";
		ifType[112] = "virtualIpAddress接口";
		ifType[113] = "mpc接口";
		ifType[114] = "ipOverAtm接口";
		ifType[115] = "iso88025Fiber接口";
		ifType[116] = "tdlc接口";
		ifType[117] = "gigabitEthernet接口";
		ifType[118] = "hdlc接口";
		ifType[119] = "lapf接口";

		ifType[120] = "v37接口";
		ifType[121] = "x25mlp接口";
		ifType[122] = "x25huntGroup接口";
		ifType[123] = "trasnpHdlc接口";
		ifType[124] = "interleave接口";
		ifType[125] = "fast接口";
		ifType[126] = "ip接口";
		ifType[127] = "docsCableMaclayer接口";
		ifType[128] = "docsCableDownstream接口";
		ifType[129] = "docsCableUpstream接口";

		ifType[130] = "a12MppSwitch接口";
		ifType[131] = "tunnel接口";
		ifType[132] = "coffee接口";
		ifType[133] = "ces接口";
		ifType[134] = "atmSubInterface接口";
		ifType[135] = "l2vlan接口";
		ifType[136] = "l3ipvlan接口";
		ifType[137] = "l3ipxvlan接口";
		ifType[138] = "digitalPowerline接口";
		ifType[139] = "mediaMailOverIp接口";

		ifType[140] = "dtm接口";
		ifType[141] = "dcn接口";
		ifType[142] = "ipForward接口";
		ifType[143] = "msdsl接口";
		ifType[144] = "ieee1394接口";
		ifType[145] = "gsn接口";
		ifType[146] = "dvbRccMacLayer接口";
		ifType[147] = "dvbRccDownstream接口";
		ifType[148] = "dvbRccUpstream接口";
		ifType[149] = "atmVirtual接口";

		ifType[150] = "mplsTunnel接口";
		ifType[151] = "srp接口";
		ifType[152] = "voiceOverAtm接口";
		ifType[153] = "voiceOverFrameRelay接口";
		ifType[154] = "idsl接口";
		ifType[155] = "compositeLink接口";
		ifType[156] = "ss7SigLink接口";
		ifType[157] = "propWirelessP2P接口";
		ifType[158] = "frForward接口";
		ifType[159] = "rfc1483接口";

		ifType[160] = "usb接口";
		ifType[161] = "ieee8023adLag接口";
		ifType[162] = "bgppolicyaccounting接口";
		ifType[163] = "frf16MfrBundle接口";
		ifType[164] = "h323Gatekeeper接口";
		ifType[165] = "h323Proxy接口";
		ifType[166] = "mpls接口";
		ifType[167] = "mfSigLink接口";
		ifType[168] = "hdsl2接口";
		ifType[169] = "shdsl接口";

		ifType[170] = "ds1FDL接口";
		ifType[171] = "pos接口";
		ifType[172] = "dvbAsiIn接口";
		ifType[173] = "dvbAsiOut接口";
		ifType[174] = "plc接口";
		ifType[175] = "nfas接口";
		ifType[176] = "tr008接口";
		ifType[177] = "gr303RDT接口";
		ifType[178] = "gr303IDT接口";
		ifType[179] = "isup接口";

		ifType[180] = "propDocsWirelessMaclayer接口";
		ifType[181] = "propDocsWirelessDownstream接口";
		ifType[182] = "propDocsWirelessUpstream接口";
		ifType[183] = "hiperlan2接口";
		ifType[184] = "propBWAp2Mp接口";
		ifType[185] = "sonetOverheadChannel接口";
		ifType[186] = "digitalWrapperOverheadChannel接口";
		ifType[187] = "aal2接口";
		ifType[188] = "radioMAC接口";
		ifType[189] = "atmRadio接口";

		ifType[190] = "imt接口";
		ifType[191] = "mvl接口";
		ifType[192] = "reachDSL接口";
		ifType[193] = "frDlciEndPt接口";
		ifType[194] = "atmVciEndPt接口";
		ifType[195] = "opticalChannel接口";
		ifType[196] = "opticalTransport接口";
		ifType[197] = "propAtm接口";
		ifType[198] = "voiceOverCable接口";
		ifType[199] = "infiniband接口";

		ifType[200] = "teLink接口";
		ifType[201] = "q2931接口";
		ifType[202] = "virtualTg接口";
		ifType[203] = "sipTg接口";
		ifType[204] = "sipSig接口";
		ifType[205] = "docsCableUpstreamChannel接口";
		ifType[206] = "econet接口";
		ifType[207] = "pon155接口";
		ifType[208] = "pon622接口";
		ifType[209] = "bridge接口";

		ifType[210] = "linegroup接口";
		ifType[211] = "voiceEMFGD接口";
		ifType[212] = "voiceFGDEANA接口";
		ifType[213] = "voiceDID接口";
		ifType[214] = "mpegTransport接口";
		ifType[215] = "sixToFour接口";
		ifType[216] = "gtp接口";
		ifType[217] = "pdnEtherLoop1接口";
		ifType[218] = "pdnEtherLoop2接口";
		ifType[219] = "opticalChannelGroup接口";

		ifType[220] = "homepna接口";
		ifType[221] = "gfp接口";
		ifType[222] = "ciscoISLvlan接口";
		ifType[223] = "actelisMetaLOOP接口";
		ifType[224] = "fcipLink接口";
		ifType[225] = "rpr接口";
		ifType[226] = "qam接口";
		ifType[227] = "lmp接口";
		ifType[228] = "cblVectaStar接口";
		ifType[229] = "docsCableMCmtsDownstream接口";

		ifType[230] = "adsl2接口";
		ifType[231] = "macSecControlledIF接口";
		ifType[232] = "macSecUncontrolledIF接口";
		ifType[233] = "aviciOpticalEther接口";
		ifType[234] = "atmbond接口";
	}

	public void setIfAdminStatus() {
		ifAdminStatus[1] = "up";
		ifAdminStatus[2] = "down";
		ifAdminStatus[3] = "testing";
	}
}