kongdeqiang
2023-02-22 9812f31d6402a268fa6fd35e8c3a2a59582b720d
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.wgcloud.controller;
 
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.wgcloud.config.CommonConfig;
import com.wgcloud.dto.ChartInfo;
import com.wgcloud.entity.HostDiskPer;
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.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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:DapingController.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: 大屏展示controller
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Controller
@RequestMapping(value = "/daping")
public class DapingController {
 
 
    private void testThread() {
        Runnable runnable = () -> {
            logger.info("DapingController----------testThread");
        };
        ThreadPoolUtil.executor.execute(runnable);
    }
 
 
    private static final Logger logger = LoggerFactory.getLogger(DapingController.class);
    @Resource
    private SystemInfoService systemInfoService;
    @Resource
    private LogInfoService logInfoService;
    @Autowired
    private HeathMonitorService heathMonitorService;
    @Autowired
    private HostGroupService hostInfoService;
    @Autowired
    private CpuStateService cpuStateService;
    @Autowired
    private MemStateService memStateService;
    @Autowired
    private SysLoadStateService sysLoadStateService;
    @Autowired
    private NetIoStateService netIoStateService;
    @Autowired
    private DeskIoService deskIoService;
    @Autowired
    private TaskUtilService taskUtilService;
    @Resource
    private CpuTemperaturesService cpuTemperaturesService;
    @Resource
    private HostDiskPerService hostDiskPerService;
    @Autowired
    private CommonConfig commonConfig;
 
    /**
     * 根据条件查询host列表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "index")
    public String index(Model model, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            List<SystemInfo> systemInfoList = systemInfoService.selectAllByParams(params);
 
            // 系统类型数量图表 begin
            List<ChartInfo> systemInfoTypeList = HostUtil.getSystemTypeList(systemInfoList);
            List<String> systemInfoTypeNameList = new ArrayList<>();
            List<Integer> systemInfoTypeValList = new ArrayList<>();
            for (ChartInfo chartInfo : systemInfoTypeList) {
                systemInfoTypeNameList.add(chartInfo.getItem());
                systemInfoTypeValList.add(chartInfo.getCount());
            }
            model.addAttribute("systemInfoTypeNameList", JSONUtil.parseArray(systemInfoTypeNameList));
            model.addAttribute("systemInfoTypeValList", JSONUtil.parseArray(systemInfoTypeValList));
            // 系统类型数量图表 end
 
 
            int totalSystemInfoSize = systemInfoService.countByParams(params);
            model.addAttribute("totalSystemInfoSize", totalSystemInfoSize);//主机数量
 
 
            params.clear();
            params.put("state", StaticKeys.DOWN_STATE);
            int hostDownSize = systemInfoService.countByParams(params);
            model.addAttribute("hostDownSize", hostDownSize);//下线主机数量
 
            //饼图
            setSysPie(model);
            //cpu使用率,内存使用率,上下行传输速率 最新上报前10
            systemTop10(model);
 
            //主机状态
            setMiddleData(systemInfoList, model);
 
            //雷达图显示
            warnInfoInit(model);
 
            return "daping/index";
        } catch (Exception e) {
            logger.error("大屏展示错误", e);
        }
        return "";
    }
 
 
    /**
     * 设置主机在线/下线饼图
     *
     * @param model
     */
    private void setSysPie(Model model) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            int totalSystemInfoSize = systemInfoService.countByParams(params);//监控主机总量
            params.put("state", StaticKeys.DOWN_STATE);
            int hostDownSize = systemInfoService.countByParams(params);
            model.addAttribute("hostDownSize", hostDownSize);//下线主机数量
            model.addAttribute("hostOnSize", (totalSystemInfoSize - hostDownSize));
        } catch (Exception e) {
            logger.error("设置主机在线/下线饼图错误", e);
        }
    }
 
    /**
     * 设置告警统计信息雷达图
     *
     * @param model
     */
    private void warnInfoInit(Model model) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            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);
            model.addAttribute("cpuWarnSize", cpuWarnSize);//cpu告警次数
 
            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);//系统负载告警次数
        } catch (Exception e) {
            logger.error("设置告警统计信息雷达图错误", e);
        }
 
    }
 
 
 
    /**
     * cpu使用率,内存使用率,上下行传输速率 最新上报前10
     *
     * @param model
     */
    private void systemTop10(Model model) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("orderBy", "CREATE_TIME");
            params.put("orderType", "DESC");
            PageInfo<SystemInfo> pageInfo = systemInfoService.selectByParams(params, 1, 10);
            List<String> systemTop10NameList = new ArrayList<>();
            List<Double> cpuTop10ValList = new ArrayList<>();
            List<String> rxbytTop10List = new ArrayList<>();
            List<String> txbytTop10List = new ArrayList<>();
            List<Double> memTop10ValList = new ArrayList<>();
            for (SystemInfo systemInfo : pageInfo.getList()) {
                systemTop10NameList.add(systemInfo.getHostname());
                cpuTop10ValList.add(systemInfo.getCpuPer());
                rxbytTop10List.add(systemInfo.getRxbyt());
                txbytTop10List.add(systemInfo.getTxbyt());
                memTop10ValList.add(systemInfo.getMemPer());
            }
            model.addAttribute("systemTop10NameList", JSONUtil.parseArray(systemTop10NameList));
            model.addAttribute("cpuTop10ValList", JSONUtil.parseArray(cpuTop10ValList));
            model.addAttribute("rxbytTop10List", JSONUtil.parseArray(rxbytTop10List));
            model.addAttribute("txbytTop10List", JSONUtil.parseArray(txbytTop10List));
            model.addAttribute("memTop10ValList", JSONUtil.parseArray(memTop10ValList));
        } catch (Exception e) {
            logger.error("cpu使用率最新上报前10错误", e);
        }
    }
 
 
    /**
     * 设置温度计效果图表
     *
     * @param model
     */
    private void setMiddleData(List<SystemInfo> systemInfoList, Model model) {
        //cpu使用率最高
        Double maxCpu = 0d;
        String maxCpuIp = "";
        //cpu使用率平均
        Double avgCpu = 0d;
        //cpu使用率最低
        Double minCpu = 1000d;
        String minCpuIp = "";
        //cpu使用率总和
        Double sumCpu = 0d;
 
        //内存使用率最高
        Double maxMem = 0d;
        String maxMemIp = "";
        //内存使用率最低
        Double minMem = 1000d;
        String minMemIp = "";
        //内存使用率平均
        Double avgMem = 0d;
        //内存使用率总和
        Double sumMem = 0d;
 
        //磁盘总使用率最高
        Double maxDiskPer = 0d;
        String maxDiskPerIp = "";
        //磁盘总使用率最低
        Double minDiskPer = 1000d;
        String minDiskPerIp = "";
        //磁盘总使用率平均
        Double avgDiskPer = 0d;
        //磁盘总使用率总和
        Double sumDiskPer = 0d;
 
        int systemSize = 0;
        //cpu总核数
        int cpuCoresSum = 0;
        //内存总和
        double memSum = 0;
 
        for (SystemInfo systemInfo : systemInfoList) {
            systemSize += 1;
            if (null != systemInfo.getCpuPer() && systemInfo.getCpuPer() > maxCpu) {
                maxCpu = systemInfo.getCpuPer();
                maxCpuIp = systemInfo.getHostname();
            }
            if (null != systemInfo.getCpuPer() && systemInfo.getCpuPer() < minCpu) {
                minCpu = systemInfo.getCpuPer();
                minCpuIp = systemInfo.getHostname();
            }
            if (null != systemInfo.getCpuPer()) {
                sumCpu += systemInfo.getCpuPer();
            }
 
            if (null != systemInfo.getMemPer() && systemInfo.getMemPer() > maxMem) {
                maxMem = systemInfo.getMemPer();
                maxMemIp = systemInfo.getHostname();
            }
            if (null != systemInfo.getMemPer() && systemInfo.getMemPer() < minMem) {
                minMem = systemInfo.getMemPer();
                minMemIp = systemInfo.getHostname();
            }
            if (null != systemInfo.getMemPer()) {
                sumMem += systemInfo.getMemPer();
            }
 
            try {
                cpuCoresSum += Integer.valueOf(systemInfo.getCpuCoreNum());
            } catch (Exception e) {
                logger.error("统计所有主机总核数错误", e);
            }
 
            try {
                memSum += Double.valueOf(systemInfo.getTotalMem().replace("G", ""));
            } catch (Exception e) {
                logger.error("统计所有主机总内存错误", e);
            }
 
        }
 
        try {
            //组织磁盘总使用率最大平均最低值 begin
            Map<String, Object> params = new HashMap<String, Object>();
            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<HostDiskPer> hostDiskPerList = hostDiskPerService.selectAllByParams(params);
            for (HostDiskPer hostDiskPer : hostDiskPerList) {
                if (null != hostDiskPer.getDiskSumPer() && hostDiskPer.getDiskSumPer() > maxDiskPer) {
                    maxDiskPer = hostDiskPer.getDiskSumPer();
                    maxDiskPerIp = hostDiskPer.getHostname();
                }
                if (null != hostDiskPer.getDiskSumPer() && hostDiskPer.getDiskSumPer() < minDiskPer) {
                    minDiskPer = hostDiskPer.getDiskSumPer();
                    minDiskPerIp = hostDiskPer.getHostname();
                }
                if (null != hostDiskPer.getDiskSumPer()) {
                    sumDiskPer += hostDiskPer.getDiskSumPer();
                }
            }
            if (hostDiskPerList.size() > 0) {
                avgDiskPer = sumDiskPer / hostDiskPerList.size();
            } else {
                minDiskPer = 0d;
            }
            //组织磁盘总使用率最大平均最低值 end
        } catch (Exception e) {
            logger.error("组织磁盘总使用率最大平均最低值错误", e);
        }
 
        if (systemSize > 0) {
            avgCpu = sumCpu / systemInfoList.size();
            avgMem = sumMem / systemInfoList.size();
        } else {
            minCpu = 0d;
            minMem = 0d;
        }
 
        model.addAttribute("cpuCoresSum", cpuCoresSum);
        model.addAttribute("memSum", FormatUtil.gToT(memSum + ""));
 
        List<String> listIp = new ArrayList<>();
        List<Double> list = new ArrayList<>();
        list.add(maxCpu);
        listIp.add(maxCpuIp);
        list.add(FormatUtil.formatDouble(avgCpu, 2));
        listIp.add("avgCpu");
        list.add(minCpu);
        listIp.add(minCpuIp);
 
        list.add(maxMem);
        listIp.add(maxMemIp);
        list.add(FormatUtil.formatDouble(avgMem, 2));
        listIp.add("avgCpu");
        list.add(minMem);
        listIp.add(minMemIp);
 
        list.add(maxDiskPer);
        listIp.add(maxDiskPerIp);
        list.add(FormatUtil.formatDouble(avgDiskPer, 2));
        listIp.add("avgDiskPer");
        list.add(minDiskPer);
        listIp.add(minDiskPerIp);
 
        model.addAttribute("middleDataList", JSONUtil.parseArray(list));
        model.addAttribute("middleDataListIp", JSONUtil.parseArray(listIp));
 
    }
 
 
}