|
|
@ -20,6 +20,8 @@ import java.util.stream.Collectors; |
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
|
|
|
|
@Component |
|
|
|
@EnableScheduling |
|
|
@ -34,28 +36,34 @@ public class DeviceStatus { |
|
|
|
private ExcelExportService excelExportService; |
|
|
|
|
|
|
|
|
|
|
|
@Scheduled(cron = "0 51 8 * * ?") |
|
|
|
@Scheduled(cron = "0 20 14 * * ?") |
|
|
|
// @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23")
|
|
|
|
public void generateDeviceStatus() { |
|
|
|
deviceStatus(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void deviceStatus() { |
|
|
|
Status status = new Status(); |
|
|
|
List<Device> deviceList = deviceService.SelectList(); |
|
|
|
for (Device device : deviceList) { |
|
|
|
try { |
|
|
|
InetAddress address = InetAddress.getByName(device.getDeviceIp()); |
|
|
|
String lostReat=getPingPacketLossRate(device.getDeviceIp()); |
|
|
|
boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
|
|
|
|
status.setDeviceNo(device.getDeviceNo()); |
|
|
|
status.setDeviceName(device.getDeviceName()); |
|
|
|
LocalDateTime localDateTime = LocalDateTime.now(); |
|
|
|
status.setTime(localDateTime); |
|
|
|
status.setLostRate(lostReat); |
|
|
|
//1-在线 0-离线
|
|
|
|
if (reachable) { |
|
|
|
status.setDeviceStatus(1); |
|
|
|
status.setSuccessRate("100.00%"); |
|
|
|
} else { |
|
|
|
status.setDeviceStatus(0); |
|
|
|
status.setSuccessRate("0.00%"); |
|
|
|
} |
|
|
|
statusService.Add(status); |
|
|
|
} catch (IOException e) { |
|
|
@ -66,7 +74,44 @@ public class DeviceStatus { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算成功率 |
|
|
|
* 计算丢包率 |
|
|
|
*/ |
|
|
|
public String getPingPacketLossRate(String ipAddress) { |
|
|
|
try { |
|
|
|
// Execute the ping command
|
|
|
|
Process process = Runtime.getRuntime().exec("ping -c 4 " + ipAddress); // Sending 4 ICMP Echo Request packets
|
|
|
|
|
|
|
|
// Read the output of the command
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
|
|
|
String line; |
|
|
|
int packetsSent = 0; |
|
|
|
int packetsLost = 0; |
|
|
|
|
|
|
|
while ((line = reader.readLine()) != null) { |
|
|
|
// Check for lines containing packet loss information
|
|
|
|
if (line.contains("packets transmitted")) { |
|
|
|
// Extract the number of packets sent and lost
|
|
|
|
String[] stats = line.split(", "); |
|
|
|
packetsSent = Integer.parseInt(stats[0].split(" ")[0]); |
|
|
|
packetsLost = Integer.parseInt(stats[0].split(" ")[0])-Integer.parseInt(stats[1].split(" ")[0]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Calculate and return the packet loss rate
|
|
|
|
if (packetsSent > 0) { |
|
|
|
return String.format("%.2f%%", (double) packetsLost / packetsSent * 100); |
|
|
|
} else { |
|
|
|
return "0.00%"; // No packets sent
|
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return "-1.00%"; // Error occurred
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 成功率和丢包率按小时分组 |
|
|
|
*/ |
|
|
|
public void calculateSuccessRate() { |
|
|
|
|
|
|
@ -79,19 +124,11 @@ public class DeviceStatus { |
|
|
|
status.setStartTime(todayStart); |
|
|
|
status.setTime(currentTime); |
|
|
|
List<Status> listStatus = statusService.list(status); |
|
|
|
Map<String, List<Status>> map = listStatus.stream().collect(Collectors.groupingBy(Status::getDeviceName)); |
|
|
|
for (String name : map.keySet()) { |
|
|
|
// 成功的次数
|
|
|
|
long successNumber = map.get(name).stream().filter(item -> item.getDeviceStatus() == 1).count(); |
|
|
|
// 此ip对应的成功率
|
|
|
|
String successRate = String.format("%.2f%%", (double) successNumber / map.get(name).size() * 100); |
|
|
|
ipMap.put(name, successRate); |
|
|
|
} |
|
|
|
Map<Integer, List<Status>> map = listStatus.stream() |
|
|
|
.collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); |
|
|
|
String filePath=CreateNamedExcel(); |
|
|
|
excelExportService.exportDataToExcel(ipMap,filePath); |
|
|
|
excelExportService.exportDataToExcel(map,filePath); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String CreateNamedExcel() { |
|
|
|
LocalDateTime dateTime = LocalDateTime.now(); |
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|
|