那位做过snmp4j的开发的,如何java 获取cpu 内存,内存

Snmp学习总结系列——开篇
时间: 09:58:39
&&&& 阅读:171
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&  进入公司以来,一直参与到公司的产品研发工作当中去,在产品研发中有一个监控远程服务器CPU使用率,内存使用情况,硬盘的需求,技术总监提出了使用Snmp协议作为远程监控的技术解决方案,头一次听说Snmp这个东西,我主动请缨,负责攻克使用snmp来实现监控远程服务器的技术难关,经过一个多月的研究,参考了网上的一些资料,总算是有所收获,在此打算写一个关于Snmp的学习总结系列,初步预定有以下内容:
1、Snmp基本概念介绍
2、Snmp协议在各个操作系统下的安装
WinXP安装和配置SNMP
Win7安装和配置SNMP
WinServer2003安装和配置SNMP
WindowsServer2008安装和配置SNMP
linux下安装和配置SNMP
3、使用Java的开源组件snmp4j实现Snmp的各种功能
以上就是打算要总结的内容。标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:http://www.cnblogs.com/xdp-gacl/p/4182744.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!博客分类:
常用SNMP OID
Linux SNMP OID’s for CPU,Memory and Disk Statistics
SNMP stands for Simple Network Management Protocol and consists of three key components: managed devices, agents, and network-management systems (NMSs). A managed device is a node that has an SNMP agent and resides on a managed network. These devices can be routers and access server, switches and bridges, hubs, computer hosts, or printers. An agent is a software module residing within a device. This agent translates information into a compatible format with SNMP. An NMS runs monitoring applications. They provide the bulk of processing and memory resources required for network management.SNMP MIBs, OIDs OverviewMIB stands for Management Information Base and is a collection of information organized hierarchically. These are accessed using a protocol such as SNMP. There are two types of MIBs: scalar and tabular. Scalar objects define a single object instance whereas tabular objects define multiple related object instances grouped in MIB tables.MIB files for specific devices or systems can be downloaded from hereOIDs or Object Identifiers uniquely identify manged objects in a MIB hierarchy. This can be depicted as a tree, the levels of which are assigned by different organizations. Top level MIB object IDs (OIDs) belong to different standard organizations. Vendors define private branches including managed objects for their own products.Here is a sample structure of an OIDIso (1).org(3).dod(6).internet(1).private(4).transition(868).products(2).chassis(4).card(1).slotCps(2)-.-cpsSlotSummary(1).cpsModuleTable(1).cpsModuleEntry(1).cpsModuleModel(3).3562.3Most of the people may be looking for OID’s for Linux OID’s for CPU,Memory and Disk Statistics for this first you need to install SNMP server and clients. If you want to install SNMP server and client installation in linux学习 check hereCPU StatisticsLoad1 minute Load: .1.3.6.1.4.1..3.15 minute Load: .1.3.6.1.4.1..3.215 minute Load: .1.3.6.1.4.1..3.3CPUpercentage of user CPU time: .1.3.6.1.4.1..0raw user cpu time: .1.3.6.1.4.1..0percentages of system CPU time: .1.3.6.1.4.1..0raw system cpu time: .1.3.6.1.4.1..0percentages of idle CPU time: .1.3.6.1.4.1..0raw idle cpu time: .1.3.6.1.4.1..0raw nice cpu time: .1.3.6.1.4.1..0Memory StatisticsTotal Swap Size: .1.3.6.1.4.1..0Available Swap Space: .1.3.6.1.4.1..0Total RAM in machine: .1.3.6.1.4.1..0Total RAM used: .1.3.6.1.4.1..0Total RAM Free: .1.3.6.1.4.1..0Total RAM Shared: .1.3.6.1.4.1..0Total RAM Buffered: .1.3.6.1.4.1..0Total Cached Memory: .1.3.6.1.4.1..0Disk StatisticsThe snmpd.conf needs to be edited. Add the following (assuming a machine with a single ‘/’ partition):disk / 100000 (or)includeAllDisks 10% for all partitions and disksThe OIDs are as followsPath where the disk is mounted: .1.3.6.1.4.1..2.1Path of the device for the partition: .1.3.6.1.4.1..3.1Total size of the disk/partion (kBytes): .1.3.6.1.4.1..6.1Available space on the disk: .1.3.6.1.4.1..7.1Used space on the disk: .1.3.6.1.4.1..8.1Percentage of space used on disk: .1.3.6.1.4.1..9.1Percentage of inodes used on disk: .1.3.6.1.4.1..10.1System Uptime: .1.3.6.1.2.1.1.3.0ExamplesThese Commands you need to run on the SNMP serverGet available disk space for / on the target host#snmpget -v 1 -c “community” target_name_or_ip .1.3.6.1.4.1..7.1this will return available disk space for the first entry in the ‘disk’ section of snmpd. replace 1 with n for the nth entryGet the 1-minute system load on the target host#snmpget -v 1 -c “community” target_name_or_ip .1.3.6.1.4.1..3.1Get the 5-minute system load on the target host#snmpget -v 1 -c “community” target_name_or_ip .1.3.6.1.4.1..3.2Get the 15-minute system load on the target host#snmpget -v 1 -c “community” target_name_or_ip .1.3.6.1.4.1..3.3Get amount of available swap space on the target host#snmpget -v 1 -c “community” target_name_or_ip .1.3.6.1.4.1..0
import java.io.IOE
import java.net.InetA
import java.util.V
import org.snmp4j.AbstractT
import org.snmp4j.CommunityT
import org.snmp4j.PDU;
import org.snmp4j.S
import org.snmp4j.TransportM
import org.snmp4j.event.ResponseE
import org.snmp4j.mp.SnmpC
import org.snmp4j.smi.A
import org.snmp4j.smi.GenericA
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetS
import org.snmp4j.smi.UdpA
import org.snmp4j.smi.VariableB
import org.snmp4j.transport.DefaultUdpTransportM
public class SNMP_oper
public static void main(String[] args)
Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161");
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();//监听
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));//设置共同体名
target.setAddress(targetAddress);//设置目标Agent地址
target.setRetries(2);//重试次数
target.setTimeout(5000);//超时设置
target.setVersion(1);//版本
PDU request = new PDU();
request.setType(PDU.GET);//操作类型GET
request.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.1.0")));//OID_sysDescr
request.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.2.0")));//OID_sysObjectID
request.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3.0")));//OID_sysUpTime
System.out.println("Request UDP:" + request);
ResponseEvent respEvt = snmp.send(request, target);
//读取得到的绑定变量
if (respEvt != null && respEvt.getResponse()!=null)
Vector &VariableBinding& revBindings = respEvt.getResponse().getVariableBindings();
for (int i=0; i&revBindings.size();i++)
VariableBinding vbs = revBindings.elementAt(i);
System.out.println(vbs.getOid()+":"+vbs.getVariable());
catch (IOException e)
e.printStackTrace();
更详细是资料 http://blog.chinaunix.net/u/10921/showart_202809.html
你好又有个问题要问你,table型的数据要怎么全部读出来。例如网络流入数据包这个信息,它的oid是1.3.6.1.2.1.2.2.1.10.0,这个oid对应着是一个表格。如果按照上面讲的方法去做,那么只能读出table的第一个数据。如果想全部读出来要怎么做呢?网上搜来一段程序,如下......OID[] columns = new OID[1];&&&&&&&&&&&& &&&&&&&&&&&& columns[0] =& new VariableBinding(new OID ("1.3.6.1.2.1.2.2.1.10.0")).getOid();&&&&&&&&&&&& List list = tu.getTable(target, columns, null, null);&&&&&&&&&&&& System.out.println(list.size());&&&&&&&&&&&& for(int i=0;i&list.size();i++){&&&&&&&&&&& & System.out.println("aaaaaa");&&&&&&&&&&& & System.out.println(list);&&&&&&&&&&&&&&&& TableEvent te = (TableEvent)list.get(i);&&&&&&&&&&&&&&&& System.out.println("bbbb");&&&&&&&&&&&&&&&& VariableBinding [] vbs = te.getColumns();&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&& System.out.println(vbs[0]);&&&&&&&&&&&&&&&& for(int j=0;j&vbs.j++){&&&&&&&&&&&&&&&&&&&& System.out.println(vbs[j]);&&&&&&&&&&&&&&&& }&&&&&&&&&&&& }..............但是运行会出错误,不知道该怎么办,能指导下吗,谢谢
上面的例子,返回的是个ResponseEvent,如果返回的是个Table,我觉得应该用TableEvent,这个我没有用到过,你可以试试看。
The TableEvent class reports events in a table retrieval operation.
不同硬件设备的OID是不一样的,与设备的制造商有关,通过一些开源的监控设备可以检测到这些OID的值,或者咨询设备生产商。如果开发通用的监控程序,必须知道你所检测设备中各项功能的OID值,如果只是自己公司内部用的,可以自己实现这些功能,比如监测CPU等信息。谢谢!明白了
你好,我最近也在做一个snmp4j的开发项目,类似于网络监测系统。要显示服务器的cpu和内存以及磁盘的使用情况。看了你的关于snmp的资料,收获很多。这里有个问题想请教下:服务器的cpu、内存的oid,不同的机器是一样的吗?还是说不同的机器的cpu和内存有不同的oid?我写了一个target的程序,能够读出public的oid,例如系统的名字和运行时间等。那如果要读取cpu的相关信息的话,直接按照你给的oid去读取如:CPUpercentage of user CPU time: .1.3.6.1.4.1..0raw user cpu time: .1.3.6.1.4.1..0percentages of system CPU time: .1.3.6.1.4.1..0raw system cpu time: .1.3.6.1.4.1..0percentages of idle CPU time: .1.3.6.1.4.1..0raw idle cpu time: .1.3.6.1.4.1..0raw nice cpu time: .1.3.6.1.4.1..0还是要查找针对相应机器的cpu的oid。还有就是如果我想查看我的目标服务器的cpu和内存以及磁盘的oid的话,要怎么做,谢谢!
不同硬件设备的OID是不一样的,与设备的制造商有关,通过一些开源的监控设备可以检测到这些OID的值,或者咨询设备生产商。
如果开发通用的监控程序,必须知道你所检测设备中各项功能的OID值,如果只是自己公司内部用的,可以自己实现这些功能,比如监测CPU等信息。
浏览: 332916 次
来自: 北京
我在Keystore.getInstance(&JK ...
好吧,还是支持下
您好,看了您的这篇文章学到很多东西。希望您提供一个QQ号(发到 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'mysqladmin&-uroot&-p123456&status注:Threads即为连接个数。没有更多推荐了,
不良信息举报
举报内容:
利用snmp4j获取CPU,内存状态
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!当前位置: >>
>> 那位做过snmp4j的开发的,如何获取cpu,内存
&&详细解决方案
那位做过snmp4j的开发的,如何获取cpu,内存
热度:213&&&发布时间: 02:29:55.0
那位做过snmp4j的开发的,怎么获取cpu,内存啊oid,网上都查遍了,就是什么都拿不到,那位做过,说说是怎么回事啊。String[] returnValueString = // oid走访结果数组
SNMPv1CommunicationInterface comInterface =
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] = snmpS
returnValueString[i] = snmpValue.toString();
} catch ( ste) {
if (log.isErrorEnabled()) {
log.error(&走访IP为& + ipAddress + &, OID为& + oid + & 时超时!&);
returnValueString =
} catch (Exception e) {
throw new AppException(&SNMP走访节点时发生错误!&, e);
} finally {
if (comInterface != null) {
comInterface.closeConnection();
} catch ( e) {
comInterface =
return returnValueS------解决方案--------------------嵌入式设备上存在snmp服务器,并且存在一个定义的OID。SNMP4J只是调用OID,OID的定义取决于设备,是嵌入式开发的人员,如MIB库的开发。
&&相关解决方案
本站暂不开放注册!

我要回帖

更多关于 linux cpu 内存 查看 的文章

 

随机推荐