fuliqi
2024-09-30 13575e2a109e607f3daffcb65103ed0233fa7621
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
package com.ycl.utils;
 
import com.ycl.platform.domain.result.SYS.TMonitorResult;
import com.ycl.utils.http.SelfHttpUtil;
import constant.RedisConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
 
/**
 * @author xp
 * @date 2022/11/16
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class CheckPointUtil {
 
    private final RedisTemplate redisTemplate;
 
    private final SelfHttpUtil selfHttpUtil;
 
    /**
     * 监测点位在线工具类
     * @return
     */
    public TMonitorResult check(TMonitorResult monitor) {
        // 先检测能否访问该ip的网页
        ResponseEntity<Object> res = null;
        log.info("监测IP:" + monitor.getIp());
        String prefix = "http://";
        if ("127.0.0.1".equals(monitor.getIp())) {
            monitor.setOnline(Boolean.FALSE);
            return monitor;
        }
        try {
            res = selfHttpUtil.get(prefix + monitor.getIp(), null, null);
            monitor.setOnline(Objects.nonNull(res) && HttpStatus.OK == res.getStatusCode());
        } catch (Exception e) {
            monitor.setOnline(Boolean.FALSE);
        }
        // 如果http得到的不在线,那么再ping一下
        boolean reachable = false;
        Integer checkTimes = 1;
        Integer offLineTimes = 0;
        Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstant.ONLINE_KEY, monitor.getIp());
        if (!CollectionUtils.isEmpty(map)) {
            checkTimes = (Integer) map.get("checkTimes") + 1;
            offLineTimes = (Integer) map.get("offLineTimes");
        } else {
            map = new HashMap<>();
        }
        if (!monitor.getOnline()) {
            try {
                reachable = InetAddress.getByName(monitor.getIp()).isReachable(3000);
            } catch (IOException e) {
                e.printStackTrace();
            }
            monitor.setOnline(reachable);
        }
        if (!monitor.getOnline()) {
            offLineTimes++;
        }
        map.put("checkTimes", checkTimes);
        map.put("offLineTimes", offLineTimes);
        redisTemplate.opsForHash().put(RedisConstant.ONLINE_KEY, monitor.getIp(), map);
        monitor.setCheckCount(checkTimes);
        monitor.setOffLineCount(offLineTimes);
        return monitor;
    }
}