使用Java写一段算法,根据给定的数据,智能判断服务器运转是否正常 [ {"cpuUsage":"3.1","warnTime":"2023-01-18 09:22:27.726","memTotal":"7265","ip":"144.123.17.106","diskUsed":"16G","diskTotal":"18G","memUsed":"3402"}, {"cpuUsage":"10.8","warnTime":"2023-01-18 09:22:57.917","memTotal":"7265","ip":"144.123.17.106","diskUsed":"16G","diskTotal":"18G","memUsed":"3423"}, {"cpuUsage":"4.8","warnTime":"2023-01-18 09:23:28.104","memTotal":"7265","ip":"144.123.17.106","diskUsed":"16G","diskTotal":"18G","memUsed":"3420"} ]
时间: 2023-02-12 12:02:22 浏览: 131
下面是一段使用Java编写的算法,可以根据给定的数据判断服务器运转是否正常:
```java
import java.util.List;
public class ServerCheck {
public static void main(String[] args) {
List<ServerData> dataList = getServerData(); // 获取服务器数据
boolean isNormal = true; // 假设服务器运转正常
for (ServerData data : dataList) {
if (data.getCpuUsage() > 8 || data.getMemUsed() > data.getMemTotal() * 0.8) {
// 如果CPU使用率超过8%或内存使用率超过80%,则认为服务器运转不正常
isNormal = false;
break;
}
}
if (isNormal) {
System.out.println("服务器运转正常");
} else {
System.out.println("服务器运转不正常");
}
}
}
class ServerData {
private String cpuUsage;
private String warnTime;
private String memTotal;
private String ip;
private String diskUsed;
private String diskTotal;
private String memUsed;
public ServerData(String cpuUsage, String warnTime, String memTotal, String ip, String diskUsed, String diskTotal, String memUsed) {
this.cpuUsage = cpuUsage;
this.warnTime = warnTime;
this.memTotal = memTotal;
this.ip = ip;
this.diskUsed = diskUsed;
this.diskTotal = diskTotal;
this.memUsed = memUsed;
}
public double getCpuUsage() {
return Double.parseDouble(cpuUsage);
}
public String getWarnTime() {
return warnTime;
}
public int getMemTotal() {
return Integer.parseInt(memTotal);
}
public String getIp() {
return ip;
}
public String getDiskUsed() {
return diskUsed;
}
public String getDiskTotal() {
return diskTotal;
}
public int getMemUsed() {
return Integer.parseInt(memUsed);
}
}
```
上面的算法假定获取服务器数据的方法是getServerData()。该算法基于一个简单的判断逻辑,如果CPU使用率
阅读全文