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";
|
}
|
}
|
|
|
}
|