提交 59979b68 编写于 作者: Y youyong205

Merge pull request #413 from jialinsun/master

删掉debug注释
<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="config" type="ConfigType"/>
<xs:complexType name="ConfigType">
<xs:sequence>
......
<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="status" type="StatusType"/>
<xs:complexType name="StatusType">
<xs:sequence>
......
......@@ -19,7 +19,7 @@ import com.dianping.cat.report.task.alert.exception.ExceptionAlert;
import com.dianping.cat.report.task.alert.network.NetworkAlert;
import com.dianping.cat.report.task.alert.system.SystemAlert;
import com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlert;
import com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlertTask;
import com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlertBuilder;
import com.dianping.cat.report.task.product.ProjectUpdateTask;
import com.dianping.cat.report.view.DomainNavManager;
import com.dianping.cat.system.config.ConfigReloadTask;
......@@ -50,10 +50,6 @@ public class CatHomeModule extends AbstractModule {
Threads.forGroup("Cat").start(domainNavManager);
Threads.forGroup("Cat").start(taskConsumer);
}
ThirdPartyAlert thirdPartyAlert = ctx.lookup(ThirdPartyAlert.class);
ThirdPartyAlertTask thirdPartyAlertTask = ctx.lookup(ThirdPartyAlertTask.class);
Threads.forGroup("Cat").start(thirdPartyAlert);
Threads.forGroup("Cat").start(thirdPartyAlertTask);
if (serverConfigManager.isAlertMachine() && !serverConfigManager.isLocalMode()) {
BusinessAlert metricAlert = ctx.lookup(BusinessAlert.class);
......@@ -61,10 +57,8 @@ public class CatHomeModule extends AbstractModule {
SystemAlert systemAlert = ctx.lookup(SystemAlert.class);
ExceptionAlert exceptionAlert = ctx.lookup(ExceptionAlert.class);
ProjectUpdateTask productUpdateTask = ctx.lookup(ProjectUpdateTask.class);
Threads.forGroup("Cat").start(thirdPartyAlert);
Threads.forGroup("Cat").start(thirdPartyAlertTask);
// ThirdPartyAlert thirdPartyAlert = ctx.lookup(ThirdPartyAlert.class);
// ThirdPartyAlertTask thirdPartyAlertTask = ctx.lookup(ThirdPartyAlertTask.class);
ThirdPartyAlert thirdPartyAlert = ctx.lookup(ThirdPartyAlert.class);
ThirdPartyAlertBuilder alertBuildingTask = ctx.lookup(ThirdPartyAlertBuilder.class);
Threads.forGroup("Cat").start(networkAlert);
Threads.forGroup("Cat").start(systemAlert);
......@@ -72,7 +66,7 @@ public class CatHomeModule extends AbstractModule {
Threads.forGroup("Cat").start(exceptionAlert);
Threads.forGroup("Cat").start(productUpdateTask);
Threads.forGroup("Cat").start(thirdPartyAlert);
Threads.forGroup("Cat").start(thirdPartyAlertTask);
Threads.forGroup("Cat").start(alertBuildingTask);
}
executeAlarmModule(ctx);
}
......
......@@ -79,9 +79,9 @@ import com.dianping.cat.report.task.alert.summary.AlertSummaryGenerator;
import com.dianping.cat.report.task.alert.summary.AlertSummaryManager;
import com.dianping.cat.report.task.alert.system.SystemAlert;
import com.dianping.cat.report.task.alert.system.SystemAlertConfig;
import com.dianping.cat.report.task.alert.thirdParty.HttpMonitor;
import com.dianping.cat.report.task.alert.thirdParty.HttpConnector;
import com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlert;
import com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlertTask;
import com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlertBuilder;
import com.dianping.cat.report.task.product.ProjectUpdateTask;
import com.dianping.cat.report.view.DomainNavManager;
import com.dianping.cat.service.IpService;
......@@ -137,9 +137,9 @@ public class ComponentsConfigurator extends AbstractResourceConfigurator {
all.add(C(ThirdPartyAlert.class).req(ProjectDao.class, MailSender.class));
all.add(C(HttpMonitor.class));
all.add(C(HttpConnector.class));
all.add(C(ThirdPartyAlertTask.class).req(HttpMonitor.class, ThirdPartyAlert.class, ThirdPartyConfigManager.class));
all.add(C(ThirdPartyAlertBuilder.class).req(HttpConnector.class, ThirdPartyAlert.class, ThirdPartyConfigManager.class));
return all;
}
......
......@@ -82,12 +82,12 @@ public class ExceptionAlert implements Task {
@Override
public void run() {
boolean active = true;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
active = false;
}
boolean active = true;
while (active) {
long current = System.currentTimeMillis();
int minute = Calendar.getInstance().get(Calendar.MINUTE);
......
......@@ -8,11 +8,11 @@ import java.net.HttpURLConnection;
import java.net.URL;
import org.codehaus.plexus.util.StringUtils;
import org.unidal.helper.Files;
public class HttpMonitor {
public class HttpConnector {
public String readFromGet(String url) {
public boolean readFromGet(String url) {
boolean result = false;
HttpURLConnection connection = null;
InputStream in = null;
BufferedReader reader = null;
......@@ -26,13 +26,10 @@ public class HttpMonitor {
int response_code = connection.getResponseCode();
if (response_code == HttpURLConnection.HTTP_OK) {
in = connection.getInputStream();
String result = Files.forIO().readFrom(in, "utf-8");
return result;
result = true;
}
} catch (Exception e) {
throw new RuntimeException(e);
return false;
} finally {
if (connection != null) {
connection.disconnect();
......@@ -45,13 +42,13 @@ public class HttpMonitor {
reader.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return null;
return result;
}
public String readFromPost(String url, String content) {
public boolean readFromPost(String url, String content) {
boolean result = false;
HttpURLConnection connection = null;
BufferedReader reader = null;
OutputStreamWriter writer = null;
......@@ -76,13 +73,10 @@ public class HttpMonitor {
int response_code = connection.getResponseCode();
if (response_code == HttpURLConnection.HTTP_OK) {
in = connection.getInputStream();
String result = Files.forIO().readFrom(in, "utf-8");
return result;
result = true;
}
} catch (Exception e) {
throw new RuntimeException(e);
return false;
} finally {
if (connection != null) {
connection.disconnect();
......@@ -98,9 +92,8 @@ public class HttpMonitor {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return null;
return result;
}
}
......@@ -22,7 +22,6 @@ import com.dianping.cat.core.dal.Project;
import com.dianping.cat.core.dal.ProjectDao;
import com.dianping.cat.core.dal.ProjectEntity;
import com.dianping.cat.helper.TimeUtil;
import com.dianping.cat.home.alert.thirdParty.entity.Domain;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.report.task.alert.sender.MailSender;
......@@ -89,7 +88,6 @@ public class ThirdPartyAlert implements Task, LogEnabled {
for (Entry<String, List<ThirdPartyAlertEntity>> entry : domain2AlertMap.entrySet()) {
sendAlert(entry.getKey(), entry.getValue());
}
t.setStatus(Transaction.SUCCESS);
} catch (Exception e) {
t.setStatus(e);
......@@ -113,18 +111,16 @@ public class ThirdPartyAlert implements Task, LogEnabled {
Map<String, List<ThirdPartyAlertEntity>> domain2AlertMap = new HashMap<String, List<ThirdPartyAlertEntity>>();
for (ThirdPartyAlertEntity entity : alertEntities) {
for (Domain domain : entity.getDomains()) {
String domainId = domain.getId();
List<ThirdPartyAlertEntity> alertList = domain2AlertMap.get(domainId);
String domain = entity.getDomain();
List<ThirdPartyAlertEntity> alertList = domain2AlertMap.get(domain);
if (alertList == null) {
alertList = new ArrayList<ThirdPartyAlertEntity>();
domain2AlertMap.put(domainId, alertList);
domain2AlertMap.put(domain, alertList);
}
alertList.add(entity);
}
}
return domain2AlertMap;
}
......
package com.dianping.cat.report.task.alert.thirdParty;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Map;
import java.util.Map.Entry;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;
import org.unidal.helper.Threads;
import org.unidal.helper.Threads.Task;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.helper.TimeUtil;
import com.dianping.cat.home.alert.thirdParty.entity.Http;
import com.dianping.cat.home.alert.thirdParty.entity.Par;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.system.config.ThirdPartyConfigManager;
public class ThirdPartyAlertTask implements Task, LogEnabled {
public class ThirdPartyAlertBuilder implements Task, LogEnabled {
@Inject
private HttpMonitor m_httpMonitor;
private HttpConnector m_httpConnector;
@Inject
private ThirdPartyAlert m_thirdPartyAlert;
......@@ -37,7 +40,13 @@ public class ThirdPartyAlertTask implements Task, LogEnabled {
while (active) {
long current = System.currentTimeMillis();
Transaction t = Cat.newTransaction("Task", "ThirdPartyAlert");
int minute = Calendar.getInstance().get(Calendar.MINUTE);
String minuteStr = String.valueOf(minute);
if (minute < 10) {
minuteStr = '0' + minuteStr;
}
Transaction t = Cat.newTransaction("ThirdPartyAlertBuilder", "M" + minuteStr);
try {
buildAlertEntities(current);
......@@ -65,30 +74,35 @@ public class ThirdPartyAlertTask implements Task, LogEnabled {
m_thirdPartyAlert.put(entity);
}
private String connectHttpUrl(Http http) {
String type = http.getType();
String result = null;
String paras = http.getPars();
private String buildPars(Map<String, Par> paras) {
String[] s = new String[paras.size()];
int i = 0;
if (StringUtils.isNotEmpty(paras)) {
paras = paras.replaceAll("[;]", "&");
http.setPars(paras);
for (Entry<String, Par> entry : paras.entrySet()) {
Par par = entry.getValue();
s[i++] = par.getId() + "=" + entry.getValue().getValue();
}
return StringUtils.join(s, "&");
}
try {
if ("get".equalsIgnoreCase(type)) {
public boolean connectHttpUrl(Http http) {
boolean result = false;
String type = http.getType();
String url = http.getUrl();
Map<String, Par> paras = http.getPars();
String joined = null;
if (StringUtils.isNotEmpty(paras)) {
url += "?" + paras;
if (paras != null) {
joined = buildPars(paras);
}
result = m_httpMonitor.readFromGet(url);
} else if ("post".equalsIgnoreCase(type)) {
String url = http.getUrl();
result = m_httpMonitor.readFromPost(url, paras);
if ("get".equalsIgnoreCase(type)) {
if (StringUtils.isNotEmpty(joined)) {
url += "?" + joined;
}
} catch (Exception e) {
m_logger.error(e.getMessage(), e);
result = m_httpConnector.readFromGet(url);
} else if ("post".equalsIgnoreCase(type)) {
result = m_httpConnector.readFromPost(url, joined);
}
return result;
}
......@@ -97,10 +111,8 @@ public class ThirdPartyAlertTask implements Task, LogEnabled {
List<Http> https = m_configManager.queryHttps();
for (Http http : https) {
String result = connectHttpUrl(http);
if (StringUtils.isEmpty(result)) {
new HttpReconnector(this, http, current + DURATION);
if (!connectHttpUrl(http)) {
Threads.forGroup("Cat").start(new HttpReconnector(this, http, current + DURATION));
}
}
}
......@@ -123,50 +135,56 @@ public class ThirdPartyAlertTask implements Task, LogEnabled {
ThirdPartyAlertEntity entity = new ThirdPartyAlertEntity();
String url = http.getUrl();
String type = http.getType();
String pars = http.getPars();
String details = "HTTP URL[" + url + "?" + pars + "] " + type.toUpperCase() + "无法访问";
Map<String, Par> pars = http.getPars();
String details = "HTTP URL[" + url + "?" + buildPars(pars) + "] " + type.toUpperCase() + "访问出现异常";
entity.setDomains(http.getDomains()).setType(type).setDetails(details);
entity.setDomain(http.getDomain()).setType(type).setDetails(details);
return entity;
}
public class HttpReconnector {
private Timer m_timer;
public HttpReconnector(ThirdPartyAlertTask alertTask, Http http, long deadLine) {
m_timer = new Timer();
m_timer.schedule(new RemindTask(alertTask, http, deadLine), 0, 5 * 1000);
}
public class HttpReconnector implements Task {
class RemindTask extends TimerTask {
private int m_reconncetNum = 2;
private ThirdPartyAlertBuilder m_alertTask;
private ThirdPartyAlertTask m_alertTask;
private int m_retryTimes = 2;
private Http m_http;
private long m_deadLine;
public RemindTask(ThirdPartyAlertTask alertTask, Http http, long deadLine) {
m_alertTask = alertTask;
public HttpReconnector(ThirdPartyAlertBuilder alertTask, Http http, long deadLine) {
m_http = http;
m_alertTask = alertTask;
m_deadLine = deadLine;
}
@Override
public void run() {
if (m_reconncetNum > 0 && System.currentTimeMillis() < m_deadLine) {
m_reconncetNum--;
String result = m_alertTask.connectHttpUrl(m_http);
if (StringUtils.isNotEmpty(result)) {
m_timer.cancel();
while (true) {
if (m_retryTimes > 0 && System.currentTimeMillis() < m_deadLine) {
m_retryTimes--;
if (m_alertTask.connectHttpUrl(m_http)) {
break;
} else {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
} else {
m_timer.cancel();
m_alertTask.putAlertEnity(m_alertTask.buildAlertEntity(m_http));
break;
}
}
}
@Override
public String getName() {
return "http-reconnector";
}
@Override
public void shutdown() {
}
}
}
package com.dianping.cat.report.task.alert.thirdParty;
import java.util.List;
import com.dianping.cat.home.alert.thirdParty.entity.Domain;
public class ThirdPartyAlertEntity {
private String m_type;
private List<Domain> m_domains;
private String m_domain;
private String m_details;
......@@ -20,8 +16,8 @@ public class ThirdPartyAlertEntity {
return m_details;
}
public List<Domain> getDomains() {
return m_domains;
public String getDomain() {
return m_domain;
}
public ThirdPartyAlertEntity setType(String type) {
......@@ -34,8 +30,8 @@ public class ThirdPartyAlertEntity {
return this;
}
public ThirdPartyAlertEntity setDomains(List<Domain> domains) {
m_domains = domains;
public ThirdPartyAlertEntity setDomain(String domain) {
m_domain = domain;
return this;
}
......
......@@ -7,16 +7,16 @@
<entity name="http">
<attribute name="url" value-type="String" />
<attribute name="type" value-type="String" />
<attribute name="pars" value-type="String" />
<entity-ref name="domain" type="list" names="domains" />
<attribute name="domain" value-type="String" />
<entity-ref name="par" type="list" names="pars" />
</entity>
<entity name="domain">
<entity name="par">
<attribute name="id" value-type="String" />
<attribute name="value" value-type="String" />
</entity>
<entity name="socket">
<attribute name="ip" value-type="String" />
<attribute name="port" value-type="int" />
<entity-ref name="domain" type="list" names="domains" />
</entity>
</model>
......@@ -7,17 +7,17 @@
</entity>
<entity name="http">
<attribute name="url" value-type="String" />
<attribute name="type" value-type="String" default-value="get" />
<attribute name="pars" value-type="String" />
<entity-ref name="domain" type="list" names="domains" />
<attribute name="type" value-type="String" />
<attribute name="domain" value-type="String" />
<entity-ref name="par" type="map" names="pars" />
</entity>
<entity name="domain">
<attribute name="id" value-type="String" />
<entity name="par">
<attribute name="id" value-type="String" key="true" />
<attribute name="value" value-type="String" />
</entity>
<entity name="socket">
<attribute name="ip" value-type="String" />
<attribute name="port" value-type="int" />
<entity-ref name="domain" type="list" names="domains" />
</entity>
</model>
......@@ -4540,15 +4540,15 @@
</requirements>
</component>
<component>
<role>com.dianping.cat.report.task.alert.thirdParty.HttpMonitor</role>
<implementation>com.dianping.cat.report.task.alert.thirdParty.HttpMonitor</implementation>
<role>com.dianping.cat.report.task.alert.thirdParty.HttpConnector</role>
<implementation>com.dianping.cat.report.task.alert.thirdParty.HttpConnector</implementation>
</component>
<component>
<role>com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlertTask</role>
<implementation>com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlertTask</implementation>
<role>com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlertBuilder</role>
<implementation>com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlertBuilder</implementation>
<requirements>
<requirement>
<role>com.dianping.cat.report.task.alert.thirdParty.HttpMonitor</role>
<role>com.dianping.cat.report.task.alert.thirdParty.HttpConnector</role>
</requirement>
<requirement>
<role>com.dianping.cat.report.task.alert.thirdParty.ThirdPartyAlert</role>
......
<third-party-config>
<http url="http://www.51ping.com:8080" type="get" />
<http url="http://www.51ping.com:8080" type="get" domain="Cat" >
<par id="domain" value="Cat"/>
<par id="group" value="Cat"/>
</http>
<http url="http://www.51ping.com:8080" type="post" />
<socket ip="10.10.10.1" port="80" />
<socket ip="10.10.10.1" port="80" />
<socket ip="10.10.10.1" port="80" />
</third-party-config>
\ No newline at end of file
<third-party-config>
<http url="http://www.51ping.com:8080" type="get" pars="name=name;sex=male" >
<domain id="cat1" />
<domain id="cat2" />
<http url="http://www.51ping.com:8080" type="get" domain="Cat" >
<par id="domain" value="Cat"/>
<par id="group" value="Cat"/>
</http>
<http url="http://www.51ping.com:8080" type="post" pars="name=abc;sex=male" />
<socket ip="10.10.10.1" port="80">
<domain id="cat1" />
<domain id="cat2" />
</socket>
<http url="http://www.51ping.com:8080" type="post" />
<socket ip="10.10.10.1" port="80" />
<socket ip="10.10.10.1" port="80" />
</third-party-config>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册