付延余
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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
package com.wgcloud.service;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.wgcloud.config.CommonConfig;
import com.wgcloud.config.MailConfig;
import com.wgcloud.dto.ChartInfo;
import com.wgcloud.dto.MessageDto;
import com.wgcloud.entity.*;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.FormatUtil;
import com.wgcloud.util.HostUtil;
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.ui.Model;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
/**
 * @version v3.3
 * @ClassName:DashboardService.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: 监控概要页面信息管理
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Service
public class DashboardService {
 
    private static final Logger logger = LoggerFactory.getLogger(DashboardService.class);
 
    @Resource
    DbTableService dbTableService;
    @Resource
    SnmpInfoService snmpInfoService;
    @Resource
    FileSafeService fileSafeService;
    @Resource
    DceInfoService dceInfoService;
    @Resource
    DbInfoService dbInfoService;
    @Resource
    SystemInfoService systemInfoService;
    @Resource
    AppInfoService appInfoService;
    @Resource
    FileWarnInfoService fileWarnInfoService;
    @Resource
    FileWarnStateService fileWarnStateService;
    @Resource
    DockerInfoService dockerInfoService;
    @Resource
    LogInfoService logInfoService;
    @Autowired
    HeathMonitorService heathMonitorService;
    @Autowired
    HostGroupService hostInfoService;
    @Autowired
    CpuStateService cpuStateService;
    @Autowired
    MemStateService memStateService;
    @Autowired
    SysLoadStateService sysLoadStateService;
    @Autowired
    NetIoStateService netIoStateService;
    @Autowired
    DeskIoService deskIoService;
    @Autowired
    DiskStateService diskStateService;
    @Autowired
    DiskSmartService diskSmartService;
    @Resource
    CpuTemperaturesService cpuTemperaturesService;
    @Resource
    private CustomInfoService customInfoService;
    @Resource
    private PortInfoService portInfoService;
    @Autowired
    CommonConfig commonConfig;
    @Autowired
    MailConfig mailConfig;
 
 
    /**
     * 获取从今天开始,往前倒数天数的日期集合
     *
     * @param days
     * @return
     */
    public List<String> getDateList() {
        int days = 30;
        List<String> dateList = new ArrayList<String>();
        String sevenDayBefore = DateUtil.getDateBefore(days);
        for (int i = 0; i < days; i++) {
            sevenDayBefore = DateUtil.getDateBefore(i);
            dateList.add(sevenDayBefore.substring(0, 10));
        }
        return dateList;
    }
 
    /**
     * 获取四个时间段
     *
     * @param days
     * @return
     */
    public List<MessageDto> getAmList() {
        MessageDto dto1 = new MessageDto();
        dto1.setCode("am1");
        dto1.setMsg("最近1小时");
        MessageDto dto2 = new MessageDto();
        dto2.setCode("am2");
        dto2.setMsg("最近2小时");
        MessageDto dto3 = new MessageDto();
        dto3.setCode("am3");
        dto3.setMsg("最近6小时");
        MessageDto dto4 = new MessageDto();
        dto4.setCode("am4");
        dto4.setMsg("最近12小时");
        MessageDto dto5 = new MessageDto();
        dto5.setCode("am5");
        dto5.setMsg("最近24小时");
        List<MessageDto> timeList = new ArrayList<MessageDto>();
        timeList.add(dto1);
        timeList.add(dto2);
        timeList.add(dto3);
        timeList.add(dto4);
        timeList.add(dto5);
        return timeList;
    }
 
 
    /**
     * 查看图表时候,组装日期查询条件
     *
     * @param am        最近1小时,2小时,6小时,12小时,24小时
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param params    查询map
     * @param model     回显页面存贮
     */
    public void setDateParam(String am, String startTime, String endTime, Map<String, Object> params, Model model) {
        if ("null".equals(am)) {
            am = "";
        }
        try {
            String nowTime = DateUtil.getCurrentDateTime();
            if (!StringUtils.isEmpty(am) && (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime))) {
                if ("am1".equals(am)) {
                    params.put(StaticKeys.SEARCH_START_TIME, DateUtil.beforeHourToNowDate(1));
                    params.put(StaticKeys.SEARCH_END_TIME, nowTime);
                }
                if ("am2".equals(am)) {
                    params.put(StaticKeys.SEARCH_START_TIME, DateUtil.beforeHourToNowDate(2));
                    params.put(StaticKeys.SEARCH_END_TIME, nowTime);
                }
                if ("am3".equals(am)) {
                    params.put(StaticKeys.SEARCH_START_TIME, DateUtil.beforeHourToNowDate(6));
                    params.put(StaticKeys.SEARCH_END_TIME, nowTime);
                }
                if ("am4".equals(am)) {
                    params.put(StaticKeys.SEARCH_START_TIME, DateUtil.beforeHourToNowDate(12));
                    params.put(StaticKeys.SEARCH_END_TIME, nowTime);
                }
                if ("am5".equals(am)) {
                    params.put(StaticKeys.SEARCH_START_TIME, DateUtil.beforeHourToNowDate(24));
                    params.put(StaticKeys.SEARCH_END_TIME, nowTime);
                }
                model.addAttribute("am", am);
                return;
            }
            if (StringUtils.isEmpty(am) && !StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
                params.put(StaticKeys.SEARCH_START_TIME, startTime + ":00");
 
                params.put(StaticKeys.SEARCH_END_TIME, endTime + ":59");
                model.addAttribute("startTime", startTime);
                model.addAttribute("endTime", endTime);
                return;
            }
            params.put(StaticKeys.SEARCH_START_TIME, DateUtil.beforeHourToNowDate(1));
            params.put(StaticKeys.SEARCH_END_TIME, nowTime);
            model.addAttribute("am", "am1");
        } catch (Exception e) {
            logger.error("查看图表组装日期查询条件错误", e);
        }
    }
 
    /**
     * 查看详细信息监控时候,组装日期查询条件
     *
     * @param params
     * @param date
     */
    public void setDateParam(String date, Map<String, Object> params) {
        params.put(StaticKeys.SEARCH_START_TIME, date + " 00:00:00");
        params.put(StaticKeys.SEARCH_END_TIME, date + " 23:59:59");
    }
 
    /**
     * 设置监控概要页面环图
     *
     * @param model
     * @param totalSystemInfoSize
     */
    public void setHuantu(Model model, int totalSystemInfoSize, int totalSizeApp, int dockerSize, int portSize, int dceSize, int fileWarnSize,
                          int dbInfoSize, int heathSize, HttpServletRequest request) throws Exception {
        //校验是否添加过滤用户查询条件
        Map<String, Object> params = new HashMap<String, Object>();
        HostUtil.addAccountquery(request, params);
        int dbTableSize = dbTableService.countByParams(params);
        int snmpInfoSize = snmpInfoService.countByParams(params);
 
 
        int huantuTotalSize = totalSystemInfoSize + totalSizeApp + dockerSize + portSize + dceSize + fileWarnSize + dbInfoSize + heathSize + dbTableSize + snmpInfoSize;
        //防止除以0的bug,所以新增一个total变量
        double total = huantuTotalSize;
        if (0 == total) {
            total = 1d;
        }
        List<ChartInfo> chartInfos = new ArrayList<>();
        double totalSystemInfoSizePercent = totalSystemInfoSize / total;
        ChartInfo totalSystemInfoChart = new ChartInfo();
        totalSystemInfoChart.setCount(totalSystemInfoSize);
        totalSystemInfoChart.setItem("主机");
        totalSystemInfoChart.setPercent(FormatUtil.formatDouble(totalSystemInfoSizePercent, 2));
        if (totalSystemInfoSize > 0) {
            chartInfos.add(totalSystemInfoChart);
        }
 
        double totalSizeAppPercent = totalSizeApp / total;
        ChartInfo totalSizeAppChart = new ChartInfo();
        totalSizeAppChart.setCount(totalSizeApp);
        totalSizeAppChart.setItem("进程");
        totalSizeAppChart.setPercent(FormatUtil.formatDouble(totalSizeAppPercent, 2));
        if (totalSizeApp > 0) {
            chartInfos.add(totalSizeAppChart);
        }
 
        double dockerSizePercent = dockerSize / total;
        ChartInfo dockerSizeChart = new ChartInfo();
        dockerSizeChart.setCount(dockerSize);
        dockerSizeChart.setItem("DOCKER");
        dockerSizeChart.setPercent(FormatUtil.formatDouble(dockerSizePercent, 2));
        if (dockerSize > 0) {
            chartInfos.add(dockerSizeChart);
        }
 
        double portSizePercent = portSize / total;
        ChartInfo portSizeChart = new ChartInfo();
        portSizeChart.setCount(portSize);
        portSizeChart.setItem("端口");
        portSizeChart.setPercent(FormatUtil.formatDouble(portSizePercent, 2));
        if (portSize > 0) {
            chartInfos.add(portSizeChart);
        }
 
        double dceSizePercent = dceSize / total;
        ChartInfo dceSizeChart = new ChartInfo();
        dceSizeChart.setCount(dceSize);
        dceSizeChart.setItem("数通PING");
        dceSizeChart.setPercent(FormatUtil.formatDouble(dceSizePercent, 2));
        if (dceSize > 0) {
            chartInfos.add(dceSizeChart);
        }
 
        double snmpInfoPercent = snmpInfoSize / total;
        ChartInfo snmpInfoChart = new ChartInfo();
        snmpInfoChart.setCount(snmpInfoSize);
        snmpInfoChart.setItem("数通SNMP");
        snmpInfoChart.setPercent(FormatUtil.formatDouble(snmpInfoPercent, 2));
        if (snmpInfoSize > 0) {
            chartInfos.add(snmpInfoChart);
        }
 
        double fileWarnSizePercent = fileWarnSize / total;
        ChartInfo fileWarnSizeChart = new ChartInfo();
        fileWarnSizeChart.setCount(fileWarnSize);
        fileWarnSizeChart.setItem("日志");
        fileWarnSizeChart.setPercent(FormatUtil.formatDouble(fileWarnSizePercent, 2));
        if (fileWarnSize > 0) {
            chartInfos.add(fileWarnSizeChart);
        }
 
        double dbInfoSizePercent = dbInfoSize / total;
        ChartInfo dbInfoSizeChart = new ChartInfo();
        dbInfoSizeChart.setCount(dbInfoSize);
        dbInfoSizeChart.setItem("数据源");
        dbInfoSizeChart.setPercent(FormatUtil.formatDouble(dbInfoSizePercent, 2));
        if (dbInfoSize > 0) {
            chartInfos.add(dbInfoSizeChart);
        }
 
        double heathSizePercent = heathSize / total;
        ChartInfo heathSizeChart = new ChartInfo();
        heathSizeChart.setCount(heathSize);
        heathSizeChart.setItem("服务接口");
        heathSizeChart.setPercent(FormatUtil.formatDouble(heathSizePercent, 2));
        if (heathSize > 0) {
            chartInfos.add(heathSizeChart);
        }
 
        double dbTableSizePercent = dbTableSize / total;
        ChartInfo dbTableSizeChart = new ChartInfo();
        dbTableSizeChart.setCount(dbTableSize);
        dbTableSizeChart.setItem("数据表");
        dbTableSizeChart.setPercent(FormatUtil.formatDouble(dbTableSizePercent, 2));
        if (dbTableSize > 0) {
            chartInfos.add(dbTableSizeChart);
        }
 
        model.addAttribute("huantuChartInfos", JSONUtil.parseArray(chartInfos));
        model.addAttribute("huantuTotalSize", huantuTotalSize);
    }
 
    /**
     * 设置监控概要页面腰线和波浪图
     *
     * @param model
     * @param totalSystemInfoSize
     */
    public void setMiddleData(Model model, HttpServletRequest request) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        List<SystemInfo> list = systemInfoService.selectAllByParams(params);
        //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;
 
        //下行传输速率最高
        Double maxRxbyt = 0d;
        //下行传输速率平均
        Double avgRxbyt = 0d;
        //下行传输速率最低
        Double minRxbyt = 1000d;
        Double sumRxbyt = 0d;
 
        //上行传输速率最高
        Double maxTxbyt = 0d;
        //上行传输速率最低
        Double minTxbyt = 1000d;
        //上行传输速率平均
        Double avgTxbyt = 0d;
        Double sumTxbyt = 0d;
 
        //5分钟负载最高
        Double maxFiveLoad = 0d;
        //5分钟负载平均
        Double avgFiveLoad = 0d;
        //5分钟负载最低
        Double minFiveLoad = 1000d;
        Double sumFiveLoad = 0d;
 
        //15分钟负载最高
        Double maxFifteenLoad = 0d;
        //15分钟负载最低
        Double minFifteenLoad = 1000d;
        //15分钟负载平均
        Double avgFifteenLoad = 0d;
        Double sumFifteenLoad = 0d;
 
        //主机数量
        int systemSize = 0;
 
        //cpu总核数
        int cpuCoresSum = 0;
        //内存总和
        double memSum = 0;
        //上报时间总和
        int submitSecondsSum = 0;
        //主机平均上报频率
        double avgSubmitSeconds = 120;
 
        for (SystemInfo systemInfo : list) {
//            if(StaticKeys.DOWN_STATE.equals(systemInfo.getState())){
//                continue;
//            }
            systemSize += 1;
            //组装cpu使用率 begin
            if (null != systemInfo.getCpuPer()) {
                if (systemInfo.getCpuPer() > maxCpu) {
                    maxCpu = systemInfo.getCpuPer();
                }
                if (systemInfo.getCpuPer() < minCpu) {
                    minCpu = systemInfo.getCpuPer();
                }
                sumCpu += systemInfo.getCpuPer();
            }
            //组装cpu使用率 end
 
            //组装内存使用率 begin
            if (null != systemInfo.getMemPer()) {
                if (systemInfo.getMemPer() > maxMem) {
                    maxMem = systemInfo.getMemPer();
                }
                if (systemInfo.getMemPer() < minMem) {
                    minMem = systemInfo.getMemPer();
                }
                sumMem += systemInfo.getMemPer();
            }
            //组装内存使用率 end
 
            //组装下行传输速率 begin
            if (null != systemInfo.getRxbyt()) {
                if (Double.valueOf(systemInfo.getRxbyt()) > maxRxbyt) {
                    maxRxbyt = Double.valueOf(systemInfo.getRxbyt());
                }
                if (Double.valueOf(systemInfo.getRxbyt()) < minRxbyt) {
                    minRxbyt = Double.valueOf(systemInfo.getRxbyt());
                }
                sumRxbyt += Double.valueOf(systemInfo.getRxbyt());
            }
            //组装下行传输速率 end
 
            //组装上行传输速率 begin
            if (null != systemInfo.getTxbyt()) {
                if (Double.valueOf(systemInfo.getTxbyt()) > maxTxbyt) {
                    maxTxbyt = Double.valueOf(systemInfo.getTxbyt());
                }
                if (Double.valueOf(systemInfo.getTxbyt()) < minTxbyt) {
                    minTxbyt = Double.valueOf(systemInfo.getTxbyt());
                }
                sumTxbyt += Double.valueOf(systemInfo.getTxbyt());
            }
            //组装上行传输速率 end
 
            //组装5分钟负载 begin
            if (null != systemInfo.getFiveLoad()) {
                if (Double.valueOf(systemInfo.getFiveLoad()) > maxFiveLoad) {
                    maxFiveLoad = Double.valueOf(systemInfo.getFiveLoad());
                }
                if (Double.valueOf(systemInfo.getFiveLoad()) < minFiveLoad) {
                    minFiveLoad = Double.valueOf(systemInfo.getFiveLoad());
                }
                sumFiveLoad += Double.valueOf(systemInfo.getFiveLoad());
            }
            //组装5分钟负载 end
 
            //组装15分钟负载 begin
            if (null != systemInfo.getFifteenLoad()) {
                if (Double.valueOf(systemInfo.getFifteenLoad()) > maxFifteenLoad) {
                    maxFifteenLoad = Double.valueOf(systemInfo.getFifteenLoad());
                }
                if (Double.valueOf(systemInfo.getFifteenLoad()) < minFifteenLoad) {
                    minFifteenLoad = Double.valueOf(systemInfo.getFifteenLoad());
                }
                sumFifteenLoad += Double.valueOf(systemInfo.getFifteenLoad());
            }
            //组装15分钟负载 end
 
            //组装cpu总核数 begin
            try {
                cpuCoresSum += Integer.valueOf(systemInfo.getCpuCoreNum());
            } catch (Exception e) {
                logger.error("统计所有主机总核数错误", e);
            }
            //组装cpu总核数 end
 
            //组装总内存 begin
            try {
                memSum += Double.valueOf(systemInfo.getTotalMem().replace("G", ""));
            } catch (Exception e) {
                logger.error("统计所有主机总内存错误", e);
            }
            //组装总内存 end
 
            //组装上报数据频率总和 begin
            try {
                submitSecondsSum += Integer.valueOf(systemInfo.getSubmitSeconds());
            } catch (Exception e) {
                logger.error("统计所有主机组装上报数据频率总和错误", e);
            }
            //组装上报数据频率总和 end
 
        }
 
        if (systemSize > 0) {
            avgCpu = sumCpu / list.size();
            avgMem = sumMem / list.size();
            avgRxbyt = sumRxbyt / list.size();
            avgTxbyt = sumTxbyt / list.size();
            avgFifteenLoad = sumFifteenLoad / list.size();
            avgFiveLoad = sumFiveLoad / list.size();
            avgSubmitSeconds = submitSecondsSum / (double) list.size();
        } else {
            minCpu = 0d;
            minMem = 0d;
            minRxbyt = 0d;
            minTxbyt = 0d;
            avgFifteenLoad = 0d;
            avgFiveLoad = 0d;
        }
 
        model.addAttribute("cpuCoresSum", cpuCoresSum);
        model.addAttribute("memSum", FormatUtil.formatDouble(memSum, 2));
        model.addAttribute("avgSubmitSeconds", FormatUtil.formatDouble(avgSubmitSeconds, 2));
 
        //最近5分钟所有主机cpu状态 begin
        List cpuInfoList = new ArrayList();
        ChartInfo maxCpuInfo = new ChartInfo();
        maxCpuInfo.setValue(maxCpu);
        maxCpuInfo.setItem("CPU%最高");
        cpuInfoList.add(maxCpuInfo);
        ChartInfo avgCpuInfo = new ChartInfo();
        avgCpuInfo.setValue(FormatUtil.formatDouble(avgCpu, 2));
        avgCpuInfo.setItem("CPU%平均");
        cpuInfoList.add(avgCpuInfo);
        ChartInfo minCpuInfo = new ChartInfo();
        minCpuInfo.setValue(minCpu);
        minCpuInfo.setItem("CPU%最低");
        cpuInfoList.add(minCpuInfo);
        model.addAttribute("cpuInfoList", cpuInfoList);
        //最近5分钟所有主机cpu状态 end
 
        //最近5分钟所有主机内存状态 begin
        List memInfoList = new ArrayList();
        ChartInfo maxMemInfo = new ChartInfo();
        maxMemInfo.setValue(maxMem);
        maxMemInfo.setItem("内存%最高");
        memInfoList.add(maxMemInfo);
        ChartInfo avgMemInfo = new ChartInfo();
        avgMemInfo.setValue(FormatUtil.formatDouble(avgMem, 2));
        avgMemInfo.setItem("内存%平均");
        memInfoList.add(avgMemInfo);
        ChartInfo minMemInfo = new ChartInfo();
        minMemInfo.setValue(minMem);
        minMemInfo.setItem("内存%最低");
        memInfoList.add(minMemInfo);
        model.addAttribute("memInfoList", memInfoList);
        //最近5分钟所有主机内存状态 end
 
        model.addAttribute("maxRxbyt", FormatUtil.kbToM(maxRxbyt + "") + "/s");
        model.addAttribute("minRxbyt", FormatUtil.kbToM(minRxbyt + "") + "/s");
        model.addAttribute("avgRxbyt", FormatUtil.kbToM(FormatUtil.formatDouble(avgRxbyt, 2) + "") + "/s");
        model.addAttribute("maxTxbyt", FormatUtil.kbToM(maxTxbyt + "") + "/s");
        model.addAttribute("minTxbyt", FormatUtil.kbToM(minTxbyt + "") + "/s");
        model.addAttribute("avgTxbyt", FormatUtil.kbToM(FormatUtil.formatDouble(avgTxbyt, 2) + "") + "/s");
 
        model.addAttribute("maxFiveLoad", maxFiveLoad);
        model.addAttribute("minFiveLoad", minFiveLoad);
        model.addAttribute("avgFiveLoad", FormatUtil.formatDouble(avgFiveLoad, 2));
        model.addAttribute("maxFifteenLoad", maxFifteenLoad);
        model.addAttribute("minFifteenLoad", minFifteenLoad);
        model.addAttribute("avgFifteenLoad", FormatUtil.formatDouble(avgFifteenLoad, 2));
 
    }
 
    /**
     * 设置监控概要页面的内存和cpu饼图
     *
     * @param model
     * @param totalSystemInfoSize
     */
    public void setPieChart(Model model, int totalSystemInfoSize, HttpServletRequest request) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        List<ChartInfo> chartInfoList1 = new ArrayList<ChartInfo>();
        List<ChartInfo> chartInfoList2 = new ArrayList<ChartInfo>();
 
        //内存>=95% begin
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        params.put("memPer", 95);
        int memPerSize_95 = systemInfoService.countByParams(params);
        double a = 0;
        if (totalSystemInfoSize != 0) {
            a = (double) memPerSize_95 / totalSystemInfoSize;
        }
        ChartInfo memPerSize_95_chart = new ChartInfo();
        memPerSize_95_chart.setItem("内存>=95%");
        memPerSize_95_chart.setCount(memPerSize_95);
        memPerSize_95_chart.setPercent(FormatUtil.formatDouble(a, 2));
        chartInfoList1.add(memPerSize_95_chart);
        params.clear();
        //内存>=95% begin
 
 
        //内存>=90%且<95% begin
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        params.put("memPer", 90);
        params.put("memPerLe", 95);
        int memPerSize_90_95 = systemInfoService.countByParams(params);
        double a_90_95 = 0;
        if (totalSystemInfoSize != 0) {
            a_90_95 = (double) memPerSize_90_95 / totalSystemInfoSize;
        }
        ChartInfo memPerSize_90_95_chart = new ChartInfo();
        memPerSize_90_95_chart.setItem("内存>=90%且<95%");
        memPerSize_90_95_chart.setCount(memPerSize_90_95);
        memPerSize_90_95_chart.setPercent(FormatUtil.formatDouble(a_90_95, 2));
        chartInfoList1.add(memPerSize_90_95_chart);
        params.clear();
        //内存>=90%且<95% begin
 
        //内存>=50%且<90% begin
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        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));
        chartInfoList1.add(memPerSize_50_90_chart);
        params.clear();
        //内存>=50%且<90% end
 
 
        //内存<50% begin
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        params.put("memPerLe", 50);
        int memPerSize_50 = systemInfoService.countByParams(params);
        double bb = 0;
        if (totalSystemInfoSize != 0) {
            bb = (double) memPerSize_50 / totalSystemInfoSize;
        }
        ChartInfo memPerSize_50_chart = new ChartInfo();
        memPerSize_50_chart.setItem("内存<50%");
        memPerSize_50_chart.setCount(memPerSize_50);
        memPerSize_50_chart.setPercent(FormatUtil.formatDouble(bb, 2));
        chartInfoList1.add(memPerSize_50_chart);
        params.clear();
        //内存<50% end
 
        model.addAttribute("chartInfoList1", JSONUtil.parseArray(chartInfoList1));
 
 
        //CPU>=95%" begin
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        params.put("cpuPer", 95);
        int cpuPerSize_95 = systemInfoService.countByParams(params);
        double c_95 = 0;
        if (totalSystemInfoSize != 0) {
            c_95 = (double) cpuPerSize_95 / totalSystemInfoSize;
        }
        ChartInfo cpuPerSize_95_chart = new ChartInfo();
        cpuPerSize_95_chart.setItem("CPU>=95%");
        cpuPerSize_95_chart.setCount(cpuPerSize_95);
        cpuPerSize_95_chart.setPercent(FormatUtil.formatDouble(c_95, 2));
        chartInfoList2.add(cpuPerSize_95_chart);
        params.clear();
        //CPU>=95%" end
 
        //CPU>=90%且<95% begin
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        params.put("cpuPer", 90);
        params.put("cpuPerLe", 95);
        int cpuPerSize_90_95 = systemInfoService.countByParams(params);
        double c_90_95 = 0;
        if (totalSystemInfoSize != 0) {
            c_90_95 = (double) cpuPerSize_90_95 / totalSystemInfoSize;
        }
        ChartInfo cpuPerSize_90_95_chart = new ChartInfo();
        cpuPerSize_90_95_chart.setItem("CPU>=90%且<95%");
        cpuPerSize_90_95_chart.setCount(cpuPerSize_90_95);
        cpuPerSize_90_95_chart.setPercent(FormatUtil.formatDouble(c_90_95, 2));
        chartInfoList2.add(cpuPerSize_90_95_chart);
        params.clear();
        //CPU>=90%且<95%  end
 
 
        //CPU>=50%且<90% begin
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        params.put("cpuPerLe", 90);
        params.put("cpuPer", 50);
        int perSize_50_90 = systemInfoService.countByParams(params);
        double e = 0;
        if (totalSystemInfoSize != 0) {
            e = (double) perSize_50_90 / totalSystemInfoSize;
        }
        ChartInfo perSize_50_90_chart = new ChartInfo();
        perSize_50_90_chart.setItem("CPU>=50%且<90%");
        perSize_50_90_chart.setCount(perSize_50_90);
        perSize_50_90_chart.setPercent(FormatUtil.formatDouble(e, 2));
        chartInfoList2.add(perSize_50_90_chart);
        params.clear();
        //CPU>=50%且<90% end
 
        //CPU<50% begin
        //校验是否添加过滤用户查询条件
        HostUtil.addAccountquery(request, params);
        params.put("cpuPerLe", 50);
        int perSize_50 = systemInfoService.countByParams(params);
        double f = 0;
        if (totalSystemInfoSize != 0) {
            f = (double) perSize_50 / totalSystemInfoSize;
        }
        ChartInfo perSize_50_chart = new ChartInfo();
        perSize_50_chart.setItem("CPU<50%");
        perSize_50_chart.setCount(perSize_50);
        perSize_50_chart.setPercent(FormatUtil.formatDouble(f, 2));
        chartInfoList2.add(perSize_50_chart);
        params.clear();
        //CPU<50% end
 
        model.addAttribute("chartInfoList2", JSONUtil.parseArray(chartInfoList2));
        params.clear();
 
    }
 
 
    /**
     * 设置监控概要页面,系统核心参数信息
     *
     * @param model
     * @param model
     */
    public void setMiddleApplicationData(Model model) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        //告警日志数量
        params.put("hostname", "告警");
        model.addAttribute("warnCount", logInfoService.countByParams(params));
 
        //历史趋势图数据保留天数
        model.addAttribute("historyDataOut", commonConfig.getHistoryDataOut());
 
        //告警方式
        String warnType = "未设置";
        if (StaticKeys.mailSet != null && !StringUtils.isEmpty(mailConfig.getWarnScript())) {
            warnType = "邮件+脚本";
        }
        if (StaticKeys.mailSet == null && !StringUtils.isEmpty(mailConfig.getWarnScript())) {
            warnType = "脚本";
        }
        if (StaticKeys.mailSet != null && StringUtils.isEmpty(mailConfig.getWarnScript())) {
            warnType = "邮件";
        }
        model.addAttribute("warnType", warnType);
 
        //公众看板服务是否开启
        String dapingView = "已关闭";
        if (StaticKeys.TRUE_VAL.equals(commonConfig.getDashView())) {
            dapingView = "已开启";
        }
        model.addAttribute("dapingView", dapingView);
 
        //管理员默认密码是否已修改
        String adminPwd = "默认";
        if (!"111111".equals(commonConfig.getAccountPwd())) {
            adminPwd = "已修改";
        }
        model.addAttribute("adminPwd", adminPwd);
 
        //只读账号默认密码是否已修改
        String guestAccountPwd = "默认";
        if (!"111111".equals(commonConfig.getGuestAccountPwd())) {
            guestAccountPwd = "已修改";
        }
        model.addAttribute("guestAccountPwd", guestAccountPwd);
 
        //通信token是否已修改
        String wgToken = "默认";
        if (!"wgcloud".equals(commonConfig.getWgToken())) {
            wgToken = "已修改";
        }
        model.addAttribute("wgToken", wgToken);
 
        //webSSH是否开启
        String webSSH = "已关闭";
        if (StaticKeys.TRUE_VAL.equals(commonConfig.getWebSsh())) {
            webSSH = "已开启";
        }
        model.addAttribute("webSSH", webSSH);
    }
 
    /**
     * 给主机画像
     *
     * @param model
     * @param systemInfoId
     */
    public void hostDraw(Model model, String systemInfoId) {
        try {
            SystemInfo systemInfo = systemInfoService.selectById(systemInfoId);
            String hostname = systemInfo.getHostname();
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("hostname", hostname);
            model.addAttribute("systemInfo", systemInfo);
            //监控进程总数量
            int totalAppSize = appInfoService.countByParams(params);
            model.addAttribute("totalAppSize", totalAppSize);
            //监控端口总数量
            int totalPortSize = portInfoService.countByParams(params);
            model.addAttribute("totalPortSize", totalPortSize);
 
            //监控进程下线数量
            params.put("state", StaticKeys.DOWN_STATE);
            int downAppSize = appInfoService.countByParams(params);
            model.addAttribute("downAppSize", downAppSize);
            //监控端口下线数量
            int downPortSize = portInfoService.countByParams(params);
            model.addAttribute("downPortSize", downPortSize);
 
            //最近监控周期天cpu使用率最大
            Double maxCpuState = cpuStateService.selectMaxByHostname(hostname);
            model.addAttribute("maxCpuState", maxCpuState);
            //最近监控周期天mem使用率最大
            Double maxMemState = memStateService.selectMaxByHostname(hostname);
            model.addAttribute("maxMemState", maxMemState);
 
            //多少天清空一次历史数据
            model.addAttribute("historyDataOut", commonConfig.getHistoryDataOut());
 
            params.clear();
            params.put("hostname", hostname);
            PageInfo pageInfoLoad = sysLoadStateService.selectByParams(params, 1, 1);
            if (!CollectionUtil.isEmpty(pageInfoLoad.getList())) {
                //最新一条系统负载
                model.addAttribute("sysLoadState", pageInfoLoad.getList().get(0));
            } else {
                //最新一条系统负载
                model.addAttribute("sysLoadState", new SysLoadState());
            }
            SysLoadState sysLoadStateMax = sysLoadStateService.selectMaxByHostname(hostname);
            if (null == sysLoadStateMax) {
                sysLoadStateMax = new SysLoadState();
            }
            //1分钟负载历史最高
            model.addAttribute("maxOneLoad", sysLoadStateMax.getOneLoad());
            //5分钟负载历史最高
            model.addAttribute("maxFiveLoad", sysLoadStateMax.getFiveLoad());
            //15分钟负载历史最高
            model.addAttribute("maxFifteenLoad", sysLoadStateMax.getFifteenLoad());
 
            //最新一条网络流量数据
            PageInfo pageInfoNetIo = netIoStateService.selectByParams(params, 1, 1);
            NetIoState netIoStateNew = new NetIoState();
            if (!CollectionUtil.isEmpty(pageInfoNetIo.getList())) {
                netIoStateNew = (NetIoState) pageInfoNetIo.getList().get(0);
                netIoStateNew.setRxbyt(FormatUtil.kbToM(netIoStateNew.getRxbyt()) + "/s");
                netIoStateNew.setTxbyt(FormatUtil.kbToM(netIoStateNew.getTxbyt()) + "/s");
                model.addAttribute("netIoState", netIoStateNew);
            } else {
                //最新一条网络流量
                model.addAttribute("netIoState", netIoStateNew);
            }
 
 
            NetIoState netIoStateMax = netIoStateService.selectMaxByHostname(hostname);
            if (null == netIoStateMax) {
                netIoStateMax = new NetIoState();
            }
            //接受字节历史最高
            model.addAttribute("maxRxbyt", FormatUtil.kbToM(netIoStateMax.getRxbyt()) + "/s");
            //发送字节历史最高
            model.addAttribute("maxTxbyt", FormatUtil.kbToM(netIoStateMax.getTxbyt()) + "/s");
            //接受包历史最高
            model.addAttribute("maxRxpck", netIoStateMax.getRxpck());
            //发送包历史最高
            model.addAttribute("maxTxpck", netIoStateMax.getTxpck());
 
            //主机下线次数
            params.clear();
            params.put("hostname", "主机下线告警:" + hostname);
            int hostDownSize = logInfoService.countByParams(params);
            model.addAttribute("hostDownSize", hostDownSize);
 
            //所有监控进程
            params.clear();
            params.put("hostname", hostname);
            List<AppInfo> appInfoList = appInfoService.selectAllByParams(params);
            for (AppInfo appInfo1 : appInfoList) {
                appInfo1.setWritesBytes(FormatUtil.mToG(appInfo1.getWritesBytes()));
                appInfo1.setReadBytes(FormatUtil.mToG(appInfo1.getReadBytes()));
            }
            //设置分组
            appInfoService.setGroupInList(appInfoList);
            model.addAttribute("appInfoList", appInfoList);
 
            //所有监控端口
            List<PortInfo> portInfoList = portInfoService.selectAllByParams(params);
            portInfoService.setGroupInList(portInfoList);
            model.addAttribute("portInfoList", portInfoList);
 
            //所有监控防篡改文件
            List<FileSafe> fileSafeInfoList = fileSafeService.selectAllByParams(params);
            model.addAttribute("fileSafeInfoList", fileSafeInfoList);
 
            //所有日志文件监控
            List<FileWarnInfo> fileWarnInfoList = fileWarnInfoService.selectAllByParams(params);
            for (FileWarnInfo info : fileWarnInfoList) {
                params.put("fileWarnId", info.getId());
                Integer count = fileWarnStateService.countByParams(params);
                info.setWarnDatas(count + "");
            }
            model.addAttribute("fileWarnInfoList", fileWarnInfoList);
 
            params.clear();
            params.put("hostname", hostname);
            //所有监控docker
            List<DockerInfo> dockerInfoList = dockerInfoService.selectAllByParams(params);
            dockerInfoService.setGroupInList(dockerInfoList);
            model.addAttribute("dockerInfoList", dockerInfoList);
 
 
            //磁盘容量信息
            List<DiskState> diskStateList = diskStateService.selectAllByParams(params);
            model.addAttribute("deskStateList", diskStateList);
 
            //磁盘IO信息
            List<DeskIo> deskIoList = deskIoService.selectAllByParams(params);
            model.addAttribute("deskIoList", deskIoList);
 
            //磁盘smart信息
            List<DiskSmart> diskSmartList = diskSmartService.selectAllByParams(params);
            model.addAttribute("diskSmartList", diskSmartList);
 
            //cpu温度
            List<CpuTemperatures> cpuTemperaturesList = cpuTemperaturesService.selectAllByParams(params);
            model.addAttribute("cpuTemperaturesList", cpuTemperaturesList);
 
            //自定义监控项列表
            List<CustomInfo> customInfoList = customInfoService.selectAllByParams(params);
            model.addAttribute("customInfoList", customInfoList);
 
 
        } catch (Exception e) {
            logger.error("主机画像错误", e);
            logInfoService.save("主机画像错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
    }
 
}