package com.wgcloud.service; import cn.hutool.core.collection.CollectionUtil; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.wgcloud.config.CommonConfig; import com.wgcloud.dto.NetIoStateDto; import com.wgcloud.dto.SubtitleDto; import com.wgcloud.entity.*; import com.wgcloud.mapper.SystemInfoMapper; import com.wgcloud.util.DateUtil; import com.wgcloud.util.FormatUtil; import com.wgcloud.util.HostUtil; import com.wgcloud.util.UUIDUtil; 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.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @version v3.3 * @ClassName:SystemInfoService.java * @author: http://www.wgstart.com * @date: 2021年1月16日 * @Description: SystemInfoService.java * @Copyright: 2019-2021 wgcloud. All rights reserved. */ @Service public class SystemInfoService { private static final Logger logger = LoggerFactory.getLogger(SystemInfoService.class); public PageInfo selectByParams(Map params, int currPage, int pageSize) throws Exception { PageHelper.startPage(currPage, pageSize); List list = systemInfoMapper.selectByParams(params); PageInfo pageInfo = new PageInfo(list); return pageInfo; } public void save(SystemInfo SystemInfo) throws Exception { SystemInfo.setId(UUIDUtil.getUUID()); SystemInfo.setCreateTime(DateUtil.getNowTime()); systemInfoMapper.save(SystemInfo); } public void downByHostName(List recordList) throws Exception { if (recordList.size() < 1) { return; } systemInfoMapper.downByHostName(recordList); } @Transactional public void saveRecord(List recordList) throws Exception { if (recordList.size() < 1) { return; } for (SystemInfo as : recordList) { as.setId(UUIDUtil.getUUID()); as.setCreateTime(DateUtil.getNowTime()); } systemInfoMapper.insertList(recordList); } @Transactional public void updateRecord(List recordList) throws Exception { if (recordList.size() < 1) { return; } systemInfoMapper.updateList(recordList); } @Transactional public void updateById(SystemInfo SystemInfo) throws Exception { systemInfoMapper.updateById(SystemInfo); } public int deleteById(String[] id) throws Exception { return systemInfoMapper.deleteById(id); } public SystemInfo selectById(String id) throws Exception { return systemInfoMapper.selectById(id); } public List selectAllByParams(Map params) throws Exception { return systemInfoMapper.selectAllByParams(params); } public int countByParams(Map params) throws Exception { return systemInfoMapper.countByParams(params); } public SystemInfo selectByHostname(String hostname) throws Exception { List list = systemInfoMapper.selectByHostname(hostname); if (list.size() > 0) { return list.get(0); } return null; } /** * 根据hostname删除数据 * * @param params * @return * @throws Exception */ public int deleteByAccHname(Map params) throws Exception { return systemInfoMapper.deleteByAccHname(params); } public PageInfo selectBytesTop3(Map params, int currPage, int pageSize) throws Exception { PageHelper.startPage(currPage, pageSize); List list = systemInfoMapper.selectBytesTop3(params); PageInfo pageInfo = new PageInfo(list); return pageInfo; } /** * 主机列表添加一些附加值参数,以及参数转换格式化 * * @param pageInfo */ public void hostAddVal(PageInfo pageInfo, List hostGroupList) throws Exception { //组装进程数量,docker数量,端口数量,监控日志,磁盘使用率 begin Map paramsAppInfo = new HashMap(); //查询所有主机分组列表,封装为map begin Map hostGroupMap = new HashMap<>(); for (HostGroup hostGroup : hostGroupList) { hostGroupMap.put(hostGroup.getId(), hostGroup.getGroupName()); } //查询所有主机分组列表,封装为map end for (SystemInfo systemInfo1 : pageInfo.getList()) { paramsAppInfo.put("hostname", systemInfo1.getHostname()); systemInfo1.setRxbyt(FormatUtil.kbToM(systemInfo1.getRxbyt()) + "/s"); systemInfo1.setTxbyt(FormatUtil.kbToM(systemInfo1.getTxbyt()) + "/s"); /*int totalAppSize = appInfoService.countByParams(paramsAppInfo); systemInfo1.setGroupName(totalAppSize + ""); int dockerSize = dockerInfoService.countByParams(paramsAppInfo); systemInfo1.setDockerName(dockerSize + ""); int portSize = portInfoService.countByParams(paramsAppInfo); systemInfo1.setPortName(portSize + ""); int fileWarnSize = fileWarnInfoService.countByParams(paramsAppInfo); systemInfo1.setFileWarnName(fileWarnSize + "");*/ //设置主机logo HostUtil.setSysImage(systemInfo1); //设置主机分组名称 String groupId = systemInfo1.getGroupId(); if (!StringUtils.isEmpty(groupId)) { systemInfo1.setGroupId(hostGroupMap.get(groupId)); } //设置磁盘总使用率 begin List deskStates = diskStateService.selectAllByParams(paramsAppInfo); HostUtil.setDiskSumPer(deskStates, systemInfo1); //设置磁盘总使用率 end //设置主机及监控资源累积告警次数 begin if (StaticKeys.TRUE_VAL.equals(commonConfig.getShowWarnCount())) { String warnQueryWd = systemInfo1.getHostname(); paramsAppInfo.clear(); paramsAppInfo.put("hostname", warnQueryWd); paramsAppInfo.put("hostnameNe", "已恢复"); systemInfo1.setWarnCount(logInfoService.countByParams(paramsAppInfo)); systemInfo1.setWarnQueryWd(warnQueryWd); } //设置主机及监控资源累积告警次数 end } //组装进程数量,docker数量,端口数量,监控日志,磁盘使用率 end } /** * 公众看板页面,未登录时候,IP脱敏显示,隐藏前5个字符 * * @param pageInfo */ public void hideLeftIp(PageInfo pageInfo, HttpServletRequest request) { if (!StaticKeys.TRUE_VAL.equals(commonConfig.getDashViewIpHide())) { return; } HttpSession session = request.getSession(); AccountInfo accountInfo = (AccountInfo) session.getAttribute(StaticKeys.LOGIN_KEY); if (null == accountInfo) { for (SystemInfo systemInfo : pageInfo.getList()) { if (StringUtils.isBlank(systemInfo.getHostname())) { continue; } int length = StringUtils.length(systemInfo.getHostname()); String resHostName = StringUtils.leftPad(StringUtils.right(systemInfo.getHostname(), length - 5), length, "*"); systemInfo.setHostname(resHostName); } } } /** * 设置图表的副标题,cpu和内存的最高、平均、最低值 * * @param model * @param cpuStateList cpu状态数据 * @param memStateList 内存状态数据 */ public void setSubtitle(Model model, List cpuStateList, List memStateList) { //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; //组装cpu使用率 begin for (CpuState cpuState : cpuStateList) { if (null != cpuState.getSys()) { if (cpuState.getSys() > maxCpu) { maxCpu = cpuState.getSys(); } if (cpuState.getSys() < minCpu) { minCpu = cpuState.getSys(); } sumCpu += cpuState.getSys(); } } if (cpuStateList.size() > 0) { avgCpu = sumCpu / cpuStateList.size(); } else { minCpu = 0d; } SubtitleDto cpuSubtitleDto = new SubtitleDto(); cpuSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgCpu, 2) + ""); cpuSubtitleDto.setMaxValue(maxCpu + ""); cpuSubtitleDto.setMinValue(minCpu + ""); model.addAttribute("cpuSubtitleDto", cpuSubtitleDto); //组装cpu使用率 end //组装内存使用率 begin for (MemState memState : memStateList) { if (null != memState.getUsePer()) { if (memState.getUsePer() > maxMem) { maxMem = memState.getUsePer(); } if (memState.getUsePer() < minMem) { minMem = memState.getUsePer(); } sumMem += memState.getUsePer(); } } if (memStateList.size() > 0) { avgMem = sumMem / memStateList.size(); } else { minMem = 0d; } SubtitleDto memSubtitleDto = new SubtitleDto(); memSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgMem, 2) + ""); memSubtitleDto.setMaxValue(maxMem + ""); memSubtitleDto.setMinValue(minMem + ""); model.addAttribute("memSubtitleDto", memSubtitleDto); //组装内存使用率 end } /** * 查找系统负载最大值 * * @param sysLoadStateList * @param model * @return */ public void findLoadMaxVal(List sysLoadStateList, Model model) { //图表属性max double maxVal = 0; //1分钟负载值最高 Double maxOneLoad = 0d; //1分钟负载值平均 Double avgOneLoad = 0d; //1分钟负载值最低 Double minOneLoad = 1000d; Double sumOneLoad = 0d; //5分钟负载值最高 Double maxFiveLoad = 0d; //5分钟负载值平均 Double avgFiveLoad = 0d; //5分钟负载值最低 Double minFiveLoad = 1000d; Double sumFiveLoad = 0d; //15分钟负载值最高 Double maxFifteenLoad = 0d; //15分钟负载值平均 Double avgFifteenLoad = 0d; //15分钟负载值最低 Double minFifteenLoad = 1000d; Double sumFifteenLoad = 0d; if (!CollectionUtil.isEmpty(sysLoadStateList)) { for (SysLoadState sysLoadState : sysLoadStateList) { if (null != sysLoadState.getOneLoad()) { if (sysLoadState.getOneLoad() > maxOneLoad) { maxOneLoad = sysLoadState.getOneLoad(); } if (sysLoadState.getOneLoad() < minOneLoad) { minOneLoad = sysLoadState.getOneLoad(); } sumOneLoad += sysLoadState.getOneLoad(); } if (null != sysLoadState.getFiveLoad()) { if (sysLoadState.getFiveLoad() > maxFiveLoad) { maxFiveLoad = sysLoadState.getFiveLoad(); } if (sysLoadState.getFiveLoad() < minFiveLoad) { minFiveLoad = sysLoadState.getFiveLoad(); } sumFiveLoad += sysLoadState.getFiveLoad(); } if (null != sysLoadState.getFifteenLoad()) { if (sysLoadState.getFifteenLoad() > maxFifteenLoad) { maxFifteenLoad = sysLoadState.getFifteenLoad(); } if (sysLoadState.getFifteenLoad() < minFifteenLoad) { minFifteenLoad = sysLoadState.getFifteenLoad(); } sumFifteenLoad += sysLoadState.getFifteenLoad(); } } } if (maxOneLoad > maxVal) { maxVal = maxOneLoad; } if (maxFiveLoad > maxVal) { maxVal = maxFiveLoad; } if (maxFifteenLoad > maxVal) { maxVal = maxFifteenLoad; } if (maxVal == 0) { maxVal = 1; } model.addAttribute("sysLoadStateMaxVal", Math.ceil(maxVal)); if (sysLoadStateList.size() > 0) { avgOneLoad = sumOneLoad / sysLoadStateList.size(); avgFiveLoad = sumFiveLoad / sysLoadStateList.size(); avgFifteenLoad = sumFifteenLoad / sysLoadStateList.size(); } else { minOneLoad = 0d; minFiveLoad = 0d; minFifteenLoad = 0d; } //设置系统负载图表副标题 begin SubtitleDto oneLoadSubtitleDto = new SubtitleDto(); oneLoadSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgOneLoad, 2) + ""); oneLoadSubtitleDto.setMaxValue(maxOneLoad + ""); oneLoadSubtitleDto.setMinValue(minOneLoad + ""); model.addAttribute("oneLoadSubtitleDto", oneLoadSubtitleDto); SubtitleDto fiveLoadSubtitleDto = new SubtitleDto(); fiveLoadSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgFiveLoad, 2) + ""); fiveLoadSubtitleDto.setMaxValue(maxFiveLoad + ""); fiveLoadSubtitleDto.setMinValue(minFiveLoad + ""); model.addAttribute("fiveLoadSubtitleDto", fiveLoadSubtitleDto); SubtitleDto fifteenLoadSubtitleDto = new SubtitleDto(); fifteenLoadSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgFifteenLoad, 2) + ""); fifteenLoadSubtitleDto.setMaxValue(maxFifteenLoad + ""); fifteenLoadSubtitleDto.setMinValue(minFifteenLoad + ""); model.addAttribute("fifteenLoadSubtitleDto", fifteenLoadSubtitleDto); //设置系统负载图表副标题 end } /** * 转为DTO,因为图表显示要用数字类型 * * @param netIoStateList * @return */ public List toNetIoStateDto(List netIoStateList) { List dtoList = new ArrayList<>(); for (NetIoState netIoState : netIoStateList) { NetIoStateDto dto = new NetIoStateDto(); dto.setCreateTime(netIoState.getCreateTime()); dto.setDateStr(netIoState.getDateStr()); dto.setHostname(netIoState.getHostname()); dto.setRxbyt(Double.valueOf(netIoState.getRxbyt())); dto.setRxpck(Double.valueOf(netIoState.getRxpck())); dto.setTxbyt(Double.valueOf(netIoState.getTxbyt())); dto.setTxpck(Double.valueOf(netIoState.getTxpck())); dto.setDropin(Double.valueOf(netIoState.getDropin())); dto.setDropout(Double.valueOf(netIoState.getDropout())); dto.setNetConnections(Integer.valueOf(netIoState.getNetConnections())); dtoList.add(dto); } return dtoList; } /** * 查找网络流量、包、丢弃包最大值,传递到页面 * * @param netIoStateList * @return */ public void findNetIoStateBytMaxVal(List netIoStateList, Model model) { //图表属性max用最大值,上下行传输速率 double maxBytesVal = 0; //图表属性max用最大值,每秒传输包数 double maxPckVal = 0; //图表属性max用最大值,每秒丢包数 double maxDropVal = 0; //下行传输速率最高 Double maxRxbyt = 0d; //下行传输速率平均 Double avgRxbyt = 0d; //下行传输速率最低 Double minRxbyt = 99999d; Double sumRxbyt = 0d; //上行传输速率最高 Double maxTxbyt = 0d; //上行传输速率最低 Double minTxbyt = 99999d; //上行传输速率平均 Double avgTxbyt = 0d; Double sumTxbyt = 0d; //每秒钟接收的数据包最高 Double maxRxpck = 0d; //每秒钟接收的数据包平均 Double avgRxpck = 0d; //每秒钟接收的数据包最低 Double minRxpck = 99999d; Double sumRxpck = 0d; //每秒钟发送的数据包最高 Double maxTxpck = 0d; //每秒钟发送的数据包最低 Double minTxpck = 99999d; //每秒钟发送的数据包平均 Double avgTxpck = 0d; Double sumTxpck = 0d; //连接数最高 Integer maxConns = 0; //连接数最低 Integer minConns = 99999; //连接数平均 Double avgConns = 0d; Integer sumConns = 0; if (!CollectionUtil.isEmpty(netIoStateList)) { for (NetIoStateDto netIoState : netIoStateList) { try { //组装图表属性用最大值max begin if (null != netIoState.getDropin() && netIoState.getDropin() > maxDropVal) { maxDropVal = netIoState.getDropin(); } if (null != netIoState.getDropout() && netIoState.getDropout() > maxDropVal) { maxDropVal = netIoState.getDropout(); } //组装图表属性用最大值max end //组装下行传输速率 begin if (null != netIoState.getRxbyt()) { if (netIoState.getRxbyt() > maxRxbyt) { maxRxbyt = netIoState.getRxbyt(); } if (netIoState.getRxbyt() < minRxbyt) { minRxbyt = netIoState.getRxbyt(); } sumRxbyt += netIoState.getRxbyt(); } if (null != netIoState.getTxbyt()) { if (netIoState.getTxbyt() > maxTxbyt) { maxTxbyt = netIoState.getTxbyt(); } if (netIoState.getTxbyt() < minTxbyt) { minTxbyt = netIoState.getTxbyt(); } sumTxbyt += netIoState.getTxbyt(); } //组装上行传输速率 end //组装每秒传输数据包 begin if (null != netIoState.getRxpck()) { if (netIoState.getRxpck() > maxRxpck) { maxRxpck = netIoState.getRxpck(); } if (netIoState.getRxpck() < minRxpck) { minRxpck = netIoState.getRxpck(); } sumRxpck += netIoState.getRxpck(); } if (null != netIoState.getTxpck()) { if (netIoState.getTxpck() > maxTxpck) { maxTxpck = netIoState.getTxpck(); } if (Double.valueOf(netIoState.getTxpck()) < minTxpck) { minTxpck = netIoState.getTxpck(); } sumTxpck += netIoState.getTxpck(); } //组装每秒传输数据包 end //组装连接数 begin if (null != netIoState.getNetConnections()) { if (netIoState.getNetConnections() > maxConns) { maxConns = netIoState.getNetConnections(); } if (Double.valueOf(netIoState.getNetConnections()) < minConns) { minConns = netIoState.getNetConnections(); } sumConns += netIoState.getNetConnections(); } //组装连接数 end } catch (Exception e) { logger.error("设置网络流量图表副标题错误", e); } } } //设置图表属性max最大值 begin if (maxRxbyt > maxBytesVal) { maxBytesVal = maxRxbyt; } if (maxTxbyt > maxBytesVal) { maxBytesVal = maxTxbyt; } if (maxRxpck > maxPckVal) { maxPckVal = maxRxpck; } if (maxTxpck > maxPckVal) { maxPckVal = maxTxpck; } if (maxBytesVal == 0) { maxBytesVal = 1; } model.addAttribute("netIoStateBytMaxVal", Math.ceil(maxBytesVal)); if (maxPckVal == 0) { maxPckVal = 1; } model.addAttribute("netIoStatePckMaxVal", Math.ceil(maxPckVal)); if (maxDropVal == 0) { maxDropVal = 1; } model.addAttribute("netIoStateDropPckMaxVal", Math.ceil(maxDropVal)); //设置图表属性max最大值 end if (netIoStateList.size() > 0) { avgRxbyt = sumRxbyt / netIoStateList.size(); avgTxbyt = sumTxbyt / netIoStateList.size(); avgRxpck = sumRxpck / netIoStateList.size(); avgTxpck = sumTxpck / netIoStateList.size(); avgConns = sumConns / (double) netIoStateList.size(); } else { minRxbyt = 0d; minTxbyt = 0d; minRxpck = 0d; minTxpck = 0d; minConns = 0; } SubtitleDto rxbytSubtitleDto = new SubtitleDto(); rxbytSubtitleDto.setAvgValue(FormatUtil.kbToM(FormatUtil.formatDouble(avgRxbyt, 2) + "") + "/s"); rxbytSubtitleDto.setMaxValue(FormatUtil.kbToM(maxRxbyt + "") + "/s"); rxbytSubtitleDto.setMinValue(FormatUtil.kbToM(minRxbyt + "") + "/s"); model.addAttribute("rxbytSubtitleDto", rxbytSubtitleDto); SubtitleDto txbytSubtitleDto = new SubtitleDto(); txbytSubtitleDto.setAvgValue(FormatUtil.kbToM(FormatUtil.formatDouble(avgTxbyt, 2) + "") + "/s"); txbytSubtitleDto.setMaxValue(FormatUtil.kbToM(maxTxbyt + "") + "/s"); txbytSubtitleDto.setMinValue(FormatUtil.kbToM(minTxbyt + "") + "/s"); model.addAttribute("txbytSubtitleDto", txbytSubtitleDto); SubtitleDto rxpckSubtitleDto = new SubtitleDto(); rxpckSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgRxpck, 2) + "/s"); rxpckSubtitleDto.setMaxValue(maxRxpck + "/s"); rxpckSubtitleDto.setMinValue(minRxpck + "/s"); model.addAttribute("rxpckSubtitleDto", rxpckSubtitleDto); SubtitleDto txpckSubtitleDto = new SubtitleDto(); txpckSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgTxpck, 2) + "/s"); txpckSubtitleDto.setMaxValue(maxTxpck + "/s"); txpckSubtitleDto.setMinValue(minTxpck + "/s"); model.addAttribute("txpckSubtitleDto", txpckSubtitleDto); SubtitleDto connsSubtitleDto = new SubtitleDto(); connsSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgConns, 2) + ""); connsSubtitleDto.setMaxValue(maxConns + ""); connsSubtitleDto.setMinValue(minConns + ""); model.addAttribute("connsSubtitleDto", connsSubtitleDto); } /** * 公众看板页面,未登录时候,IP脱敏显示,隐藏前5个字符 * * @param systemInfo */ public void hideLeftIp(SystemInfo systemInfo, HttpServletRequest request) { if (StringUtils.isBlank(systemInfo.getHostname())) { return; } if (!StaticKeys.TRUE_VAL.equals(commonConfig.getDashViewIpHide())) { return; } HttpSession session = request.getSession(); AccountInfo accountInfo = (AccountInfo) session.getAttribute(StaticKeys.LOGIN_KEY); if (null == accountInfo) { int length = StringUtils.length(systemInfo.getHostname()); String resHostName = StringUtils.leftPad(StringUtils.right(systemInfo.getHostname(), length - 5), length, "*"); systemInfo.setHostname(resHostName); } } @Autowired private SystemInfoMapper systemInfoMapper; @Autowired private CommonConfig commonConfig; @Resource private DiskStateService diskStateService; @Resource private LogInfoService logInfoService; }