...
|
...
|
@@ -9,6 +9,8 @@ import com.monitor.influxdb.mapper.IDNSMonitorMapper; |
|
|
import com.monitor.other.dns.common.DNSConfig;
|
|
|
import com.monitor.other.dns.constant.InterVar;
|
|
|
import com.monitor.other.dns.model.DNSIPsModel;
|
|
|
import com.monitor.other.dns.model.HostDNSConnectModel;
|
|
|
import com.monitor.other.dns.model.IPConnectModel;
|
|
|
import com.monitor.other.dns.model.IPModel;
|
|
|
import com.monitor.other.dns.service.IDNSMonitorService;
|
|
|
import com.monitor.other.dns.service.IRestTemplateNoEncode;
|
...
|
...
|
@@ -21,7 +23,9 @@ import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/8/1.
|
...
|
...
|
@@ -58,27 +62,45 @@ public class DNSMonitorServiceImpl implements IDNSMonitorService{ |
|
|
*/
|
|
|
@Override
|
|
|
public void monitor() {
|
|
|
List<HostDNSConnectModel> hostDNSConnectModelList = new ArrayList<>();
|
|
|
|
|
|
for (String host : dnsConfig.getHosts()) {
|
|
|
for (String id : dnsConfig.getUids()) {
|
|
|
|
|
|
String result = restTemplateNoEncode.doGet(buildDNSUrl(host,id));
|
|
|
List<IPModel> ipModelList = parseIPModels(result);
|
|
|
HostDNSConnectModel hostDNSConnectModel = new HostDNSConnectModel(host);
|
|
|
|
|
|
//key为uid value为host+uid下的ip以及其状态
|
|
|
Map<String, List<IPConnectModel>> ipConnectModelsMap = new HashMap<>();
|
|
|
|
|
|
for (String uid : dnsConfig.getUids()) {
|
|
|
|
|
|
String result = restTemplateNoEncode.doGet(buildDNSUrl(host,uid));
|
|
|
List<String> ipModelList = parseIPModels(result);
|
|
|
|
|
|
//调用dns api无返回ip,即异常,发送告警短信,把数据存数influxdb中
|
|
|
if (null == ipModelList) {
|
|
|
alarmMsgService.sendSms("dns_exception", buildDNSFailedMsnContent(host, id),
|
|
|
|
|
|
//短信告警
|
|
|
alarmMsgService.sendSms("dns_exception", buildDNSFailedMsnContent(host, uid),
|
|
|
result, alarmMobile);
|
|
|
//host dns 检测数据存入influxdb
|
|
|
dnsMonitorMapper.insert(host, uid, IDNSMonitorMapper.FAILED, IDNSMonitorMapper.NULL_IP);
|
|
|
|
|
|
ipConnectModelsMap.put(uid, null);
|
|
|
logger.error("DNSMonitor Connect DNS no ip return.host:{},uid{}",
|
|
|
host, id);
|
|
|
dnsMonitorMapper.insert(host, id, IDNSMonitorMapper.FAILED, IDNSMonitorMapper.NULL_IP);
|
|
|
host, uid);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
dnsMonitorMapper.insert(host, id, IDNSMonitorMapper.SUCCESSED, JSONArray.toJSONString(ipModelList));
|
|
|
monitIPConnection(host, id, ipModelList);
|
|
|
//host dns 检测数据存入influxdb
|
|
|
dnsMonitorMapper.insert(host, uid, IDNSMonitorMapper.SUCCESSED, JSONArray.toJSONString(ipModelList));
|
|
|
|
|
|
monitIPConnection(host, uid, ipModelList, ipConnectModelsMap);
|
|
|
}
|
|
|
hostDNSConnectModel.setIps(ipConnectModelsMap);
|
|
|
hostDNSConnectModelList.add(hostDNSConnectModel);
|
|
|
}
|
|
|
|
|
|
InterVar.hostDNSConnectModelList.clear();
|
|
|
InterVar.hostDNSConnectModelList.addAll(hostDNSConnectModelList);
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -86,14 +108,15 @@ public class DNSMonitorServiceImpl implements IDNSMonitorService{ |
|
|
*
|
|
|
* m.yoho.com域名返回为网页,其他域名不注明则为api接口
|
|
|
* @param host
|
|
|
* @param ipModelList
|
|
|
* @param ipList
|
|
|
*/
|
|
|
private void monitIPConnection(String host,String uid, List<IPModel> ipModelList) {
|
|
|
private void monitIPConnection(String host,String uid, List<String> ipList,
|
|
|
Map<String, List<IPConnectModel>> ipConnectionMaps) {
|
|
|
|
|
|
if (InterVar.M_YOHOBUY_COM.equals(host)) {
|
|
|
monitMYohoBuyComIp(host, uid, ipModelList);
|
|
|
monitMYohoBuyComIp(host, uid, ipList, ipConnectionMaps);
|
|
|
} else {
|
|
|
monitApiIp(host, uid, ipModelList);
|
|
|
monitApiIp(host, uid, ipList, ipConnectionMaps);
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -101,33 +124,37 @@ public class DNSMonitorServiceImpl implements IDNSMonitorService{ |
|
|
*
|
|
|
* @param host
|
|
|
* @param uid
|
|
|
* @param ipModelList
|
|
|
* @param ipList
|
|
|
*/
|
|
|
private void monitApiIp(String host, String uid, List<IPModel> ipModelList) {
|
|
|
private void monitApiIp(String host, String uid, List<String> ipList,
|
|
|
Map<String, List<IPConnectModel>> ipConnectionMap) {
|
|
|
List<String> errorIPs = new ArrayList<>();
|
|
|
StringBuilder alarmInfo = new StringBuilder();
|
|
|
List<IPConnectModel> ipConnectModels = new ArrayList<>();
|
|
|
|
|
|
for (IPModel ipModel : ipModelList) {
|
|
|
for (String ip : ipList) {
|
|
|
|
|
|
String responseCode = restTemplateNoEncode.doGetResponseCode(
|
|
|
InterVar.HTTP + ipModel.getIp() + dnsConfig.getServices().get(host));
|
|
|
InterVar.HTTP + ip + dnsConfig.getServices().get(host));
|
|
|
|
|
|
//ip不通,告警
|
|
|
if (!InterVar.RESPONSE_CODE_200.equals(responseCode) || null == responseCode) {
|
|
|
errorIPs.add(ipModel.getIp());
|
|
|
errorIPs.add(ip);
|
|
|
alarmInfo.append(" ip: ");
|
|
|
alarmInfo.append(ipModel.getIp());
|
|
|
alarmInfo.append(ip);
|
|
|
alarmInfo.append(" 不通. ");
|
|
|
logger.error("DNSMonitor Connect DNS API: host {}, uid {}, ip {} 不通。",
|
|
|
host, uid, ipModel.getIp());
|
|
|
dnsMonitorMapper.insert(host, uid, ipModel.getIp(),
|
|
|
IDNSMonitorMapper.FAILED, IDNSMonitorMapper.HTTP_STATUS_CODE_NOT_200, "");
|
|
|
host, uid, ip);
|
|
|
dnsMonitorMapper.insert(host, uid, ip, IDNSMonitorMapper.FAILED,
|
|
|
IDNSMonitorMapper.HTTP_STATUS_CODE_NOT_200, "");
|
|
|
|
|
|
ipConnectModels.add(new IPConnectModel(ip, InterVar.FAILED, InterVar.HTTP_STATUS_CODE_NOT_200));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//获取服务请求返回的数据是否正确 code:200
|
|
|
String response = httpRestClientService.doGet(
|
|
|
InterVar.HTTP + ipModel.getIp() + dnsConfig.getServices().get(host), null);
|
|
|
InterVar.HTTP + ip + dnsConfig.getServices().get(host), null);
|
|
|
|
|
|
String resultCode = null;
|
|
|
|
...
|
...
|
@@ -140,32 +167,36 @@ public class DNSMonitorServiceImpl implements IDNSMonitorService{ |
|
|
|
|
|
//返回数据 code!=200 告警
|
|
|
if (!InterVar.RESPONSE_CODE_200.equals(resultCode) || null == response) {
|
|
|
errorIPs.add(ipModel.getIp());
|
|
|
errorIPs.add(ip);
|
|
|
|
|
|
alarmInfo.append(" ip: ");
|
|
|
alarmInfo.append(ipModel.getIp());
|
|
|
alarmInfo.append(ip);
|
|
|
alarmInfo.append(";response: ");
|
|
|
alarmInfo.append(response);
|
|
|
alarmInfo.append(". ");
|
|
|
logger.info("DNSMonitor Connect DNS API: host {}, uid {}, ip {}, response {} ",
|
|
|
host, uid, ipModel.getIp(), response);
|
|
|
host, uid, ip, response);
|
|
|
|
|
|
dnsMonitorMapper.insert(host, uid, ipModel.getIp(),
|
|
|
dnsMonitorMapper.insert(host, uid, ip,
|
|
|
IDNSMonitorMapper.FAILED, IDNSMonitorMapper.RETURN_DATA_CODE_NOT_200,
|
|
|
response);
|
|
|
ipConnectModels.add(new IPConnectModel(ip, InterVar.FAILED, InterVar.RETURN_DATA_CODE_NOT_200));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
dnsMonitorMapper.insert(host, uid, ipModel.getIp(), IDNSMonitorMapper.SUCCESSED, "", "");
|
|
|
dnsMonitorMapper.insert(host, uid, ip, IDNSMonitorMapper.SUCCESSED, "", "");
|
|
|
ipConnectModels.add(new IPConnectModel(ip, InterVar.SUCCESSED, ""));
|
|
|
}
|
|
|
|
|
|
ipConnectionMap.put(uid, ipConnectModels);
|
|
|
|
|
|
if (!errorIPs.isEmpty()) {
|
|
|
alarmMsgService.sendSms("dns_exception",
|
|
|
buildIPConnectFailedMsnContent(host, uid, ipModelList, errorIPs),
|
|
|
buildIPConnectFailedMsnContent(host, uid, ipList, errorIPs),
|
|
|
alarmInfo.toString(), alarmMobile);
|
|
|
|
|
|
logger.error("DNSMonitor Connect DNS API: host {}, uid {}, dnsIP {}, connextFailedIP {}",
|
|
|
host, uid, ipModelList.toString(), errorIPs.toString());
|
|
|
host, uid, ipList.toString(), errorIPs.toString());
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -176,55 +207,63 @@ public class DNSMonitorServiceImpl implements IDNSMonitorService{ |
|
|
* 2.返回预期数据
|
|
|
* @param host
|
|
|
* @param uid
|
|
|
* @param ipModelList
|
|
|
* @param ipList
|
|
|
*/
|
|
|
private void monitMYohoBuyComIp(String host, String uid, List<IPModel> ipModelList) {
|
|
|
private void monitMYohoBuyComIp(String host, String uid, List<String> ipList,
|
|
|
Map<String, List<IPConnectModel>> ipConnectionMap) {
|
|
|
List<String> errorIPs = new ArrayList<>();
|
|
|
StringBuilder alarmInfo = new StringBuilder();
|
|
|
List<IPConnectModel> ipConnectModels = new ArrayList<>();
|
|
|
|
|
|
for (IPModel ipModel : ipModelList) {
|
|
|
for (String ip : ipList) {
|
|
|
|
|
|
String responseCode = restTemplateNoEncode.doGetResponseCode(InterVar.HTTPS + ipModel.getIp());
|
|
|
String responseCode = restTemplateNoEncode.doGetResponseCode(InterVar.HTTPS + ip);
|
|
|
|
|
|
//ip不通,异常数据告警
|
|
|
if (!InterVar.RESPONSE_CODE_200.equals(responseCode) || null == responseCode) {
|
|
|
errorIPs.add(ipModel.getIp());
|
|
|
errorIPs.add(ip);
|
|
|
alarmInfo.append(" ip: ");
|
|
|
alarmInfo.append(ipModel.getIp());
|
|
|
alarmInfo.append(ip);
|
|
|
alarmInfo.append(" 不通. ");
|
|
|
logger.info("DNSMonitor Connect DNS API: host {}, uid {}, ip {} 不通。",
|
|
|
host, uid, ipModel.getIp());
|
|
|
dnsMonitorMapper.insert(host, uid, ipModel.getIp(),
|
|
|
host, uid, ip);
|
|
|
dnsMonitorMapper.insert(host, uid, ip,
|
|
|
IDNSMonitorMapper.FAILED, IDNSMonitorMapper.HTTP_STATUS_CODE_NOT_200, "");
|
|
|
|
|
|
ipConnectModels.add(new IPConnectModel(ip, InterVar.FAILED, InterVar.HTTP_STATUS_CODE_NOT_200));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//检测该ip是否返回预期网页
|
|
|
String response = restTemplateNoEncode.doGet(
|
|
|
InterVar.HTTPS + ipModel.getIp() + dnsConfig.getServices().get(host));
|
|
|
InterVar.HTTPS + ip + dnsConfig.getServices().get(host));
|
|
|
|
|
|
//验证返回网页是否正确 如果不含有该字符串则返回结果错误
|
|
|
if (-1 == response.indexOf(InterVar.M_YOHOBUY_COM_RESPONSE_PATTEN) || null == response) {
|
|
|
errorIPs.add(ipModel.getIp());
|
|
|
errorIPs.add(ip);
|
|
|
alarmInfo.append(" ip: ");
|
|
|
alarmInfo.append(ipModel.getIp());
|
|
|
alarmInfo.append(ip);
|
|
|
alarmInfo.append(" 返回网页错误. ");
|
|
|
dnsMonitorMapper.insert(host, uid, ipModel.getIp(),
|
|
|
dnsMonitorMapper.insert(host, uid, ip,
|
|
|
IDNSMonitorMapper.FAILED, IDNSMonitorMapper.RETURN_DATA_CODE_NOT_200, response);
|
|
|
logger.info("DNSMonitor Connect DNS API: host {}, uid {}, ip {} 结果错误。",
|
|
|
host, uid, ipModel.getIp());
|
|
|
host, uid, ip);
|
|
|
ipConnectModels.add(new IPConnectModel(ip, InterVar.FAILED, InterVar.RETURN_DATA_CODE_NOT_200));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
dnsMonitorMapper.insert(host, uid, ipModel.getIp(), IDNSMonitorMapper.SUCCESSED, "", "");
|
|
|
dnsMonitorMapper.insert(host, uid, ip, IDNSMonitorMapper.SUCCESSED, "", "");
|
|
|
ipConnectModels.add(new IPConnectModel(ip, InterVar.SUCCESSED, ""));
|
|
|
}
|
|
|
|
|
|
ipConnectionMap.put(uid, ipConnectModels);
|
|
|
|
|
|
if (!errorIPs.isEmpty()) {
|
|
|
alarmMsgService.sendSms("dns_exception",
|
|
|
buildIPConnectFailedMsnContent(host, uid, ipModelList, errorIPs),
|
|
|
buildIPConnectFailedMsnContent(host, uid, ipList, errorIPs),
|
|
|
alarmInfo.toString(), alarmMobile);
|
|
|
logger.error("DNSMonitor Connect DNS API: host {}, uid {}, dnsIP {}, connextFailedIP {}",
|
|
|
host, uid, ipModelList.toString(), errorIPs.toString());
|
|
|
host, uid, ipList.toString(), errorIPs.toString());
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -235,13 +274,21 @@ public class DNSMonitorServiceImpl implements IDNSMonitorService{ |
|
|
* @param result
|
|
|
* @return
|
|
|
*/
|
|
|
private List<IPModel> parseIPModels(String result) {
|
|
|
private List<String> parseIPModels(String result) {
|
|
|
String decodeResult = AESUtil.decode(result);
|
|
|
|
|
|
DNSIPsModel dnsiPsModel = JSON.parseObject(decodeResult, DNSIPsModel.class);
|
|
|
List<IPModel> ipModelList = JSON.parseArray(dnsiPsModel.getContent() , IPModel.class);
|
|
|
|
|
|
return ipModelList;
|
|
|
if (null == ipModelList) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
List<String> ipList = new ArrayList<String>();
|
|
|
for (IPModel ipModel : ipModelList) {
|
|
|
ipList.add(ipModel.getIp());
|
|
|
}
|
|
|
return ipList;
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -269,7 +316,7 @@ public class DNSMonitorServiceImpl implements IDNSMonitorService{ |
|
|
* @param connectFailedIps 访问失败的ip组
|
|
|
* @return
|
|
|
*/
|
|
|
private String buildIPConnectFailedMsnContent(String host, String uid, List<IPModel> ips,
|
|
|
private String buildIPConnectFailedMsnContent(String host, String uid, List<String> ips,
|
|
|
List<String> connectFailedIps) {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
sb.append("DNS解析IP错误!");
|
...
|
...
|
|