package com.wgcloud.controller; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.comparator.PropertyComparator; import cn.hutool.json.JSONUtil; import com.github.pagehelper.PageInfo; import com.wgcloud.config.CommonConfig; import com.wgcloud.dto.ChartDapingNewInfo; import com.wgcloud.entity.DiskState; import com.wgcloud.entity.HostDiskPer; import com.wgcloud.entity.LogInfo; import com.wgcloud.entity.SystemInfo; import com.wgcloud.service.*; import com.wgcloud.util.DateUtil; import com.wgcloud.util.FormatUtil; import com.wgcloud.util.HostUtil; import com.wgcloud.util.ThreadPoolUtil; import com.wgcloud.util.staticvar.StaticKeys; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * @version v3.3 * @ClassName:DapingNewController.java * @author: http://www.wgstart.com * @date: 20212年6月9日 * @Description: 大屏新版controller * @Copyright: 2019-2022 wgcloud. All rights reserved. */ @Controller @RequestMapping(value = "/dapingNew") public class DapingNewController { private void testThread() { Runnable runnable = () -> { logger.info("DapingNewController----------testThread"); }; ThreadPoolUtil.executor.execute(runnable); } private static final Logger logger = LoggerFactory.getLogger(DapingNewController.class); @Resource private DbTableService dbTableService; @Resource private PortInfoService portInfoService; @Resource private FileSafeService fileSafeService; @Resource private DceInfoService dceInfoService; @Resource private SnmpInfoService snmpInfoService; @Resource private DbInfoService dbInfoService; @Resource private SystemInfoService systemInfoService; @Resource private AppInfoService appInfoService; @Resource private FileWarnInfoService fileWarnInfoService; @Resource private FileWarnStateService fileWarnStateService; @Resource private HostDiskPerService hostDiskPerService; @Resource private DockerInfoService dockerInfoService; @Resource private LogInfoService logInfoService; @Autowired private HeathMonitorService heathMonitorService; @Autowired private ShellInfoService shellInfoService; @Resource private AccountInfoService accountInfoService; @Resource private DiskStateService diskStateService; @Autowired private CommonConfig commonConfig; /** * 根据条件查询host列表 * * @param model * @param request * @return */ @RequestMapping(value = "index") public String index(Model model, HttpServletRequest request) { Map params = new HashMap(); try { int totalSystemInfoSize = systemInfoService.countByParams(params); //监控主机总量 model.addAttribute("totalSystemInfoSize", totalSystemInfoSize); params.put("state", StaticKeys.DOWN_STATE); int hostDownSize = systemInfoService.countByParams(params); //下线主机数量 model.addAttribute("hostDownSize", hostDownSize); params.clear(); int totalSizeApp = appInfoService.countByParams(params); //进程总数量 model.addAttribute("totalSizeApp", totalSizeApp); int dockerSize = dockerInfoService.countByParams(params); //docker总数量 model.addAttribute("dockerSize", dockerSize); params.put("state", StaticKeys.DOWN_STATE); int downAppSize = appInfoService.countByParams(params); //监控进程下线数量 model.addAttribute("downAppSize", downAppSize); int downDockerSize = dockerInfoService.countByParams(params); //监控docker下线数量 model.addAttribute("downDockerSize", downDockerSize); params.clear(); int portSize = portInfoService.countByParams(params); //端口总数量 model.addAttribute("portSize", portSize); params.put("state", StaticKeys.DOWN_STATE); int portDownSize = portInfoService.countByParams(params); //端口下线数量 model.addAttribute("portDownSize", portDownSize); params.clear(); int dceSize = dceInfoService.countByParams(params); //数通设备总数量 model.addAttribute("dceSize", dceSize); params.put("resTimes", -1); int dceDownSize = dceInfoService.countByParams(params); //数通设备下线数量 model.addAttribute("dceDownSize", dceDownSize); params.clear(); int snmpSize = snmpInfoService.countByParams(params); //snmp设备总数量 model.addAttribute("snmpSize", snmpSize); params.put("state", StaticKeys.DOWN_STATE); int snmpDownSize = snmpInfoService.countByParams(params); //snmp设备下线数量 model.addAttribute("snmpDownSize", snmpDownSize); params.clear(); Integer fileWarnSize = fileWarnInfoService.countByParams(params); //日志监控总量 model.addAttribute("fileWarnSize", fileWarnSize == null ? 0 : fileWarnSize); params.clear(); Integer fileSafeSize = fileSafeService.countByParams(params); //文件防篡改总量 model.addAttribute("fileSafeSize", fileSafeSize == null ? 0 : fileSafeSize); params.put("state", StaticKeys.DOWN_STATE); int fileSafeDownSize = fileSafeService.countByParams(params); //文件防篡改下线数量 model.addAttribute("fileSafeDownSize", fileSafeDownSize); params.clear(); Integer dbTableSize = dbTableService.countByParams(params); //数据表总量 model.addAttribute("dbTableSize", dbTableSize); params.clear(); int dbInfoSize = dbInfoService.countByParams(params); //数据源数量 model.addAttribute("dbInfoSize", dbInfoSize); params.clear(); params.put("dbState", StaticKeys.DOWN_STATE); int dbInfoDownSize = dbInfoService.countByParams(params); //失败数据源数量 model.addAttribute("dbInfoDownSize", dbInfoDownSize); params.clear(); int heathSize = heathMonitorService.countByParams(params); //服务接口数量 model.addAttribute("heathSize", heathSize); params.put("heathStatus", StaticKeys.HEATH_STATUS_200); int heath200Size = heathMonitorService.countByParams(params); model.addAttribute("heath200Size", heath200Size); //服务接口失败数量 model.addAttribute("heatherrSize", (heathSize - heath200Size)); params.clear(); int shellInfoSize = shellInfoService.countByParams(params); //失败数据源数量 model.addAttribute("shellInfoSize", shellInfoSize); //监控资源总数量 Integer resourceAllSize = totalSystemInfoSize + totalSizeApp + dockerSize + portSize + dceSize + snmpSize + fileWarnSize + dbInfoSize + heathSize + fileSafeSize + dbTableSize; model.addAttribute("resourceAllSize", resourceAllSize); //设置每个系统类型的数量 params.clear(); List systemInfoList = systemInfoService.selectAllByParams(params); setSystemType(systemInfoList, model); //累积告警(次数) params.clear(); params.put("hostname", "告警"); model.addAttribute("warnCount", logInfoService.countByParams(params)); // 主机及其资源告警次数最高,取前6个 setTopDownHostChart(model); //设置cpu总核数,内存总大小 setMiddleData(systemInfoList, model); //设置大屏显示的日志告警和恢复信息 setLogInfo(model); //设置服务接口状态类型饼图 setHeathMonitorType(model, heathSize); // 设置告警统计信息蜘蛛网图 warnInfoInit(model); //主机进程数量,连接数量,mem使用率,cpu使用率,磁盘使用率,最新上报前6 hostInfoTop6(model); return "daping/indexNew"; } catch (Exception e) { logger.error("大屏展示错误", e); } return ""; } /** * 设置服务接口状态类型饼图 * * @param model */ private void setHeathMonitorType(Model model, Integer heathSize) { Map params = new HashMap(); try { params.put("heathStatus", StaticKeys.HEATH_STATUS_200); int heath200Size = heathMonitorService.countByParams(params); model.addAttribute("heath200Size", heath200Size); params.put("heathStatus", "400"); int heath400Size = heathMonitorService.countByParams(params); model.addAttribute("heath400Size", heath400Size); params.put("heathStatus", "403"); int heath403Size = heathMonitorService.countByParams(params); model.addAttribute("heath403Size", heath403Size); params.put("heathStatus", "404"); int heath404Size = heathMonitorService.countByParams(params); model.addAttribute("heath404Size", heath404Size); params.put("heathStatus", "500"); int heath500Size = heathMonitorService.countByParams(params); model.addAttribute("heath500Size", heath500Size); int heathOtherSize = heathSize - heath200Size - heath400Size - heath403Size - heath404Size - heath500Size; model.addAttribute("heathOtherSize", heathOtherSize); } catch (Exception e) { logger.error("setHeathMonitorType错误", e); } } /** * 设置每个系统类型的数量 * * @param systemInfoList * @param model */ private void setSystemType(List systemInfoList, Model model) { Map map = HostUtil.getSystemTypeMap(systemInfoList); Integer windows = 0; Integer centos = 0; Integer redhat = 0; Integer ubuntu = 0; Integer debian = 0; Integer others = 0; if (null != map.get("windows")) { windows = map.get("windows"); } if (null != map.get("centos")) { centos = map.get("centos"); } if (null != map.get("redhat")) { redhat = map.get("redhat"); } if (null != map.get("ubuntu")) { ubuntu = map.get("ubuntu"); } if (null != map.get("debian")) { debian = map.get("debian"); } if (null != map.get("其他Linux")) { others = map.get("其他Linux"); } model.addAttribute("windowsSize", windows); model.addAttribute("centosSize", centos); model.addAttribute("redhatSize", redhat); model.addAttribute("ubuntuSize", ubuntu); model.addAttribute("debianSize", debian); model.addAttribute("othersSize", others); } /** * 主机及其资源告警次数最高,取前6个 * * @param model */ private void setTopDownHostChart(Model model) { Map params = new HashMap(); List chartInfoList = new ArrayList(); try { //取最近一周的数据比对 int days = 7; List dateList = getDateList(days); String startDay = dateList.get(0); String endDay = dateList.get(dateList.size() - 1); params.put(StaticKeys.SEARCH_START_TIME, startDay + " 00:00:00"); params.put(StaticKeys.SEARCH_END_TIME, endDay + " 23:59:59"); List systemInfoList = systemInfoService.selectAllByParams(params); for (SystemInfo systemInfo : systemInfoList) { params.put("hostname", systemInfo.getHostname()); params.put("hostnameNe", "已恢复"); int size = logInfoService.countByParams(params); ChartDapingNewInfo chartInfo = new ChartDapingNewInfo(); chartInfo.setName(systemInfo.getHostname()); chartInfo.setValue(size); chartInfoList.add(chartInfo); } Collections.sort(chartInfoList, new PropertyComparator<>("value")); //告警次数最高6个主机 begin CollectionUtil.reverse(chartInfoList); List chartInfoListTmp = new ArrayList(); if (chartInfoList.size() > 6) { chartInfoListTmp = chartInfoList.subList(0, 6); } else { chartInfoListTmp = chartInfoList; } //添加主机备注信息 for (ChartDapingNewInfo dto : chartInfoListTmp) { dto.setName(dto.getName() + HostUtil.addRemark(dto.getName())); } model.addAttribute("topDownHostChart", JSONUtil.parseArray(chartInfoListTmp)); //告警次数最高6个主机 end } catch (Exception e) { logger.error("统计分析主机及资源告警次数TOP10错误", e); } } /** * 设置大屏显示的系统日志前10条,两边各显示5条 * * @param model */ private void setLogInfo(Model model) throws Exception { List recordList = new ArrayList<>(); Map params = new HashMap(); PageInfo logListPage = logInfoService.selectByParams(params, 1, 10); if (logListPage.getList().size() <= 5) { model.addAttribute("logLeftList", logListPage.getList()); model.addAttribute("logRightList", recordList); } if (logListPage.getList().size() > 5) { model.addAttribute("logLeftList", logListPage.getList().subList(0, 5)); model.addAttribute("logRightList", logListPage.getList().subList(5, logListPage.getList().size())); } } /** * 设置告警统计信息蜘蛛网图 * * @param model */ private void warnInfoInit(Model model) { try { Map params = new HashMap(); params.put("hostname", "主机下线告警"); int hostDownWarnSize = logInfoService.countByParams(params); //主机下线次数 model.addAttribute("hostDownWarnSize", hostDownWarnSize); params.clear(); params.put("hostname", "内存告警"); int memWarnSize = logInfoService.countByParams(params); //内存告警次数 model.addAttribute("memWarnSize", memWarnSize); params.clear(); params.put("hostname", "CPU告警"); int cpuWarnSize = logInfoService.countByParams(params); //cpu告警次数 model.addAttribute("cpuWarnSize", cpuWarnSize); params.clear(); params.put("hostname", "传输速率告警"); int netWarnSize = logInfoService.countByParams(params); //上行下行速率告警次数 model.addAttribute("netWarnSize", netWarnSize); params.clear(); params.put("hostname", "磁盘告警"); int diskWarnSize = logInfoService.countByParams(params); //磁盘告警次数 model.addAttribute("diskWarnSize", diskWarnSize); params.clear(); params.put("hostname", "系统负载(5分钟)告警"); int load5WarnSize = logInfoService.countByParams(params); //系统负载告警次数 model.addAttribute("load5WarnSize", load5WarnSize); //设置告警次数最大值 begin int WarnCountMaxSize = 1; if (hostDownWarnSize > WarnCountMaxSize) { WarnCountMaxSize = hostDownWarnSize; } if (memWarnSize > WarnCountMaxSize) { WarnCountMaxSize = memWarnSize; } if (cpuWarnSize > WarnCountMaxSize) { WarnCountMaxSize = cpuWarnSize; } if (netWarnSize > WarnCountMaxSize) { WarnCountMaxSize = netWarnSize; } if (diskWarnSize > WarnCountMaxSize) { WarnCountMaxSize = diskWarnSize; } if (load5WarnSize > WarnCountMaxSize) { WarnCountMaxSize = load5WarnSize; } model.addAttribute("WarnCountMaxSize", WarnCountMaxSize); //设置告警次数最大值 begin } catch (Exception e) { logger.error("设置告警统计信息蜘蛛网图错误", e); } } /** * 设置所有主机最近10分钟状态,cpu总核数,总内存 * * @param model * @param list */ private void setMiddleData(List list, Model model) { //cpu使用率最高 Double maxCpu = 0d; //cpu使用率平均 Double avgCpu = 0d; //cpu使用率最低 Double minCpu = 1000d; Double sumCpu = 0d; //内存使用率最高 Double maxMem = 0d; //内存使用率最低 Double minMem = 1000d; //内存使用率平均 Double avgMem = 0d; Double sumMem = 0d; //5分钟负载最高 Double maxFiveLoad = 0d; //5分钟负载平均 Double avgFiveLoad = 0d; //5分钟负载最低 Double minFiveLoad = 1000d; Double sumFiveLoad = 0d; //15分钟负载最高 Double maxFifteenLoad = 0d; //15分钟负载最低 Double minFifteenLoad = 1000d; //15分钟负载平均 Double avgFifteenLoad = 0d; Double sumFifteenLoad = 0d; //主机数量 int systemSize = 0; //cpu总核数 int cpuCoresSum = 0; //内存总和 double memSum = 0; //所有主机运行总时间秒 long uptimeSum = 0; for (SystemInfo systemInfo : list) { systemSize += 1; //组装cpu使用率 begin if (null != systemInfo.getCpuPer()) { if (systemInfo.getCpuPer() > maxCpu) { maxCpu = systemInfo.getCpuPer(); } if (systemInfo.getCpuPer() < minCpu) { minCpu = systemInfo.getCpuPer(); } sumCpu += systemInfo.getCpuPer(); } //组装cpu使用率 end //组装内存使用率 begin if (null != systemInfo.getMemPer()) { if (systemInfo.getMemPer() > maxMem) { maxMem = systemInfo.getMemPer(); } if (systemInfo.getMemPer() < minMem) { minMem = systemInfo.getMemPer(); } sumMem += systemInfo.getMemPer(); } //组装内存使用率 end //组装5分钟负载 begin if (null != systemInfo.getFiveLoad()) { if (Double.valueOf(systemInfo.getFiveLoad()) > maxFiveLoad) { maxFiveLoad = Double.valueOf(systemInfo.getFiveLoad()); } if (Double.valueOf(systemInfo.getFiveLoad()) < minFiveLoad) { minFiveLoad = Double.valueOf(systemInfo.getFiveLoad()); } sumFiveLoad += Double.valueOf(systemInfo.getFiveLoad()); } //组装5分钟负载 end //组装15分钟负载 begin if (null != systemInfo.getFifteenLoad()) { if (Double.valueOf(systemInfo.getFifteenLoad()) > maxFifteenLoad) { maxFifteenLoad = Double.valueOf(systemInfo.getFifteenLoad()); } if (Double.valueOf(systemInfo.getFifteenLoad()) < minFifteenLoad) { minFifteenLoad = Double.valueOf(systemInfo.getFifteenLoad()); } sumFifteenLoad += Double.valueOf(systemInfo.getFifteenLoad()); } //组装15分钟负载 end //组装cpu总核数 begin try { cpuCoresSum += Integer.valueOf(systemInfo.getCpuCoreNum()); } catch (Exception e) { logger.error("统计所有主机总核数错误", e); } //组装cpu总核数 end //组装总内存 begin try { memSum += Double.valueOf(systemInfo.getTotalMem().replace("G", "")); } catch (Exception e) { logger.error("统计所有主机总内存错误", e); } //组装总内存 end //运行总时间秒 begin if (null != systemInfo.getUptime()) { uptimeSum += systemInfo.getUptime(); } //运行总时间秒 end } model.addAttribute("cpuCoresSum", cpuCoresSum); model.addAttribute("memSum", FormatUtil.gToT(memSum + "")); model.addAttribute("uptimeSum", FormatUtil.secondsToDays(uptimeSum)); if (systemSize > 0) { avgCpu = sumCpu / list.size(); avgMem = sumMem / list.size(); avgFifteenLoad = sumFifteenLoad / list.size(); avgFiveLoad = sumFiveLoad / list.size(); } else { minCpu = 0d; minMem = 0d; avgFifteenLoad = 0d; avgFiveLoad = 0d; } //最近5分钟所有主机cpu状态 begin model.addAttribute("maxCpuInfo", maxCpu); model.addAttribute("avgCpuInfo", FormatUtil.formatDouble(avgCpu, 2)); model.addAttribute("minCpuInfo", minCpu); //最近5分钟所有主机cpu状态 end //最近5分钟所有主机内存状态 begin model.addAttribute("maxMemInfo", maxMem); model.addAttribute("avgMemInfo", FormatUtil.formatDouble(avgMem, 2)); model.addAttribute("minMemInfo", minMem); //最近5分钟所有主机内存状态 end model.addAttribute("maxFiveLoad", maxFiveLoad); model.addAttribute("minFiveLoad", minFiveLoad); model.addAttribute("avgFiveLoad", FormatUtil.formatDouble(avgFiveLoad, 2)); model.addAttribute("maxFifteenLoad", maxFifteenLoad); model.addAttribute("minFifteenLoad", minFifteenLoad); model.addAttribute("avgFifteenLoad", FormatUtil.formatDouble(avgFifteenLoad, 2)); //磁盘总使用率最高 Double maxDiskPer = 0d; String maxDiskPerIp = ""; //磁盘总使用率最低 Double minDiskPer = 1000d; String minDiskPerIp = ""; //磁盘总使用率平均 Double avgDiskPer = 0d; //磁盘总使用率总和 Double sumDiskPer = 0d; try { //组织磁盘总使用率最大平均最低值 begin Map params = new HashMap(); String oneDayBefore = DateUtil.getDateBefore(1); params.put(StaticKeys.SEARCH_START_TIME, oneDayBefore.substring(0, 10) + " 00:00:00"); params.put(StaticKeys.SEARCH_END_TIME, oneDayBefore.substring(0, 10) + " 23:59:59"); List hostDiskPerList = hostDiskPerService.selectAllByParams(params); for (HostDiskPer hostDiskPer : hostDiskPerList) { if (null != hostDiskPer.getDiskSumPer() && hostDiskPer.getDiskSumPer() > maxDiskPer) { maxDiskPer = hostDiskPer.getDiskSumPer(); } if (null != hostDiskPer.getDiskSumPer() && hostDiskPer.getDiskSumPer() < minDiskPer) { minDiskPer = hostDiskPer.getDiskSumPer(); } if (null != hostDiskPer.getDiskSumPer()) { sumDiskPer += hostDiskPer.getDiskSumPer(); } } if (hostDiskPerList.size() > 0) { avgDiskPer = sumDiskPer / hostDiskPerList.size(); } else { minDiskPer = 0d; } model.addAttribute("maxDiskPer", maxDiskPer); model.addAttribute("avgDiskPer", avgDiskPer); model.addAttribute("minDiskPer", minDiskPer); //组织磁盘总使用率最大平均最低值 end } catch (Exception e) { logger.error("组织磁盘总使用率最大平均最低值错误", e); } } /** * mem使用率,cpu使用率,最新上报前6 * * @param model */ private void hostInfoTop6(Model model) { try { Double maxSystemValue = 1d; Map params = new HashMap(); params.put("orderBy", "CREATE_TIME"); params.put("orderType", "DESC"); PageInfo pageInfo = systemInfoService.selectByParams(params, 1, 6); List hostNameTop6List = new ArrayList<>(); List hostMemTop6List = new ArrayList<>(); List hostCpuTop6List = new ArrayList<>(); List hostProcsTop6List = new ArrayList<>(); List hostNetConnectionsTop6List = new ArrayList<>(); List hostDiskPerTop6List = new ArrayList<>(); Map paramsAppInfo = new HashMap(); for (SystemInfo systemInfo : pageInfo.getList()) { hostNameTop6List.add(systemInfo.getHostname()); hostMemTop6List.add(systemInfo.getMemPer()); hostCpuTop6List.add(systemInfo.getCpuPer()); if (!StringUtils.isEmpty(systemInfo.getProcs())) { hostProcsTop6List.add(Integer.valueOf(systemInfo.getProcs())); } else { hostProcsTop6List.add(0); } if (!StringUtils.isEmpty(systemInfo.getNetConnections())) { hostNetConnectionsTop6List.add(Integer.valueOf(systemInfo.getNetConnections())); } else { hostNetConnectionsTop6List.add(0); } //设置磁盘总使用率 begin paramsAppInfo.put("hostname", systemInfo.getHostname()); List deskStates = diskStateService.selectAllByParams(paramsAppInfo); HostUtil.setDiskSumPer(deskStates, systemInfo); hostDiskPerTop6List.add(systemInfo.getDiskPer()); //设置磁盘总使用率 end } //取最大值 begin for (Double memPer : hostMemTop6List) { if (memPer > maxSystemValue) { maxSystemValue = memPer; } } for (Double cpuPer : hostCpuTop6List) { if (cpuPer > maxSystemValue) { maxSystemValue = cpuPer; } } for (Double diskPer : hostDiskPerTop6List) { if (diskPer > maxSystemValue) { maxSystemValue = diskPer; } } maxSystemValue = FormatUtil.formatDouble(maxSystemValue * 1.2, 2); //取最大值 end model.addAttribute("hostNameTop6List", JSONUtil.parseArray(hostNameTop6List)); model.addAttribute("hostMemTop6List", JSONUtil.parseArray(hostMemTop6List)); model.addAttribute("hostCpuTop6List", JSONUtil.parseArray(hostCpuTop6List)); model.addAttribute("hostProcsTop6List", JSONUtil.parseArray(hostProcsTop6List)); model.addAttribute("hostNetConnectionsTop6List", JSONUtil.parseArray(hostNetConnectionsTop6List)); model.addAttribute("hostDiskPerTop6List", JSONUtil.parseArray(hostDiskPerTop6List)); model.addAttribute("maxSystemValue", maxSystemValue); } catch (Exception e) { logger.error("主机最新上报前10错误", e); } } /** * 获取时间列表 * * @param days 几天 * @return */ public List getDateList(int days) { List dateList = new ArrayList(); String sevenDayBefore = DateUtil.getDateBefore(days); for (int i = 1; i < days + 1; i++) { sevenDayBefore = DateUtil.getDateBefore(i); dateList.add(sevenDayBefore.substring(0, 10)); } CollectionUtil.reverse(dateList); return dateList; } }