付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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<String, Object> params = new HashMap<>();
            List<AccountInfo> 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<String, Object> 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<DiskState> 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<ChartInfo>
     *
     * @param systemInfoList
     * @return
     */
    public static List<ChartInfo> getSystemTypeList(List<SystemInfo> systemInfoList) {
        List<ChartInfo> chartInfoList = new ArrayList<>();
        Map<String, Integer> map = getSystemTypeMap(systemInfoList);
        for (Map.Entry<String, Integer> 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<String, Integer> getSystemTypeMap(List<SystemInfo> systemInfoList) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        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();
    }
 
}