#
Junjie
昨天 2e7dbd705fc82e8db74b073e55af938d67d8c19f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package com.zy.core.task;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.Cools;
import com.zy.asrs.domain.DevicePingSample;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.asrs.service.DevicePingFileStorageService;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
@Component
public class DevicePingScheduler {
 
    private static final Pattern LATENCY_PATTERN = Pattern.compile("(?:time|时间)\\s*[=<]?\\s*([0-9]+(?:[\\.,][0-9]+)?)\\s*(?:ms|毫秒)", Pattern.CASE_INSENSITIVE);
    private static final int OUTPUT_MESSAGE_LIMIT = 120;
 
    @Value("${devicePingStorage.enabled:true}")
    private boolean enabled;
 
    @Value("${devicePingStorage.timeoutMs:800}")
    private int timeoutMs;
 
    @Value("${devicePingStorage.probeCount:3}")
    private int probeCount;
 
    @Value("${devicePingStorage.maxParallel:8}")
    private int maxParallel;
 
    @Value("${devicePingStorage.packetSize:-1}")
    private int packetSize;
 
    @Autowired
    private DeviceConfigService deviceConfigService;
 
    @Autowired
    private DevicePingFileStorageService devicePingFileStorageService;
 
    private final AtomicBoolean running = new AtomicBoolean(false);
    private volatile ExecutorService executorService;
    private volatile ExecutorService probeExecutorService;
    private volatile long lastCleanupAt = 0L;
 
    @Scheduled(fixedDelayString = "${devicePingStorage.intervalMs:1000}")
    public void schedule() {
        if (!enabled) {
            return;
        }
        if (!running.compareAndSet(false, true)) {
            return;
        }
        ensureExecutor().submit(() -> {
            try {
                collectOnce();
            } finally {
                running.set(false);
            }
        });
    }
 
    private void collectOnce() {
        List<DeviceConfig> configs = loadConfigs();
        if (configs.isEmpty()) {
            maybeCleanup();
            return;
        }
 
        List<Future<DevicePingSample>> futures = new ArrayList<>();
        for (DeviceConfig config : configs) {
            futures.add(ensureProbeExecutor().submit(() -> probe(config)));
        }
 
        List<DevicePingSample> samples = new ArrayList<>();
        for (Future<DevicePingSample> future : futures) {
            try {
                DevicePingSample sample = future.get();
                if (sample != null) {
                    samples.add(sample);
                }
            } catch (Exception ignored) {
            }
        }
        devicePingFileStorageService.appendSamples(samples);
        maybeCleanup();
    }
 
    private List<DeviceConfig> loadConfigs() {
        try {
            QueryWrapper<DeviceConfig> wrapper = new QueryWrapper<DeviceConfig>()
                    .isNotNull("ip")
                    .ne("ip", "")
                    .orderBy(true, true, "device_type", "device_no");
            return deviceConfigService.list(wrapper);
        } catch (Exception ignored) {
            return new ArrayList<>();
        }
    }
 
    private DevicePingSample probe(DeviceConfig config) {
        DevicePingSample sample = new DevicePingSample();
        sample.setCreateTime(new Date());
        sample.setDeviceType(config.getDeviceType());
        sample.setDeviceNo(config.getDeviceNo());
        sample.setIp(config.getIp());
        sample.setPort(config.getPort());
        sample.setPacketSize(packetSize);
        int actualProbeCount = Math.max(1, probeCount);
        sample.setProbeCount(actualProbeCount);
 
        List<Long> successLatencies = new ArrayList<>();
        String lastError = "";
        int successCount = 0;
        int perProbeTimeoutMs = Math.max(100, timeoutMs / actualProbeCount);
 
        for (int i = 0; i < actualProbeCount; i++) {
            try {
                PingResult pingResult = systemPing(config.getIp(), perProbeTimeoutMs);
                if (pingResult.success) {
                    successLatencies.add(pingResult.latencyMs);
                    successCount++;
                } else {
                    lastError = "第" + (i + 1) + "次探测失败";
                    if (!Cools.isEmpty(pingResult.message)) {
                        lastError = lastError + "," + pingResult.message;
                    }
                }
            } catch (Exception ex) {
                lastError = Cools.isEmpty(ex.getMessage()) ? ex.getClass().getSimpleName() : ex.getMessage();
            }
        }
 
        sample.setSuccessProbeCount(successCount);
        sample.setReachable(successCount > 0);
        if (!successLatencies.isEmpty()) {
            long minLatency = Long.MAX_VALUE;
            long maxLatency = Long.MIN_VALUE;
            long latencySum = 0L;
            for (Long latency : successLatencies) {
                if (latency == null) {
                    continue;
                }
                latencySum += latency;
                if (latency < minLatency) {
                    minLatency = latency;
                }
                if (latency > maxLatency) {
                    maxLatency = latency;
                }
            }
            long avgLatency = Math.round(latencySum * 1.0D / successLatencies.size());
            sample.setLatencyMs(avgLatency);
            sample.setAvgLatencyMs(avgLatency);
            sample.setMinLatencyMs(minLatency);
            sample.setMaxLatencyMs(maxLatency);
        } else {
            sample.setLatencyMs(null);
            sample.setAvgLatencyMs(null);
            sample.setMinLatencyMs(null);
            sample.setMaxLatencyMs(null);
        }
 
        if (successCount == actualProbeCount) {
            sample.setStatus("OK");
            sample.setMessage(actualProbeCount + "/" + actualProbeCount + " 次探测成功");
        } else if (successCount > 0) {
            sample.setStatus("UNSTABLE");
            sample.setMessage(successCount + "/" + actualProbeCount + " 次探测成功" + (Cools.isEmpty(lastError) ? "" : "," + lastError));
        } else {
            sample.setStatus("TIMEOUT");
            sample.setMessage(Cools.isEmpty(lastError) ? "全部探测均超时" : lastError);
        }
        return sample;
    }
 
    private PingResult systemPing(String ip, int timeoutMs) throws Exception {
        List<String> command = buildPingCommand(ip, timeoutMs);
        ProcessBuilder processBuilder = new ProcessBuilder(command);
        processBuilder.redirectErrorStream(true);
        if (!isWindows()) {
            processBuilder.environment().put("LC_ALL", "C");
            processBuilder.environment().put("LANG", "C");
        }
 
        long start = System.nanoTime();
        Process process = processBuilder.start();
        String output;
        try (InputStream inputStream = process.getInputStream()) {
            boolean finished = process.waitFor(Math.max(1L, timeoutMs + 500L), TimeUnit.MILLISECONDS);
            if (!finished) {
                process.destroyForcibly();
                return new PingResult(false, null, "超时");
            }
            output = readFully(inputStream);
        }
 
        int exitCode = process.exitValue();
        Long latencyMs = parseLatencyMs(output);
        if (exitCode == 0) {
            if (latencyMs == null) {
                latencyMs = Math.max(0L, (System.nanoTime() - start) / 1_000_000L);
            }
            return new PingResult(true, latencyMs, "成功");
        }
        return new PingResult(false, null, extractFailureMessage(output, exitCode));
    }
 
    private List<String> buildPingCommand(String ip, int timeoutMs) {
        int actualPacketSize = Math.max(-1, packetSize);
        if (isWindows()) {
            List<String> command = new ArrayList<>(Arrays.asList("ping", "-n", "1", "-w", String.valueOf(Math.max(1, timeoutMs))));
            if (actualPacketSize >= 0) {
                command.add("-l");
                command.add(String.valueOf(actualPacketSize));
            }
            command.add(ip);
            return command;
        }
        if (isMac()) {
            List<String> command = new ArrayList<>(Arrays.asList("ping", "-n", "-c", "1", "-W", String.valueOf(Math.max(1, timeoutMs))));
            if (actualPacketSize >= 0) {
                command.add("-s");
                command.add(String.valueOf(actualPacketSize));
            }
            command.add(ip);
            return command;
        }
        int timeoutSec = Math.max(1, (int) Math.ceil(timeoutMs / 1000.0D));
        List<String> command = new ArrayList<>(Arrays.asList("ping", "-n", "-c", "1", "-W", String.valueOf(timeoutSec)));
        if (actualPacketSize >= 0) {
            command.add("-s");
            command.add(String.valueOf(actualPacketSize));
        }
        command.add(ip);
        return command;
    }
 
    private boolean isWindows() {
        return System.getProperty("os.name", "").toLowerCase().startsWith("windows");
    }
 
    private boolean isMac() {
        String osName = System.getProperty("os.name", "").toLowerCase();
        return osName.startsWith("mac") || osName.startsWith("darwin");
    }
 
    private Long parseLatencyMs(String output) {
        if (Cools.isEmpty(output)) {
            return null;
        }
        Matcher matcher = LATENCY_PATTERN.matcher(output.replace(',', '.'));
        if (matcher.find()) {
            try {
                return Math.round(Double.parseDouble(matcher.group(1)));
            } catch (Exception ignored) {
                return null;
            }
        }
        return null;
    }
 
    private String extractFailureMessage(String output, int exitCode) {
        if (Cools.isEmpty(output)) {
            return "exit=" + exitCode;
        }
        String[] lines = output.split("\\R");
        for (String line : lines) {
            String value = line == null ? "" : line.trim();
            if (value.isEmpty()) {
                continue;
            }
            String lower = value.toLowerCase();
            if (lower.contains("request timeout")
                    || lower.contains("100.0% packet loss")
                    || lower.contains("100% packet loss")
                    || lower.contains("destination host unreachable")
                    || lower.contains("could not find host")
                    || lower.contains("name or service not known")
                    || lower.contains("temporary failure in name resolution")
                    || value.contains("请求超时")
                    || value.contains("找不到主机")
                    || value.contains("无法访问目标主机")
                    || value.contains("100.0% 丢失")) {
                return trimMessage(value);
            }
        }
        for (String line : lines) {
            String value = line == null ? "" : line.trim();
            if (!value.isEmpty()) {
                return trimMessage(value);
            }
        }
        return "exit=" + exitCode;
    }
 
    private String trimMessage(String message) {
        if (Cools.isEmpty(message)) {
            return "";
        }
        String value = message.trim();
        if (value.length() > OUTPUT_MESSAGE_LIMIT) {
            return value.substring(0, OUTPUT_MESSAGE_LIMIT);
        }
        return value;
    }
 
    private String readFully(InputStream inputStream) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
        return outputStream.toString(Charset.defaultCharset());
    }
 
    private ExecutorService ensureExecutor() {
        ExecutorService current = executorService;
        if (current != null && !current.isShutdown()) {
            return current;
        }
        synchronized (this) {
            current = executorService;
            if (current == null || current.isShutdown()) {
                executorService = Executors.newSingleThreadExecutor(new NamedThreadFactory("device-ping-schedule-"));
            }
            return executorService;
        }
    }
 
    private ExecutorService ensureProbeExecutor() {
        ExecutorService current = probeExecutorService;
        if (current != null && !current.isShutdown()) {
            return current;
        }
        synchronized (this) {
            current = probeExecutorService;
            if (current == null || current.isShutdown()) {
                probeExecutorService = Executors.newFixedThreadPool(Math.max(1, maxParallel), new NamedThreadFactory("device-ping-probe-"));
            }
            return probeExecutorService;
        }
    }
 
    private void maybeCleanup() {
        long now = System.currentTimeMillis();
        if (now - lastCleanupAt < 60L * 60L * 1000L) {
            return;
        }
        lastCleanupAt = now;
        devicePingFileStorageService.cleanupExpired();
    }
 
    @PreDestroy
    public void shutdown() {
        ExecutorService current = executorService;
        if (current != null) {
            current.shutdownNow();
        }
        ExecutorService probeCurrent = probeExecutorService;
        if (probeCurrent != null) {
            probeCurrent.shutdownNow();
        }
    }
 
    private static class NamedThreadFactory implements ThreadFactory {
 
        private final String prefix;
        private final AtomicInteger index = new AtomicInteger(1);
 
        private NamedThreadFactory(String prefix) {
            this.prefix = prefix;
        }
 
        @Override
        public Thread newThread(Runnable runnable) {
            Thread thread = new Thread(runnable, prefix + index.getAndIncrement());
            thread.setDaemon(true);
            return thread;
        }
    }
 
    private static class PingResult {
 
        private final boolean success;
        private final Long latencyMs;
        private final String message;
 
        private PingResult(boolean success, Long latencyMs, String message) {
            this.success = success;
            this.latencyMs = latencyMs;
            this.message = message;
        }
    }
}