1、安装snmp
rpm -qa|grep snmp
yum search snmp
yum install -y net-snmp.x86_64
cd /etc/snmp
mv snmpd.conf snmpd.conf.bak
cd /etc/snmp
sh io
sh size
sh cpu
yum search iostat
yum install -y sysstat.x86_64
yum install -y net-snmp-utils.x86_64
新建snmpChange.sh文件
cd /etc/snmp
sh snmpChange.sh
snmpget -v 2c -c public localhost .1.3.6.1.2.1.1.1.0.255.0
snmpget -v 2c -c public localhost .1.3.6.1.2.1.1.1.0.255.1
snmpget -v 2c -c public localhost .1.3.6.1.2.1.1.1.0.255.2
lsof -i:161
kill pid 或者 service snmpd stop
snmpChange.sh
#!/bin/bash
#1
if [ -f '/etc/snmp/snmpd.conf' ];then
echo "修改配置文件"
sed -i 's/\/home\/snmp/\/etc\/snmp/g' ./snmpd.conf
echo "配置文件修改完成"
else
echo "配置文件不存在"
fi
#2
#echo "变更脚本文件夹"
#if [ ! -d '/usr/local/local' ];then
#echo "目标目录不存在"
#mkdir /usr/local/local
#echo "目录已创建"
#else
#echo "目标目录已存在"
#fi
#3
if [ -f '/home/snmp/io' ];then
echo "copy io"
mv /home/snmp/io /etc/snmp/io
fi
if [ -f '/home/snmp/cpu' ];then
echo "copy cpu"
mv /home/snmp/cpu /etc/snmp/cpu
fi
if [ -f '/home/snmp/size' ];then
echo "copy size"
mv /home/snmp/size /etc/snmp/size
fi
#4
if [ -d '/home/snmp' ];then
echo "删除原目录"
rm -rf /home/snmp
fi
#5
echo "添加开机自启"
chkconfig snmpd on
size
#!bin/bash
echo 1.3.6.1.2.1.1.1.0.255.2
echo string
df -B 1|grep /usr/local/nginx/html|awk '{print$4}'
exit 0
cpu
echo .1.3.6.1.2.1.1.1.0.255.1
echo string
more /proc/cpuinfo|grep "model name"
io
#!bin/bash
echo .1.3.6.1.2.1.1.1.0.255.0
echo string
iostat -x|awk 'NR==7 {print $12}'
exit 0
方法1、GET单个OID值
SnmpData.java
package com.bjb.TestSnmp;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.Null;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.TableEvent;
/**
* 演示: GET单个OID值
*
* @ClassName: SnmpData
* @Description: TODO(这里用一句话描述这个类的作用)
* @author majm
* @date 2017年12月12日 下午4:56:32
*
*/
public class SnmpData {
public static final int DEFAULT_VERSION = SnmpConstants.version2c;
public static final String DEFAULT_PROTOCOL = "udp";
public static final int DEFAULT_PORT = 161;
public static final long DEFAULT_TIMEOUT = 3 * 1000L;
public static final int DEFAULT_RETRY = 3;
/**
* 创建对象communityTarget,用于返回target
*
* @param targetAddress
* @param community
* @param version
* @param timeOut
* @param retry
* @return CommunityTarget
*/
public static CommunityTarget createDefault(String ip, String community) {
Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(address);
target.setVersion(DEFAULT_VERSION);
target.setTimeout(DEFAULT_TIMEOUT); // milliseconds
target.setRetries(DEFAULT_RETRY);
return target;
}
/* 根据OID,获取单条消息 */
public static void snmpGet(String ip, String community, String oid) {
CommunityTarget target = createDefault(ip, community);
Snmp snmp = null;
try {
PDU pdu = new PDU();
// pdu.add(new VariableBinding(new OID(new int[]
// {1,3,6,1,2,1,1,2})));
pdu.add(new VariableBinding(new OID(oid)));
DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
snmp.listen();
System.out.println("-------> 发送PDU <-------");
pdu.setType(PDU.GET);
ResponseEvent respEvent = snmp.send(pdu, target);
System.out.println("PeerAddress:" + respEvent.getPeerAddress());
PDU response = respEvent.getResponse();
if (response == null) {
System.out.println("response is null, request time out");
} else {
// Vector<VariableBinding> vbVect =
// response.getVariableBindings();
// System.out.println("vb size:" + vbVect.size());
// if (vbVect.size() == 0) {
// System.out.println("response vb size is 0 ");
// } else {
// VariableBinding vb = vbVect.firstElement();
// System.out.println(vb.getOid() + " = " + vb.getVariable());
// }
System.out.println("response pdu size is " + response.size());
for (int i = 0; i < response.size(); i++) {
VariableBinding vb = response.get(i);
System.out.println(vb.getOid() + " = " + vb.getVariable());
}
}
System.out.println("SNMP GET one OID value finished !");
} catch (Exception e) {
e.printStackTrace();
System.out.println("SNMP Get Exception:" + e);
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException ex1) {
snmp = null;
}
}
}
}
/* 根据OID列表,一次获取多条OID数据,并且以List形式返回 */
public static void snmpGetList(String ip, String community, List<String> oidList) {
CommunityTarget target = createDefault(ip, community);
Snmp snmp = null;
try {
PDU pdu = new PDU();
for (String oid : oidList) {
pdu.add(new VariableBinding(new OID(oid)));
}
DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
snmp.listen();
System.out.println("-------> 发送PDU <-------");
pdu.setType(PDU.GET);
ResponseEvent respEvent = snmp.send(pdu, target);
System.out.println("PeerAddress:" + respEvent.getPeerAddress());
PDU response = respEvent.getResponse();
if (response == null) {
System.out.println("response is null, request time out");
} else {
System.out.println("response pdu size is " + response.size());
for (int i = 0; i < response.size(); i++) {
VariableBinding vb = response.get(i);
System.out.println(vb.getOid() + " = " + vb.getVariable());
}
}
System.out.println("SNMP GET one OID value finished !");
} catch (Exception e) {
e.printStackTrace();
System.out.println("SNMP Get Exception:" + e);
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException ex1) {
snmp = null;
}
}
}
}
/* 根据OID列表,采用异步方式一次获取多条OID数据,并且以List形式返回 */
public static void snmpAsynGetList(String ip, String community, List<String> oidList) {
CommunityTarget target = createDefault(ip, community);
Snmp snmp = null;
try {
PDU pdu = new PDU();
for (String oid : oidList) {
pdu.add(new VariableBinding(new OID(oid)));
}
DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
snmp.listen();
System.out.println("-------> 发送PDU <-------");
pdu.setType(PDU.GET);
ResponseEvent respEvent = snmp.send(pdu, target);
System.out.println("PeerAddress:" + respEvent.getPeerAddress());
PDU response = respEvent.getResponse();
/* 异步获取 */
final CountDownLatch latch = new CountDownLatch(1);
ResponseListener listener = new ResponseListener() {
public void onResponse(ResponseEvent event) {
((Snmp) event.getSource()).cancel(event.getRequest(), this);
PDU response = event.getResponse();
PDU request = event.getRequest();
System.out.println("[request]:" + request);
if (response == null) {
System.out.println("[ERROR]: response is null");
} else if (response.getErrorStatus() != 0) {
System.out.println("[ERROR]: response status" + response.getErrorStatus() + " Text:"
+ response.getErrorStatusText());
} else {
System.out.println("Received response Success!");
for (int i = 0; i < response.size(); i++) {
VariableBinding vb = response.get(i);
System.out.println(vb.getOid() + " = " + vb.getVariable());
}
System.out.println("SNMP Asyn GetList OID finished. ");
latch.countDown();
}
}
};
pdu.setType(PDU.GET);
snmp.send(pdu, target, null, listener);
System.out.println("asyn send pdu wait for response...");
boolean wait = latch.await(30, TimeUnit.SECONDS);
System.out.println("latch.await =:" + wait);
snmp.close();
System.out.println("SNMP GET one OID value finished !");
} catch (Exception e) {
e.printStackTrace();
System.out.println("SNMP Get Exception:" + e);
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException ex1) {
snmp = null;
}
}
}
}
/* 根据targetOID,获取树形数据 */
public static void snmpWalk(String ip, String community, String targetOid) {
CommunityTarget target = createDefault(ip, community);
TransportMapping transport = null;
Snmp snmp = null;
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
PDU pdu = new PDU();
OID targetOID = new OID(targetOid);
pdu.add(new VariableBinding(targetOID));
boolean finished = false;
System.out.println("----> demo start <----");
while (!finished) {
VariableBinding vb = null;
ResponseEvent respEvent = snmp.getNext(pdu, target);
PDU response = respEvent.getResponse();
if (null == response) {
System.out.println("responsePDU == null");
finished = true;
break;
} else {
vb = response.get(0);
}
// check finish
finished = checkWalkFinished(targetOID, pdu, vb);
if (!finished) {
System.out.println("==== walk each vlaue :");
System.out.println(vb.getOid() + " = " + vb.getVariable());
// Set up the variable binding for the next entry.
pdu.setRequestID(new Integer32(0));
pdu.set(0, vb);
} else {
System.out.println("SNMP walk OID has finished.");
snmp.close();
}
}
System.out.println("----> demo end <----");
} catch (Exception e) {
e.printStackTrace();
System.out.println("SNMP walk Exception: " + e);
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException ex1) {
snmp = null;
}
}
}
}
private static boolean checkWalkFinished(OID targetOID, PDU pdu, VariableBinding vb) {
boolean finished = false;
if (pdu.getErrorStatus() != 0) {
System.out.println("[true] responsePDU.getErrorStatus() != 0 ");
System.out.println(pdu.getErrorStatusText());
finished = true;
} else if (vb.getOid() == null) {
System.out.println("[true] vb.getOid() == null");
finished = true;
} else if (vb.getOid().size() < targetOID.size()) {
System.out.println("[true] vb.getOid().size() < targetOID.size()");
finished = true;
} else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) {
System.out.println("[true] targetOID.leftMostCompare() != 0");
finished = true;
} else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) {
System.out.println("[true] Null.isExceptionSyntax(vb.getVariable().getSyntax())");
finished = true;
} else if (vb.getOid().compareTo(targetOID) <= 0) {
System.out.println("[true] Variable received is not " + "lexicographic successor of requested " + "one:");
System.out.println(vb.toString() + " <= " + targetOID);
finished = true;
}
return finished;
}
/* 根据targetOID,异步获取树形数据 */
public static void snmpAsynWalk(String ip, String community, String oid) {
final CommunityTarget target = createDefault(ip, community);
Snmp snmp = null;
try {
System.out.println("----> demo start <----");
DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
snmp.listen();
final PDU pdu = new PDU();
final OID targetOID = new OID(oid);
final CountDownLatch latch = new CountDownLatch(1);
pdu.add(new VariableBinding(targetOID));
ResponseListener listener = new ResponseListener() {
public void onResponse(ResponseEvent event) {
((Snmp) event.getSource()).cancel(event.getRequest(), this);
try {
PDU response = event.getResponse();
// PDU request = event.getRequest();
// System.out.println("[request]:" + request);
if (response == null) {
System.out.println("[ERROR]: response is null");
} else if (response.getErrorStatus() != 0) {
System.out.println("[ERROR]: response status" + response.getErrorStatus() + " Text:"
+ response.getErrorStatusText());
} else {
System.out.println("Received Walk response value :");
VariableBinding vb = response.get(0);
boolean finished = checkWalkFinished(targetOID, pdu, vb);
if (!finished) {
System.out.println(vb.getOid() + " = " + vb.getVariable());
pdu.setRequestID(new Integer32(0));
pdu.set(0, vb);
((Snmp) event.getSource()).getNext(pdu, target, null, this);
} else {
System.out.println("SNMP Asyn walk OID value success !");
latch.countDown();
}
}
} catch (Exception e) {
e.printStackTrace();
latch.countDown();
}
}
};
snmp.getNext(pdu, target, null, listener);
System.out.println("pdu 已发送,等到异步处理结果...");
boolean wait = latch.await(30, TimeUnit.SECONDS);
System.out.println("latch.await =:" + wait);
snmp.close();
System.out.println("----> demo end <----");
} catch (Exception e) {
e.printStackTrace();
System.out.println("SNMP Asyn Walk Exception:" + e);
}
}
/* 根据OID和指定string来设置设备的数据 */
public static void setPDU(String ip, String community, String oid, String val) throws IOException {
CommunityTarget target = createDefault(ip, community);
Snmp snmp = null;
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(oid), new OctetString(val)));
pdu.setType(PDU.SET);
DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
snmp.listen();
System.out.println("-------> 发送PDU <-------");
snmp.send(pdu, target);
snmp.close();
}
}
方法2、定义oid配置文件、初始化CommunityTarget
SnmpUtil.java
package com.bjb.TestSnmp;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;
import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;
/**
*
* snmp
*
*/
public class SnmpUtil {
private static Logger errorLogger = Logger.getLogger("errorFile");
private static Snmp snmp=null;
private CommunityTarget target;
public SnmpUtil(String intranetDeviceIp,Integer snmpPort) throws IOException{
if(snmp==null){
snmp=new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
}
//初始化CommunityTarget
target=new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress(intranetDeviceIp+"/"+snmpPort));
target.setTimeout(1000);
target.setRetries(1);
}
public ResponseEvent snmpGet(String OID) throws IOException{
PDU pdu=new PDU();
pdu.addOID(new VariableBinding(new org.snmp4j.smi.OID(OID)));
ResponseEvent re=snmp.get(pdu, target);
return re;
}
public List<TableEvent> snmpWalk(String OID){
TableUtils utils=new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));
OID[] columnOids=new OID[]{new OID(OID)};
List<TableEvent> list=utils.getTable(target,columnOids,null,null);
return list;
}
/**
* 获取cpu使用率
* @return
*/
public Float getCpuUsage(){
List<TableEvent> list=snmpWalk(SnmpConfig.SNMPWALK_HRPROCESSLOAD);
float usage=0.0f;
for (TableEvent tableEvent : list) {
try{
usage+=Integer.parseInt(tableEvent.toString().split("=")[3].split("]")[0].trim());
}catch(Exception e){
return null;
}
}
usage=usage/list.size();
return usage;
}
/**
* 获取内存使用率
* @return
* @throws IOException
*/
public Float getMemoryUsage() throws IOException{
Float usage=null;
Float totalSize=null;
ResponseEvent event=snmpGet(SnmpConfig.SNMPGET_HRMEMORYSIZE);
if(event.getResponse()!=null){
totalSize=Float.parseFloat(event.getResponse().toString().split("=")[4].split("]")[0].trim());
/*ResponseEvent event2=snmpGet(SnmpConfig.SNMPGET_MEMORYUSE);
usage=Float.parseFloat(event2.getResponse().toString().split("=")[4].split("]")[0].trim());*/
usage=getMemoryUsed();
return usage/totalSize*100;
}
return null;
}
private Float getMemoryUsed(){
List<TableEvent> list=snmpWalk(SnmpConfig.SNMPWALK_HRSTORAGEDESCR);
int index=0;
for(int i=0;i<list.size();i++){
if(list.get(i).toString().split("=")[3].split("]")[0].trim().contains("Physical memory")){
index=i;
}
}
List<TableEvent> usedList=snmpWalk(SnmpConfig.SNMPWALK_HRSTORAGEUSED);
return Float.parseFloat(usedList.get(index).toString().split("=")[3].split("]")[0].trim());
}
/**
* 获取IO负载
* @return
* @throws IOException
*/
public Float getIOUsage() throws IOException{
ResponseEvent event=snmpGet(SnmpConfig.SNMPGET_IOLOAD);
if(event.getResponse()!=null){
//errorLogger.error("每隔10分钟获取设备IO使用率:" + event.getResponse().toString());
float usage=Float.parseFloat(event.getResponse().toString().split("=")[4].split("]")[0].trim());
//errorLogger.error("每隔10分钟获取设备IO使用率:" + usage);
if(usage>1){
return usage;
//return 100.0f;
}else{
return usage*100;
}
}
return null;
}
/**
* 获取性某性能参数数据
* @param parameterId
* @return
*/
public Object getData(Integer parameterId) {
Object data=null;
try{
if(parameterId.equals(SnmpConfig.PERFORMANCE_PARAM_CPUUSAGE)){
data=getCpuUsage();
}else if(parameterId.equals(SnmpConfig.PERFORMANCE_PARAM_MEMORYUSAGE)){
data=getMemoryUsage();
}else if(parameterId.equals(SnmpConfig.PERFORMANCE_PARAM_IOUSAGE)){
data=getIOUsage();
}
}catch(Exception e){
e.printStackTrace();
}
return data;
}
/**
* 获取设备物理地址
* @return
*/
public String getMacAddress(){
List<TableEvent> list=snmpWalk(SnmpConfig.SNMPWALK_IFDESCR);
int index=0;
for(int i=0;i<list.size();i++){
if(list.get(i).toString().split("=")[3].split("]")[0].trim().contains("eth")){
index=i;
}
}
List<TableEvent> ifAddressList=snmpWalk(SnmpConfig.SNMPWALK_IFPHYSADDRESS);
return ifAddressList.get(index).toString().split("=")[3].split("]")[0].trim();
}
/**
* 获取设备内存大小 单位为GB
* @return
* @throws IOException
*/
public String getMemoryDesc() throws IOException{
ResponseEvent event=snmpGet(SnmpConfig.SNMPGET_HRMEMORYSIZE);
Long bytes=Long.parseLong(event.getResponse().toString().split("=")[4].split("]")[0].trim());
float gb=bytes/1024.0f/1024.0f;
DecimalFormat decimalFormat=new DecimalFormat(".00");
String size=decimalFormat.format(gb)+"GB";
return size;
}
/**
* @throws IOException
* 获取cpu描述信息
* @return
*/
public String getCpuDesc() throws IOException {
ResponseEvent event=snmpGet(SnmpConfig.SNMPGET_CPUDESC);
return event.getResponse().toString().split("=")[4].split("]")[0].trim().split(":")[1].trim();
}
/**
* 获取存储设备大小 (空盘空间不足百分百)
* @param string
* @return
* @throws IOException
*/
public long getDiskSize(String deviceCode) throws IOException{
/*List<TableEvent> events=snmpWalk(SnmpConfig.SNMPWALK_HRSTORAGEDESCR);
int index=0;
String base=CommonUtil.BASE_PATH;
base=base.substring(0,base.length()-1);
for(int i=0;i<events.size();i++){
if(events.get(i).toString().split("=")[3].split("]")[0].trim().indexOf(base)!=-1){
index=i;
}
}
List<TableEvent> storageAllocationUnits=snmpWalk(SnmpConfig.SNMPWALK_HRSTORAGEALLOCATIONUNITS);
List<TableEvent> storageSizes=snmpWalk(SnmpConfig.SNMPWALK_HRSTORAGESIZE);
Long storageAllocationUnit=Long.parseLong(storageAllocationUnits.get(index).toString().split("=")[3].split("]")[0].trim());
Long storageSize=Long.parseLong(storageSizes.get(index).toString().split("=")[3].split("]")[0].trim());
Long size=storageAllocationUnit*storageSize;
return size;*/
ResponseEvent event=snmpGet(SnmpConfig.SNMPGET_CACHESIZE);
System.out.println(event.getResponse().toString().split("=")[4].split("]")[0].trim());
return Long.parseLong(event.getResponse().toString().split("=")[4].split("]")[0].trim());
}
/**
* 获取磁盘描述
* @param deviceCode
* @return
* @throws IOException
*/
public String getDiskDesc(String deviceCode) throws IOException{
float size=getDiskSize(deviceCode)/1024.0f/1024/1024;
DecimalFormat decimalFormat=new DecimalFormat("0.00");
String desc=decimalFormat.format(size)+"GB";
return desc;
}
/**
* snmp协议检测
* @throws IOException
*/
public boolean snmpCheck() throws IOException{
ResponseEvent re=snmpGet(".1.3.6.1.4.1.2021.255.1");
if(re.getResponse()==null){
return false;
}
return true;
}
}
SnmpConfig.java
package com.bjb.TestSnmp;
public class SnmpConfig {
/**
* walk 存储设备 簇的数目
*/
public static final String SNMPWALK_HRSTORAGESIZE=".1.3.6.1.2.1.25.2.3.1.5";
/**
* walk 存储设备 簇的大小
*/
public static final String SNMPWALK_HRSTORAGEALLOCATIONUNITS=".1.3.6.1.2.1.25.2.3.1.4";
/**
* get cpu描述信息
*/
public static final String SNMPGET_CPUDESC=".1.3.6.1.2.1.1.1.0.255.1";
/**
* walk 网络接口描述
*/
public static final String SNMPWALK_IFDESCR=".1.3.6.1.2.1.2.2.1.2";
/**
* walk 接口物理地址
*/
public static final String SNMPWALK_IFPHYSADDRESS=".1.3.6.1.2.1.2.2.1.6";
/**
* get IO负载
*/
public static final String SNMPGET_IOLOAD=".1.3.6.1.2.1.1.1.0.255.0";
/**
* 缓存设备存储空间可用大小
*/
public static final String SNMPGET_CACHESIZE=".1.3.6.1.2.1.1.1.0.255.2";
/**
* walk cpu使用率
*/
public static final String SNMPWALK_HRPROCESSLOAD=". 1.3.6.1.2.1.25.3.3.1.2";
/**
* walk 存储设备描述
*/
public static final String SNMPWALK_HRSTORAGEDESCR=".1.3.6.1.2.1.25.2.3.1.3";
/**
*walk 存储设备使用大小
*/
public static final String SNMPWALK_HRSTORAGEUSED=".1.3.6.1.2.1.25.2.3.1.6";
/**
* get 获取内存大小
*/
public static final String SNMPGET_HRMEMORYSIZE=".1.3.6.1.2.1.25.2.2.0";
/**
* get 获取内存已使用大小
*/
public static final String SNMPGET_MEMORYUSE=".1.3.6.1.4.1.2021.4.6.0";
/**
* 收集间隔
*/
public static final Integer INTERVAL=1000;
/**
* 处理间隔
*/
public static final Integer HANDLEINTERVAL=1000*60*60;
/**
* 性能参数ID-cpu使用率
*/
public static final Integer PERFORMANCE_PARAM_CPUUSAGE=1;
/**
* 性能参数ID-内存使用率
*/
public static final Integer PERFORMANCE_PARAM_MEMORYUSAGE=2;
/**
* 性能参数ID-io带宽占用率
*/
public static final Integer PERFORMANCE_PARAM_IOUSAGE=3;
/**
* performanceSocketMessage 获取数据
*/
public static final String MESSAGE_GETDATA="getData";
/**
* performanceSocketMessage 获取更多数据
*/
public static final String MESSAGE_GETMORE="getMore";
public static final String MESSAGE_REMOVEPARAMETER="removeParameter";
/**
* performanceSocketMessage 停止获取数据
*/
public static final String MESSAGE_OVER="over";
/**
* 实时性能监控间隔
*/
public static final Integer PERFORMANCE_REALTIME_MONITOR_INTERVAL=1000;
/**
* 持续时间 允许间隔
*/
public static final Integer ALARM_MONITOR_LAST_INTERVAL=5000;
}
3、测试类
package com.bjb.TestSnmp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.bjb.TestSnmp.SnmpData;
/**
* SnmpGet.java
* @ClassName: TestSnmpGet
* @Description: TODO(这里用一句话描述这个类的作用)
* @author majm
* @date 2017年12月12日 下午4:55:34
*
*/
public class TestSnmpGet {
@Test
public void testSnmp() throws IOException {
String snmpIp = "192.168.0.130";
Integer snmpPort = 161;
SnmpUtil snmpUtil=new SnmpUtil(snmpIp, snmpPort);
System.out.println(snmpUtil.getDiskSize("asd"));
System.out.println(snmpUtil.getDiskDesc("asd"));
System.out.println(snmpUtil.getCpuDesc());
System.out.println(snmpUtil.getIOUsage());
System.out.println(snmpUtil.getMacAddress());
}
public void testGet() {
String ip = "192.168.0.130";
String community = "public";
String oidval = "1.3.6.1.2.1.1.1.0.255.2";
SnmpData.snmpGet(ip, community, oidval);
}
public void testGetList() {
String ip = "127.0.0.1";
String community = "public";
List<String> oidList = new ArrayList<String>();
oidList.add("1.3.6.1.2.1.1.5.0");
oidList.add("1.3.6.1.2.1.1.7.0");
SnmpData.snmpGetList(ip, community, oidList);
}
public void testGetAsyList() {
String ip = "127.0.0.1";
String community = "public";
List<String> oidList = new ArrayList<String>();
oidList.add("1.3.6.1.2.1");
oidList.add("1.3.6.1.2.12");
SnmpData.snmpAsynGetList(ip, community, oidList);
System.out.println("i am first!");
}
public void testWalk() {
String ip = "127.0.0.1";
String community = "public";
String targetOid = "1.3.6.1.2.1.1.5.0";
SnmpData.snmpWalk(ip, community, targetOid);
}
public void testAsyWalk() {
String ip = "127.0.0.1";
String community = "public";
// 异步采集数据
SnmpData.snmpAsynWalk(ip, community, "1.3.6.1.2.1.25.4.2.1.2");
}
public void testSetPDU() throws Exception {
String ip = "127.0.0.1";
String community = "public";
SnmpData.setPDU(ip, community, "1.3.6.1.2.1.1.6.0", "jianghuiwen");
}
public void testVersion() {
// System.out.println(org.snmp4j.version.VersionInfo.getVersion());
}
}