| | |
| | | |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.wgcloud.entity.OspfErrorLog; |
| | | import com.wgcloud.entity.OspfInfo; |
| | | import com.wgcloud.entity.SnmpInfo; |
| | | import com.wgcloud.entity.vo.OspfNeighbor; |
| | | import com.wgcloud.mapper.OspfErrorLogMapper; |
| | | import com.wgcloud.mapper.OspfInfoMapper; |
| | | import com.wgcloud.util.DateUtil; |
| | | import com.wgcloud.util.OspfNeighborUtil; |
| | | import com.wgcloud.util.UUIDUtil; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.slf4j.Logger; |
| | |
| | | |
| | | @Autowired |
| | | private RouterConnectionService routerConnectionService; |
| | | @Autowired |
| | | private OspfNeighborUtil ospfNeighborUtil; |
| | | @Autowired |
| | | private OspfErrorLogMapper errorLogMapper; |
| | | |
| | | 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; |
| | | } |
| | | // 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邻居信息 |
| | | */ |
| | | public String getDetailedOspfNeighborInfo() { |
| | | String command = "display ospf peer"; |
| | | return routerConnectionService.executeCommand(command); |
| | | public void getDetailedOspfNeighborInfo() { |
| | | List<OspfInfo> ospfInfos = OspfInfoMapper.selectAllByParams(null); |
| | | for (OspfInfo ospfInfo : ospfInfos) { |
| | | String command = "display ospf peer "+ospfInfo.getHostname(); |
| | | String result = routerConnectionService.executeCommand(command,ospfInfo.getMonitorUsername(),ospfInfo.getMonitorPassword(),ospfInfo.getMonitorIp()); |
| | | if(StringUtils.isNotBlank(result)){ |
| | | List<OspfNeighbor> neighbors = ospfNeighborUtil.analysisStr(result); |
| | | if(neighbors != null || neighbors.size()>0){ |
| | | OspfNeighbor ospfNeighbor = neighbors.get(0); |
| | | String state = ospfNeighbor.getState(); |
| | | if (state.contains("Down") || state.contains("Init")) { |
| | | ospfInfo.setState("1"); |
| | | OspfInfoMapper.updateById(ospfInfo); |
| | | |
| | | 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(); |
| | | } |
| | | } |
| | | }else { |
| | | ospfInfo.setState("0"); |
| | | OspfInfoMapper.updateById(ospfInfo); |
| | | } |
| | | } |
| | | }else { |
| | | ospfInfo.setState("1"); |
| | | OspfInfoMapper.updateById(ospfInfo); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取OSPF接口状态 |
| | | */ |
| | | public String getOspfInterfaceStatus() { |
| | | String command = "display ospf interface"; |
| | | return routerConnectionService.executeCommand(command); |
| | | } |
| | | // public String getOspfInterfaceStatus() { |
| | | // String command = "display ospf interface"; |
| | | // return routerConnectionService.executeCommand(command); |
| | | // } |
| | | |
| | | |
| | | |