| | |
| | | import java.io.IOException; |
| | | import java.net.InetAddress; |
| | | import java.net.UnknownHostException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * 监测点位在线工具类 |
| | | * |
| | | * @return |
| | | */ |
| | | public TMonitorResult check(TMonitorResult monitor) { |
| | | public TMonitorResult check(TMonitorResult monitor,Integer times) { |
| | | // 先检测能否访问该ip的网页 |
| | | ResponseEntity<String> res = null; |
| | | String prefix = "http://"; |
| | |
| | | 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)) { |
| | | Integer continueOffTimes = 0; |
| | | List<String> offTimeList = new ArrayList<>(); |
| | | // 从Redis获取数据时进行类型安全的转换 |
| | | Map<String, Object> map = null; |
| | | Object mapObj = redisTemplate.opsForHash().get(RedisConstant.ONLINE_KEY, monitor.getNo()); |
| | | if (mapObj != null) { |
| | | map = (Map<String, Object>) mapObj; |
| | | checkTimes = (Integer) map.get("checkTimes") + 1; |
| | | offLineTimes = (Integer) map.get("offLineTimes"); |
| | | continueOffTimes = (Integer) map.get("continueOffTimes"); |
| | | Object offTimeListObj = map.get("offTimeList"); |
| | | if (offTimeListObj instanceof List) { |
| | | offTimeList = new ArrayList<>((List<String>) offTimeListObj); |
| | | } |
| | | } else { |
| | | map = new HashMap<>(); |
| | | } |
| | | |
| | | if (!monitor.getPingOnline()) { |
| | | try { |
| | | reachable = InetAddress.getByName(monitor.getIp()).isReachable(5000); |
| | |
| | | } |
| | | if (!monitor.getPingOnline()) { |
| | | offLineTimes++; |
| | | List<Date> offLineTime = monitor.getOffLineTime(); |
| | | if(CollectionUtils.isEmpty(offLineTime)) offLineTime = new ArrayList<>(); |
| | | offLineTime.add(new Date()); |
| | | monitor.setOffLineTime(offLineTime); |
| | | continueOffTimes++; |
| | | //记录离线时间 |
| | | Date now = new Date(); |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | offTimeList.add(dateFormat.format(now)); |
| | | //到达产生工单的阈值次数 |
| | | if (continueOffTimes>=times) { |
| | | //产生了工单才会存储离线时间,存储最近一次产生工单的这几个离线时间点 |
| | | monitor.setOffLineTimeStr(offTimeList); |
| | | monitor.setCreateWorkOrder(Boolean.TRUE); |
| | | //产生了一次工单则清除 |
| | | continueOffTimes = 0; |
| | | offTimeList = new ArrayList<>(); |
| | | } |
| | | }else { |
| | | //如果在线了,清空连续离线次数,清空离线时间 |
| | | continueOffTimes = 0; |
| | | offTimeList = new ArrayList<>(); |
| | | } |
| | | map.put("checkTimes", checkTimes); |
| | | map.put("offLineTimes", offLineTimes); |
| | | redisTemplate.opsForHash().put(RedisConstant.ONLINE_KEY, monitor.getIp(), map); |
| | | map.put("continueOffTimes", continueOffTimes); |
| | | map.put("offTimeList", offTimeList); |
| | | redisTemplate.opsForHash().put(RedisConstant.ONLINE_KEY, monitor.getNo(), map); |
| | | monitor.setCheckCount(checkTimes); |
| | | monitor.setOffLineCount(offLineTimes); |
| | | return monitor; |
| | |
| | | |
| | | /** |
| | | * 监测点位在线 |
| | | * |
| | | * @return |
| | | */ |
| | | public CheckResult webCheck(CheckUtil checkUtil) { |