付延余
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
package com.wgcloud.util.msg;
 
import com.wgcloud.common.ApplicationContextHelper;
import com.wgcloud.config.CommonConfig;
import com.wgcloud.config.MailConfig;
import com.wgcloud.dto.HostWarnDiyDto;
import com.wgcloud.entity.*;
import com.wgcloud.service.LogInfoService;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.ExecUtil;
import com.wgcloud.util.FormatUtil;
import com.wgcloud.util.HostUtil;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
 
import java.util.Date;
 
/**
 * @version v3.3
 * @ClassName:WarnMailUtil.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: 告警通知发送工具类
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
public class WarnMailUtil {
 
    private static final Logger logger = LoggerFactory.getLogger(WarnMailUtil.class);
 
    private static CommonConfig commonConfig = (CommonConfig) ApplicationContextHelper.getBean(CommonConfig.class);
 
    private static LogInfoService logInfoService = (LogInfoService) ApplicationContextHelper.getBean(LogInfoService.class);
    private static MailConfig mailConfig = (MailConfig) ApplicationContextHelper.getBean(MailConfig.class);
 
    /**
     * 主机通用指标告警前置处理,验证完成返回true,否则返回false不发告警
     *
     * @param hostname 主机ip
     * @param warnMail 是否告警
     * @param warnKey  告警缓存key
     * @return
     */
    private static boolean preWarnInit(String hostname, String warnMail, String warnKey) {
        //是否在屏蔽ip告警列表里
        if (!StringUtils.isEmpty(mailConfig.getBlockIps()) && mailConfig.getBlockIps().contains(hostname)) {
            return false;
        }
        //是否设置不告警
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(warnMail)) {
            return false;
        }
        //是否在告警缓存里
        if (WarnPools.isExpireWarnTime(warnKey, commonConfig.getWarnCacheTimes())) {
            return false;
        }
        return true;
    }
 
    /**
     * 判断系统内存使用率是否超过告警值,超过则发送告警邮件
     *
     * @param memState
     * @param toMail
     * @return
     */
    public static boolean sendWarnInfo(MemState memState) {
        String key = memState.getHostname() + "_mem";
        boolean sign = preWarnInit(memState.getHostname(), mailConfig.getMemWarnMail(), key);
        if (!sign) {
            return false;
        }
        Double memWarnVal = mailConfig.getMemWarnVal();
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(memState.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getMemWarnMail())) {
                return false;
            }
            if (null != hostWarnDiyDto.getMemWarnVal()) {
                memWarnVal = hostWarnDiyDto.getMemWarnVal();
            }
        }
        //是否设置自定义ip告警 end
        if (memState.getUsePer() != null && memState.getUsePer() >= memWarnVal) {
            try {
                String remark = HostUtil.addRemark(memState.getHostname());
                String title = "内存告警:" + memState.getHostname() + remark;
                String commContent = "主机:" + memState.getHostname() + remark + ",当前内存使用率为" + Double.valueOf(memState.getUsePer()) + ",超过告警值" + memWarnVal;
                //发送告警
                String account = getAccount(memState.getHostname());
                sendUtil(title, commContent, account, key, true);
 
            } catch (Exception e) {
                logger.error("发送内存告警邮件错误:", e);
                logInfoService.save("发送内存告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
 
        return false;
    }
 
    /**
     * 判断系统负载值(5分钟)是否超过告警值,超过则发送告警邮件
     *
     * @param sysLoadState
     * @param toMail
     * @return
     */
    public static boolean sendSysLoadWarnInfo(SysLoadState sysLoadState) {
        String key = sysLoadState.getHostname() + "_load";
        boolean sign = preWarnInit(sysLoadState.getHostname(), mailConfig.getSysLoadWarnMail(), key);
        if (!sign) {
            return false;
        }
        Double sysLoadWarnVal = mailConfig.getSysLoadWarnVal();
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(sysLoadState.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getSysLoadWarnMail())) {
                return false;
            }
            if (null != hostWarnDiyDto.getSysLoadWarnVal()) {
                sysLoadWarnVal = hostWarnDiyDto.getSysLoadWarnVal();
            }
        }
        //是否设置自定义ip告警 end
        if (sysLoadState.getFiveLoad() != null && sysLoadState.getFiveLoad() >= sysLoadWarnVal) {
            try {
                String remark = HostUtil.addRemark(sysLoadState.getHostname());
                String title = "系统负载(5分钟)告警:" + sysLoadState.getHostname() + remark;
                String commContent = "主机:" + sysLoadState.getHostname() + remark + ",当前系统负载(5分钟)为" + Double.valueOf(sysLoadState.getFiveLoad()) + ",超过告警值" + sysLoadWarnVal;
                //发送告警
                String account = getAccount(sysLoadState.getHostname());
                sendUtil(title, commContent, account, key, true);
            } catch (Exception e) {
                logger.error("发送系统负载(5分钟)告警邮件错误:", e);
                logInfoService.save("发送系统负载(5分钟)告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
 
        return false;
    }
 
    /**
     * 判断系统cpu使用率是否超过98%,超过则发送告警邮件
     *
     * @param cpuState
     * @param toMail
     * @return
     */
    public static boolean sendCpuWarnInfo(CpuState cpuState) {
        String key = cpuState.getHostname() + "_cpu";
        boolean sign = preWarnInit(cpuState.getHostname(), mailConfig.getCpuWarnMail(), key);
        if (!sign) {
            return false;
        }
        Double cpuWarnVal = mailConfig.getCpuWarnVal();
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(cpuState.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getCpuWarnMail())) {
                return false;
            }
            if (null != hostWarnDiyDto.getCpuWarnVal()) {
                cpuWarnVal = hostWarnDiyDto.getCpuWarnVal();
            }
        }
        //是否设置自定义ip告警 end
        if (cpuState.getSys() != null && cpuState.getSys() >= cpuWarnVal) {
            try {
                String remark = HostUtil.addRemark(cpuState.getHostname());
                String title = "CPU告警:" + cpuState.getHostname() + remark;
                String commContent = "主机:" + cpuState.getHostname() + remark + ",当前CPU使用率为" + Double.valueOf(cpuState.getSys()) + ",超过告警值" + cpuWarnVal;
                //发送告警
                String account = getAccount(cpuState.getHostname());
                sendUtil(title, commContent, account, key, true);
            } catch (Exception e) {
                logger.error("发送内存告警邮件错误:", e);
                logInfoService.save("发送内存告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
 
        return false;
    }
 
    /**
     * 判断主机上行速率bytes sent是否超过告警值,超过则发送告警邮件
     *
     * @param netIoState
     * @param toMail
     * @return
     */
    public static boolean sendUpSpeedWarnInfo(NetIoState netIoState) {
        String key = netIoState.getHostname() + "_txbyt";
        boolean sign = preWarnInit(netIoState.getHostname(), mailConfig.getUpSpeedMail(), key);
        if (!sign) {
            return false;
        }
        Double upSpeedVal = mailConfig.getUpSpeedVal();
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(netIoState.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getUpSpeedMail())) {
                return false;
            }
            if (null != hostWarnDiyDto.getUpSpeedVal()) {
                upSpeedVal = hostWarnDiyDto.getUpSpeedVal();
            }
        }
        //是否设置自定义ip告警 end
        if (!StringUtils.isEmpty(netIoState.getTxbyt()) && Double.valueOf(netIoState.getTxbyt()) >= upSpeedVal) {
            try {
                String remark = HostUtil.addRemark(netIoState.getHostname());
                String title = "上行传输速率告警:" + netIoState.getHostname() + remark;
                String commContent = "主机:" + netIoState.getHostname() + remark + ",当前上行传输速率为" + FormatUtil.kbToM(netIoState.getTxbyt()) + "/s,超过告警值" + upSpeedVal + "KB/s";
                //发送告警
                String account = getAccount(netIoState.getHostname());
                sendUtil(title, commContent, account, key, true);
            } catch (Exception e) {
                logger.error("发送上行传输速率告警邮件错误:", e);
                logInfoService.save("发送上行传输速率告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 判断主机下行传输速率bytes sent是否超过告警值,超过则发送告警邮件
     *
     * @param netIoState
     * @param toMail
     * @return
     */
    public static boolean sendDownSpeedWarnInfo(NetIoState netIoState) {
        String key = netIoState.getHostname() + "_rxbyt";
        boolean sign = preWarnInit(netIoState.getHostname(), mailConfig.getDownSpeedMail(), key);
        if (!sign) {
            return false;
        }
        Double downSpeedVal = mailConfig.getDownSpeedVal();
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(netIoState.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getDownSpeedMail())) {
                return false;
            }
            if (null != hostWarnDiyDto.getDownSpeedVal()) {
                downSpeedVal = hostWarnDiyDto.getDownSpeedVal();
            }
        }
        //是否设置自定义ip告警 end
        if (!StringUtils.isEmpty(netIoState.getRxbyt()) && Double.valueOf(netIoState.getRxbyt()) >= downSpeedVal) {
            try {
                String remark = HostUtil.addRemark(netIoState.getHostname());
                String title = "下行传输速率告警:" + netIoState.getHostname() + remark;
                String commContent = "主机:" + netIoState.getHostname() + remark + ",当前下行传输速率为" + FormatUtil.kbToM(netIoState.getRxbyt()) + "/s,超过告警值" + downSpeedVal + "KB/s";
                //发送告警
                String account = getAccount(netIoState.getHostname());
                sendUtil(title, commContent, account, key, true);
            } catch (Exception e) {
                logger.error("发送下行传输速率告警邮件错误:", e);
                logInfoService.save("发送下行传输速率告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 磁盘smart健康为FAILED,发送告警邮件
     *
     * @param smartBean
     * @param toMail
     * @return
     */
    public static boolean sendDiskSmartWarnInfo(DiskSmart smartBean) {
        String key = smartBean.getHostname() + "_smart";
        boolean sign = preWarnInit(smartBean.getHostname(), mailConfig.getSmartWarnMail(), key);
        if (!sign) {
            return false;
        }
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(smartBean.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getSmartWarnMail())) {
                return false;
            }
        }
        //是否设置自定义ip告警 end
        if (!StringUtils.isEmpty(smartBean.getDiskState()) && StaticKeys.DISK_SMART_FAILED.equals(smartBean.getDiskState())) {
            try {
                String remark = HostUtil.addRemark(smartBean.getHostname());
                String title = "磁盘告警SMART:" + smartBean.getHostname() + remark;
                String commContent = "主机:" + smartBean.getHostname() + remark + ",磁盘" + smartBean.getFileSystem() + ",SMART健康检测结果为" + StaticKeys.DISK_SMART_FAILED;
                //发送告警
                String account = getAccount(smartBean.getHostname());
                sendUtil(title, commContent, account, key, true);
            } catch (Exception e) {
                logger.error("发送磁盘告警SMART邮件错误:", e);
                logInfoService.save("发送磁盘告警SMART邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 判断系统磁盘使用率是否超过设置的告警值,超过则发送告警邮件
     *
     * @param deskState
     * @param toMail
     * @return
     */
    public static boolean sendDiskWarnInfo(DiskState deskState) {
        logger.debug("告警磁盘-------------" + deskState.getFileSystem());
        String key = deskState.getHostname() + "_disk";
        boolean sign = preWarnInit(deskState.getHostname(), mailConfig.getDiskWarnMail(), key);
        if (!sign) {
            return false;
        }
        if (blockDisk(deskState)) {
            return false;
        }
        Double diskWarnVal = mailConfig.getDiskWarnVal();
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(deskState.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getDiskWarnMail())) {
                return false;
            }
            if (null != hostWarnDiyDto.getDiskWarnVal()) {
                diskWarnVal = hostWarnDiyDto.getDiskWarnVal();
            }
        }
        //是否设置自定义ip告警 end
        Double usePer = Double.valueOf(deskState.getUsePer().replace("%", ""));
        if (usePer != null && usePer >= diskWarnVal) {
            try {
                String remark = HostUtil.addRemark(deskState.getHostname());
                String title = "磁盘告警:" + deskState.getHostname() + remark;
                String commContent = "主机磁盘告警:" + deskState.getHostname() + remark + ",磁盘" + deskState.getFileSystem() + "使用率为" + usePer + ",超过告警值" + diskWarnVal;
                //发送告警
                String account = getAccount(deskState.getHostname());
                sendUtil(title, commContent, account, key, true);
            } catch (Exception e) {
                logger.error("发送磁盘告警邮件错误:", e);
                logInfoService.save("发送磁盘告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
 
        return false;
    }
 
    /**
     * 判断是否是不需要告警的磁盘,true是,false否
     *
     * @param deskState
     * @return
     */
    private static boolean blockDisk(DiskState deskState) {
        String diskBlock = mailConfig.getDiskBlock();
        //是否设置自定义ip告警屏蔽磁盘 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(deskState.getHostname());
        if (null != hostWarnDiyDto) {
            if (!StringUtils.isEmpty(hostWarnDiyDto.getDiskBlock())) {
                diskBlock = hostWarnDiyDto.getDiskBlock();
            }
        }
        //是否设置自定义ip告警屏蔽磁盘 end
 
        if (!StringUtils.isEmpty(diskBlock)) {
            String[] blocks = diskBlock.split(",");
            PathMatcher pm = new AntPathMatcher();
            for (String diskBlcok : blocks) {
                diskBlcok = diskBlcok.replace("'", "");
                if ("/".equals(deskState.getFileSystem())) {
                    if (diskBlcok.equals(deskState.getFileSystem())) {
                        return true;
                    }
                } else {
                    boolean matchStart = pm.matchStart(diskBlcok, deskState.getFileSystem());
                    if (matchStart) {
                        return matchStart;
                    }
                }
            }
        }
        return false;
    }
 
    /**
     * 判断系统cpu温度是否超过设置的告警值,超过则发送告警邮件
     *
     * @param cpuTemperatures
     * @return
     */
    public static boolean sendCpuTemperatures(CpuTemperatures cpuTemperatures) {
        String key = cpuTemperatures.getHostname() + "_temperatures";
        boolean sign = preWarnInit(cpuTemperatures.getHostname(), mailConfig.getCpuTemperatureWarnMail(), key);
        if (!sign) {
            return false;
        }
        Double cpuTemperatureWarnVal = mailConfig.getCpuTemperatureWarnVal();
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(cpuTemperatures.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getCpuTemperatureWarnMail())) {
                return false;
            }
            if (null != hostWarnDiyDto.getCpuTemperatureWarnVal()) {
                cpuTemperatureWarnVal = hostWarnDiyDto.getCpuTemperatureWarnVal();
            }
        }
        //是否设置自定义ip告警 end
        Double inputVal = Double.valueOf(cpuTemperatures.getInput().replace("℃", "").replace("+", ""));
        if (inputVal != null && inputVal >= cpuTemperatureWarnVal) {
            try {
                String remark = HostUtil.addRemark(cpuTemperatures.getHostname());
                String title = "CPU温度告警:" + cpuTemperatures.getHostname() + remark;
                String commContent = "主机:" + cpuTemperatures.getHostname() + remark + ",CPU当前温度为" + cpuTemperatures.getInput() + ",超过告警值" + cpuTemperatureWarnVal + "℃";
                //发送告警
                String account = getAccount(cpuTemperatures.getHostname());
                sendUtil(title, commContent, account, key, true);
            } catch (Exception e) {
                logger.error("发送CPU温度告警邮件错误:", e);
                logInfoService.save("发送CPU温度告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
 
        return false;
    }
 
    /**
     * 服务接口不通发送告警邮件
     *
     * @param cpuState
     * @param toMail
     * @return
     */
    public static boolean sendHeathInfo(HeathMonitor heathMonitor, boolean isDown) {
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getHeathWarnMail())) {
            return false;
        }
        String key = heathMonitor.getId();
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                //判断是否达到连续告警次数 begin
                if (WarnPools.HEATH_WARN_COUNT_MAP.get(key) == null) {
                    WarnPools.HEATH_WARN_COUNT_MAP.put(key, 1);
                } else {
                    WarnPools.HEATH_WARN_COUNT_MAP.put(key, WarnPools.HEATH_WARN_COUNT_MAP.get(key) + 1);
                }
                if (WarnPools.HEATH_WARN_COUNT_MAP.get(key) < mailConfig.getHeathWarnCount()) {
                    logger.info(heathMonitor.getAppName() + "---服务接口没有达到告警次数---" + WarnPools.HEATH_WARN_COUNT_MAP.get(key));
                    return false;
                }
                //判断是否达到连续告警次数 end
 
                String title = "服务接口检测告警:" + heathMonitor.getAppName();
                String commContent = "服务接口已连续" + mailConfig.getHeathWarnCount() + "次检测失败:" + heathMonitor.getAppName() + ",url:" + heathMonitor.getHeathUrl() + ",响应状态码为" + heathMonitor.getHeathStatus();
                //发送告警
                sendUtil(title, commContent, heathMonitor.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送服务健康检测告警邮件错误:", e);
                logInfoService.save("发送服务健康检测告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            try {
                String title = "服务接口已恢复:" + heathMonitor.getAppName();
                String commContent = "服务接口已恢复:" + heathMonitor.getAppName() + ",url:" + heathMonitor.getHeathUrl() + ",响应状态码为" + heathMonitor.getHeathStatus() + "";
                //发送告警
                sendUtil(title, commContent, heathMonitor.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送服务接口已恢复邮件错误:", e);
                logInfoService.save("发送服务接口已恢复邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 数通设备ping不通发送告警邮件
     *
     * @param dceInfo
     * @param isDown
     * @return
     */
    public static boolean sendDceInfo(DceInfo dceInfo, boolean isDown) {
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getDceWarnMail())) {
            return false;
        }
        String key = dceInfo.getId();
        String remark = dceInfo.getRemark();
        if (StringUtils.isEmpty(remark)) {
            remark = "";
        } else {
            remark = "(" + dceInfo.getRemark() + ")";
        }
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                //判断是否达到连续告警次数 begin
                if (WarnPools.PING_WARN_COUNT_MAP.get(key) == null) {
                    WarnPools.PING_WARN_COUNT_MAP.put(key, 1);
                } else {
                    WarnPools.PING_WARN_COUNT_MAP.put(key, WarnPools.PING_WARN_COUNT_MAP.get(key) + 1);
                }
                if (WarnPools.PING_WARN_COUNT_MAP.get(key) < mailConfig.getDceWarnCount()) {
                    logger.info(dceInfo.getHostname() + "---数通设备PING超时没有达到告警次数---" + WarnPools.PING_WARN_COUNT_MAP.get(key));
                    return false;
                }
                //判断是否达到连续告警次数 end
 
                String title = "数通设备PING超时告警:" + dceInfo.getHostname() + remark;
                String commContent = "数通设备已连续" + mailConfig.getDceWarnCount() + "次检测失败:" + dceInfo.getHostname() + remark + ",PING超时或失败";
                //发送告警
                sendUtil(title, commContent, dceInfo.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送数通设备PING告警邮件错误:", e);
                logInfoService.save("发送数通设备PING告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            try {
                String title = "数通设备PING已恢复:" + dceInfo.getHostname() + remark;
                String commContent = "数通设备PING已恢复:" + dceInfo.getHostname() + remark + ",响应时间ms:" + dceInfo.getResTimes();
                //发送告警
                sendUtil(title, commContent, dceInfo.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送数通设备PING已恢复邮件错误:", e);
                logInfoService.save("发送数通设备PING已恢复邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * snmp设备监测发送告警邮件
     *
     * @param snmpInfo
     * @param isDown
     * @return
     */
    public static boolean sendSnmpInfo(SnmpInfo snmpInfo, boolean isDown) {
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getSnmpWarnMail())) {
            return false;
        }
        String key = snmpInfo.getId();
        String remark = snmpInfo.getRemark();
        if (StringUtils.isEmpty(remark)) {
            remark = "";
        } else {
            remark = "(" + snmpInfo.getRemark() + ")";
        }
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                String title = "snmp设备监测告警:" + snmpInfo.getHostname() + remark;
                String commContent = "snmp设备:" + snmpInfo.getHostname() + remark + ",可能已下线";
                //发送告警
                sendUtil(title, commContent, snmpInfo.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送snmp设备监测告警邮件错误:", e);
                logInfoService.save("发送snmp设备监测告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            try {
                String title = "snmp设备监测已恢复:" + snmpInfo.getHostname() + remark;
                String commContent = "snmp设备监测已恢复:" + snmpInfo.getHostname() + remark;
                //发送告警
                sendUtil(title, commContent, snmpInfo.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送snmp设备监测已恢复邮件错误:", e);
                logInfoService.save("发送snmp设备监测已恢复邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 主机下线发送告警邮件
     *
     * @param systemInfo 主机信息
     * @param isDown     是否是下线告警,true下线告警,false上线恢复
     * @return
     */
    public static boolean sendHostDown(SystemInfo systemInfo, boolean isDown) {
        if (!StringUtils.isEmpty(mailConfig.getBlockIps()) && mailConfig.getBlockIps().contains(systemInfo.getHostname())) {
            return false;
        }
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getHostDownWarnMail())) {
            return false;
        }
        //是否设置自定义ip告警 begin
        HostWarnDiyDto hostWarnDiyDto = StaticKeys.HOST_WARN_MAP.get(systemInfo.getHostname());
        if (null != hostWarnDiyDto) {
            if (StaticKeys.WARN_NO.equals(hostWarnDiyDto.getHostDownWarnMail())) {
                return false;
            }
        }
        //是否设置自定义ip告警 end
        String key = systemInfo.getId();
        String remark = StaticKeys.HOST_MAP.get(systemInfo.getHostname());
        if (!StringUtils.isEmpty(remark)) {
            remark = "(" + remark + ")";
        } else {
            remark = "";
        }
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                String title = "主机下线告警:" + systemInfo.getHostname() + remark;
                String commContent = "主机可能已下线:" + systemInfo.getHostname() + remark;
                //发送告警
                sendUtil(title, commContent, systemInfo.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送主机下线告警邮件失败:", e);
                logInfoService.save("发送主机下线告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            try {
                String title = "主机已恢复上线:" + systemInfo.getHostname() + remark;
                String commContent = "主机已恢复上线:" + systemInfo.getHostname() + remark;
                //发送告警
                sendUtil(title, commContent, systemInfo.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送主机恢复上线通知邮件错误:", e);
                logInfoService.save("发送主机恢复上线通知邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 进程下线发送告警邮件
     *
     * @param AppInfo 进程信息
     * @param isDown  是否是下线告警,true下线告警,false上线恢复
     * @return
     */
    public static boolean sendAppDown(AppInfo appInfo, boolean isDown) {
        if (!StringUtils.isEmpty(mailConfig.getBlockIps()) && mailConfig.getBlockIps().contains(appInfo.getHostname())) {
            return false;
        }
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getAppDownWarnMail())) {
            return false;
        }
        String key = appInfo.getId();
        String remark = StaticKeys.HOST_MAP.get(appInfo.getHostname());
        if (!StringUtils.isEmpty(remark)) {
            remark = "(" + remark + ")";
        } else {
            remark = "";
        }
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                String title = "进程下线告警:" + appInfo.getAppName() + "," + appInfo.getHostname() + remark;
                String commContent = "进程可能已下线:" + appInfo.getHostname() + remark + ",进程:" + appInfo.getAppName();
                //发送告警
                String account = getAccount(appInfo.getHostname());
                sendUtil(title, commContent, account, key, isDown);
            } catch (Exception e) {
                logger.error("发送进程下线告警邮件失败:", e);
                logInfoService.save("发送进程下线告警错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            try {
                String title = "进程已恢复上线:" + appInfo.getAppName() + "," + appInfo.getHostname() + remark;
                String commContent = "进程已恢复上线:" + appInfo.getHostname() + remark + ",进程:" + appInfo.getAppName();
                //发送告警
                String account = getAccount(appInfo.getHostname());
                sendUtil(title, commContent, account, key, isDown);
            } catch (Exception e) {
                logger.error("发送进程恢复上线通知邮件失败:", e);
                logInfoService.save("发送进程恢复上线通知错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * docker下线发送告警邮件
     *
     * @param dockerInfo 进程信息
     * @param isDown     是否是下线告警,true下线告警,false上线恢复
     * @return
     */
    public static boolean sendDockerDown(DockerInfo dockerInfo, boolean isDown) {
        if (!StringUtils.isEmpty(mailConfig.getBlockIps()) && mailConfig.getBlockIps().contains(dockerInfo.getHostname())) {
            return false;
        }
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getDockerDownWarnMail())) {
            return false;
        }
        String key = dockerInfo.getId();
        String remark = StaticKeys.HOST_MAP.get(dockerInfo.getHostname());
        if (!StringUtils.isEmpty(remark)) {
            remark = "(" + remark + ")";
        } else {
            remark = "";
        }
        String dockerTypeStr = "CONTAINER ID";
        if ("2".equals(dockerInfo.getAppType())) {
            dockerTypeStr = "CONTAINER NAME";
        }
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                String title = "docker下线告警:" + dockerInfo.getDockerName() + "," + dockerInfo.getHostname() + remark;
                String commContent = "docker可能已下线:" + dockerInfo.getHostname() + remark + ",名称:" + dockerInfo.getDockerName() + "," + dockerTypeStr + ":" + dockerInfo.getDockerId();
                //发送告警
                String account = getAccount(dockerInfo.getHostname());
                sendUtil(title, commContent, account, key, isDown);
            } catch (Exception e) {
                logger.error("发送docker下线告警邮件失败:", e);
                logInfoService.save("发送docker下线告警错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            WarnPools.MEM_WARN_MAP.remove(key);
            try {
                String title = "docker已恢复上线:" + dockerInfo.getDockerName() + "," + dockerInfo.getHostname() + remark;
                String commContent = "docker已恢复上线:" + dockerInfo.getHostname() + remark + ",名称:" + dockerInfo.getDockerName() + "," + dockerTypeStr + ":" + dockerInfo.getDockerId();
                //发送告警
                String account = getAccount(dockerInfo.getHostname());
                sendUtil(title, commContent, account, key, isDown);
            } catch (Exception e) {
                logger.error("发送docker已恢复上线邮件失败:", e);
                logInfoService.save("发送docker已恢复上线错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
 
    /**
     * 端口telnet不通下线发送告警邮件
     *
     * @param portInfo 端口信息
     * @param isDown   是否是下线告警,true下线告警,false上线恢复
     * @return
     */
    public static boolean sendPortDown(PortInfo portInfo, boolean isDown) {
        if (!StringUtils.isEmpty(mailConfig.getBlockIps()) && mailConfig.getBlockIps().contains(portInfo.getHostname())) {
            return false;
        }
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getPortWarnMail())) {
            return false;
        }
        String key = portInfo.getId();
        String remark = StaticKeys.HOST_MAP.get(portInfo.getHostname());
        if (!StringUtils.isEmpty(remark)) {
            remark = "(" + remark + ")";
        } else {
            remark = "";
        }
        //telnetIp太长,截取处理 begin
        String telnetIp = portInfo.getTelnetIp();
        if (!StringUtils.isEmpty(telnetIp) && telnetIp.length() > 50) {
            telnetIp = telnetIp.substring(0, 50);
        }
        //telnetIp太长,截取处理 end
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                String title = "端口telnet不通告警:" + portInfo.getPortName() + ",telnet-" + telnetIp + "-" + portInfo.getPort() + "," + portInfo.getHostname() + remark;
                String commContent = "端口telnet不通,名称:" + portInfo.getPortName() + ",telnet-" + telnetIp + "-" + portInfo.getPort() + ",监控主机:" + portInfo.getHostname() + remark;
                //发送告警
                String account = getAccount(portInfo.getHostname());
                sendUtil(title, commContent, account, key, isDown);
            } catch (Exception e) {
                logger.error("发送端口telnet不通告警邮件错误:", e);
                logInfoService.save("发送端口telnet不通告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            WarnPools.MEM_WARN_MAP.remove(key);
            try {
                String title = "端口已恢复上线:" + portInfo.getPortName() + ",telnet-" + telnetIp + "-" + portInfo.getPort() + "," + portInfo.getHostname() + remark;
                String commContent = "端口已恢复上线,名称:" + portInfo.getPortName() + ",telnet-" + telnetIp + "-" + portInfo.getPort() + ",监控主机:" + portInfo.getHostname() + remark;
                //发送告警
                String account = getAccount(portInfo.getHostname());
                sendUtil(title, commContent, account, key, isDown);
            } catch (Exception e) {
                logger.error("发送端口telnet已恢复告警邮件错误:", e);
                logInfoService.save("发送端口telnet已恢复告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 文件防篡改监测失败,下线发送告警邮件
     *
     * @param fileSafe 文件信息
     * @param isDown   是否是下线告警,true下线告警,false上线恢复
     * @return
     */
    public static boolean sendFileSafeDown(FileSafe fileSafe, boolean isDown) {
        if (!StringUtils.isEmpty(mailConfig.getBlockIps()) && mailConfig.getBlockIps().contains(fileSafe.getHostname())) {
            return false;
        }
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getFileSafeWarnMail())) {
            return false;
        }
        String key = fileSafe.getId();
        String remark = StaticKeys.HOST_MAP.get(fileSafe.getHostname());
        if (!StringUtils.isEmpty(remark)) {
            remark = "(" + remark + ")";
        } else {
            remark = "";
        }
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                String title = "文件防篡改监测告警:" + fileSafe.getFileName() + "," + fileSafe.getHostname() + remark;
                String commContent = "文件防篡改监测可能已下线:" + fileSafe.getHostname() + remark + ",文件:" + fileSafe.getFileName() + ",文件路径:" + fileSafe.getFilePath() + ",文件最后修改时间:" + fileSafe.getFileModtime();
                //发送告警
                String account = getAccount(fileSafe.getHostname());
                sendUtil(title, commContent, account, key, isDown);
            } catch (Exception e) {
                logger.error("文件防篡改监测告警邮件错误:", e);
                logInfoService.save("文件防篡改监测告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            WarnPools.MEM_WARN_MAP.remove(key);
            try {
                String title = "文件防篡改监测已恢复:" + fileSafe.getFileName() + "," + fileSafe.getHostname() + remark;
                String commContent = "文件防篡改监测已恢复上线:" + fileSafe.getHostname() + remark + ",文件:" + fileSafe.getFileName() + ",文件路径:" + fileSafe.getFilePath() + ",文件最后修改时间:" + fileSafe.getFileModtime();
                //发送告警
                String account = getAccount(fileSafe.getHostname());
                sendUtil(title, commContent, account, key, isDown);
            } catch (Exception e) {
                logger.error("文件防篡改监测已恢复告警邮件错误:", e);
                logInfoService.save("文件防篡改监测已恢复警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
 
    /**
     * 数据源链接失败发送告警邮件
     *
     * @param dbInfo 数据源信息
     * @param isDown 是否是下线告警,true下线告警,false上线恢复
     * @return
     */
    public static boolean sendDbDown(DbInfo dbInfo, boolean isDown) {
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getDbDownWarnMail())) {
            return false;
        }
        String key = dbInfo.getId();
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                String title = "数据源连接失败告警:" + dbInfo.getAliasName();
                String commContent = "数据源连接失败:" + dbInfo.getAliasName();
                //发送告警
                sendUtil(title, commContent, dbInfo.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送数据源连接失败邮件错误:", e);
                logInfoService.save("发送数据源连接失败告警错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        } else {
            WarnPools.MEM_WARN_MAP.remove(key);
            try {
                String title = "数据源已恢复上线:" + dbInfo.getAliasName();
                String commContent = "数据源已恢复上线:" + dbInfo.getAliasName();
                //发送告警
                sendUtil(title, commContent, dbInfo.getAccount(), key, isDown);
            } catch (Exception e) {
                logger.error("发送数据源已恢复上线邮件错误:", e);
                logInfoService.save("发送数据源已恢复上线错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 数据监控发送告警邮件(告警表达式成立为true时候发送告警)
     *
     * @param dbTable 数据表信息
     * @param account 数据表所属用户信息
     * @return
     */
    public static boolean sendDbTableDown(DbTable dbTable, String account) {
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getDbDownWarnMail())) {
            return false;
        }
        String key = dbTable.getId();
        if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
            return false;
        }
        try {
            String title = "数据表告警:" + dbTable.getRemark();
            String commContent = "数据表告警:" + dbTable.getRemark() + ",告警表达式成立:" + dbTable.getResultExp() + ",result当前值为:" + dbTable.getTableCount();
            //发送告警
            sendUtil(title, commContent, account, key, true);
        } catch (Exception e) {
            logger.error("发送数据表告警邮件错误", e);
            logInfoService.save("发送数据表告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
        }
        return false;
    }
 
    /**
     * 自定义监控项发送告警邮件(告警表达式成立为true时候发送告警)
     *
     * @param customInfo 自定义监控项
     * @param account    数据表所属用户信息
     * @return
     */
    public static boolean sendCustomInfoDown(CustomInfo customInfo, String account) {
        if (!StringUtils.isEmpty(mailConfig.getBlockIps()) && mailConfig.getBlockIps().contains(customInfo.getHostname())) {
            return false;
        }
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getCustomInfoWarnMail())) {
            return false;
        }
        String key = customInfo.getId();
        String remark = StaticKeys.HOST_MAP.get(customInfo.getHostname());
        if (!StringUtils.isEmpty(remark)) {
            remark = "(" + remark + ")";
        } else {
            remark = "";
        }
        if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
            return false;
        }
        try {
            String title = "自定义监控项告警:" + customInfo.getCustomName() + "," + customInfo.getHostname() + remark;
            String commContent = "自定义监控项告警:" + customInfo.getHostname() + remark + "," + customInfo.getCustomName() + ",告警表达式成立:" + customInfo.getResultExp() + ",result当前值为:" + customInfo.getCustomValue();
            //发送告警
            sendUtil(title, commContent, account, key, true);
        } catch (Exception e) {
            logger.error("发送自定义监控项告警邮件错误", e);
            logInfoService.save("发送自定义监控项告警邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
        }
        return false;
    }
 
    /**
     * 日志监控发送告警邮件
     * 日志监控告警,不受告警缓存时间约束,有告警就发
     *
     * @param fileWarnInfo 日志信息
     * @param filePath     日志文件完整路径
     * @param isDown       是否是下线告警,true下线告警,false上线恢复
     * @return
     */
    public static boolean sendFileWarnDown(FileWarnInfo fileWarnInfo, String filePath, String warnContent, boolean isDown) {
        if (!StringUtils.isEmpty(mailConfig.getBlockIps()) && mailConfig.getBlockIps().contains(fileWarnInfo.getHostname())) {
            return false;
        }
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getFileLogWarnMail())) {
            return false;
        }
        String key = fileWarnInfo.getId();
        String remark = StaticKeys.HOST_MAP.get(fileWarnInfo.getHostname());
        if (!StringUtils.isEmpty(remark)) {
            remark = "(" + remark + ")";
        } else {
            remark = "";
        }
 
        String fileRemark = "";
        if (!StringUtils.isEmpty(fileWarnInfo.getRemark())) {
            fileRemark = fileWarnInfo.getRemark();
        }
        if (isDown) {
            if (WarnPools.isExpireWarnTime(key, commonConfig.getWarnCacheTimes())) {
                return false;
            }
            try {
                String title = "日志监控告警:" + fileWarnInfo.getHostname() + remark;
                String commContent = "日志监控告警:" + fileWarnInfo.getHostname() + remark + ",日志备注:" + fileRemark + ",日志文件:" + filePath + "," + warnContent;
                //发送告警
                String account = getAccount(fileWarnInfo.getHostname());
                sendUtil(title, commContent, account, key, false);
            } catch (Exception e) {
                logger.error("发送日志监控告警邮件错误", e);
                logInfoService.save("发送日志监控告警错误", e.toString(), StaticKeys.LOG_YWGJ);
            }
        }
        return false;
    }
 
    /**
     * 指令下发发送邮件提醒
     * 指令下发不受告警缓存时间约束,有通知就发
     *
     * @param shellInfo   下发指令信息
     * @param titlePrefix 下发指令标题前缀
     * @return
     */
    public static boolean sendShellInfo(ShellInfo shellInfo, String titlePrefix) {
        if (StaticKeys.NO_SEND_WARN.equals(mailConfig.getAllWarnMail()) || StaticKeys.NO_SEND_WARN.equals(mailConfig.getShellWarnMail())) {
            return false;
        }
        try {
            String shellTime = "立即下发";
            if (StaticKeys.SHELL_TYPE_2.equals(shellInfo.getShellType())) {
                shellTime = shellInfo.getShellTime();
            }
            String title = titlePrefix + ":" + shellInfo.getShellName();
            String commContent = titlePrefix + ":" + shellInfo.getShellName() + ",指令内容:" + shellInfo.getShell() + ",下发时间:" + shellTime;
            //发送告警
            sendUtil(title, commContent, shellInfo.getAccount(), "null", false);
        } catch (Exception e) {
            logger.error("下发指令信息邮件错误:", e);
            logInfoService.save("下发指令信息错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return false;
    }
 
    /**
     * 获取告警资源所属用户账号
     *
     * @param hostName
     * @return
     */
    private static String getAccount(String hostname) {
        if (StaticKeys.TRUE_VAL.equals(commonConfig.getUserInfoManage()) && !StringUtils.isEmpty(hostname)) {
            return StaticKeys.HOST_ACCOUNT_MAP.get(hostname);
        }
        return "";
    }
 
    /**
     * 执行发送告警通知的具体执行工具方法
     *
     * @param title       告警标题
     * @param commContent 告警内容
     * @param account     用户登录账号
     * @param key         已发送告警标识缓存key
     * @param isDown      告警类型,true告警,false恢复通知同时移除缓存key
     */
    public static void sendUtil(String title, String commContent, String account, String key, boolean isDown) {
        //是否在告警设置时间段内,在时间段内告警,否则不告警
        if (!StaticKeys.WARN_CRON_TIME_SIGN) {
            return;
        }
        //日志表保存发送信息
        if (title.indexOf("恢复") > -1) {
            logInfoService.save(title, commContent, StaticKeys.LOG_YWGJHF);
        } else {
            logInfoService.save(title, commContent, StaticKeys.LOG_YWGJ);
        }
 
        //告警计数器添加1,恢复不计数
        if (!StringUtils.isEmpty(title) && title.indexOf("告警") > -1) {
            WarnPools.WARN_COUNT_LIST.add(1);
        }
 
        MailSet mailSet = StaticKeys.mailSet;
        if (StringUtils.isEmpty(account) || !StaticKeys.TRUE_VAL.equals(commonConfig.getUserInfoManage())) {
            //没有启用账号默认发送
            //发送邮件
            if (StaticKeys.mailSet != null) {
                sendMail(mailSet.getToMail(), title, commContent);
            }
            //执行告警脚本
            ExecUtil.runScript(mailConfig.getWarnScript(), commContent, "");
        } else {
            //启用账号后,发送给指定的账号
            if (StaticKeys.TRUE_VAL.equals(commonConfig.getUserInfoManage())) {
                String accountMail = "";
                String accountKey = "";
                if (null != StaticKeys.ACCOUNT_INFO_MAP.get(account)) {
                    accountMail = StaticKeys.ACCOUNT_INFO_MAP.get(account).getEmail();
                    accountKey = StaticKeys.ACCOUNT_INFO_MAP.get(account).getAccountKey();
                }
                //发送邮件
                if (StaticKeys.mailSet != null) {
                    String addMail = StringUtils.isEmpty(accountMail) ? "" : ";" + accountMail;
                    sendMail(mailSet.getToMail() + addMail, title, commContent);
                }
                //执行告警脚本
                ExecUtil.runScript(mailConfig.getWarnScript(), commContent, accountKey);
            }
        }
        if (isDown) {
            //标记已发送过告警信息
            WarnPools.MEM_WARN_MAP.put(key, StaticKeys.WARN_SEND);
        } else {
            //已恢复通知,移除缓存key
            WarnPools.MEM_WARN_MAP.remove(key);
        }
    }
 
    public static String sendMail(String mails, String mailTitle, String mailContent) {
        if (StringUtils.isEmpty(mails)) {
            return "error";
        }
        if (mails.startsWith(";")) {
            mails = mails.substring(1);
        }
        try {
            String mailTitlePrefix = "[WGCLOUD]";
            String mailContentSuffix = "<p><p><p><a target='_blank' href='http://www.wgstart.com'>WGCLOUD</a>敬上";
            mailTitlePrefix = commonConfig.getMailTitlePrefix();
            mailContentSuffix = commonConfig.getMailContentSuffix();
            HtmlEmail email = new HtmlEmail();
            email.setHostName(StaticKeys.mailSet.getSmtpHost());
            email.setSmtpPort(Integer.valueOf(StaticKeys.mailSet.getSmtpPort()));
            if ("1".equals(StaticKeys.mailSet.getSmtpSSL())) {
                email.setSSL(true);
            }
            email.setAuthenticator(new DefaultAuthenticator(StaticKeys.mailSet.getFromMailName(), StaticKeys.mailSet.getFromPwd()));
            //发信者
            email.setFrom(StaticKeys.mailSet.getFromMailName());
            //标题
            email.setSubject(mailTitlePrefix + mailTitle);
            //编码格式
            email.setCharset("UTF-8");
            //内容
            email.setHtmlMsg(mailContent + "<p><p><p><p>" + DateUtil.getCurrentDateTime() + mailContentSuffix);
            email.addTo(mails.split(";"));
            email.setSentDate(new Date());
            //发送
            email.send();
            return "success";
        } catch (Exception e) {
            logger.error("发送邮件错误", e);
            logInfoService.save("发送邮件错误", e.toString(), StaticKeys.LOG_YWGJ);
            return "error";
        }
    }
 
 
}