fix
This commit is contained in:
@@ -70,7 +70,11 @@
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.snmp4j</groupId>
|
||||
<artifactId>snmp4j</artifactId>
|
||||
<version>2.8.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
|
||||
@@ -141,9 +141,9 @@ public abstract class DeviceDTO {
|
||||
@Data
|
||||
public static class Detail extends DeviceView {
|
||||
|
||||
private Double cpuRate;
|
||||
private Double memoryRate;
|
||||
private Double diskRate;
|
||||
private String cpuRate;
|
||||
private String memoryRate;
|
||||
private String diskRate;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,14 @@ import com.sunyard.chsm.model.entity.Device;
|
||||
import com.sunyard.chsm.service.DeviceService;
|
||||
import com.sunyard.chsm.service.TmkService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.*;
|
||||
import org.snmp4j.transport.DefaultUdpTransportMapping;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -22,6 +30,7 @@ import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.NumberFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@@ -110,14 +119,80 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
.map(ManufacturerModelEnum::getName)
|
||||
.ifPresent(detail::setManufacturerModelText);
|
||||
|
||||
// todo
|
||||
detail.setCpuRate(0.5D);
|
||||
detail.setMemoryRate(0.5D);
|
||||
detail.setDiskRate(0.5D);
|
||||
try {
|
||||
int port = 161;
|
||||
String community = "public";
|
||||
// 获取 CPU 使用率
|
||||
String cpuOid = ".1.3.6.1.4.1.2021.11.10.0";
|
||||
String cpuUsage = getSnmpData(exist.getServiceIp(), port, community, cpuOid);
|
||||
detail.setCpuRate(cpuUsage + "%");
|
||||
|
||||
// 获取磁盘使用率
|
||||
String diskUsageOid = ".1.3.6.1.4.1.2021.9.1.9.1";
|
||||
String diskUsage = getSnmpData(exist.getServiceIp(), port, community, diskUsageOid);
|
||||
detail.setDiskRate(diskUsage + "%");
|
||||
|
||||
// 获取内存使用率
|
||||
double memoryUsage = getMemoryUsage(exist.getServiceIp(), port, community);
|
||||
detail.setMemoryRate(NumberFormat.getIntegerInstance().format(memoryUsage) + "%");
|
||||
} catch (Exception ex) {
|
||||
log.warn("设备snmp协议连接异常:{}", exist.getServiceIp(), ex);
|
||||
}
|
||||
|
||||
return detail;
|
||||
}
|
||||
|
||||
public static double getMemoryUsage(String ip, int port, String community) throws Exception {
|
||||
// OIDs for total and available memory
|
||||
String totalMemoryOid = ".1.3.6.1.4.1.2021.4.5.0";
|
||||
String freeMemoryOid = ".1.3.6.1.4.1.2021.4.6.0";
|
||||
|
||||
// 获取总内存和可用内存
|
||||
String totalMemoryStr = getSnmpData(ip, port, community, totalMemoryOid);
|
||||
String freeMemoryStr = getSnmpData(ip, port, community, freeMemoryOid);
|
||||
|
||||
// 转换为数值
|
||||
double totalMemory = Double.parseDouble(totalMemoryStr);
|
||||
double freeMemory = Double.parseDouble(freeMemoryStr);
|
||||
|
||||
// 计算使用率
|
||||
return ((totalMemory - freeMemory) / totalMemory) * 100;
|
||||
}
|
||||
|
||||
public static String getSnmpData(String ip, int port, String community, String oid) throws Exception {
|
||||
String address = "udp:" + ip + "/" + port;
|
||||
|
||||
// 创建 SNMP 传输和会话
|
||||
TransportMapping<UdpAddress> transport = new DefaultUdpTransportMapping();
|
||||
Snmp snmp = new Snmp(transport);
|
||||
transport.listen();
|
||||
|
||||
// 构造目标
|
||||
Address targetAddress = GenericAddress.parse(address);
|
||||
CommunityTarget target = new CommunityTarget();
|
||||
target.setCommunity(new OctetString(community));
|
||||
target.setAddress(targetAddress);
|
||||
target.setRetries(2);
|
||||
target.setTimeout(1500);
|
||||
target.setVersion(SnmpConstants.version2c);
|
||||
|
||||
// 创建 PDU
|
||||
PDU pdu = new PDU();
|
||||
pdu.add(new VariableBinding(new OID(oid)));
|
||||
pdu.setType(PDU.GET);
|
||||
|
||||
// 发送请求并接收响应
|
||||
ResponseEvent event = snmp.send(pdu, target, null);
|
||||
snmp.close();
|
||||
|
||||
// 解析响应
|
||||
if (event != null && event.getResponse() != null) {
|
||||
return event.getResponse().get(0).getVariable().toString();
|
||||
} else {
|
||||
throw new RuntimeException("SNMP Request Timeout or No Response");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Long save(DeviceDTO.DeviceSave save) {
|
||||
|
||||
Reference in New Issue
Block a user