snmp中的trap发送方法

public static void sendTrap(String monitorContent) {
		PDU pdu = new PDU();
		pdu.setType(PDU.TRAP);
		pdu.add(new VariableBinding(new OID("1.3.6.1.6.3.1.1.4.1.0"), new OID(".1.3.6.1.4.1.9.9.13.3.0.112")));
		pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.9.112.13.1.4.1.2.0"), new OctetString("1")));
		pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.9.112.13.1.4.1.3.0"), new OctetString("0")));
		pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.9.112.13.1.4.1.4.0"), new OctetString("机房温度>=" + tempNum)));
		pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.9.112.13.1.4.1.5.0"), new OctetString(monitorContent)));
		pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.9.112.13.1.4.1.6.0"), new OctetString("192.168.22.35")));
		pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.9.112.13.1.4.1.7.0"), new OctetString("broada_chenxing")));
		pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.9.112.13.1.4.1.8.0"), new OctetString(monitorContent)));
		CommunityTarget target = new CommunityTarget(GenericAddress.parse("udp:192.168.22.35/" + 162), new OctetString(
		"public"));
		target.setVersion(SnmpConstants.version2c);
		
		Snmp snmp = null;
		try {
			snmp = new Snmp(new DefaultUdpTransportMapping());
			snmp.send(pdu, target);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("发送trap信息 :" + monitorContent);
		//System.out.println("finish trap");
	}

 

 上述一个是一个发送trap 的方法,调用即可,只要服务器端配置了相应的trap文件,此方法需要snmp4.jar包

你可能感兴趣的:(java技术)