|
@ -20,6 +20,8 @@ import java.util.stream.Collectors; |
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
import java.io.FileOutputStream; |
|
|
import java.io.FileOutputStream; |
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
|
|
import java.io.InputStreamReader; |
|
|
|
|
|
|
|
|
@Component |
|
|
@Component |
|
|
@EnableScheduling |
|
|
@EnableScheduling |
|
@ -34,28 +36,34 @@ public class DeviceStatus { |
|
|
private ExcelExportService excelExportService; |
|
|
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")
|
|
|
// @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23")
|
|
|
public void generateDeviceStatus() { |
|
|
public void generateDeviceStatus() { |
|
|
deviceStatus(); |
|
|
deviceStatus(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void deviceStatus() { |
|
|
public void deviceStatus() { |
|
|
Status status = new Status(); |
|
|
Status status = new Status(); |
|
|
List<Device> deviceList = deviceService.SelectList(); |
|
|
List<Device> deviceList = deviceService.SelectList(); |
|
|
for (Device device : deviceList) { |
|
|
for (Device device : deviceList) { |
|
|
try { |
|
|
try { |
|
|
InetAddress address = InetAddress.getByName(device.getDeviceIp()); |
|
|
InetAddress address = InetAddress.getByName(device.getDeviceIp()); |
|
|
|
|
|
String lostReat=getPingPacketLossRate(device.getDeviceIp()); |
|
|
boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
|
|
|
boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
|
|
|
status.setDeviceNo(device.getDeviceNo()); |
|
|
status.setDeviceNo(device.getDeviceNo()); |
|
|
status.setDeviceName(device.getDeviceName()); |
|
|
status.setDeviceName(device.getDeviceName()); |
|
|
LocalDateTime localDateTime = LocalDateTime.now(); |
|
|
LocalDateTime localDateTime = LocalDateTime.now(); |
|
|
status.setTime(localDateTime); |
|
|
status.setTime(localDateTime); |
|
|
|
|
|
status.setLostRate(lostReat); |
|
|
//1-在线 0-离线
|
|
|
//1-在线 0-离线
|
|
|
if (reachable) { |
|
|
if (reachable) { |
|
|
status.setDeviceStatus(1); |
|
|
status.setDeviceStatus(1); |
|
|
|
|
|
status.setSuccessRate("100.00%"); |
|
|
} else { |
|
|
} else { |
|
|
status.setDeviceStatus(0); |
|
|
status.setDeviceStatus(0); |
|
|
|
|
|
status.setSuccessRate("0.00%"); |
|
|
} |
|
|
} |
|
|
statusService.Add(status); |
|
|
statusService.Add(status); |
|
|
} catch (IOException e) { |
|
|
} 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() { |
|
|
public void calculateSuccessRate() { |
|
|
|
|
|
|
|
@ -79,19 +124,11 @@ public class DeviceStatus { |
|
|
status.setStartTime(todayStart); |
|
|
status.setStartTime(todayStart); |
|
|
status.setTime(currentTime); |
|
|
status.setTime(currentTime); |
|
|
List<Status> listStatus = statusService.list(status); |
|
|
List<Status> listStatus = statusService.list(status); |
|
|
Map<String, List<Status>> map = listStatus.stream().collect(Collectors.groupingBy(Status::getDeviceName)); |
|
|
Map<Integer, List<Status>> map = listStatus.stream() |
|
|
for (String name : map.keySet()) { |
|
|
.collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); |
|
|
// 成功的次数
|
|
|
|
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
String filePath=CreateNamedExcel(); |
|
|
String filePath=CreateNamedExcel(); |
|
|
excelExportService.exportDataToExcel(ipMap,filePath); |
|
|
excelExportService.exportDataToExcel(map,filePath); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String CreateNamedExcel() { |
|
|
public String CreateNamedExcel() { |
|
|
LocalDateTime dateTime = LocalDateTime.now(); |
|
|
LocalDateTime dateTime = LocalDateTime.now(); |
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|