- 浏览: 291394 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
chenchangqun:
这段示例代码没有客户端 没法实验
Java NIO原理和使用 -
congjl2002:
误,整理好了在发phk070832 写道垃圾代码,一大推的编译 ...
snmp trap的接收(jdmk方式) -
phk070832:
垃圾代码,一大推的编译错误,整理好了在发表吧。
snmp trap的接收(jdmk方式) -
zhangcctao:
我也遇到线程池的内存溢出问题。。不知道是不是这个原因呀?
多线程中使用静态方法 -
新入职:
分享了,这个eclise版本有没有讲究?我的3.3的可以么
windows下安装android及eclipse配置
利用snmp4j开源包来获取指定OID的信息(SNMPv3)
第一步测试Main方法:
Java code
public class MainSnmpWalk{
static Logger log = Logger.getLogger(MainSnmpWalk.class);
public MainSnmpWalk(){
}
public static void main(String[] args) throws InterruptedException{
//建立一个SNMPv2c PDU,它会从system(1.3.6.1.2.1.1)OID开始游走
// SnmpWalk walk = new SnmpWalk("192.168.100.10","1.3.6.1.2.1.1.5.0");//物理位置
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.2.2");//RAM
// SnmpWalk walk = new SnmpWalk("127.0.0.1","1.3.6.1.2.1.25.2.3.1.6");//Hard Disk
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.5.1.1.1");//CPU Utilization
// SnmpWalk walk = new SnmpWalk("127.0.0.1","1.3.6.1.2.1.25.1");//也含本机物理总内存
//cpu:1.3.6.1.2.1.25.5.1.1.1
//memory:1.3.6.1.2.1.25.5.1.1.2 //得到的是每个进程的的内存使用值
//1.3.6.1.2.1.25.3.3.1.2.1 //一个比较通用的看设备CPU利用率的OID值。取最后一分钟的平均值。
// Linux下可以.1.3.6.1.2.1.25.2和.1.3.6.1.2.1.25.5这两个值试试
//====================================================
//.1.3.6.1.4.1.311.1.1.3.1.1.1.2.0
// SnmpWalk walk = new SnmpWalk("127.0.0.1","1.3.6.1.2.1.1.1");//系统描述
// SnmpWalk walk = new SnmpWalk("127.0.0.1","1.3.6.1.2.1.25.1.7.0");//本机物理总内存
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.4.2.1.2");//列出系统进程
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.4.1.77.1.2.25.1.1");//列出系统用户列表
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.4.1.77.1.4.1");//列出域名(工作组)
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.6.3.1.2");//列出安装的软件
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.3.3.1.2");
//SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.4.2.1.2");//当前进程列表
//可用的内存数量: .1.3.6.1.4.1.311.1.1.3.1.1.1.2.0
//磁盘利用率的OID: .1.3.6.1.4.1.311.1.1.3.1.1.5.1.3.0
//CPU的利用率: .1.3.6.1.4.1.311.1.1.3.1.1.2.1.3.1.48.0
//发送的字节数(包括组帧字符): 1.3.6.1.2.1.2.2.1.16.1
//收到的字节数(包括组帧字符): 1.3.6.1.2.1.2.2.1.10.1
//cpuPercentProcessorTime .1.3.6.1.4.1.311.1.1.3.1.1.2.1.3
//memoryAvailableKBytes (OID: .1.3.6.1.4.1.311.1.1.3.1.1.1.29)
//memoryAvailableMBytes (OID: .1.3.6.1.4.1.311.1.1.3.1.1.1.30)
//==================================================================================
// log.debug("Doing SNMPv2 walk..");
//1.3.6.1.2.1.1.6.0
// SnmpWalk walk = new SnmpWalk("192.168.100.57","1.3.6.1.2.1.1.4.0");//1.3.6.1.2.1.1.5.0
// String str = walk.doWalk();
// log.debug("主方法str: "+ str);
//==================================================================================
/**
* 在Windows操作系统上默认是没有安装snmp协议的,需要安装snmp,安装细节较简单,这里不再描述
* 1.在Windows上安装好了snmp之后,它默认是不支持SNMPv3的,需要重新安装一个SNMPv3的代理
* 2.在Windows上直接安装软件:snmpv3agent setup.exe并配置好安全名称(用户)、认证协订、认证密码、保密协定以及保密密码
* 3.设置好了之后,在代码里直接相对应就OK了.
*/
//.1.3.6.1.4.1.77.1.2.25.1.1 //1.3.6.1.2.1.1.5.0
// 用来设定安全名称(用户)、认证协订、认证的通行密码、保密协定以及保密的通行密码
log.debug("doing snmpv3 walk..");
//建立一个snmpv3c pdu,它会以kschmidt做为安全名称,md5做为认证协定
SnmpWalk walk = new SnmpWalk("192.168.100.10","1.3.6.1.2.1.1.6.0","zmq","MD5","mysnmpzmq","DES","mysnmpzmq");
String str3 = walk.doWalk();
log.debug("主方法str: "+ str3);
}
}
第二步:类SnmpWalk.java
public class SnmpWalk {
public static final int WALK = 1;
private SnmpUtil _util = null;
//会建立一出一个SNMPv2c walk命令,引数是"执行agent的主机"以及"我们想要游走之OIDs的前缀(prefix)"
public SnmpWalk(String host, String oid){
_util = new SnmpUtil(host,oid,false,0);
_util.setOperation(WALK);
}
//会建立出一个SNMPv3 walk命令,前两个引数与上面建构式一样,其它引数则与SNMPv3的安全设定有关
public SnmpWalk(String host, String oid, String user, String authProtocol, String authPasshrase, String privProtocol, String privPassphrase) {
_util = new SnmpUtil(host,oid,user,authProtocol,authPasshrase,privProtocol,privPassphrase,false,0);
_util.setOperation(WALK);
}
public String doWalk() {
return _util.sendAndProcessResponse();
}
}
第三部分:
public class SnmpUtil extends Thread implements PDUFactory, CommandResponder {
private static Logger log = Logger.getLogger(SnmpUtil.class);
public static final int DEFAULT = 0;
public static final int WALK = 1;
private Target _target;
private Address _address;
private OID _authProtocol;
private OID _privProtocol;
private OctetString _privPassphrase;
private OctetString _authPassphrase;
private OctetString _community = new OctetString("mysnmp");//你自己的community
private OctetString _contextEngineID; //SnmpEngineId s = null;
private OctetString _contextName = new OctetString();
private OctetString _securityName = new OctetString();
private static Snmp _snmp = null;
private int _numThreads = 1;
private int _port = 0;
private ThreadPool _threadPool = null;
private boolean _isReceiver = false;
private OctetString _authoritativeEngineID = new OctetString("1234567");
private TransportMapping _transport = null;
private TimeTicks _sysUpTime = new TimeTicks(0);
private OID _trapOID = new OID("1.3.6.1.4.1.****");
private int _version = 0;
private int _retries = 1;
private int _timeout = 4000;
private int _pduType = 0;
private Vector _vbs = new Vector();
protected int _operation = DEFAULT;
static {
if (System.getProperty("log4j.configuration") == null) {
BasicConfigurator.configure();
}
}
public SnmpUtil(String host, String varbind, boolean receiver, int type) {
_version = SnmpConstants.version2c;
_isReceiver = receiver;
if (type == 2) {
_pduType = PDU.INFORM;
} else if (type == 1) {
_pduType = PDU.TRAP;
} else if (type == 0) {
_pduType = PDU.SET;
}
setPort();
if (!_isReceiver) {
init(host, varbind);
} else {
initReceiver(host);
}
}
// 用来设定安全名称(用户)、认证协订、认证的通行密码、保密协定以及保密的通行密码
// 保密的通行密码、认证的通行密码以及用户在内部都会被储成一个OctetString(与String不同,OctetString被实作成一个
// 与字符集无关的位元组字串)
// 认证和保密协定在内部会被储存成OIDs
public SnmpUtil(String host, String varbind, String user,
String authProtocol, String authPasshrase, String privProtocol,
String privPassphrase, boolean receiver, int type) {
_version = SnmpConstants.version3;
_isReceiver = receiver;
_privPassphrase = new OctetString(privPassphrase);
_authPassphrase = new OctetString(authPasshrase);
_securityName = new OctetString(user);
if (authProtocol.equals("MD5")) {
_authProtocol = AuthMD5.ID;
} else if (authProtocol.equals("SHA")) {
_authProtocol = AuthSHA.ID;
}
if (privProtocol.equals("DES")) {
_privProtocol = PrivDES.ID;
} else if ((privProtocol.equals("AES128"))
|| (privProtocol.equals("AES"))) {
_privProtocol = PrivAES128.ID;
} else if (privProtocol.equals("AES192")) {
_privProtocol = PrivAES192.ID;
} else if (privProtocol.equals("AES256")) {
_privProtocol = PrivAES256.ID;
}
if (type == 2) {
_pduType = PDU.INFORM;
} else if (type == 1) {
_pduType = PDU.TRAP;
}
else if (type == 0) {
_pduType = PDU.SET;
}
setPort();
if (!_isReceiver) {
init(host, varbind);
} else {
initReceiver(host);
}
}
public void setVersion(int version) {
_version = version;
}
public void setOperation(int operation) {
_operation = operation;
if (_operation == WALK) {
log.debug("==setOperation is=="+operation);
// _pduType = PDU.GETNEXT;
_pduType = PDU.GET;
}
}
public int getOperation() {
return _operation;
}
public int getPduType() {
return _pduType;
}
public void setPort() {
if (_pduType == PDU.INFORM || _pduType == PDU.TRAP || _isReceiver) {
_port = 162;
} else {
_port = 161;
}
}
public void init(String host, String varbind) {
_vbs = getVariableBinding(varbind);
if (_pduType == PDU.INFORM || _pduType == PDU.TRAP) {
checkTrapVariables(_vbs);
}
_address = new UdpAddress(host + "/" + _port);
LogFactory.setLogFactory(new Log4jLogFactory());
BER.setCheckSequenceLength(false);
}
public Vector getVariableBindings() {
return _vbs;
}
// 如果使用的是SNMPv3,则会调用addUsmUser(snmp)方法,以便把一个用户加入此期程.<<BB
// 接着会呼叫createTarget()
private void addUsmUser(Snmp snmp) {
snmp.getUSM().addUser(
_securityName,
new UsmUser(_securityName, _authProtocol, _authPassphrase,
_privProtocol, _privPassphrase));
}
// 第一步:创建一个SNMP期程(session),如果使用的是SNMPv3,则会调用addUsmUser(snmp)方法=============AA
private Snmp createSnmpSession() throws IOException {
AbstractTransportMapping transport;
if (_address instanceof TcpAddress) {
transport = new DefaultTcpTransportMapping();
} else {
transport = new DefaultUdpTransportMapping();
}
// Could save some CPU cycles:
// transport.setAsyncMsgProcessingSupported(false);
Snmp snmp = new Snmp(transport);
if (_version == SnmpConstants.version3) {
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
addUsmUser(snmp);
}
return snmp;
}
// 建立一个目标:这所建立的若不是一个SNMPv3,就是一个SNMPv1和SNMPv2
private Target createTarget() {// CC
if (_version == SnmpConstants.version3) {
UserTarget target = new UserTarget();
if (_authPassphrase != null) {
if (_privPassphrase != null) {
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
} else {
target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
}
} else {
target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
}
target.setSecurityName(_securityName);
return target;
} else {
CommunityTarget target = new CommunityTarget();
target.setCommunity(_community);
return target;
}
}
// 一旦我们从createTarget()返回,send()方法会为该目标设定版本、位址、逾时和尝试次数等值
// 它还会让所有的传输对映(transport mappings)进入监听模式,这确保我们能够回应SNMP引擎所发现到的请求
public PDU send() throws IOException {
_snmp = createSnmpSession();
this._target = createTarget();
_target.setVersion(_version);
_target.setAddress(_address);
_target.setRetries(_retries);
_target.setTimeout(_timeout);
_snmp.listen();
PDU request = createPDU(_target);
for (int i = 0; i < _vbs.size(); i++) {
request.add((VariableBinding) _vbs.get(i));
}
PDU response = null;
if (_operation == WALK) {
response = walk(_snmp, request, _target);
} else {
ResponseEvent responseEvent;
long startTime = System.currentTimeMillis();
responseEvent = _snmp.send(request, _target);
if (responseEvent != null) {
response = responseEvent.getResponse();
log.debug("=第241行==responseEvent.getResponse()===: "
+ responseEvent.getResponse());
log.debug("Received response after "
+ (System.currentTimeMillis() - startTime) + " millis");
}
}
_snmp.close();
return response;
}
//续接上面第三部分:
/**
* 将十进六制转换成为中文
*/
private static String hexString="0123456789ABCDEF";
public static String toStringHex(String bytes){
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
//将每2位16进制整数组装成一个字节
for(int i=0;i<bytes.length();i+=2){
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
}
return new String(baos.toByteArray());
}
protected static String printVariableBindings(PDU response) {
String strCom="";
for (int i = 0; i < response.size(); i++) {
VariableBinding vb = response.get(i);
String[] str= vb.getVariable().toString().toUpperCase().split(":");
String strOut = "";
int intLength = str.length;
log.debug("长度==> "+ intLength);
for(int j=0; j<intLength; j++){
strOut += str[j];
}
log.debug("==第270=vb.getOid()="+vb.getOid());
System.out.println("==第283行=vb.getVariable()="+vb.getVariable());
strCom += vb.getVariable();
if(str.length!=1){
log.debug("==第284行=vb.getVariable()="+ toStringHex(strOut));//显示中文
}
}
return strCom;
}
protected static String printReport(PDU response) {
if (response.size() < 1) {
System.out
.println("REPORT PDU does not contain a variable binding.");
return "";
}
VariableBinding vb = response.get(0);
OID oid = vb.getOid();
if (SnmpConstants.usmStatsUnsupportedSecLevels.equals(oid)) {
log.info("REPORT: Unsupported Security Level.");
} else if (SnmpConstants.usmStatsNotInTimeWindows.equals(oid)) {
log.info("REPORT: Message not within time window.");
} else if (SnmpConstants.usmStatsUnknownUserNames.equals(oid)) {
log.info("REPORT: Unknown user name.");
} else if (SnmpConstants.usmStatsUnknownEngineIDs.equals(oid)) {
log.info("REPORT: Unknown engine id.");
} else if (SnmpConstants.usmStatsWrongDigests.equals(oid)) {
log.info("REPORT: Wrong digest.");
} else if (SnmpConstants.usmStatsDecryptionErrors.equals(oid)) {
log.info("REPORT: Decryption error.");
} else if (SnmpConstants.snmpUnknownSecurityModels.equals(oid)) {
log.info("REPORT: Unknown security model.");
} else if (SnmpConstants.snmpInvalidMsgs.equals(oid)) {
log.info("REPORT: Invalid message.");
} else if (SnmpConstants.snmpUnknownPDUHandlers.equals(oid)) {
log.info("REPORT: Unknown PDU handler.");
} else if (SnmpConstants.snmpUnavailableContexts.equals(oid)) {
log.info("REPORT: Unavailable context.");
} else if (SnmpConstants.snmpUnknownContexts.equals(oid)) {
log.info("REPORT: Unknown context.");
} else {
log.info("REPORT contains unknown OID (" + oid.toString()+ ").");
}
System.out.println(" Current counter value is "
+ vb.getVariable().toString() + ".");
return vb.getVariable().toString();
}
// 可以为SNMPv3建立一个ScopedPDU,或是为SNMPv1和SNMPv2c建立一个PDU
// 它会为ScopedPDU设定context Name和context Engine ID.
// 最后传回的PDU,会被当成变数request,而且捎后可供其它方法使用
public PDU createPDU(Target target) {
PDU request;
if (_target.getVersion() == SnmpConstants.version3) {
request = new ScopedPDU();
ScopedPDU scopedPDU = (ScopedPDU) request;
if (_contextEngineID != null) {
scopedPDU.setContextEngineID(_contextEngineID);
}
if (_contextName != null) {
scopedPDU.setContextName(_contextName);
}
} else {
request = new PDU();
}
request.setType(_pduType);
return request;
}
private Vector getVariableBinding(String varbind) {
Vector v = new Vector(varbind.length());
String oid = null;
char type = 'i';
String value = null;
int equal = varbind.indexOf("={");
if (equal > 0) {
oid = varbind.substring(0, equal);
type = varbind.charAt(equal + 2);
value = varbind.substring(varbind.indexOf('}') + 1);
} else {
v.add(new VariableBinding(new OID(varbind)));
return v;
}
VariableBinding vb = new VariableBinding(new OID(oid));
if (value != null) {
Variable variable;
switch (type) {
case 'i':
variable = new Integer32(Integer.parseInt(value));
break;
case 'u':
variable = new UnsignedInteger32(Long.parseLong(value));
break;
case 's':
variable = new OctetString(value);
break;
case 'x':
variable = OctetString.fromString(value, ':', 16);
break;
case 'd':
variable = OctetString.fromString(value, '.', 10);
break;
case 'b':
variable = OctetString.fromString(value, ' ', 2);
break;
case 'n':
variable = new Null();
break;
case 'o':
variable = new OID(value);
break;
case 't':
variable = new TimeTicks(Long.parseLong(value));
break;
case 'a':
variable = new IpAddress(value);
break;
default:
throw new IllegalArgumentException("Variable type " + type
+ " not supported");
}
vb.setVariable(variable);
}
v.add(vb);
return v;
}
// 继createPDU()方法后
// 因为request是一个PDU实体,呼叫get(0)会传回一个VariableBinding.
// 在一个VariableBinding上呼叫getOid()会为VariableBinding取得它的OID
private static PDU walk(Snmp snmp, PDU request, Target target)
throws IOException {
request.setNonRepeaters(0);
OID rootOID = request.get(0).getOid();// 首先取得root OID
PDU response = null;
int objects = 0;
int requests = 0;
long startTime = System.currentTimeMillis();
// 该代码段会按字典编纂顺序在我们想要游走的MIB树中取回下一个物件,以及从目标取得回应.
// 如查回应为null,则我们不会收到回应
do {
requests++;
ResponseEvent responseEvent = _snmp.send(request, target);
response = responseEvent.getResponse();
if (response != null) {
objects += response.size();
}
} while (!processWalk(response, request, rootOID));
log.info("Total requests sent: " + requests);
log.info("Total objects received: " + objects);
log.info("Total walk time: "
+ (System.currentTimeMillis() - startTime) + " milliseconds");
return response;
}
// 如果我们抵达MIB的结尾、收到一个错误讯息,或是所收到的回应在我们所游走的MIB中并未按照字典编纂次序,则返回true
// 否则印出目标送来的结果。在请求物件中对所收到的OID进行编码,所以当我们送出下一个请求时,目标将会(按字典编纂次序)
// 传送给我们树中下一个OID,如果存在的话
private static boolean processWalk(PDU response, PDU request, OID rootOID) {
if ((response == null) || (response.getErrorStatus() != 0)
|| (response.getType() == PDU.REPORT)) {
return true;
}
boolean finished = false;
OID lastOID = request.get(0).getOid();
for (int i = 0; (!finished) && (i < response.size()); i++) {
VariableBinding vb = response.get(i);
if ((vb.getOid() == null)
|| (vb.getOid().size() < rootOID.size())
|| (rootOID.leftMostCompare(rootOID.size(), vb.getOid()) != 0)) {
finished = true;
} else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) {
System.out.println(vb.toString());
finished = true;
} else if (vb.getOid().compareTo(lastOID) <= 0) {
log.info("Variable received is not lexicographic successor of requested one:");
log.info(vb.toString() + " <= " + lastOID);
finished = true;
} else {
log.info(vb.toString());// 这里显示结果的
lastOID = vb.getOid();
}
}
if (response.size() == 0) {
finished = true;
}
if (!finished) {
VariableBinding next = response.get(response.size() - 1);
next.setVariable(new Null());
request.set(0, next);
request.setRequestID(new Integer32(0));
}
return finished;
}
//续接上面类:SnmpUtil
public void initReceiver(String host) {
Address address = new UdpAddress(host + "/" + _port);
try {
_transport = new DefaultUdpTransportMapping((UdpAddress) address);
} catch (IOException ioex) {
log.info("Unable to bind to local IP and port: " + ioex);
System.exit(-1);
}
// 使用_numThreads建立一个执行绪集区以便回应送进来的traps讯息
_threadPool = ThreadPool.create(this.getClass().getName(), _numThreads);
// 将一个SNMPv2c讯息处理模型加入MessageDispatcher
MessageDispatcher mtDispatcher = new MultiThreadedMessageDispatcher(
_threadPool, new MessageDispatcherImpl());
// add message processing models,加入讯息处理模型
mtDispatcher.addMessageProcessingModel(new MPv1());
mtDispatcher.addMessageProcessingModel(new MPv2c());
// add all security protocols,加信所有的安全协定
SecurityProtocols.getInstance().addDefaultProtocols();
_snmp = new Snmp(mtDispatcher, _transport);
if (_snmp != null) {
_snmp.addCommandResponder(this);
} else {
log.info("Unable to create Target object");
System.exit(-1);
}
if (_version == SnmpConstants.version3) {
mtDispatcher.addMessageProcessingModel(new MPv3());
MPv3 mpv3 = (MPv3) _snmp
.getMessageProcessingModel(MessageProcessingModel.MPv3);
// 为了接收找寻请求,我们还会建立一个USM项目以及一个本地引擎识别代号
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
mpv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
if (_authoritativeEngineID != null) {
_snmp.setLocalEngine(_authoritativeEngineID.getValue(), 0, 0);
}
this.addUsmUser(_snmp);
}
}
// 此类别的用户端会呼叫listen()以便开始一个期程
public synchronized void listen() {
try {
_transport.listen();// listen()被呼叫时,会让让程式进入等待状态
} catch (IOException ioex) {
System.out.println("Unable to listen: " + ioex);
System.exit(-1);
}
System.out.println("Waiting for traps..");
try {
this.wait();// Wait for traps to come
// in,等待traps讯息被送进来,//当有讯息被送进来时,processPdu()会被调用
} catch (InterruptedException ex) {
log.info("Interrupted while waiting for traps: " + ex);
System.exit(-1);
}
}
// 当有讯息被送进来时,processPdu()会被调用
// 负责处理送进来的请求,因为SnmpWalk支援SNMPv3,所以我们必须能够回应来自权威SNMP引擎的找寻请求
public synchronized void processPdu(CommandResponderEvent e) {
PDU command = e.getPDU();
if (command != null) {
this.printVariableBindings(command);
//String[] str = command.get(0).getVariable().toString().toUpperCase().split(":");
log.debug("这是==> "+command.toString());
if ((command.getType() != PDU.TRAP)
&& (command.getType() != PDU.V1TRAP)
&& (command.getType() != PDU.REPORT)
&& (command.getType() != PDU.RESPONSE)) {
command.setErrorIndex(0);
command.setErrorStatus(0);
command.setType(PDU.RESPONSE);
StatusInformation statusInformation = new StatusInformation();
StateReference ref = e.getStateReference();
try {
e.getMessageDispatcher().returnResponsePdu(
e.getMessageProcessingModel(),
e.getSecurityModel(), e.getSecurityName(),
e.getSecurityLevel(), command,
e.getMaxSizeResponsePDU(), ref, statusInformation);
} catch (MessageException ex) {
log.info("Error while sending response: "
+ ex.getMessage());
}
}
}
}
public String sendAndProcessResponse() {
try {
PDU response = this.send();
if ((getPduType() == PDU.TRAP) || (getPduType() == PDU.REPORT)
|| (getPduType() == PDU.V1TRAP)
|| (getPduType() == PDU.RESPONSE)) {
log.info(PDU.getTypeString(getPduType())+ " sent successfully");
return PDU.getTypeString(getPduType());
} else if (response == null) {
log.info("Request timed out.");
return "Request timed out";
} else if (response.getType() == PDU.REPORT) {
return printReport(response);
} else if (getOperation() == WALK) {
log.info("End of walked subtree '"
+ ((VariableBinding) getVariableBindings().get(0))
.getOid() + "' reached at:");
return printVariableBindings(response);
} else {
log.info("Received something strange: requestID="
+ response.getRequestID() + ", errorIndex="
+ response.getErrorIndex() + ", " + "errorStatus="
+ response.getErrorStatusText() + "("
+ response.getErrorStatus() + ")");
return printVariableBindings(response);
}
} catch (IOException ex) {
log.info("Error while trying to send request: "
+ ex.getMessage());
ex.printStackTrace();
return "s";
}
}
private void checkTrapVariables(Vector vbs) {
if ((_pduType == PDU.INFORM) || (_pduType == PDU.TRAP)) {
if ((vbs.size() == 0)
|| ((vbs.size() > 1) && (!((VariableBinding) vbs.get(0))
.getOid().equals(SnmpConstants.sysUpTime)))) {
vbs.add(0, new VariableBinding(SnmpConstants.sysUpTime,
_sysUpTime));
}
if ((vbs.size() == 1)
|| ((vbs.size() > 2) && (!((VariableBinding) vbs.get(1))
.getOid().equals(SnmpConstants.snmpTrapOID)))) {
vbs.add(1, new VariableBinding(SnmpConstants.snmpTrapOID,
_trapOID));
}
}
}
}
第一步测试Main方法:
Java code
public class MainSnmpWalk{
static Logger log = Logger.getLogger(MainSnmpWalk.class);
public MainSnmpWalk(){
}
public static void main(String[] args) throws InterruptedException{
//建立一个SNMPv2c PDU,它会从system(1.3.6.1.2.1.1)OID开始游走
// SnmpWalk walk = new SnmpWalk("192.168.100.10","1.3.6.1.2.1.1.5.0");//物理位置
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.2.2");//RAM
// SnmpWalk walk = new SnmpWalk("127.0.0.1","1.3.6.1.2.1.25.2.3.1.6");//Hard Disk
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.5.1.1.1");//CPU Utilization
// SnmpWalk walk = new SnmpWalk("127.0.0.1","1.3.6.1.2.1.25.1");//也含本机物理总内存
//cpu:1.3.6.1.2.1.25.5.1.1.1
//memory:1.3.6.1.2.1.25.5.1.1.2 //得到的是每个进程的的内存使用值
//1.3.6.1.2.1.25.3.3.1.2.1 //一个比较通用的看设备CPU利用率的OID值。取最后一分钟的平均值。
// Linux下可以.1.3.6.1.2.1.25.2和.1.3.6.1.2.1.25.5这两个值试试
//====================================================
//.1.3.6.1.4.1.311.1.1.3.1.1.1.2.0
// SnmpWalk walk = new SnmpWalk("127.0.0.1","1.3.6.1.2.1.1.1");//系统描述
// SnmpWalk walk = new SnmpWalk("127.0.0.1","1.3.6.1.2.1.25.1.7.0");//本机物理总内存
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.4.2.1.2");//列出系统进程
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.4.1.77.1.2.25.1.1");//列出系统用户列表
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.4.1.77.1.4.1");//列出域名(工作组)
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.6.3.1.2");//列出安装的软件
// SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.3.3.1.2");
//SnmpWalk walk = new SnmpWalk("127.0.0.1",".1.3.6.1.2.1.25.4.2.1.2");//当前进程列表
//可用的内存数量: .1.3.6.1.4.1.311.1.1.3.1.1.1.2.0
//磁盘利用率的OID: .1.3.6.1.4.1.311.1.1.3.1.1.5.1.3.0
//CPU的利用率: .1.3.6.1.4.1.311.1.1.3.1.1.2.1.3.1.48.0
//发送的字节数(包括组帧字符): 1.3.6.1.2.1.2.2.1.16.1
//收到的字节数(包括组帧字符): 1.3.6.1.2.1.2.2.1.10.1
//cpuPercentProcessorTime .1.3.6.1.4.1.311.1.1.3.1.1.2.1.3
//memoryAvailableKBytes (OID: .1.3.6.1.4.1.311.1.1.3.1.1.1.29)
//memoryAvailableMBytes (OID: .1.3.6.1.4.1.311.1.1.3.1.1.1.30)
//==================================================================================
// log.debug("Doing SNMPv2 walk..");
//1.3.6.1.2.1.1.6.0
// SnmpWalk walk = new SnmpWalk("192.168.100.57","1.3.6.1.2.1.1.4.0");//1.3.6.1.2.1.1.5.0
// String str = walk.doWalk();
// log.debug("主方法str: "+ str);
//==================================================================================
/**
* 在Windows操作系统上默认是没有安装snmp协议的,需要安装snmp,安装细节较简单,这里不再描述
* 1.在Windows上安装好了snmp之后,它默认是不支持SNMPv3的,需要重新安装一个SNMPv3的代理
* 2.在Windows上直接安装软件:snmpv3agent setup.exe并配置好安全名称(用户)、认证协订、认证密码、保密协定以及保密密码
* 3.设置好了之后,在代码里直接相对应就OK了.
*/
//.1.3.6.1.4.1.77.1.2.25.1.1 //1.3.6.1.2.1.1.5.0
// 用来设定安全名称(用户)、认证协订、认证的通行密码、保密协定以及保密的通行密码
log.debug("doing snmpv3 walk..");
//建立一个snmpv3c pdu,它会以kschmidt做为安全名称,md5做为认证协定
SnmpWalk walk = new SnmpWalk("192.168.100.10","1.3.6.1.2.1.1.6.0","zmq","MD5","mysnmpzmq","DES","mysnmpzmq");
String str3 = walk.doWalk();
log.debug("主方法str: "+ str3);
}
}
第二步:类SnmpWalk.java
public class SnmpWalk {
public static final int WALK = 1;
private SnmpUtil _util = null;
//会建立一出一个SNMPv2c walk命令,引数是"执行agent的主机"以及"我们想要游走之OIDs的前缀(prefix)"
public SnmpWalk(String host, String oid){
_util = new SnmpUtil(host,oid,false,0);
_util.setOperation(WALK);
}
//会建立出一个SNMPv3 walk命令,前两个引数与上面建构式一样,其它引数则与SNMPv3的安全设定有关
public SnmpWalk(String host, String oid, String user, String authProtocol, String authPasshrase, String privProtocol, String privPassphrase) {
_util = new SnmpUtil(host,oid,user,authProtocol,authPasshrase,privProtocol,privPassphrase,false,0);
_util.setOperation(WALK);
}
public String doWalk() {
return _util.sendAndProcessResponse();
}
}
第三部分:
public class SnmpUtil extends Thread implements PDUFactory, CommandResponder {
private static Logger log = Logger.getLogger(SnmpUtil.class);
public static final int DEFAULT = 0;
public static final int WALK = 1;
private Target _target;
private Address _address;
private OID _authProtocol;
private OID _privProtocol;
private OctetString _privPassphrase;
private OctetString _authPassphrase;
private OctetString _community = new OctetString("mysnmp");//你自己的community
private OctetString _contextEngineID; //SnmpEngineId s = null;
private OctetString _contextName = new OctetString();
private OctetString _securityName = new OctetString();
private static Snmp _snmp = null;
private int _numThreads = 1;
private int _port = 0;
private ThreadPool _threadPool = null;
private boolean _isReceiver = false;
private OctetString _authoritativeEngineID = new OctetString("1234567");
private TransportMapping _transport = null;
private TimeTicks _sysUpTime = new TimeTicks(0);
private OID _trapOID = new OID("1.3.6.1.4.1.****");
private int _version = 0;
private int _retries = 1;
private int _timeout = 4000;
private int _pduType = 0;
private Vector _vbs = new Vector();
protected int _operation = DEFAULT;
static {
if (System.getProperty("log4j.configuration") == null) {
BasicConfigurator.configure();
}
}
public SnmpUtil(String host, String varbind, boolean receiver, int type) {
_version = SnmpConstants.version2c;
_isReceiver = receiver;
if (type == 2) {
_pduType = PDU.INFORM;
} else if (type == 1) {
_pduType = PDU.TRAP;
} else if (type == 0) {
_pduType = PDU.SET;
}
setPort();
if (!_isReceiver) {
init(host, varbind);
} else {
initReceiver(host);
}
}
// 用来设定安全名称(用户)、认证协订、认证的通行密码、保密协定以及保密的通行密码
// 保密的通行密码、认证的通行密码以及用户在内部都会被储成一个OctetString(与String不同,OctetString被实作成一个
// 与字符集无关的位元组字串)
// 认证和保密协定在内部会被储存成OIDs
public SnmpUtil(String host, String varbind, String user,
String authProtocol, String authPasshrase, String privProtocol,
String privPassphrase, boolean receiver, int type) {
_version = SnmpConstants.version3;
_isReceiver = receiver;
_privPassphrase = new OctetString(privPassphrase);
_authPassphrase = new OctetString(authPasshrase);
_securityName = new OctetString(user);
if (authProtocol.equals("MD5")) {
_authProtocol = AuthMD5.ID;
} else if (authProtocol.equals("SHA")) {
_authProtocol = AuthSHA.ID;
}
if (privProtocol.equals("DES")) {
_privProtocol = PrivDES.ID;
} else if ((privProtocol.equals("AES128"))
|| (privProtocol.equals("AES"))) {
_privProtocol = PrivAES128.ID;
} else if (privProtocol.equals("AES192")) {
_privProtocol = PrivAES192.ID;
} else if (privProtocol.equals("AES256")) {
_privProtocol = PrivAES256.ID;
}
if (type == 2) {
_pduType = PDU.INFORM;
} else if (type == 1) {
_pduType = PDU.TRAP;
}
else if (type == 0) {
_pduType = PDU.SET;
}
setPort();
if (!_isReceiver) {
init(host, varbind);
} else {
initReceiver(host);
}
}
public void setVersion(int version) {
_version = version;
}
public void setOperation(int operation) {
_operation = operation;
if (_operation == WALK) {
log.debug("==setOperation is=="+operation);
// _pduType = PDU.GETNEXT;
_pduType = PDU.GET;
}
}
public int getOperation() {
return _operation;
}
public int getPduType() {
return _pduType;
}
public void setPort() {
if (_pduType == PDU.INFORM || _pduType == PDU.TRAP || _isReceiver) {
_port = 162;
} else {
_port = 161;
}
}
public void init(String host, String varbind) {
_vbs = getVariableBinding(varbind);
if (_pduType == PDU.INFORM || _pduType == PDU.TRAP) {
checkTrapVariables(_vbs);
}
_address = new UdpAddress(host + "/" + _port);
LogFactory.setLogFactory(new Log4jLogFactory());
BER.setCheckSequenceLength(false);
}
public Vector getVariableBindings() {
return _vbs;
}
// 如果使用的是SNMPv3,则会调用addUsmUser(snmp)方法,以便把一个用户加入此期程.<<BB
// 接着会呼叫createTarget()
private void addUsmUser(Snmp snmp) {
snmp.getUSM().addUser(
_securityName,
new UsmUser(_securityName, _authProtocol, _authPassphrase,
_privProtocol, _privPassphrase));
}
// 第一步:创建一个SNMP期程(session),如果使用的是SNMPv3,则会调用addUsmUser(snmp)方法=============AA
private Snmp createSnmpSession() throws IOException {
AbstractTransportMapping transport;
if (_address instanceof TcpAddress) {
transport = new DefaultTcpTransportMapping();
} else {
transport = new DefaultUdpTransportMapping();
}
// Could save some CPU cycles:
// transport.setAsyncMsgProcessingSupported(false);
Snmp snmp = new Snmp(transport);
if (_version == SnmpConstants.version3) {
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
addUsmUser(snmp);
}
return snmp;
}
// 建立一个目标:这所建立的若不是一个SNMPv3,就是一个SNMPv1和SNMPv2
private Target createTarget() {// CC
if (_version == SnmpConstants.version3) {
UserTarget target = new UserTarget();
if (_authPassphrase != null) {
if (_privPassphrase != null) {
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
} else {
target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
}
} else {
target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
}
target.setSecurityName(_securityName);
return target;
} else {
CommunityTarget target = new CommunityTarget();
target.setCommunity(_community);
return target;
}
}
// 一旦我们从createTarget()返回,send()方法会为该目标设定版本、位址、逾时和尝试次数等值
// 它还会让所有的传输对映(transport mappings)进入监听模式,这确保我们能够回应SNMP引擎所发现到的请求
public PDU send() throws IOException {
_snmp = createSnmpSession();
this._target = createTarget();
_target.setVersion(_version);
_target.setAddress(_address);
_target.setRetries(_retries);
_target.setTimeout(_timeout);
_snmp.listen();
PDU request = createPDU(_target);
for (int i = 0; i < _vbs.size(); i++) {
request.add((VariableBinding) _vbs.get(i));
}
PDU response = null;
if (_operation == WALK) {
response = walk(_snmp, request, _target);
} else {
ResponseEvent responseEvent;
long startTime = System.currentTimeMillis();
responseEvent = _snmp.send(request, _target);
if (responseEvent != null) {
response = responseEvent.getResponse();
log.debug("=第241行==responseEvent.getResponse()===: "
+ responseEvent.getResponse());
log.debug("Received response after "
+ (System.currentTimeMillis() - startTime) + " millis");
}
}
_snmp.close();
return response;
}
//续接上面第三部分:
/**
* 将十进六制转换成为中文
*/
private static String hexString="0123456789ABCDEF";
public static String toStringHex(String bytes){
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
//将每2位16进制整数组装成一个字节
for(int i=0;i<bytes.length();i+=2){
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
}
return new String(baos.toByteArray());
}
protected static String printVariableBindings(PDU response) {
String strCom="";
for (int i = 0; i < response.size(); i++) {
VariableBinding vb = response.get(i);
String[] str= vb.getVariable().toString().toUpperCase().split(":");
String strOut = "";
int intLength = str.length;
log.debug("长度==> "+ intLength);
for(int j=0; j<intLength; j++){
strOut += str[j];
}
log.debug("==第270=vb.getOid()="+vb.getOid());
System.out.println("==第283行=vb.getVariable()="+vb.getVariable());
strCom += vb.getVariable();
if(str.length!=1){
log.debug("==第284行=vb.getVariable()="+ toStringHex(strOut));//显示中文
}
}
return strCom;
}
protected static String printReport(PDU response) {
if (response.size() < 1) {
System.out
.println("REPORT PDU does not contain a variable binding.");
return "";
}
VariableBinding vb = response.get(0);
OID oid = vb.getOid();
if (SnmpConstants.usmStatsUnsupportedSecLevels.equals(oid)) {
log.info("REPORT: Unsupported Security Level.");
} else if (SnmpConstants.usmStatsNotInTimeWindows.equals(oid)) {
log.info("REPORT: Message not within time window.");
} else if (SnmpConstants.usmStatsUnknownUserNames.equals(oid)) {
log.info("REPORT: Unknown user name.");
} else if (SnmpConstants.usmStatsUnknownEngineIDs.equals(oid)) {
log.info("REPORT: Unknown engine id.");
} else if (SnmpConstants.usmStatsWrongDigests.equals(oid)) {
log.info("REPORT: Wrong digest.");
} else if (SnmpConstants.usmStatsDecryptionErrors.equals(oid)) {
log.info("REPORT: Decryption error.");
} else if (SnmpConstants.snmpUnknownSecurityModels.equals(oid)) {
log.info("REPORT: Unknown security model.");
} else if (SnmpConstants.snmpInvalidMsgs.equals(oid)) {
log.info("REPORT: Invalid message.");
} else if (SnmpConstants.snmpUnknownPDUHandlers.equals(oid)) {
log.info("REPORT: Unknown PDU handler.");
} else if (SnmpConstants.snmpUnavailableContexts.equals(oid)) {
log.info("REPORT: Unavailable context.");
} else if (SnmpConstants.snmpUnknownContexts.equals(oid)) {
log.info("REPORT: Unknown context.");
} else {
log.info("REPORT contains unknown OID (" + oid.toString()+ ").");
}
System.out.println(" Current counter value is "
+ vb.getVariable().toString() + ".");
return vb.getVariable().toString();
}
// 可以为SNMPv3建立一个ScopedPDU,或是为SNMPv1和SNMPv2c建立一个PDU
// 它会为ScopedPDU设定context Name和context Engine ID.
// 最后传回的PDU,会被当成变数request,而且捎后可供其它方法使用
public PDU createPDU(Target target) {
PDU request;
if (_target.getVersion() == SnmpConstants.version3) {
request = new ScopedPDU();
ScopedPDU scopedPDU = (ScopedPDU) request;
if (_contextEngineID != null) {
scopedPDU.setContextEngineID(_contextEngineID);
}
if (_contextName != null) {
scopedPDU.setContextName(_contextName);
}
} else {
request = new PDU();
}
request.setType(_pduType);
return request;
}
private Vector getVariableBinding(String varbind) {
Vector v = new Vector(varbind.length());
String oid = null;
char type = 'i';
String value = null;
int equal = varbind.indexOf("={");
if (equal > 0) {
oid = varbind.substring(0, equal);
type = varbind.charAt(equal + 2);
value = varbind.substring(varbind.indexOf('}') + 1);
} else {
v.add(new VariableBinding(new OID(varbind)));
return v;
}
VariableBinding vb = new VariableBinding(new OID(oid));
if (value != null) {
Variable variable;
switch (type) {
case 'i':
variable = new Integer32(Integer.parseInt(value));
break;
case 'u':
variable = new UnsignedInteger32(Long.parseLong(value));
break;
case 's':
variable = new OctetString(value);
break;
case 'x':
variable = OctetString.fromString(value, ':', 16);
break;
case 'd':
variable = OctetString.fromString(value, '.', 10);
break;
case 'b':
variable = OctetString.fromString(value, ' ', 2);
break;
case 'n':
variable = new Null();
break;
case 'o':
variable = new OID(value);
break;
case 't':
variable = new TimeTicks(Long.parseLong(value));
break;
case 'a':
variable = new IpAddress(value);
break;
default:
throw new IllegalArgumentException("Variable type " + type
+ " not supported");
}
vb.setVariable(variable);
}
v.add(vb);
return v;
}
// 继createPDU()方法后
// 因为request是一个PDU实体,呼叫get(0)会传回一个VariableBinding.
// 在一个VariableBinding上呼叫getOid()会为VariableBinding取得它的OID
private static PDU walk(Snmp snmp, PDU request, Target target)
throws IOException {
request.setNonRepeaters(0);
OID rootOID = request.get(0).getOid();// 首先取得root OID
PDU response = null;
int objects = 0;
int requests = 0;
long startTime = System.currentTimeMillis();
// 该代码段会按字典编纂顺序在我们想要游走的MIB树中取回下一个物件,以及从目标取得回应.
// 如查回应为null,则我们不会收到回应
do {
requests++;
ResponseEvent responseEvent = _snmp.send(request, target);
response = responseEvent.getResponse();
if (response != null) {
objects += response.size();
}
} while (!processWalk(response, request, rootOID));
log.info("Total requests sent: " + requests);
log.info("Total objects received: " + objects);
log.info("Total walk time: "
+ (System.currentTimeMillis() - startTime) + " milliseconds");
return response;
}
// 如果我们抵达MIB的结尾、收到一个错误讯息,或是所收到的回应在我们所游走的MIB中并未按照字典编纂次序,则返回true
// 否则印出目标送来的结果。在请求物件中对所收到的OID进行编码,所以当我们送出下一个请求时,目标将会(按字典编纂次序)
// 传送给我们树中下一个OID,如果存在的话
private static boolean processWalk(PDU response, PDU request, OID rootOID) {
if ((response == null) || (response.getErrorStatus() != 0)
|| (response.getType() == PDU.REPORT)) {
return true;
}
boolean finished = false;
OID lastOID = request.get(0).getOid();
for (int i = 0; (!finished) && (i < response.size()); i++) {
VariableBinding vb = response.get(i);
if ((vb.getOid() == null)
|| (vb.getOid().size() < rootOID.size())
|| (rootOID.leftMostCompare(rootOID.size(), vb.getOid()) != 0)) {
finished = true;
} else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) {
System.out.println(vb.toString());
finished = true;
} else if (vb.getOid().compareTo(lastOID) <= 0) {
log.info("Variable received is not lexicographic successor of requested one:");
log.info(vb.toString() + " <= " + lastOID);
finished = true;
} else {
log.info(vb.toString());// 这里显示结果的
lastOID = vb.getOid();
}
}
if (response.size() == 0) {
finished = true;
}
if (!finished) {
VariableBinding next = response.get(response.size() - 1);
next.setVariable(new Null());
request.set(0, next);
request.setRequestID(new Integer32(0));
}
return finished;
}
//续接上面类:SnmpUtil
public void initReceiver(String host) {
Address address = new UdpAddress(host + "/" + _port);
try {
_transport = new DefaultUdpTransportMapping((UdpAddress) address);
} catch (IOException ioex) {
log.info("Unable to bind to local IP and port: " + ioex);
System.exit(-1);
}
// 使用_numThreads建立一个执行绪集区以便回应送进来的traps讯息
_threadPool = ThreadPool.create(this.getClass().getName(), _numThreads);
// 将一个SNMPv2c讯息处理模型加入MessageDispatcher
MessageDispatcher mtDispatcher = new MultiThreadedMessageDispatcher(
_threadPool, new MessageDispatcherImpl());
// add message processing models,加入讯息处理模型
mtDispatcher.addMessageProcessingModel(new MPv1());
mtDispatcher.addMessageProcessingModel(new MPv2c());
// add all security protocols,加信所有的安全协定
SecurityProtocols.getInstance().addDefaultProtocols();
_snmp = new Snmp(mtDispatcher, _transport);
if (_snmp != null) {
_snmp.addCommandResponder(this);
} else {
log.info("Unable to create Target object");
System.exit(-1);
}
if (_version == SnmpConstants.version3) {
mtDispatcher.addMessageProcessingModel(new MPv3());
MPv3 mpv3 = (MPv3) _snmp
.getMessageProcessingModel(MessageProcessingModel.MPv3);
// 为了接收找寻请求,我们还会建立一个USM项目以及一个本地引擎识别代号
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
mpv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
if (_authoritativeEngineID != null) {
_snmp.setLocalEngine(_authoritativeEngineID.getValue(), 0, 0);
}
this.addUsmUser(_snmp);
}
}
// 此类别的用户端会呼叫listen()以便开始一个期程
public synchronized void listen() {
try {
_transport.listen();// listen()被呼叫时,会让让程式进入等待状态
} catch (IOException ioex) {
System.out.println("Unable to listen: " + ioex);
System.exit(-1);
}
System.out.println("Waiting for traps..");
try {
this.wait();// Wait for traps to come
// in,等待traps讯息被送进来,//当有讯息被送进来时,processPdu()会被调用
} catch (InterruptedException ex) {
log.info("Interrupted while waiting for traps: " + ex);
System.exit(-1);
}
}
// 当有讯息被送进来时,processPdu()会被调用
// 负责处理送进来的请求,因为SnmpWalk支援SNMPv3,所以我们必须能够回应来自权威SNMP引擎的找寻请求
public synchronized void processPdu(CommandResponderEvent e) {
PDU command = e.getPDU();
if (command != null) {
this.printVariableBindings(command);
//String[] str = command.get(0).getVariable().toString().toUpperCase().split(":");
log.debug("这是==> "+command.toString());
if ((command.getType() != PDU.TRAP)
&& (command.getType() != PDU.V1TRAP)
&& (command.getType() != PDU.REPORT)
&& (command.getType() != PDU.RESPONSE)) {
command.setErrorIndex(0);
command.setErrorStatus(0);
command.setType(PDU.RESPONSE);
StatusInformation statusInformation = new StatusInformation();
StateReference ref = e.getStateReference();
try {
e.getMessageDispatcher().returnResponsePdu(
e.getMessageProcessingModel(),
e.getSecurityModel(), e.getSecurityName(),
e.getSecurityLevel(), command,
e.getMaxSizeResponsePDU(), ref, statusInformation);
} catch (MessageException ex) {
log.info("Error while sending response: "
+ ex.getMessage());
}
}
}
}
public String sendAndProcessResponse() {
try {
PDU response = this.send();
if ((getPduType() == PDU.TRAP) || (getPduType() == PDU.REPORT)
|| (getPduType() == PDU.V1TRAP)
|| (getPduType() == PDU.RESPONSE)) {
log.info(PDU.getTypeString(getPduType())+ " sent successfully");
return PDU.getTypeString(getPduType());
} else if (response == null) {
log.info("Request timed out.");
return "Request timed out";
} else if (response.getType() == PDU.REPORT) {
return printReport(response);
} else if (getOperation() == WALK) {
log.info("End of walked subtree '"
+ ((VariableBinding) getVariableBindings().get(0))
.getOid() + "' reached at:");
return printVariableBindings(response);
} else {
log.info("Received something strange: requestID="
+ response.getRequestID() + ", errorIndex="
+ response.getErrorIndex() + ", " + "errorStatus="
+ response.getErrorStatusText() + "("
+ response.getErrorStatus() + ")");
return printVariableBindings(response);
}
} catch (IOException ex) {
log.info("Error while trying to send request: "
+ ex.getMessage());
ex.printStackTrace();
return "s";
}
}
private void checkTrapVariables(Vector vbs) {
if ((_pduType == PDU.INFORM) || (_pduType == PDU.TRAP)) {
if ((vbs.size() == 0)
|| ((vbs.size() > 1) && (!((VariableBinding) vbs.get(0))
.getOid().equals(SnmpConstants.sysUpTime)))) {
vbs.add(0, new VariableBinding(SnmpConstants.sysUpTime,
_sysUpTime));
}
if ((vbs.size() == 1)
|| ((vbs.size() > 2) && (!((VariableBinding) vbs.get(1))
.getOid().equals(SnmpConstants.snmpTrapOID)))) {
vbs.add(1, new VariableBinding(SnmpConstants.snmpTrapOID,
_trapOID));
}
}
}
}
发表评论
-
java阻塞队列学习(转帖)
2010-06-06 11:50 3482import java.util.concurrent.Arr ... -
httpclient链接关闭问题(转帖)
2010-03-11 22:09 2156HttpClient client = new HttpCli ... -
J2EE集群原理(转贴)
2010-03-09 19:57 1229J2EE集群原理 什么是集群呢?总的来说,集群包括两个概念: ... -
Spring 实现ibatis事务回滚
2010-03-05 20:34 3654网上发现这篇文章,转过来学习一下,好不好用还没有实践,需要自己 ... -
java 可变参数
2009-12-14 20:50 14155vararg的意思是变量参数,它是与Jav ... -
java中的string
2009-11-25 14:45 1332从javaeye的论坛里看到这 ... -
使用JDBC对BLOB和CLOB进行处理
2009-11-12 20:33 4872从网上看到这篇文章,转过来做为学习用 设有表:create ... -
避免或最小化 Servlet 中的同步
2009-09-24 21:02 983servlet 是多线程的。基于 servlet 的应用程序必 ... -
httpclient 使用小记
2009-09-03 21:05 3399最近学习apache的httpclient 在使用postm ... -
httpclient 学习笔记
2009-08-23 16:52 1552前阵子,学习了一下APACHE 的httpclient,感觉h ... -
log4j使用
2009-06-19 21:08 1560从网上看到得,发现写 ... -
java虚拟机的一些概念
2009-05-24 10:22 1351从网上看到这篇文章,感觉适合初学jvm,就转来作为以后学习参考 ... -
xmlbean 生成带包路径的jar
2009-05-23 23:12 3309因为使用xmlbean生成 ... -
多线程中使用静态方法
2009-05-14 19:54 6649终于把内存泄漏问题解决了,不是多线程访问静态函数的问题,事实证 ... -
java Annotation入门
2009-04-16 21:22 1263因为偶然的原因需要使用Annotation,所以从网上找了这篇 ... -
MINA2.0简单使用
2009-04-12 16:12 5156一直知道MINA是apache 开发的一个开发socket编程 ... -
log4j参数配置说明
2009-03-22 20:31 2014前几天用到log4j,到网上查了一下他的参数配置,发现还是比较 ... -
portal之SSO开发文档
2009-03-02 20:12 4071前两天看到别人写的,感觉不错,就转了过来做为参考 ... -
cas在tomcat中的配置
2009-02-25 21:22 2852cas在tomcat上配置时,需要打开tomc ... -
SSO 原理浅谈
2009-02-25 19:41 2120SSO 是一个非常大的主题,我对这个主题有着深深的感受,自从 ...
相关推荐
SNMP4J是Java语言开发的一个开放源代码的简单网络管理协议(SNMP)实现工具包,主要用于在Java环境中进行网络设备的管理和监控。标题中的"snmp4j-1.9.3d.zip_snmp4j-1.9.3d_snmp管理工具"表明这是一个版本为1.9.3d的...
SNMP4J是一个Java库,专门用于实现简单网络管理协议(SNMP)的应用程序。SNMP是一种广泛应用于网络设备管理的标准协议,它允许系统管理员远程监控和管理网络设备的状态。在本示例中,我们将深入探讨如何使用SNMP4J...
SNMP4J是一个Java库,专门用于实现简单网络管理协议(SNMP)的应用程序。SNMP是一种广泛应用于网络设备管理的标准协议,它允许系统管理员远程监控和管理网络设备,如路由器、交换机、服务器等。在Java环境中,SNMP4J...
SNMP4J-Agentx是一个基于Java的开源工具,主要用于实现简单网络管理协议(SNMP)的代理功能。这个工具在SNMP4J项目下开发,是SNMP4J的一个扩展,提供了更丰富的功能和更方便的接口来创建和管理SNMP代理。SNMP4J-...
在Java环境中,SNMP4J库为开发者提供了便利,可以用来读取和操作SNMP协议的数据。本文将深入探讨如何利用SNMP4j包来读取SNMP协议数据。 首先,理解SNMP4j的基本概念至关重要。SNMP4j是一个开源的Java库,完全实现了...
SNMP4J的API文档(如SNMP4J_API_en_s1.chm)包含了详细的类和方法描述,以及示例代码,是学习和使用SNMP4J的重要参考资料。开发者可以通过查阅文档,了解每个类的功能、方法的参数和返回值,以便更有效地使用SNMP4J...
SNMP4J是一个Java实现的简单网络管理协议(SNMP)的应用编程接口(API),它提供了全面的功能,用于开发SNMP应用。SNMP是网络管理领域的重要标准,用于监控和管理网络设备,如路由器、交换机和服务器。下面将详细...
这个压缩包"snmp4j的发送和接收trap.zip"包含了使用SNMP4j进行Trap操作的相关资源。 首先,我们要了解SNMP Trap。在SNMP中,Trap是一种由网络设备主动发送到管理站(通常是一个SNMP管理软件)的通知,用于报告异常...
SNMP4J是一个Java实现的简单网络管理协议(SNMP)库,用于开发与SNMP兼容的网络管理系统或应用程序。SNMP是一种广泛用于网络设备管理的标准协议,它允许管理员远程监控和配置网络设备,如路由器、交换机和服务器。在...
Snmp4j提供了丰富的API来执行SNMP操作,以下是一个简单的示例代码: ```java import org.snmp4j.*; import org.snmp4j.mp.MPv1; import org.snmp4j.mp.MPv2c; import org.snmp4j.security.*; import org.snmp4j.smi...
SNMP4J是一个Java实现的简单网络管理协议(SNMP)的应用编程接口(API),它为开发者提供了在Java环境中编写SNMP应用的工具。SNMP是网络设备管理和监控的标准协议,广泛应用于网络设备配置、性能监控和故障排查。在...
在提供的压缩包文件"SnmpProject"中,可能包含一个示例项目,演示了如何使用SNMP4j进行SNMP通信。你可以参考该项目的源代码,了解更具体的实现细节和最佳实践。记得在实际操作时,确保遵循网络管理的最佳实践,尊重...
本文将深入探讨如何使用SNMP4J-Agent工具在个人计算机上模拟SNMP服务,以便进行测试和开发。 SNMP4J-Agent是SNMP4J项目的一部分,是一个开源的Java库,用于实现SNMP代理功能。它提供了丰富的API,可以方便地构建...
SNMP4J是一个Java库,专门用于实现简单网络管理协议(SNMP)的应用程序。SNMP是网络管理和监控的标准协议,允许系统管理员远程收集和配置网络设备的信息。在这个"SNMP4J远程获取设备信息案例"中,我们将深入探讨如何...
SNMP(简单网络管理协议)是用于网络设备管理的标准协议,而SNMP4J是一个Java实现的SNMP库,它提供了在Java应用程序中与SNMP兼容设备交互的能力。"SNMP get walk" 是一个关键的操作,它允许从网络设备中批量获取MIB...
SNMP4J是Java中一个流行的SNMP库,它封装了SNMP协议的复杂性,提供了简单易用的API接口。使用SNMP4J,开发者能够通过编写Java代码来实现对网络设备的操作,比如检索设备信息、修改设备配置、接收告警通知等。 在这...
在这个“snmp4j中agent程序demo”中,我们将探讨SNMP代理的基本概念、Snmp4j库的使用以及如何创建一个简单的SNMP代理程序。 首先,理解SNMP代理的概念是至关重要的。SNMP代理是网络设备上的软件组件,负责收集设备...
SNMP4j是一款开源的Java库,专门用于实现简单网络管理协议(SNMP)的应用程序。这个库使得Java开发者能够轻松地与支持SNMP的网络设备进行通信,从而监控、管理和配置网络基础设施。"SNMP4j帮助文件"是针对这款库的...
总的来说,这个压缩包提供了一个了解和实践SNMP4J的完整环境,从基本的示例代码到运行环境的搭建。通过学习这个入门实例,开发者可以掌握如何使用SNMP4J进行设备查询和配置,同时对SNMP协议有更深入的理解。而XP ...
SNMP4J是一个开源的Java实现,用于支持简单网络管理协议(SNMP)的应用程序开发。它提供了完整的SNMPv1、SNMPv2c和SNMPv3的支持,包括安全模型和认证协议。这份文档详细介绍了如何使用SNMP4J库来构建SNMP应用程序,...