有时候需要监控不同操作系统的服务器性能指标,这时可以使用SNMP简单网络管理协议来获取某个网络设备上一些信息,达到数据监控的目的。snmp就不介绍了,网上有说明,现在就介绍下使用开源框架snmp4j,监控设备数据的方法。这里需要说明的是,被监控服务器上必须开启snmp服务。
/**
* SNMP远程会话
*
* @author lipeng
* @since 1.0
* @version 2012-11-25 lipeng
*/
public class SNMPSession extends Snmp4JSession implements IRemoteSession {
/** 节点信息 */
private NodeInfoVO nodeInfoVO;
/**
* @return 获取 nodeInfoVO属性值
*/
public NodeInfoVO getNodeInfoVO() {
return nodeInfoVO;
}
/**
* 构造函数
*
* @param target
* @throws IOException
* @throws IOException
*/
public SNMPSession(NodeInfoVO nodeInfoVO) throws IOException {
super(new Snmp4JTarget(nodeInfoVO.getServerIp(), 161));
this.nodeInfoVO = nodeInfoVO;
}
/**
* @see com.comtop.numen.monitor.collection.appservice.device.remote.IRemoteSession#execCommand(java.lang.String)
*/
@Override
public String execCommand(String cmd) {
return null;
}
/**
* @see com.comtop.numen.monitor.collection.appservice.device.remote.IRemoteSession#execCommand(java.lang.String)
*/
@Override
public void close() {
try {
super.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* FIXME 类注释信息(此标记自动生成,注释填写完成后请删除)
*
* @author lipeng
* @since 1.0
* @version 2012-11-25 lipeng
*/
public class SNMPDeviceAdapter extends DeviceAdapter {
private SNMPSession snmpSession = null;
/**
* 构造函数
*/
public SNMPDeviceAdapter(IRemoteSession remoteSession) {
this.snmpSession = (SNMPSession) remoteSession;
}
/**
* 获取CPU信息
*
* @see com.comtop.numen.monitor.collection.appservice.device.adapter.DeviceAdapter#getCpuInfo()
*/
@Override
public CPUInformationVO getCpuInfo() {
CPUInformationVO objCPUInfo = null;
try {
objCPUInfo = new CPUInformationVO();
objCPUInfo.setId(CommonUtils.getUUID());
objCPUInfo.setIpAddress(snmpSession.getNodeInfoVO().getServerIp());
objCPUInfo.setCollectTime(CommonUtils.getCurrentDate(new Date()));
objCPUInfo.setNodeId(snmpSession.getNodeInfoVO().getNodeInfoId());
List lstProcessorMIB = snmpSession.getTable(HrProcessorEntry.class);
double dCpuRate = 0;
for (HrProcessorEntry hrStorageEntry : lstProcessorMIB) {
dCpuRate += hrStorageEntry.getHrProcessorLoad();
}
objCPUInfo.setCpuUsedrate(String.valueOf(CommonUtils.getDoubleToPattern(dCpuRate / lstProcessorMIB.size(),
2)));
} catch (Exception e) {
e.printStackTrace();
}
return objCPUInfo;
}
/**
* 获取磁盘信息
*
* @see com.comtop.numen.monitor.collection.appservice.device.adapter.DeviceAdapter#getDiskInfo()
*/
@Override
public DiskInformationVO getDiskInfo() {
DiskInformationVO objDiskInfo = null;
try {
objDiskInfo = new DiskInformationVO();
objDiskInfo.setId(CommonUtils.getUUID());
objDiskInfo.setIpAddress(snmpSession.getNodeInfoVO().getServerIp());
objDiskInfo.setCollectTime(CommonUtils.getCurrentDate(new Date()));
objDiskInfo.setNodeId(snmpSession.getNodeInfoVO().getNodeInfoId());
List lstHrStorage = snmpSession.getTable(HrStorageEntry.class);
List lstDeskDetails = new ArrayList(lstHrStorage.size());
DiskDetailVO objDetail = null;
for (HrStorageEntry hrStorageEntry : lstHrStorage) {
if (hrStorageEntry.getHrStorageDescr().contains("/")
|| hrStorageEntry.getHrStorageDescr().contains(":")) {
objDetail = new DiskDetailVO();
objDetail.setId(CommonUtils.getUUID());
objDetail.setDiskLetter(hrStorageEntry.getHrStorageDescr());
int iStorSize = hrStorageEntry.getHrStorageSize() * hrStorageEntry.getHrStorageAllocationUnits();
int iStorUsedSize =
hrStorageEntry.getHrStorageUsed() * hrStorageEntry.getHrStorageAllocationUnits();
objDetail.setDiskLetterSize(String.valueOf(iStorSize));
objDetail.setDiskLetterUsedSize(String.valueOf(iStorUsedSize));
objDetail.setDiskLetterFreeSize(String.valueOf(iStorSize - iStorUsedSize));
lstDeskDetails.add(objDetail);
}
}
objDiskInfo.setDetail(lstDeskDetails);
} catch (Exception e) {
e.printStackTrace();
}
return objDiskInfo;
}
/**
* 获取内存信息
*
* @see com.comtop.numen.monitor.collection.appservice.device.adapter.DeviceAdapter#getMemoryInfo()
*/
@Override
public MemoryInformationVO getMemoryInfo() {
MemoryInformationVO objMemoryInfo = null;
try {
objMemoryInfo = new MemoryInformationVO();
objMemoryInfo.setId(CommonUtils.getUUID());
objMemoryInfo.setIpAddress(snmpSession.getNodeInfoVO().getServerIp());
objMemoryInfo.setCollectTime(CommonUtils.getCurrentDate(new Date()));
objMemoryInfo.setNodeId(snmpSession.getNodeInfoVO().getNodeInfoId());
List lstHrStorage = snmpSession.getTable(HrStorageEntry.class);
for (HrStorageEntry hrStorageEntry : lstHrStorage) {
if (hrStorageEntry.getHrStorageDescr().contains("System RAM")
|| hrStorageEntry.getHrStorageDescr().contains("Physical Memory")) {
int iStorSize = hrStorageEntry.getHrStorageSize() * hrStorageEntry.getHrStorageAllocationUnits();
int iStorUsedSize =
hrStorageEntry.getHrStorageUsed() * hrStorageEntry.getHrStorageAllocationUnits();
objMemoryInfo.setMemorySize(String.valueOf(iStorSize));
objMemoryInfo.setMemoryUsedSize(String.valueOf(iStorUsedSize));
objMemoryInfo.setMemoryFreeSize(String.valueOf(iStorSize - iStorUsedSize));
objMemoryInfo.setMemoryUsedRate(String.valueOf(CommonUtils.getDoubleToPattern(iStorUsedSize
/ iStorSize, 2)));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return objMemoryInfo;
}
/**
* @see com.comtop.numen.monitor.collection.appservice.device.adapter.DeviceAdapter#getNetworkInfo()
*/
@Override
public RealNetworkInformationVO getNetworkInfo() {
return null;
}
/**
* 获取操作系统信息
*
* @see com.comtop.numen.monitor.collection.appservice.device.adapter.DeviceAdapter#getOsInfo()
*/
@Override
public OsInformationVO getOsInfo() {
OsInformationVO objOsInfo = null;
System sysMIB = null;
try {
sysMIB = snmpSession.get(System.class);
objOsInfo = new OsInformationVO();
objOsInfo.setId(CommonUtils.getUUID());
objOsInfo.setIpAddress(snmpSession.getNodeInfoVO().getServerIp());
objOsInfo.setCollectTime(CommonUtils.getCurrentDate(new Date()));
objOsInfo.setClassPath(null);
objOsInfo.setNodeId(snmpSession.getNodeInfoVO().getNodeInfoId());
objOsInfo.setOsName(sysMIB.getSysName());
objOsInfo.setOsVersion(sysMIB.getSysDesc());
} catch (Exception e) {
e.printStackTrace();
}
return objOsInfo;
}
/**
* 获取进程信息
*
* @see com.comtop.numen.monitor.collection.appservice.device.adapter.DeviceAdapter#getProcessInfo()
*/
@Override
public ProcessInformationVO getProcessInfo() {
ProcessInformationVO objProcessInfo = null;
try {
objProcessInfo = new ProcessInformationVO();
String strProcessId = CommonUtils.getUUID();
objProcessInfo.setId(strProcessId);
objProcessInfo.setIpAddress(snmpSession.getNodeInfoVO().getServerIp());
objProcessInfo.setCollectTime(CommonUtils.getCurrentDate(new Date()));
objProcessInfo.setNodeId(snmpSession.getNodeInfoVO().getNodeInfoId());
List lstHrSWRun = snmpSession.getTable(HrSWRunEntry.class);
List lstProcessDetails = new ArrayList(lstHrSWRun.size());
ProcessDetailVO objDetail = null;
List lstHrSWRunPerf = snmpSession.getTable(HrSWRunPerfEntry.class);
Map objMap = new HashMap();
for (HrSWRunPerfEntry hrSWRunPerfEntry : lstHrSWRunPerf) {
objMap.put(hrSWRunPerfEntry.getHrSWRunIndex(), hrSWRunPerfEntry);
}
for (HrSWRunEntry objRun : lstHrSWRun) {
HrSWRunPerfEntry hrSWRunPerfEntry = objMap.get(objRun.getHrSWRunIndex());
objDetail = new ProcessDetailVO();
objDetail.setId(CommonUtils.getUUID());
objDetail.setCpuPercentage("");
objDetail.setMemPercentage(String.valueOf(hrSWRunPerfEntry.getHrSWRunPerfMem()));
objDetail.setProcessId(strProcessId);
objDetail.setProcessCommand(objRun.getHrSWRunName());
objDetail.setProcessCpuTime(String.valueOf(hrSWRunPerfEntry.getHrSWRunPerfCPU()));
objDetail.setProcessPID(String.valueOf(objRun.getHrSWRunIndex()));
objDetail.setProcessUserName("");
lstProcessDetails.add(objDetail);
}
objProcessInfo.setDetail(lstProcessDetails);
} catch (Exception e) {
e.printStackTrace();
}
return objProcessInfo;
}
}