13693261870
2024-08-16 7efa8af608c37abf09b817bda0f2f5e216ec6e0e
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package com.wgcloud.controller;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.wgcloud.dto.ChartInfo;
import com.wgcloud.dto.NetIoStateDto;
import com.wgcloud.entity.*;
import com.wgcloud.service.*;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.FormatUtil;
import com.wgcloud.util.PageUtil;
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 org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
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 v2.3
 * @ClassName:DashboardCotroller.java
 * @author: http://www.wgstart.com
 * @date: 2019年11月16日
 * @Description: DashboardCotroller.java
 * @Copyright: 2017-2024 wgcloud. All rights reserved.
 */
@Controller
@RequestMapping(value = "/dash")
public class DashboardCotroller {
 
    private static final Logger logger = LoggerFactory.getLogger(DashboardCotroller.class);
    @Resource
    DashboardService dashboardService;
    @Resource
    CpuStateService cpuStateService;
    @Resource
    DeskStateService deskStateService;
    @Resource
    MemStateService memStateService;
    @Resource
    NetIoStateService netIoStateService;
    @Resource
    SysLoadStateService sysLoadStateService;
    @Resource
    DbTableService dbTableService;
    @Resource
    DbInfoService dbInfoService;
    @Resource
    TcpStateService tcpStateService;
    @Resource
    SystemInfoService systemInfoService;
    @Resource
    MailSetService mailSetService;
    @Resource
    AppInfoService appInfoService;
    @Resource
    private LogInfoService logInfoService;
    @Autowired
    HeathMonitorService heathMonitorService;
    @Autowired
    HostInfoService hostInfoService;
 
    /**
     * 根据条件查询host列表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "main")
    public String mainList(Model model, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<ChartInfo> chartInfoList = new ArrayList<ChartInfo>();
        try {
            int totalSystemInfoSize = systemInfoService.countByParams(params);
            model.addAttribute("totalSystemInfoSize", totalSystemInfoSize);
            int totalSizeApp = appInfoService.countByParams(params);
            model.addAttribute("totalSizeApp", totalSizeApp);
 
            params.put("memPer", 90);
            int memPerSize_90 = systemInfoService.countByParams(params);
            double a = 0;
            if (totalSystemInfoSize != 0) {
                a = (double) memPerSize_90 / totalSystemInfoSize;
            }
            ChartInfo memPerSize_90_chart = new ChartInfo();
            memPerSize_90_chart.setItem("内存>90%");
            memPerSize_90_chart.setCount(memPerSize_90);
            memPerSize_90_chart.setPercent(FormatUtil.formatDouble(a, 2));
            chartInfoList.add(memPerSize_90_chart);
 
            params.put("memPer", 50);
            params.put("memPerLe", 90);
            int memPerSize_50_90 = systemInfoService.countByParams(params);
            double b = 0;
            if (totalSystemInfoSize != 0) {
                b = (double) memPerSize_50_90 / totalSystemInfoSize;
            }
            ChartInfo memPerSize_50_90_chart = new ChartInfo();
            memPerSize_50_90_chart.setItem("内存>50%且<90%");
            memPerSize_50_90_chart.setCount(memPerSize_50_90);
            memPerSize_50_90_chart.setPercent(FormatUtil.formatDouble(b, 2));
            chartInfoList.add(memPerSize_50_90_chart);
            params.clear();
 
            params.put("cpuPer", 90);
            int cpuPerSize_90 = systemInfoService.countByParams(params);
            double c = 0;
            if (totalSystemInfoSize != 0) {
                c = (double) cpuPerSize_90 / totalSystemInfoSize;
            }
            ChartInfo cpuPerSize_90_chart = new ChartInfo();
            cpuPerSize_90_chart.setItem("CPU>90%");
            cpuPerSize_90_chart.setCount(cpuPerSize_90);
            cpuPerSize_90_chart.setPercent(FormatUtil.formatDouble(c, 2));
            chartInfoList.add(cpuPerSize_90_chart);
            params.clear();
 
            params.put("cpuPer", 90);
            params.put("memPer", 90);
            int perSize_90_90 = systemInfoService.countByParams(params);
            double d = 0;
            if (totalSystemInfoSize != 0) {
                d = (double) perSize_90_90 / totalSystemInfoSize;
            }
            ChartInfo perSize_90_90_chart = new ChartInfo();
            perSize_90_90_chart.setItem("CPU和内存>90%");
            perSize_90_90_chart.setCount(perSize_90_90);
            perSize_90_90_chart.setPercent(FormatUtil.formatDouble(d, 2));
            chartInfoList.add(perSize_90_90_chart);
            params.clear();
 
            params.put("memPerLe", 50);
            params.put("cpuPerLe", 50);
            int perSize_50_50 = systemInfoService.countByParams(params);
            double e = 0;
            if (totalSystemInfoSize != 0) {
                e = (double) perSize_50_50 / totalSystemInfoSize;
            }
            ChartInfo perSize_50_50_chart = new ChartInfo();
            perSize_50_50_chart.setItem("CPU和内存<50%");
            perSize_50_50_chart.setCount(perSize_50_50);
            perSize_50_50_chart.setPercent(FormatUtil.formatDouble(e, 2));
            chartInfoList.add(perSize_50_50_chart);
            model.addAttribute("chartInfoList", JSONUtil.parseArray(chartInfoList));
            params.clear();
 
            params.put("cpuPer", 90);
            int memPerSizeApp = appInfoService.countByParams(params);
            model.addAttribute("memPerSizeApp", memPerSizeApp);
            params.clear();
 
            int logSize = logInfoService.countByParams(params);
            model.addAttribute("logSize", logSize);
 
            params.clear();
            int dbTableSize = dbTableService.countByParams(params);
            model.addAttribute("dbTableSize", dbTableSize);
 
            Long dbTableSum = dbTableService.sumByParams(params);
            model.addAttribute("dbTableSum", dbTableSum == null ? 0 : dbTableSum);
 
            PageInfo pageInfoDbTableList = dbTableService.selectByParams(params, 1, 10);
            model.addAttribute("dbTableList", JSONUtil.parseArray(pageInfoDbTableList.getList()));
 
            int dbInfoSize = dbInfoService.countByParams(params);
            model.addAttribute("dbInfoSize", dbInfoSize);
 
            int heathSize = heathMonitorService.countByParams(params);
            model.addAttribute("heathSize", heathSize);
            params.put("heathStatus", "200");
            int heath200Size = heathMonitorService.countByParams(params);
            model.addAttribute("heath200Size", heath200Size);
            model.addAttribute("heatherrSize", (heathSize - heath200Size));
 
 
        } catch (Exception e) {
            logger.error("主面板信息异常:", e);
            logInfoService.save("dash/main", "主面板信息错误:" + e.toString(), StaticKeys.LOG_ERROR);
        }
        if (request.getParameter(StaticKeys.DASH_VIEW_ACCOUNT) != null) {
            return "dashView/index";
        } else {
            return "index";
        }
    }
 
    /**
     * 根据条件查询host列表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "systemInfoList")
    public String systemInfoList(SystemInfo systemInfo, Model model, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            StringBuffer url = new StringBuffer();
            if (request.getParameter(StaticKeys.DASH_VIEW_ACCOUNT) != null) {
                url.append("&dashView=1");
            }
            PageInfo<SystemInfo> pageInfo = systemInfoService.selectByParams(params, systemInfo.getPage(), systemInfo.getPageSize());
 
            //设置磁盘总使用率 begin
            for (SystemInfo systemInfo1 : pageInfo.getList()) {
                params.put("hostname", systemInfo1.getHostname());
                List<DeskState> deskStates = deskStateService.selectAllByParams(params);
                try {
                    Double sumSize = 0d;
                    Double useSize = 0d;
                    for (DeskState deskState : deskStates) {
                        if (!StringUtils.isEmpty(deskState.getSize()) && !StringUtils.isEmpty(deskState.getUsed())) {
                            sumSize += Double.valueOf(deskState.getSize().replace("G", ""));
                            useSize += Double.valueOf(deskState.getUsed().replace("G", ""));
                        }
                    }
                    systemInfo1.setDiskPer(0D);
                    if (sumSize != 0) {
                        systemInfo1.setDiskPer(FormatUtil.formatDouble((useSize / sumSize) * 100, 2));
                    }
                } catch (Exception e){
                    logger.error("设置磁盘总使用率错误", e);
                }
            }
            //设置磁盘总使用率 end
 
            PageUtil.initPageNumber(pageInfo, model);
            model.addAttribute("pageUrl", "/dash/systemInfoList?1=1" + url.toString());
            model.addAttribute("page", pageInfo);
        } catch (Exception e) {
            logger.error("查询服务器列表错误:", e);
            logInfoService.save("查询服务器列表错误", e.toString(), StaticKeys.LOG_ERROR);
        }
        if (request.getParameter(StaticKeys.DASH_VIEW_ACCOUNT) != null) {
            return "dashView/list";
        } else {
            return "host/list";
        }
    }
 
 
    /**
     * 根据IP查询服务器详情信息
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "detail")
    public String hostDetail(Model model, HttpServletRequest request) {
        //服务器名称
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return "error/500";
        }
        String hostname = "";
        try {
            SystemInfo systemInfo = systemInfoService.selectById(id);
            hostname = systemInfo.getHostname();
            model.addAttribute("systemInfo", systemInfo);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("hostname", systemInfo.getHostname());
            List<DeskState> deskStateList = deskStateService.selectAllByParams(params);
            model.addAttribute("deskStateList", deskStateList);
        } catch (Exception e) {
            logger.error("服务器详细信息错误:", e);
            logInfoService.save(hostname, "查看服务器详细信息错误", e.toString());
        }
        if (request.getParameter(StaticKeys.DASH_VIEW_ACCOUNT) != null) {
            return "dashView/view";
        } else {
            return "host/view";
        }
 
    }
 
    /**
     * 删除主机
     *
     * @param id
     * @param model
     * @param request
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "del")
    public String delete(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        String errorMsg = "删除主机信息错误:";
        try {
            if (!StringUtils.isEmpty(request.getParameter("id"))) {
                String[] ids = request.getParameter("id").split(",");
                for (String id : ids) {
                    SystemInfo sys = systemInfoService.selectById(id);
                    if (!StringUtils.isEmpty(sys.getHostname())) {
                        hostInfoService.deleteByIp(sys.getHostname().split(","));
                    }
                    logInfoService.save("删除主机:" + sys.getHostname(), sys.getHostname(), StaticKeys.LOG_ERROR);
                }
                systemInfoService.deleteById(ids);
            }
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_ERROR);
        }
        return "redirect:/dash/systemInfoList";
    }
 
 
    /**
     * 根据IP查询服务器图形报表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "chart")
    public String hostChart(Model model, HttpServletRequest request) {
        //服务器名称
        String id = request.getParameter("id");
        String date = request.getParameter("date");
        if (StringUtils.isEmpty(id)) {
            return "error/500";
        }
        String hostname = "";
        try {
            SystemInfo systemInfo = systemInfoService.selectById(id);
            hostname = systemInfo.getHostname();
            model.addAttribute("systemInfo", systemInfo);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("hostname", systemInfo.getHostname());
            if (StringUtils.isEmpty(date)) {
                date = DateUtil.getCurrentDate();
            }
            dashboardService.setDateParam(date, params);
            model.addAttribute("datenow", date);
            model.addAttribute("dateList", dashboardService.getDateList());
            List<CpuState> cpuStateList = cpuStateService.selectAllByParams(params);
            model.addAttribute("cpuStateList", JSONUtil.parseArray(cpuStateList));
            model.addAttribute("cpuStateMaxVal", findCpuMaxVal(cpuStateList));
            List<MemState> memStateList = memStateService.selectAllByParams(params);
            model.addAttribute("memStateList", JSONUtil.parseArray(memStateList));
            List<SysLoadState> ysLoadSstateList = sysLoadStateService.selectAllByParams(params);
            model.addAttribute("ysLoadSstateList", JSONUtil.parseArray(ysLoadSstateList));
            model.addAttribute("ysLoadSstateMaxVal", findLoadMaxVal(ysLoadSstateList));
            List<NetIoState> netIoStateList = netIoStateService.selectAllByParams(params);
            List<NetIoStateDto> netIoStateDtoList = toNetIoStateDto(netIoStateList);
            model.addAttribute("netIoStateList", JSONUtil.parseArray(netIoStateDtoList));
            model.addAttribute("netIoStateBytMaxVal", findNetIoStateBytMaxVal(netIoStateDtoList));
            model.addAttribute("netIoStatePckMaxVal", findNetIoStatePckMaxVal(netIoStateDtoList));
 
        } catch (Exception e) {
            logger.error("服务器图形报表错误:", e);
            logInfoService.save(hostname, "图形报表错误", e.toString());
        }
        if (request.getParameter(StaticKeys.DASH_VIEW_ACCOUNT) != null) {
            return "dashView/viewChart";
        } else {
            return "host/viewChart";
        }
    }
 
 
    private double findCpuMaxVal(List<CpuState> cpuStateList) {
        double maxval = 0;
        if (!CollectionUtil.isEmpty(cpuStateList)) {
            for (CpuState cpuState : cpuStateList) {
                if (null != cpuState.getIdle() && cpuState.getIdle() > maxval) {
                    maxval = cpuState.getIdle();
                }
                if (null != cpuState.getSys() && cpuState.getSys() > maxval) {
                    maxval = cpuState.getSys();
                }
                if (null != cpuState.getIowait() && cpuState.getIowait() > maxval) {
                    maxval = cpuState.getIowait();
                }
            }
        }
        if (maxval == 0) {
            maxval = 100;
        }
        return Math.ceil(maxval);
    }
 
    private double findLoadMaxVal(List<SysLoadState> ysLoadSstateList) {
        double maxval = 0;
        if (!CollectionUtil.isEmpty(ysLoadSstateList)) {
            for (SysLoadState sysLoadState : ysLoadSstateList) {
                if (null != sysLoadState.getOneLoad() && sysLoadState.getOneLoad() > maxval) {
                    maxval = sysLoadState.getOneLoad();
                }
                if (null != sysLoadState.getFiveLoad() && sysLoadState.getFiveLoad() > maxval) {
                    maxval = sysLoadState.getFiveLoad();
                }
                if (null != sysLoadState.getFifteenLoad() && sysLoadState.getFifteenLoad() > maxval) {
                    maxval = sysLoadState.getFifteenLoad();
                }
            }
        }
        if (maxval == 0) {
            maxval = 1;
        }
        return Math.ceil(maxval);
    }
 
    private List<NetIoStateDto> toNetIoStateDto(List<NetIoState> netIoStateList) {
        List<NetIoStateDto> 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(Integer.valueOf(netIoState.getRxbyt()));
            dto.setRxpck(Integer.valueOf(netIoState.getRxpck()));
            dto.setTxbyt(Integer.valueOf(netIoState.getTxbyt()));
            dto.setTxpck(Integer.valueOf(netIoState.getTxpck()));
            dtoList.add(dto);
        }
        return dtoList;
    }
 
    private double findNetIoStateBytMaxVal(List<NetIoStateDto> netIoStateList) {
        double maxval = 0;
        if (!CollectionUtil.isEmpty(netIoStateList)) {
            for (NetIoStateDto netIoState : netIoStateList) {
                if (null != netIoState.getRxbyt() && netIoState.getRxbyt() > maxval) {
                    maxval = netIoState.getRxbyt();
                }
                if (null != netIoState.getTxbyt() && netIoState.getTxbyt() > maxval) {
                    maxval = netIoState.getTxbyt();
                }
 
            }
        }
        if (maxval == 0) {
            maxval = 1;
        }
        return Math.ceil(maxval);
    }
 
    private double findNetIoStatePckMaxVal(List<NetIoStateDto> netIoStateList) {
        double maxval = 0;
        if (!CollectionUtil.isEmpty(netIoStateList)) {
            for (NetIoStateDto netIoState : netIoStateList) {
                if (null != netIoState.getRxpck() && netIoState.getRxpck() > maxval) {
                    maxval = netIoState.getRxpck();
                }
                if (null != netIoState.getTxpck() && netIoState.getTxpck() > maxval) {
                    maxval = netIoState.getTxpck();
                }
 
            }
        }
        if (maxval == 0) {
            maxval = 1;
        }
        return Math.ceil(maxval);
    }
 
}