Authored by skinny.wu

dns monitor 添加当前监控数据 存储在内存中

... ... @@ -111,9 +111,9 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
zkConfig.setName(chi);
zkConfig.setRoot(rootString);
zkConfig.setIp(req.getIp());
// if(chi.equals("config")){
if(chi.equals("config")){
list.add(zkConfig);
// }
}
page++;
}
... ...
... ... @@ -37,8 +37,8 @@ public class DNSMonitorMapperImpl implements IDNSMonitorMapper{
.addField("info", info).build();
batchPoints.point(point);
inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB()
.write(batchPoints);
// inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB()
// .write(batchPoints);
}
/**
... ... @@ -77,7 +77,7 @@ public class DNSMonitorMapperImpl implements IDNSMonitorMapper{
}
batchPoints.point(point);
inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB()
.write(batchPoints);
// inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB()
// .write(batchPoints);
}
}
... ...
package com.monitor.other.dns.constant;
import com.monitor.other.dns.model.HostDNSConnectModel;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yoho on 2016/8/3.
*/
... ... @@ -16,4 +21,16 @@ public interface InterVar {
String RESPONSE_CODE_200 = "200";
String STRING_CODE = "code";
String LOCK = "lock";
String SUCCESSED = "SUCCESSED";
String FAILED = "failed";
String HTTP_STATUS_CODE_NOT_200 = "HTTP_STATUS_CODE_NOT_200";
String RETURN_DATA_CODE_NOT_200 = "RETURN_DATA_CODE_NOT_200";
List<HostDNSConnectModel> hostDNSConnectModelList = new ArrayList<>();
}
... ...
package com.monitor.other.dns.ctrl;
import com.alibaba.fastjson.JSON;
import com.monitor.other.dns.constant.InterVar;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by yoho on 2016/8/4.
*/
@Controller
@RequestMapping("/other/dns_monitor")
public class DNSMonitorCtrl {
@RequestMapping("/current")
@ResponseBody
public String getCurrentInfo() {
return JSON.toJSONString(InterVar.hostDNSConnectModelList);
}
}
... ...
package com.monitor.other.dns.model;
import lombok.Data;
import java.util.List;
import java.util.Map;
/**
* Created by yoho on 2016/8/4.
*/
public class HostDNSConnectModel {
String host;
/**
* key为uid value为host+uid对应的ip以及检测状态
*/
Map<String, List<IPConnectModel>> ips;
public String getHost() {
return host;
}
public Map<String, List<IPConnectModel>> getIps() {
return ips;
}
public HostDNSConnectModel() {}
public HostDNSConnectModel(String host) {
this.host = host;
}
public void setIps(Map<String, List<IPConnectModel>> ips) {
this.ips = ips;
}
}
... ...
package com.monitor.other.dns.model;
import lombok.Data;
/**
* Created by yoho on 2016/8/3.
*/
public class IPConnectModel {
/**
* 对应域名
* 解析出的ip
*/
String host;
String ip;
/**
* 解析出的ip
* 状态
*/
String ip;
String status;
/**
* 链接相应
* 信息
*/
String response;
String info;
public String getIp() {
return ip;
}
public String getStatus() {
return status;
}
public String getInfo() {
return info;
}
public IPConnectModel() {}
public IPConnectModel(String ip, String status, String info) {
this.ip = ip;
this.status = status;
this.info = info;
}
}
... ...
... ... @@ -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错误!");
... ...
package com.monitor.other.dns.task;
import com.monitor.other.dns.constant.InterVar;
import com.monitor.other.dns.service.IDNSMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
... ... @@ -16,6 +17,8 @@ public class DNSMonitorTask {
@Scheduled(cron = "${cron_task_dns_monit}")
public void monitor() {
dnsMonitorService.monitor();
synchronized (InterVar.LOCK.intern()) {
dnsMonitorService.monitor();
}
}
}
... ...
# ******************** dns monitor common configs ********************
dns.config.url=api.yoho.cn/?method=app.resources.config.clientInitConfig&client_secret=bf8f49754dc7058349146f922640000c&client_type=android;service.yoho.cn/?method=app.resources.config.clientInitConfig&client_secret=bf8f49754dc7058349146f922640000c&client_type=android;m.yohobuy.com/boys
#dns.config.hosts=api.yoho.cn;service.yoho.cn;m.yohobuy.com
#dns.config.services=/?method=app.resources.config.clientInitConfig&client_secret=bf8f49754dc7058349146f922640000c&client_type=android;/operations/api/v5/resource/get?app_version=3.9.0.1512170008&client_secret=00be285c73a816177652240fea5a4a54&client_type=iphone&content_code=a7989369aa86681c678bc40f171b8f1d&os_version=9.2&screen_size=375x667&v=7;/boys
dns.config.api.url=http://123.206.2.55/?
\ No newline at end of file
... ...
... ... @@ -10,5 +10,5 @@ cron_task_rabbit_monit=0 0/2 * * * ?
cron_task_rabbit_scan=0 0/1 * * * ?
cron_task_dns_monit=0 0/3 * * * ?
cron_task_dns_monit=0 1/5 * * * ?
... ...