通过yum安装服务
yum install -y net-snmp
yum install -y net-snmp-devel
yum install -y net-snmp-libs
yum install -y net-snmp-perl
yum install -y net-snmp-utils
yum install -y mrtg
修改配置,使之可用查看机器的数据
cd /etc/snmp
vim snmpd.conf
找到具体位置 然后改成这样
####
# First, map the community name "public" into a "security name"
# sec.name source community
com2sec notConfigUser default public
com2sec notConfigUser localhost public
####
# Second, map the security name into a group name:
# groupName securityModel securityName
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
# Second, map the security name into a group name:
# groupName securityModel securityName
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
####
# Third, create a view for us to let the group have rights to:
# Make at least snmpwalk -v 1 localhost -c public system fast again.
# name incl/excl subtree mask(optional)
view all included .1
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
####
# Finally, grant the group read-only access to the systemview view.
# group context sec.model sec.level prefix read write notif
access notConfigGroup "" any noauth exact all none none
# -----------------------------------------------------------------------------
然后 esc :wq 保存退出
启动snmp
输入 service snmpd start 并用 service snmpd status 查看服务状态。
Java代码
<dependency>
<groupId>org.snmp4j</groupId>
<artifactId>snmp4j</artifactId>
<version>2.8.2</version>
</dependency>
import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
/**
* <p>
* ClassName: GetOID
* <p>
* <p>
* Description:获得本机的信息
* <p>
*
*/
public class testSNMP {
public static void main(String[] args) throws Exception {
try {
// 设定CommunityTarget
CommunityTarget myTarget = new CommunityTarget();
// 定义远程主机的地址
// Address deviceAdd =
// GenericAddress.parse("udp:192.168.1.233/161");
// 定义本机的地址
Address localAdd = GenericAddress.parse("udp:192.168.1.130/161");
// 设定远程主机的地址
// myTarget.setAddress(deviceAdd);
// 设定本地主机的地址
myTarget.setAddress(localAdd);
// 设置snmp共同体
myTarget.setCommunity(new OctetString("public"));
// 设置超时重试次数
myTarget.setRetries(2);
// 设置超时的时间
myTarget.setTimeout(5 * 60);
// 设置使用的snmp版本
myTarget.setVersion(SnmpConstants.version2c);
// 设定采取的协议 // 设定传输协议为UDP
TransportMapping transport = new DefaultUdpTransportMapping();
// 调用TransportMapping中的listen()方法,启动监听进程,接收消息,由于该监听进程是守护进程,最后应调用close()方法来释放该进程
transport.listen();
// 创建SNMP对象,用于发送请求PDU
Snmp protocol = new Snmp(transport);
// 创建请求pdu,获取mib
PDU request = new PDU();
// 调用的add方法绑定要查询的OID // linux常见的oid
request.add(new VariableBinding(new OID(
".1.3.6.1.2.1.1.1")));
request.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3")));
request.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.2")));
request.add(new VariableBinding(new OID(".1.3.6.1.4.1.2021.4.5.0")));
// 调用setType()方法来确定该pdu的类型
request.setType(PDU.GETNEXT);
// 调用 send(PDU pdu,Target target)发送pdu,返回一个ResponseEvent对象
ResponseEvent responseEvent = protocol.send(request, myTarget);
// 通过ResponseEvent对象来获得SNMP请求的应答pdu,方法:public PDU getResponse()
PDU response = responseEvent.getResponse();
// 输出
if (response != null) {
System.out.println("request.size()=" + request.size());
System.out.println("response.size()=" + response.size());
// 通过应答pdu获得mib信息(之前绑定的OID的值),方法:VaribleBinding get(int index)
for (int i = 0; i < response.size(); i++) {
VariableBinding vb1 = response.get(i);
System.out.println(vb1);
}
// 调用close()方法释放该进程
transport.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
附: 锐捷交换机 获取各个端口的流量 流入 流出的数据
import java.io.IOException;
import java.util.Vector;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class testSNMP {
public static void main(String[] args) throws Exception {
try {
// 设定CommunityTarget
CommunityTarget myTarget = new CommunityTarget();
// 定义机器的地址
Address localAdd = GenericAddress.parse("udp:192.168.1.254/161");
// 设定机器的地址
myTarget.setAddress(localAdd);
// 设置snmp共同体
myTarget.setCommunity(new OctetString("ruijie"));
// 设置超时重试次数
myTarget.setRetries(2);
// 设置超时的时间
myTarget.setTimeout(5 * 60);
// 设置使用的snmp版本
myTarget.setVersion(SnmpConstants.version2c);
// 设定采取的协议 // 设定传输协议为UDP
TransportMapping transport = new DefaultUdpTransportMapping();
// 调用TransportMapping中的listen()方法,启动监听进程,接收消息,由于该监听进程是守护进程,最后应调用close()方法来释放该进程
transport.listen();
// 创建SNMP对象,用于发送请求PDU
Snmp protocol = new Snmp(transport);
// 创建请求pdu,获取mib
PDU request = new PDU();
// 调用的add方法绑定要查询的OID
for (int i = 0; i < 28; i++) {
//28个接口 输入字节数
request.add(new VariableBinding(new OID(".1.3.6.1.2.1.31.1.1.1.6." + i)));
}
for (int i = 0; i < 28; i++) {
//28个接口 输出字节数
request.add(new VariableBinding(new OID(".1.3.6.1.2.1.31.1.1.1.10." + i)));
}
// 调用setType()方法来确定该pdu的类型
request.setType(PDU.GETNEXT);
// 调用 send(PDU pdu,Target target)发送pdu,返回一个ResponseEvent对象
ResponseEvent responseEvent = protocol.send(request, myTarget);
// 通过ResponseEvent对象来获得SNMP请求的应答pdu,方法:public PDU getResponse()
PDU response = responseEvent.getResponse();
Vector<? extends VariableBinding> vector = response.getVariableBindings();
// 输出
System.err.println(vector.toString());
// 通过应答pdu获得mib信息(之前绑定的OID的值),方法:VaribleBinding get(int index)
for (int i = 0; i < response.size(); i++) {
VariableBinding vb1 = response.get(i);
System.out.println(vb1);
}
// 调用close()方法释放该进程
transport.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}