package com.wgcloud.util; import cn.hutool.core.collection.CollectionUtil; import com.wgcloud.common.ApplicationContextHelper; import com.wgcloud.config.CommonConfig; import com.wgcloud.dto.ChartInfo; import com.wgcloud.entity.AccountInfo; import com.wgcloud.entity.DiskState; import com.wgcloud.entity.SystemInfo; import com.wgcloud.service.AccountInfoService; import com.wgcloud.util.staticvar.StaticKeys; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ui.Model; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @version v3.3 * @ClassName:HostUtil.java * @author: http://www.wgstart.com * @date: 2021年1月16日 * @Description: 主机处理工具类 * @Copyright: 2019-2021 wgcloud. All rights reserved. */ public class HostUtil { private static final Logger logger = LoggerFactory.getLogger(HostUtil.class); private static CommonConfig commonConfig = (CommonConfig) ApplicationContextHelper.getBean(CommonConfig.class); private static AccountInfoService accountInfoService = (AccountInfoService) ApplicationContextHelper.getBean(AccountInfoService.class); /** * 查询所有用户登录账号,传输到页面 * * @param model */ public static void addAccountListModel(Model model) { try { if (!StaticKeys.TRUE_VAL.equals(commonConfig.getUserInfoManage())) { return; } Map params = new HashMap<>(); List accountInfoList = accountInfoService.selectAllByParams(params); if (!CollectionUtil.isEmpty(accountInfoList)) { model.addAttribute("accountList", accountInfoList); } } catch (Exception e) { logger.error("addAccountListModel", e); } } /** * 处理根据登录用户查询所属资源,如果是管理员或者只读账号则不需要添加查询参数,默认查询所有 * 如果是其他账号,则只能查询自己的所属资源 * * @param request * @param params */ public static void addAccountquery(HttpServletRequest request, Map params) { if (null == request || null == params) { return; } AccountInfo accountInfo = getAccountByRequest(request); if (null != accountInfo) { if (!StaticKeys.ROLE_ADMIN.equals(accountInfo.getRole()) && !StaticKeys.ROLE_GUEST.equals(accountInfo.getRole())) { params.put("account", accountInfo.getAccount()); } } } /** * 设置磁盘总使用率 * * * @param systemInfo 主机信息 */ public static void setDiskSumPer(List diskStates, SystemInfo systemInfo) { try { Double sumSize = 0d; Double useSize = 0d; for (DiskState diskState : diskStates) { if (!StringUtils.isEmpty(diskState.getDiskSize()) && !StringUtils.isEmpty(diskState.getUsed())) { sumSize += Double.valueOf(diskState.getDiskSize().replace("G", "")); useSize += Double.valueOf(diskState.getUsed().replace("G", "")); } } systemInfo.setDiskPer(0D); if (sumSize != 0) { systemInfo.setDiskPer(FormatUtil.formatDouble((useSize / sumSize) * 100, 2)); } } catch (Exception e) { logger.error("设置磁盘总使用率错误", e); } } /** * 设置操作系统logo * * @param systemInfo */ public static void setSysImage(SystemInfo systemInfo) { if (!StringUtils.isEmpty(systemInfo.getPlatForm())) { String platForm = systemInfo.getPlatForm().toLowerCase(); if (platForm.contains("windows")) { systemInfo.setImage("/tssw/static/images/windows.png"); } else if (platForm.contains("centos")) { systemInfo.setImage("/tssw/static/images/centos.png"); } else if (platForm.contains("hat")) { systemInfo.setImage("/tssw/static/images/redhat.png"); } else if (platForm.contains("ubuntu")) { systemInfo.setImage("/tssw/static/images/ubuntu.png"); } else if (platForm.contains("debian")) { systemInfo.setImage("/tssw/static/images/debian.png"); } else if (platForm.contains("darwin")) { systemInfo.setImage("/tssw/static/images/darwin.png"); } else if (platForm.contains("android")) { systemInfo.setImage("/tssw/static/images/android.png"); } else if (platForm.contains("suse")) { systemInfo.setImage("/tssw/static/images/suse.png"); } else if (platForm.contains("fedora")) { systemInfo.setImage("/tssw/static/images/fedora.png"); } else { systemInfo.setImage("/tssw/static/images/linux.png"); } } else { systemInfo.setImage("/tssw/static/images/linux.png"); } } /** * 大屏统计各种机型数量 返回List * * @param systemInfoList * @return */ public static List getSystemTypeList(List systemInfoList) { List chartInfoList = new ArrayList<>(); Map map = getSystemTypeMap(systemInfoList); for (Map.Entry entry : map.entrySet()) { ChartInfo chartInfo = new ChartInfo(); chartInfo.setItem(entry.getKey()); chartInfo.setCount(entry.getValue()); chartInfoList.add(chartInfo); } return chartInfoList; } /** * 大屏统计各种机型数量 * 返回Map * * @param systemInfoList * @return */ public static Map getSystemTypeMap(List systemInfoList) { Map map = new HashMap(); for (SystemInfo systemInfo : systemInfoList) { if (!StringUtils.isEmpty(systemInfo.getPlatForm())) { String platForm = systemInfo.getPlatForm().toLowerCase(); if (platForm.contains("windows")) { if (map.get("windows") != null) { map.put("windows", map.get("windows") + 1); } else { map.put("windows", 1); } } else if (platForm.contains("centos")) { if (map.get("centos") != null) { map.put("centos", map.get("centos") + 1); } else { map.put("centos", 1); } } else if (platForm.contains("redhat")) { if (map.get("redhat") != null) { map.put("redhat", map.get("redhat") + 1); } else { map.put("redhat", 1); } } else if (platForm.contains("ubuntu")) { if (map.get("ubuntu") != null) { map.put("ubuntu", map.get("ubuntu") + 1); } else { map.put("ubuntu", 1); } } else if (platForm.contains("debian")) { if (map.get("debian") != null) { map.put("debian", map.get("debian") + 1); } else { map.put("debian", 1); } } else if (platForm.contains("darwin")) { if (map.get("macOS") != null) { map.put("macOS", map.get("macOS") + 1); } else { map.put("macOS", 1); } } else if (platForm.contains("android")) { if (map.get("android") != null) { map.put("android", map.get("android") + 1); } else { map.put("android", 1); } } else { if (map.get("其他Linux") != null) { map.put("其他Linux", map.get("其他Linux") + 1); } else { map.put("其他Linux", 1); } } } else { if (map.get("其他Linux") != null) { map.put("其他Linux", map.get("其他Linux") + 1); } else { map.put("其他Linux", 1); } } } return map; } /** * 提取主机备注信息 * * @param hostname * @return */ public static String addRemark(String hostname) { if (StringUtils.isEmpty(hostname)) { return ""; } String remark = StaticKeys.HOST_MAP.get(hostname); if (!StringUtils.isEmpty(remark)) { remark = "(" + remark + ")"; } else { remark = ""; } return remark; } /** * 提取主机所属用户账号信息 * * @param hostname * @return */ public static String getAccount(String hostname) { if (StringUtils.isEmpty(hostname)) { return ""; } String account = StaticKeys.HOST_ACCOUNT_MAP.get(hostname); return account; } /** * 从requst中获取sessoin,获取当前登录账号 * * @param request * @return */ public static AccountInfo getAccountByRequest(HttpServletRequest request) { AccountInfo accountInfo = (AccountInfo) request.getSession().getAttribute(StaticKeys.LOGIN_KEY); if (accountInfo == null) { return new AccountInfo(); } else { return accountInfo; } } /** * 每天早上定时清空缓存map */ public static void clearCacheMap(){ logger.info("清空缓存主机缓存的map"); StaticKeys.HOST_MAP.clear(); StaticKeys.HOST_ACCOUNT_MAP.clear(); } }