kongdeqiang
2025-12-02 55f4c8cda0f426e3a8d31908018a6b9c890bc006
src/main/java/com/wgcloud/service/OspfInfoService.java
@@ -1,17 +1,25 @@
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 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;
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;
@@ -30,6 +38,15 @@
    @Autowired
    private RouterConnectionService routerConnectionService;
    @Autowired
    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);
@@ -70,44 +87,111 @@
        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邻居信息
     */
    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());
            System.out.println("获取到的邻居信息为:"+result);
            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);
            }
        }
    }
    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");
                        ospfErrorLog.setId(UUIDUtil.getUUID());
                        ospfErrorLog.setCreateTime(DateUtil.getNowTime());
                        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接口状态
     */
    public String getOspfInterfaceStatus() {
        String command = "display ospf interface";
        return routerConnectionService.executeCommand(command);
    }
//    public String getOspfInterfaceStatus() {
//        String command = "display ospf interface";
//        return routerConnectionService.executeCommand(command);
//    }
    public int countByParams(Map<String, Object> params) throws Exception {