| | |
| | | package com.wgcloud.service; |
| | | |
| | | import cn.hutool.http.HttpRequest; |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.wgcloud.entity.OspfErrorLog; |
| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import com.alibaba.fastjson.JSON; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | private OspfNeighborUtil ospfNeighborUtil; |
| | | @Autowired |
| | | private OspfErrorLogMapper errorLogMapper; |
| | | |
| | | @Value("${osfService.host}") |
| | | private String remoteHost; |
| | | @Value("${osfService.port}") |
| | | private String remotePort; |
| | | |
| | | private static final Logger logger = LoggerFactory.getLogger(OspfInfoService.class); |
| | | |
| | |
| | | OspfInfoMapper.updateById(ospfInfo); |
| | | } |
| | | |
| | | // public boolean checkOspfStatus() { |
| | | // String command = "display ospf brief"; |
| | | // String output = routerConnectionService.executeCommand(command); |
| | | // |
| | | // if (output == null) { |
| | | // return false; |
| | | // } |
| | | // |
| | | // // 检查输出中是否包含OSPF相关信息 |
| | | // boolean containsRouterId = StringUtils.containsIgnoreCase(output, "Router ID"); |
| | | // boolean containsArea = StringUtils.containsIgnoreCase(output, "Area"); |
| | | // boolean containsProcessId = StringUtils.containsIgnoreCase(output, "Process ID"); |
| | | // |
| | | // // 检查是否有错误信息 |
| | | // boolean containsError = StringUtils.containsIgnoreCase(output, "Error") || |
| | | // StringUtils.containsIgnoreCase(output, "Fail") || |
| | | // StringUtils.containsIgnoreCase(output, "Invalid"); |
| | | // |
| | | // return (containsRouterId && containsArea && containsProcessId) && !containsError; |
| | | // } |
| | | |
| | | /** |
| | | * 获取详细的OSPF邻居信息 |
| | | */ |
| | |
| | | for (OspfInfo ospfInfo : ospfInfos) { |
| | | String command = "display ospf peer "+ospfInfo.getHostname(); |
| | | String result = routerConnectionService.executeCommand(command,ospfInfo.getMonitorUsername(),ospfInfo.getMonitorPassword(),ospfInfo.getMonitorIp()); |
| | | System.out.println("获取到的邻居信息为:"+result); |
| | | if(StringUtils.isNotBlank(result)){ |
| | | List<OspfNeighbor> neighbors = ospfNeighborUtil.analysisStr(result); |
| | | if(neighbors != null || neighbors.size()>0){ |
| | |
| | | } |
| | | } |
| | | |
| | | public void getRouterStatus() { |
| | | List<OspfInfo> ospfInfos = OspfInfoMapper.selectAllByParams(null); |
| | | for (OspfInfo ospfInfo : ospfInfos) { |
| | | String command = "display ospf peer "+ospfInfo.getHostname(); |
| | | Map<String,String> map = new HashMap<>(); |
| | | map.put("command",command); |
| | | map.put("username",ospfInfo.getMonitorUsername()); |
| | | map.put("password",ospfInfo.getMonitorPassword()); |
| | | map.put("monitorIp",ospfInfo.getMonitorIp()); |
| | | String str = JSON.toJSONString(map); |
| | | System.out.println(str); |
| | | try { |
| | | String url = "http://"+remoteHost+":"+remotePort+"/api/connect/getResult"; |
| | | String responseBody = HttpRequest.post(url) |
| | | .timeout(5000) |
| | | .body(str) |
| | | .execute() |
| | | .body(); |
| | | System.out.println(responseBody); |
| | | |
| | | if(responseBody.equals("1")){ |
| | | HashMap<String, Object> hashMap = new HashMap<>(); |
| | | hashMap.put("state","1"); |
| | | hashMap.put("hostname",ospfInfo.getHostname()); |
| | | List<OspfErrorLog> ospfErrorLogs = errorLogMapper.selectAllByParams(hashMap); |
| | | if(ospfErrorLogs==null||ospfErrorLogs.size()==0){ |
| | | OspfErrorLog ospfErrorLog = new OspfErrorLog(); |
| | | ospfErrorLog.setInfoContent(ospfInfo.getInfoContent()); |
| | | ospfErrorLog.setHostname(ospfInfo.getHostname()); |
| | | ospfErrorLog.setState("1"); |
| | | try { |
| | | errorLogMapper.save(ospfErrorLog); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | ospfInfo.setState(responseBody); |
| | | OspfInfoMapper.updateById(ospfInfo); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | ospfInfo.setState("1"); |
| | | OspfInfoMapper.updateById(ospfInfo); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取OSPF接口状态 |
| | | */ |