本例用到两个开源包,snmpget使用SNMP4J框架,snmpwalk使用Java SNMP Package开源包,下载地址分别为:
http://www.snmp4j.org/html/download.html
http://gicl.cs.drexel.edu/people/sevy/snmp/
/**
* SNMP管理类
*/
public class SnmpManager {
private static final Log log = LogFactory.getLog(SnmpManager.class);
private static int version = 0; // SNMP版本, 0表示版本1
private static String protocol = "udp"; // 监控时使用的协议
private static String port = "161"; // 监控时使用的端口
/**
* 获取SNMP节点值
*
* @param ipAddress 目标IP地址
* @param community 公同体
* @param oid 对象ID
* @return String 监控结果代号
* @throws AppException
*/
@SuppressWarnings("unchecked")
public static String snmpGet(String ipAddress, String community, String oid) throws AppException {
String resultStat = null; // 监控结果状态
StringBuffer address = new StringBuffer();
address.append(protocol);
address.append(":");
address.append(ipAddress);
address.append("/");
address.append(port);
// Address targetAddress = GenericAddress.parse(protocol + ":" + ipAddress + "/" + port);
Address targetAddress = GenericAddress.parse(address.toString());
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(oid)));
pdu.setType(PDU.GET);
// 创建共同体对象CommunityTarget
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(targetAddress);
target.setVersion(SnmpConstants.version1);
target.setTimeout(2000);
target.setRetries(1);
DefaultUdpTransportMapping udpTransportMap = null;
Snmp snmp = null;
try {
// 发送同步消息
udpTransportMap = new DefaultUdpTransportMapping();
udpTransportMap.listen();
snmp = new Snmp(udpTransportMap);
ResponseEvent response = snmp.send(pdu, target);
PDU resposePdu = response.getResponse();
if (resposePdu == null) {
log.info(ipAddress + ": Request timed out.");
} else {
//errorStatus = resposePdu.getErrorStatus();
Object obj = resposePdu.getVariableBindings().firstElement();
VariableBinding variable = (VariableBinding) obj;
resultStat = variable.getVariable().toString();
}
} catch (Exception e) {
throw new AppException("获取SNMP节点状态时发生错误!", e);
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException e) {
snmp = null;
}
}
if (udpTransportMap != null) {
try {
udpTransportMap.close();
} catch (IOException e) {
udpTransportMap = null;
}
}
}
if (log.isInfoEnabled()) {
log.info("IP:" + ipAddress + " resultStat:" + resultStat);
}
return resultStat;
}
/**
* 走访SNMP节点
*
* @param ipAddress 目标IP地址
* @param community 共同体
* @param oid 节点起始对象标志符
* @return String[] 走方结果
* @throws AppException
*/
public static String[] snmpWalk(String ipAddress, String community, String oid) throws AppException {
String[] returnValueString = null; // oid走访结果数组
SNMPv1CommunicationInterface comInterface = null;
try {
InetAddress hostAddress = InetAddress.getByName(ipAddress);
comInterface = new SNMPv1CommunicationInterface(
version, hostAddress, community);
comInterface.setSocketTimeout(2000);
// 返回所有以oid开始的管理信息库变量值
SNMPVarBindList tableVars = comInterface.retrieveMIBTable(oid);
returnValueString = new String[tableVars.size()];
// 循环处理所有以oid开始的节点的返回值
for (int i = 0; i < tableVars.size(); i++) {
SNMPSequence pair = (SNMPSequence) tableVars.getSNMPObjectAt(i); // 获取SNMP序列对象, 即(OID,value)对
SNMPObject snmpValue = pair.getSNMPObjectAt(1); // 获取某个节点的返回值
String typeString = snmpValue.getClass().getName(); // 获取SNMP值类型名
// 设置返回值
if (typeString.equals("snmp.SNMPOctetString")) {
String snmpString = snmpValue.toString();
int nullLocation = snmpString.indexOf('\0');
if (nullLocation >= 0)
snmpString = snmpString.substring(0, nullLocation);
returnValueString[i] = snmpString;
} else {
returnValueString[i] = snmpValue.toString();
}
}
} catch (SocketTimeoutException ste) {
if (log.isErrorEnabled()) {
log.error("走访IP为" + ipAddress + ", OID为" + oid + " 时超时!");
}
returnValueString = null;
} catch (Exception e) {
throw new AppException("SNMP走访节点时发生错误!", e);
} finally {
if (comInterface != null) {
try {
comInterface.closeConnection();
} catch (SocketException e) {
comInterface = null;
}
}
}
return returnValueString;
}
}
分享到:
- 2008-12-13 00:06
- 浏览 8839
- 评论(8)
- 论坛回复 / 浏览 (5 / 16452)
- 查看更多
相关推荐
利用snmp协议获取机器设备信息 代码 snmp java
snmp 获取服务器信息 snmp 获取服务器信息 snmp 获取服务器信息
Python利用SNMP读取交换机端口信息及状态,通过路由器和交换机上启用SNMP协议,Python环境下安装net-snmp-python包实现
通过snmp的OID获取对方主机的内存利用率及CPU的使用率
java利用snmp4j包来读取snmp协议数据
要想试用snmp获取dell服务器信息的,这个是必须要的
shell脚本,通过snmp方式获取远程服务器的CPU内存和硬盘的使用率,需要支持snmpget和snmpwalk命令,并且为了支持小数运算,需要安装bc命令。
快速获取SNMP设备oid的软件工具
snmp 获取机器信息 控制关机重启(在被控制端运行客户端的情况下) linux&windows; java实现 利用别人的snmp java开发包实现,文件里面包含这个包。 包括一个文档。 被控制端包括linux版本的,和windows的。
通过使用SNMP协议获取某个交换机或服务器相关设备信息,如获取交换机设备,则可以通过设备OID获取相关信息,以及能获取交换机下所有连接的PC机的MAC地址与IP地址以及端口号信息。
通过snmp和oid获取电脑的内存硬盘的使用情况和总量
利用SNMP4J实现Snmp Trap完整例子 - 企业应用 - Java - JavaEye论坛.mht
Java来实现snmp的get、walk代码示例
SNMP查看CPU及内存使用率 NET-SNMP 是SNMP 协议的一套开源实现工具,可作为NMS 或Agent。本文主要介绍NETSNMP 作为NMS 通过SNMP 协议查看H3C 设备(作为Agent)CPU 利用率的配置应用。
snmp snmp snmp 获取MAC MACMAC的一些资料
4-FortiGate防火墙SNMP状态监控OID值说明-v1.1归类.pdf
NET-SNMP服务端添加自定义节点.doc
基于java的Oid获取软件.rar Java进行SNMP通信的指南SnmpProgrammingGuideViaJava.doc Java实现snmp的get和walk代码示例.doc snmp4j.chm snmp.chm SnmpMain.java snmp配置说明.doc snmp协议详细讲解.doc ...