/*
* SNMP Package
*
* Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
*
* This is free software. Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package com.linewell.monitor.snmp;
import java.io.*;
import java.net.*;
/**
* The class SNMPv1CommunicationInterface defines methods for communicating with SNMP entities.
* The approach is that from version 1 of SNMP, using no encryption of data. Communication occurs
* via UDP, using port 161, the standard SNMP port, unless an alternate (non-standard) port is
* supplied in the constructor.
*/
public class SNMPv1CommunicationInterface
{
public static final int SNMP_PORT = 161;
// port to which requests will be sent
private int remotePort = SNMP_PORT;
// largest size for datagram packet payload; based on
// RFC 1157, need to handle messages of at least 484 bytes
private int receiveBufferSize = 512;
private int version;
private InetAddress hostAddress;
private String community;
DatagramSocket dSocket;
public int requestID = 1;
/**
* Construct a new communication object to communicate with the specified host using the
* given community name. The version setting should be either 0 (version 1) or 1 (version 2,
* a la RFC 1157).
*/
public SNMPv1CommunicationInterface(int version, InetAddress hostAddress, String community)
throws SocketException
{
this(version, hostAddress, community, SNMP_PORT);
}
/**
* Construct a new communication object to communicate with the specified host using the
* given community name, and sending requests to the specified port. The version setting
* should be either 0 (version 1) or 1 (version 2, a la RFC 1157).
*/
public SNMPv1CommunicationInterface(int version, InetAddress hostAddress, String community, int remotePort)
throws SocketException
{
this.remotePort = remotePort;
this.version = version;
this.hostAddress = hostAddress;
this.community = community;
dSocket = new DatagramSocket();
dSocket.setSoTimeout(15000); //15 seconds
}
/**
* Permits setting timeout value for underlying datagram socket (in milliseconds).
*/
public void setSocketTimeout(int socketTimeout)
throws SocketException
{
dSocket.setSoTimeout(socketTimeout);
}
/**
* Close the "connection" with the device.
*/
public void closeConnection()
throws SocketException
{
dSocket.close();
}
/**
* Retrieve all MIB variable values subsequent to the starting object identifier
* given in startID (in dotted-integer notation). Return as SNMPVarBindList object.
* Uses SNMPGetNextRequests to retrieve variable values in sequence.
* @throws IOException Thrown when timeout experienced while waiting for response to request.
* @throws SNMPBadValueException
*/
public SNMPVarBindList retrieveAllMIBInfo(String startID)
throws IOException, SNMPBadValueException
{
// send GetNextRequests until receive
// an error message or a repeat of the object identifier we sent out
SNMPVarBindList retrievedVars = new SNMPVarBindList();
int errorStatus = 0;
int errorIndex = 0;
SNMPObjectIdentifier requestedObjectIdentifier = new SNMPObjectIdentifier(startID);
SNMPVariablePair nextPair = new SNMPVariablePair(requestedObjectIdentifier, new SNMPNull());
SNMPSequence varList = new SNMPSequence();
varList.addSNMPObject(nextPair);
SNMPPDU pdu = new SNMPPDU(SNMPBERCodec.SNMPGETNEXTREQUEST, requestID, errorStatus, errorIndex, varList);
SNMPMessage message = new SNMPMessage(version, community, pdu);
byte[] messageEncoding = message.getBEREncoding();
DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, remotePort);
dSocket.send(outPacket);
while (errorStatus == 0)
{
DatagramPacket inPacket = new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize);
dSocket.receive(inPacket);
byte[] encodedMessage = inPacket.getData();
SNMPMessage receivedMessage = new SNMPMessage(SNMPBERCodec.extractNextTLV(encodedMessage,0).value);
//errorStatus = ((BigInteger)((SNMPInteger)((receivedMessage.getPDU()).getSNMPObjectAt(1))).getValue()).intValue();
varList = (receivedMessage.getPDU()).getVarBindList();
SNMPSequence newPair = (SNMPSequence)(varList.getSNMPObjectAt(0));
SNMPObjectIdentifier newObjectIdentifier = (SNMPObjectIdentifier)(newPair.getSNMPObjectAt(0));
SNMPObject newValue = newPair.getSNMPObjectAt(1);
retrievedVars.addSNMPObject(newPair);
if (requestedObjectIdentifier.equals(newObjectIdentifier))
break;
requestedObjectIdentifier = newObjectIdentifier;
requestID++;
nextPair = new SNMPVariablePair(requestedObjectIdentifier, new SNMPNull());
varList = new SNMPSequence();
varList.addSNMPObject(nextPair);
pdu = new SNMPPDU(SNMPBERCodec.SNMPGETNEXTREQUEST, requestID, errorStatus, errorIndex, varList);
message = new SNMPMessage(version, community, pdu);
messageEncoding = message.getBEREncoding();
outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, remotePort);
dSocket.send(outPacket);
}
return retrievedVars;
}
private String hexByte(byte b)
{
int pos = b;
if (pos < 0)
pos += 256;
String returnString = new String();
returnString += Integer.toHexString(pos/16);
returnString += Integer.toHexString(pos%16);
return returnString;
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
snmp.rar (94个子文件)
snmp
SNMPObjectIdentifier.java 14KB
server
MonitorServerInfoDAO.java 6KB
MonitorServerInfo.java 2KB
MonitorServerInfoSetter.java 2KB
MonitorServerInfoBusiness.java 2KB
MonitorServerInfoAction.java 9KB
MonitorServerInfoQuery.java 6KB
SNMPCounter64.java 4KB
SNMPUnknownObject.java 3KB
SNMPUInteger32.java 4KB
SNMPv2TrapPDU.java 9KB
SNMPv2TrapListener.java 2KB
SNMPGauge32.java 4KB
SNMPCounter32.java 4KB
SNMPOctetString.java 8KB
SNMPv2InformRequestPDU.java 9KB
SNMPNull.java 3KB
memory
MonitorServerMemoryHistory.java 2KB
MonitorServerMemoryDAO.java 7KB
MonitorServerMemoryHistoryDAO.java 5KB
MonitorServerMemoryHistoryQuery.java 6KB
MonitorServerMemoryAction.java 22KB
MonitorServerMemoryQuery.java 6KB
MonitorServerMemorySetter.java 3KB
MonitorServerMemoryBusiness.java 2KB
MonitorServerMemoryHistoryBusiness.java 2KB
MonitorServerMemory.java 3KB
MonitorServerMemoryHistorySetter.java 2KB
SNMPInteger.java 6KB
SNMPObject.java 4KB
SNMPTimeTicks.java 2KB
SNMPv2InformRequestListener.java 2KB
SNMPv1AgentInterface.java 19KB
SNMPTrapReceiverInterface.java 15KB
SNMPTrapSenderInterface.java 7KB
SNMPPDU.java 9KB
app
SnmpTool.java 13KB
SnmpConstants.java 6KB
storage
MonitorServerStorageHistory.java 2KB
MonitorServerStorageHistoryDAO.java 6KB
MonitorServerStorageHistoryBusiness.java 2KB
MonitorServerStorageAction.java 25KB
MonitorServerStorageHistorySetter.java 3KB
MonitorServerStorageHistoryQuery.java 6KB
SNMPSetException.java 3KB
snmpif
SnmpConnector.java 14KB
SnmpProperty.java 4KB
MibParserAPI.java 4KB
SNMPVarBindList.java 3KB
SNMPInformRequestSenderInterface.java 5KB
SNMPNSAPAddress.java 6KB
SNMPTLV.java 2KB
run
MonitorServerRunAction.java 12KB
MonitorServerAllRunDAO.java 6KB
MonitorServerRunHistoryBusiness.java 2KB
MonitorServerRunHistoryQuery.java 6KB
MonitorServerRunHistorySetter.java 2KB
MonitorServerRunHistoryDAO.java 5KB
MonitorServerAllRunSetter.java 3KB
MonitorServerAllRunBusiness.java 2KB
MonitorServerRunHistory.java 2KB
MonitorServerAllRunQuery.java 7KB
MonitorServerAllRun.java 2KB
SNMPJob.java 14KB
SNMPBitString.java 2KB
SNMPv2BulkRequestPDU.java 9KB
SNMPException.java 2KB
SNMPBadValueException.java 2KB
SNMPGetException.java 3KB
SNMPv1CommunicationInterface.java 48KB
SNMPIPAddress.java 6KB
entity
IfEntry.java 3KB
HrStorageEntry.java 3KB
SystemEntry.java 2KB
HrSWRunEntry.java 3KB
SNMPBERCodec.java 11KB
SNMPv1TrapListener.java 2KB
SNMPv1TrapPDU.java 12KB
SNMPMessage.java 10KB
cpu
MonitorServerCpuSetter.java 3KB
MonitorServerCpuHistory.java 2KB
MonitorServerCpuHistoryDAO.java 5KB
MonitorServerCpuHistoryBusiness.java 2KB
MonitorServerCpuDAO.java 6KB
MonitorServerCpuHistorySetter.java 2KB
MonitorServerCpuQuery.java 6KB
MonitorServerCpuAction.java 24KB
MonitorServerCpuBusiness.java 2KB
MonitorServerCpu.java 3KB
MonitorServerCpuHistoryQuery.java 6KB
SNMPVariablePair.java 3KB
SNMPSequence.java 8KB
SNMPRequestListener.java 7KB
SNMPRequestException.java 3KB
共 94 条
- 1
林当时
- 粉丝: 96
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0