diff --git a/cat-agent/pom.xml b/cat-agent/pom.xml
index 2b84487829f2ed2ff1d24a8ac12c1bc4e5669ac7..8b5140d93e231b8cff7e644a8abaaa6d6c52bdbf 100644
--- a/cat-agent/pom.xml
+++ b/cat-agent/pom.xml
@@ -19,7 +19,7 @@
org.unidal.framework
foundation-service
- 2.0.5
+ 2.0.8
com.dianping.cat
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/build/ComponentsConfigurator.java b/cat-agent/src/main/java/com/dianping/cat/agent/build/ComponentsConfigurator.java
index 3390f0e5e4601180912f5aa297db94f1fdbc6433..2a041c2304bb05563f762e935507ce4a463564b2 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/build/ComponentsConfigurator.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/build/ComponentsConfigurator.java
@@ -6,14 +6,15 @@ import java.util.List;
import org.unidal.lookup.configuration.AbstractResourceConfigurator;
import org.unidal.lookup.configuration.Component;
-import com.dianping.cat.agent.monitor.DataSender;
import com.dianping.cat.agent.monitor.EnvironmentConfig;
-import com.dianping.cat.agent.monitor.Executor;
-import com.dianping.cat.agent.monitor.TaskExecutors;
-import com.dianping.cat.agent.monitor.jvm.JVMMemoryExecutor;
-import com.dianping.cat.agent.monitor.jvm.JVMStateExecutor;
-import com.dianping.cat.agent.monitor.system.SystemPerformanceExecutor;
-import com.dianping.cat.agent.monitor.system.SystemStateExecutor;
+import com.dianping.cat.agent.monitor.executors.DataSender;
+import com.dianping.cat.agent.monitor.executors.Executor;
+import com.dianping.cat.agent.monitor.executors.TaskExecutors;
+import com.dianping.cat.agent.monitor.executors.jvm.JVMMemoryExecutor;
+import com.dianping.cat.agent.monitor.executors.jvm.JVMStateExecutor;
+import com.dianping.cat.agent.monitor.executors.system.SystemPerformanceExecutor;
+import com.dianping.cat.agent.monitor.executors.system.SystemStateExecutor;
+import com.dianping.cat.agent.monitor.puppet.PuppetTask;
public class ComponentsConfigurator extends AbstractResourceConfigurator {
@Override
@@ -27,11 +28,11 @@ public class ComponentsConfigurator extends AbstractResourceConfigurator {
all.add(C(Executor.class, SystemPerformanceExecutor.ID, SystemPerformanceExecutor.class).req(
EnvironmentConfig.class));
all.add(C(Executor.class, SystemStateExecutor.ID, SystemStateExecutor.class).req(EnvironmentConfig.class));
-
all.add(C(DataSender.class).req(EnvironmentConfig.class));
-
all.add(C(TaskExecutors.class).req(DataSender.class));
+ all.add(C(PuppetTask.class).req(EnvironmentConfig.class));
+
// Please keep it as last
all.addAll(new WebComponentConfigurator().defineComponents());
return all;
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/core/page/index/Handler.java b/cat-agent/src/main/java/com/dianping/cat/agent/core/page/index/Handler.java
index 98c30e76ce9bf15c95b3daeee8211917d2c98690..2f8a49c57b5688cb41dd42fb019073ca207a9494 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/core/page/index/Handler.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/core/page/index/Handler.java
@@ -11,8 +11,9 @@ import org.unidal.web.mvc.annotation.OutboundActionMeta;
import org.unidal.web.mvc.annotation.PayloadMeta;
import com.dianping.cat.agent.core.CorePage;
-import com.dianping.cat.agent.monitor.DataSender;
-import com.dianping.cat.agent.monitor.TaskExecutors;
+import com.dianping.cat.agent.monitor.executors.DataSender;
+import com.dianping.cat.agent.monitor.executors.TaskExecutors;
+import com.dianping.cat.agent.monitor.puppet.PuppetTask;
public class Handler implements PageHandler {
@Inject
@@ -24,6 +25,9 @@ public class Handler implements PageHandler {
@Inject
private TaskExecutors m_taskExecutors;
+ @Inject
+ private PuppetTask m_puppetTask;
+
@Override
@PayloadMeta(Payload.class)
@InboundActionMeta(name = "index")
@@ -37,7 +41,7 @@ public class Handler implements PageHandler {
model.setAction(Action.VIEW);
model.setPage(CorePage.INDEX);
-
+
if (!ctx.isProcessStopped()) {
m_jspViewer.view(ctx, model);
}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/EnvironmentConfig.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/EnvironmentConfig.java
index 7e07815cbd4e20c9d3b320e04e31cbb1c289b013..698350cbaa0ee1651ecca13e52874eaffd3ade21 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/EnvironmentConfig.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/EnvironmentConfig.java
@@ -15,15 +15,19 @@ public class EnvironmentConfig implements Initializable {
private static final String CONFIG_FILE = "/data/webapps/server.properties";
- private static final String URL_FORMAT = "http://%1$s/cat/r/monitor?op=batch×tamp=%2$s&group=%3$s&domain=%4$s";
+ private static final String SYSTEM_URL = "http://%1$s/cat/r/monitor?op=batch×tamp=%2$s&group=%3$s&domain=%4$s";
- private static final List m_servers = Arrays.asList("localhost:2281", "10.1.110.57:8080",
+ private static final String ALTERATION_URL = "http://%1$s/cat/r/alteration";
+
+ private static final List CAT_SERVERS = Arrays.asList("10.128.120.60:2281", "10.1.110.57:8080",
"10.1.110.23:8080", "10.1.110.21:8080");
private String m_ip;
private String m_domain;
+ private String m_hostName;
+
// host.name 配置规则:
// [${domain}01.nh0] [${domain}01.beta] [${domain}-ppe01.hm] [${domain}-sl-**] [${domain}-gp-**]
private String buildDomain(String hostName) {
@@ -43,11 +47,15 @@ public class EnvironmentConfig implements Initializable {
return domain;
}
- public String buildUrl(String server) {
+ public String buildSystemUrl(String server) {
String group = getGroup();
long current = System.currentTimeMillis();
- return String.format(URL_FORMAT, server, current, group, m_domain);
+ return String.format(SYSTEM_URL, server, current, group, m_domain);
+ }
+
+ public String buildAlterationUrl(String server) {
+ return String.format(ALTERATION_URL, server);
}
public String getDomain() {
@@ -63,7 +71,11 @@ public class EnvironmentConfig implements Initializable {
}
public List getServers() {
- return m_servers;
+ return CAT_SERVERS;
+ }
+
+ public String getHostName() {
+ return m_hostName;
}
@Override
@@ -73,13 +85,13 @@ public class EnvironmentConfig implements Initializable {
InputStream in = new BufferedInputStream(new FileInputStream(CONFIG_FILE));
properties.load(in);
- String hostName = properties.getProperty("host.name");
+ m_hostName = properties.getProperty("host.name");
- if (hostName == null) {
- hostName = NetworkInterfaceManager.INSTANCE.getLocalHostName();
+ if (m_hostName == null) {
+ m_hostName = NetworkInterfaceManager.INSTANCE.getLocalHostName();
}
- m_domain = buildDomain(hostName);
+ m_domain = buildDomain(m_hostName);
m_ip = properties.getProperty("host.ip");
if (m_ip == null) {
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/AbstractExecutor.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/AbstractExecutor.java
similarity index 88%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/AbstractExecutor.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/AbstractExecutor.java
index 022b886dd5ea6afbba09a50a7d7d3a5c5c04c4f7..4a494cc36703fabe0035634a701fb2b1f4d8916e 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/AbstractExecutor.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/AbstractExecutor.java
@@ -1,7 +1,9 @@
-package com.dianping.cat.agent.monitor;
+package com.dianping.cat.agent.monitor.executors;
import org.unidal.lookup.annotation.Inject;
+import com.dianping.cat.agent.monitor.EnvironmentConfig;
+
public abstract class AbstractExecutor implements Executor {
@Inject
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataEntity.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/DataEntity.java
similarity index 93%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataEntity.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/DataEntity.java
index 9448ab7ade27a7e65d78dd511ee842d1602d0528..e0b4d2a4b41ae99fc01a964a7aded0ace74d4a84 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataEntity.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/DataEntity.java
@@ -1,4 +1,4 @@
-package com.dianping.cat.agent.monitor;
+package com.dianping.cat.agent.monitor.executors;
public class DataEntity {
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataSender.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/DataSender.java
similarity index 81%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataSender.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/DataSender.java
index b39def68f1f4c86b2249cc313d3266d065f65ed4..346c9392a76dafdb093e68a02d55179f835bde97 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/DataSender.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/DataSender.java
@@ -1,4 +1,4 @@
-package com.dianping.cat.agent.monitor;
+package com.dianping.cat.agent.monitor.executors;
import java.io.InputStream;
import java.io.OutputStreamWriter;
@@ -18,6 +18,7 @@ import org.unidal.helper.Threads.Task;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
+import com.dianping.cat.agent.monitor.EnvironmentConfig;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Transaction;
@@ -26,7 +27,7 @@ public class DataSender implements Task, Initializable {
@Inject
private EnvironmentConfig m_environmentConfig;
- private BlockingQueue m_entities = new ArrayBlockingQueue(5000);
+ private static BlockingQueue m_entities = new ArrayBlockingQueue(5000);
private List m_dataEntities = new ArrayList();
@@ -37,14 +38,19 @@ public class DataSender implements Task, Initializable {
public boolean put(List entities) {
boolean result = true;
- for (DataEntity entity : entities) {
- boolean temp = m_entities.offer(entity);
+ try {
+ for (DataEntity entity : entities) {
+ boolean temp = m_entities.offer(entity, 5, TimeUnit.MILLISECONDS);
- if (!temp) {
- result = temp;
+ if (!temp) {
+ result = temp;
+ }
}
+ return result;
+ } catch (Exception e) {
+ Cat.logError(e);
}
- return result;
+ return false;
}
private String buildBatchEntities(List entities) {
@@ -85,7 +91,7 @@ public class DataSender implements Task, Initializable {
List servers = m_environmentConfig.getServers();
for (String server : servers) {
- String url = m_environmentConfig.buildUrl(server);
+ String url = m_environmentConfig.buildSystemUrl(server);
String entityContent = buildBatchEntities(m_dataEntities);
String content = "&batch=" + entityContent;
@@ -96,17 +102,19 @@ public class DataSender implements Task, Initializable {
@Override
public void run() {
- while (true) {
+ boolean active = true;
+
+ while (active) {
Transaction t = Cat.newTransaction("Data", "Send");
+ long current = System.currentTimeMillis();
try {
- long current = System.currentTimeMillis();
int maxSize = MAX_ENTITIES;
try {
while (m_entities.size() > 0 && maxSize > 0) {
DataEntity entity = m_entities.poll(5, TimeUnit.MILLISECONDS);
-
+
m_dataEntities.add(entity);
maxSize--;
}
@@ -122,14 +130,13 @@ public class DataSender implements Task, Initializable {
Cat.logError(e);
}
long duration = System.currentTimeMillis() - current;
- long sleeptime = DURATION - duration;
- if (sleeptime > 0) {
- try {
- Thread.sleep(sleeptime);
- } catch (InterruptedException e) {
- break;
+ try {
+ if (duration < DURATION) {
+ Thread.sleep(DURATION - duration);
}
+ } catch (InterruptedException e) {
+ active = false;
}
t.setStatus(Transaction.SUCCESS);
} catch (Exception e) {
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/Executor.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/Executor.java
similarity index 70%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/Executor.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/Executor.java
index c0c2326370fb5a46efebea0ffb5e3c6b879b2678..39fec0484f0aec9392a8372dc597dd2072ae79c8 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/Executor.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/Executor.java
@@ -1,4 +1,4 @@
-package com.dianping.cat.agent.monitor;
+package com.dianping.cat.agent.monitor.executors;
import java.util.List;
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/TaskExecutors.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/TaskExecutors.java
similarity index 87%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/TaskExecutors.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/TaskExecutors.java
index de094268f2d268fccf9466b0997ebda3c526f893..07958bd383468fb45db82b29e921329cbbf07de0 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/TaskExecutors.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/TaskExecutors.java
@@ -1,4 +1,4 @@
-package com.dianping.cat.agent.monitor;
+package com.dianping.cat.agent.monitor.executors;
import java.util.Collection;
import java.util.List;
@@ -38,7 +38,9 @@ public class TaskExecutors extends ContainerHolder implements Task, Initializabl
@Override
public void run() {
- while (true) {
+ boolean active = true;
+
+ while (active) {
Transaction t = Cat.newTransaction("Data", "Fetch");
try {
@@ -48,7 +50,7 @@ public class TaskExecutors extends ContainerHolder implements Task, Initializabl
Transaction t2 = Cat.newTransaction("Executor", executor.getId());
try {
List entities = executor.execute();
-
+
m_sender.put(entities);
t2.setStatus(Transaction.SUCCESS);
} catch (Exception e) {
@@ -59,14 +61,13 @@ public class TaskExecutors extends ContainerHolder implements Task, Initializabl
}
}
long duration = System.currentTimeMillis() - current;
- long sleeptime = DURATION - duration;
- if (sleeptime > 0) {
- try {
- Thread.sleep(sleeptime);
- } catch (InterruptedException e) {
- break;
+ try {
+ if (duration < DURATION) {
+ Thread.sleep(DURATION - duration);
}
+ } catch (InterruptedException e) {
+ active = false;
}
t.setStatus(Transaction.SUCCESS);
} catch (Exception e) {
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMMemoryExecutor.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/jvm/JVMMemoryExecutor.java
similarity index 92%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMMemoryExecutor.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/jvm/JVMMemoryExecutor.java
index 0b973c82c700bd6665ed2b588dc70d686ee69a83..27cac6ee13b2b2bb1f6ac1777c893405b2d822f8 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMMemoryExecutor.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/jvm/JVMMemoryExecutor.java
@@ -1,4 +1,4 @@
-package com.dianping.cat.agent.monitor.jvm;
+package com.dianping.cat.agent.monitor.executors.jvm;
import java.io.BufferedReader;
import java.io.InputStreamReader;
@@ -6,8 +6,8 @@ import java.util.ArrayList;
import java.util.List;
import com.dianping.cat.Cat;
-import com.dianping.cat.agent.monitor.AbstractExecutor;
-import com.dianping.cat.agent.monitor.DataEntity;
+import com.dianping.cat.agent.monitor.executors.AbstractExecutor;
+import com.dianping.cat.agent.monitor.executors.DataEntity;
public class JVMMemoryExecutor extends AbstractExecutor {
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMStateExecutor.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/jvm/JVMStateExecutor.java
similarity index 89%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMStateExecutor.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/jvm/JVMStateExecutor.java
index 36aecade21d7e1dd9a5ad40f6d19607af79dd4f1..5ce4272823d4d5e061c4bee701aed7f34418f87a 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/jvm/JVMStateExecutor.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/jvm/JVMStateExecutor.java
@@ -1,11 +1,11 @@
-package com.dianping.cat.agent.monitor.jvm;
+package com.dianping.cat.agent.monitor.executors.jvm;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
-import com.dianping.cat.agent.monitor.AbstractExecutor;
-import com.dianping.cat.agent.monitor.DataEntity;
+import com.dianping.cat.agent.monitor.executors.AbstractExecutor;
+import com.dianping.cat.agent.monitor.executors.DataEntity;
public class JVMStateExecutor extends AbstractExecutor {
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemPerformanceExecutor.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/system/SystemPerformanceExecutor.java
similarity index 96%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemPerformanceExecutor.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/system/SystemPerformanceExecutor.java
index 6288385929c5c23cb54240cfb7a5d15fff50e5e7..970f4619e7e0c131c67539a341384e603f79c914 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemPerformanceExecutor.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/system/SystemPerformanceExecutor.java
@@ -1,4 +1,4 @@
-package com.dianping.cat.agent.monitor.system;
+package com.dianping.cat.agent.monitor.executors.system;
import java.util.ArrayList;
import java.util.Arrays;
@@ -13,8 +13,8 @@ import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;
import com.dianping.cat.Cat;
-import com.dianping.cat.agent.monitor.AbstractExecutor;
-import com.dianping.cat.agent.monitor.DataEntity;
+import com.dianping.cat.agent.monitor.executors.AbstractExecutor;
+import com.dianping.cat.agent.monitor.executors.DataEntity;
public class SystemPerformanceExecutor extends AbstractExecutor {
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemStateExecutor.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/system/SystemStateExecutor.java
similarity index 96%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemStateExecutor.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/system/SystemStateExecutor.java
index 7bf0b4ff891e6cd77b3c463dac48456b7f81f180..ceb87233e94bdb7675024c1efbb1ee38bed34a22 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/system/SystemStateExecutor.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/executors/system/SystemStateExecutor.java
@@ -1,4 +1,4 @@
-package com.dianping.cat.agent.monitor.system;
+package com.dianping.cat.agent.monitor.executors.system;
import java.io.BufferedReader;
import java.io.FileReader;
@@ -15,8 +15,8 @@ import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Uptime;
import com.dianping.cat.Cat;
-import com.dianping.cat.agent.monitor.AbstractExecutor;
-import com.dianping.cat.agent.monitor.DataEntity;
+import com.dianping.cat.agent.monitor.executors.AbstractExecutor;
+import com.dianping.cat.agent.monitor.executors.DataEntity;
public class SystemStateExecutor extends AbstractExecutor implements Initializable {
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Alertation.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Alteration.java
similarity index 81%
rename from cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Alertation.java
rename to cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Alteration.java
index 154f4d37d63df145810e5e13187b129832ecf002..49ec3d74b8425278b1b9856099f54b7d29db44f1 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Alertation.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Alteration.java
@@ -1,6 +1,13 @@
package com.dianping.cat.agent.monitor.puppet;
-public class Alertation {
+public class Alteration {
+ @Override
+ public String toString() {
+ return "Alteration [type=" + type + ", ip=" + ip + ", hostname=" + hostname + ", user=" + user + ", url=" + url
+ + ", op=" + op + ", date=" + date + ", title=" + title + ", content=" + content + ", domain=" + domain
+ + ", group=" + group + "]";
+ }
+
private String type;
private String ip;
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/AlterationParser.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/AlterationParser.java
new file mode 100644
index 0000000000000000000000000000000000000000..653297686ae92ff76fa264afaf6541b32a1fea08
--- /dev/null
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/AlterationParser.java
@@ -0,0 +1,121 @@
+package com.dianping.cat.agent.monitor.puppet;
+
+import java.io.File;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Locale;
+import java.util.regex.Pattern;
+
+import com.dianping.cat.Cat;
+import com.dianping.cat.agent.monitor.EnvironmentConfig;
+
+public class AlterationParser {
+
+ private EnvironmentConfig m_environmentConfig;
+
+ public AlterationParser(EnvironmentConfig config) {
+ m_environmentConfig = config;
+ }
+
+ public Alteration parse(String line) {
+ String title_add = "";
+ String group = "puppet";
+ String type = "puppet";
+ String user = "puppet";
+ String url = "";
+ String op = "insert";
+ String date = "";
+ String content = "";
+ String host = m_environmentConfig.getHostName();
+ String IP = m_environmentConfig.getIp();
+ String domain = m_environmentConfig.getDomain();
+ String title = "puppet";
+ String regEx = ".*puppet-agent.*\\(\\/Stage";
+ String regEx_Filebucketed = ".*Filebucketed.*";
+ String tmp = "";
+ Alteration alertation = new Alteration();
+
+ // 'Mar 25 10:56:27 localhost puppet-agent[24773]: (/Stage[main]/Zabbix::Agentd/Exec[restart_zabbix_agentd]) Triggered
+ // 'refresh' from 1 events'
+
+ if (Pattern.compile(regEx).matcher(line).find()) {
+ String[] tmp_list = line.split(" +");
+ Calendar c = Calendar.getInstance();
+ int year = c.get(Calendar.YEAR);
+
+ if (tmp_list.length >= 4) {
+ date = tmp_list[0] + " " + tmp_list[1] + " " + tmp_list[2] + " " + Integer.toString(year);
+ }
+
+ String all_content = line.split("\\(")[1]; // "/Stage[main]/Zabbix::Agentd/Exec[restart_zabbix_agentd]) Triggered 'refresh' from 1 events"
+ String[] tmp_string = all_content.split("\\)"); // '/Stage[main]/Zabbix::Agentd/Exec[restart_zabbix_agentd]', ''
+ String[] tmp_string_main = tmp_string[0].split("\\[main\\]\\/");
+ if (tmp_string_main.length >= 2) {
+ title = tmp_string_main[1].split("\\[")[1].split("\\]")[0]; // restart_zabbix_agentd
+ if (title == "") {
+ title = "puppet";
+ }
+ }
+ if (tmp_string.length >= 2) {
+ content = all_content.split("\\)")[1]; // " Triggered 'refresh' from 1 events"
+ String[] tmpContent = content.split(" ");
+
+ if (tmpContent.length >= 3) {
+ title_add = tmpContent[1] + " " + tmpContent[2];
+ title = title + " " + title_add; // restart_zabbix_agentd Triggered 'refresh'
+ }
+ }
+ if (Pattern.compile(regEx_Filebucketed).matcher(content).find()) {
+ String[] tmpContent = content.split(" ");
+ String new_file = tmpContent[2];// "/usr/local/nginx/conf/nginx_app.conf"
+
+ // " Filebucketed /usr/local/nginx/conf/nginx_app.conf to puppet with sum 99c3e5f79645493fdcf4340dd457cbe4"
+ if (tmpContent.length >= 8) {
+ String old_file_index = tmpContent[7]; // 99c3e5f79645493fdcf4340dd457cbe4
+ String old_file_dir = Utils.runSysCmd("find /var/lib/puppet/clientbucket -name " + old_file_index)
+ .toString().split("\n")[0];
+ String old_file = old_file_dir + "/contents";
+
+ if (new File(new_file).exists() && new File(old_file).exists()) {
+ tmp = Utils.runSysCmd("diff " + old_file + " " + new_file).toString();
+ }
+ if (tmp.trim() != "") {
+ content = tmp;
+ }
+ }
+ }
+ SimpleDateFormat sdf_mmm = new SimpleDateFormat("MMMM dd HH:mm:ss yyyy", Locale.US);
+ SimpleDateFormat sdf_normal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
+
+ try {
+ date = sdf_normal.format(sdf_mmm.parse(date));
+ } catch (ParseException e1) {
+ Cat.logError(e1);
+ sdf_mmm = new SimpleDateFormat("MMMM dd HH:mm:ss yyyy", Locale.US);
+ try {
+ date = sdf_normal.format(sdf_mmm.parse(date));
+ } catch (ParseException e) {
+ Cat.logError(e);
+ }
+ return null;
+ }
+
+ alertation.setHostname(host);
+ alertation.setIp(IP);
+ alertation.setDomain(domain);
+ alertation.setTitle(title);
+ alertation.setContent(content);
+ alertation.setOp(op);
+ alertation.setUrl(url);
+ alertation.setUser(user);
+ alertation.setType(type);
+ alertation.setDate(date);
+ alertation.setGroup(group);
+ } else {
+ alertation = null;
+ }
+ return alertation;
+ }
+
+}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/DataSender.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/DataSender.java
new file mode 100644
index 0000000000000000000000000000000000000000..a65da8690d4d942e7a7a42fc24d4b162d7a035e1
--- /dev/null
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/DataSender.java
@@ -0,0 +1,87 @@
+package com.dianping.cat.agent.monitor.puppet;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.unidal.helper.Files;
+
+import com.dianping.cat.Cat;
+import com.dianping.cat.agent.monitor.EnvironmentConfig;
+import com.dianping.cat.message.Event;
+
+public class DataSender {
+
+ private EnvironmentConfig m_environmentConfig;
+
+ public DataSender(EnvironmentConfig config) {
+ m_environmentConfig = config;
+ }
+
+ public void send(Alteration alertation) {
+ String[] pars = new String[11];
+ pars[0] = "type=" + alertation.getType();
+ pars[1] = "title=" + alertation.getTitle();
+ pars[2] = "domain=" + alertation.getDomain();
+ pars[3] = "ip=" + alertation.getIp();
+ pars[4] = "user=" + alertation.getUser();
+ pars[5] = "content=" + alertation.getContent();
+ pars[6] = "url=" + alertation.getUrl();
+ pars[7] = "op=" + alertation.getOp();
+ pars[8] = "alterationDate=" + alertation.getDate();
+ pars[9] = "hostname=" + alertation.getHostname();
+ pars[10] = "group=" + alertation.getGroup();
+
+ for (String server : m_environmentConfig.getServers()) {
+ String url = m_environmentConfig.buildAlterationUrl(server);
+ if (postData(pars, url)) {
+ break;
+ }
+ }
+ }
+
+ public boolean postData(String[] params, String urlStr) {
+ URL url = null;
+ HttpURLConnection con = null;
+ String result = null;
+ String paramsTemp = "";
+
+ try {
+ url = new URL(urlStr);
+
+ con = (HttpURLConnection) url.openConnection();
+ con.setUseCaches(false);
+ con.setDoOutput(true);
+ con.setRequestMethod("POST");
+
+ for (String param : params) {
+ if (param != null && !"".equals(param.trim())) {
+ paramsTemp += "&" + param;
+ }
+ }
+
+ byte[] b = paramsTemp.getBytes();
+
+ con.getOutputStream().write(b); // to be tested
+ con.getOutputStream().flush();
+ con.getOutputStream().close();
+
+ result = Files.forIO().readFrom(con.getInputStream(), "utf-8");
+ } catch (IOException e) {
+ Cat.logError(e);
+ } finally {
+ if (con != null) {
+ con.disconnect();
+ }
+ }
+ boolean flag = false;
+
+ if (result.contains("200")) {
+ flag = true;
+ } else {
+ Cat.logEvent("Puppet", "Failed in posting data", Event.SUCCESS, result);
+ }
+ return flag;
+ }
+
+}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/PuppetTask.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/PuppetTask.java
index 4f19186d997e7a0a7cf86c5e72375c550ef69e96..ad0a08eb5a4232af6ed68c3490f0d21445016471 100644
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/PuppetTask.java
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/PuppetTask.java
@@ -3,43 +3,46 @@ package com.dianping.cat.agent.monitor.puppet;
import java.io.IOException;
import java.io.RandomAccessFile;
-import org.apache.log4j.Logger;
-import org.apache.log4j.PropertyConfigurator;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
+import org.unidal.helper.Threads;
import org.unidal.helper.Threads.Task;
+import org.unidal.lookup.annotation.Inject;
-import com.dianping.cat.agent.monitor.puppet.util.CreatDir;
-import com.dianping.cat.agent.monitor.puppet.util.GetReaderPostion;
-import com.dianping.cat.agent.monitor.puppet.util.Parse;
-import com.dianping.cat.agent.monitor.puppet.util.RunSysCmd;
-import com.dianping.cat.agent.monitor.puppet.util.SendHttp;
-import com.dianping.cat.agent.monitor.puppet.util.SetReaderPostion;
+import com.dianping.cat.Cat;
+import com.dianping.cat.agent.monitor.EnvironmentConfig;
+import com.dianping.cat.message.Transaction;
public class PuppetTask implements Task, Initializable {
- private static String m_logFile;
+ @Inject
+ private EnvironmentConfig m_environmentConfig;
- private static String m_lineFile;
+ private DataSender m_dataSender;
- private static Logger puppetLogger = Logger.getLogger("myLogger");
+ private AlterationParser m_alterationParser;
+
+ private static final String LOG_FILE = "/var/log/messages";
+
+ private static final int DURATION = 60 * 1000;
+
+ private ReaderManager m_readerManager = new ReaderManager();
@Override
public void run() {
- SendHttp sendhttp = new SendHttp();
- Parse parse = new Parse();
- GetReaderPostion getreaderpostion = new GetReaderPostion();
- SetReaderPostion setreaderpostion = new SetReaderPostion();
boolean active = true;
Long end_position = 0L;
- RunSysCmd runsyscmd = new RunSysCmd();
+ Transaction t = Cat.newTransaction("Puppet", "Task");
while (active) {
- Alertation alertation = null;
- Long position = getreaderpostion.getReaderPostion(m_lineFile);
+ long current = System.currentTimeMillis();
+ Alteration alertation = null;
+ Long position = m_readerManager.queryPointer();
RandomAccessFile reader = null;
+
try {
- reader = new RandomAccessFile(m_logFile, "r");
+ reader = new RandomAccessFile(LOG_FILE, "r");
+
reader.seek(position);
// 判断日志是否切割了,一定要放在while((line=reader.readLine())!=null)之前,否则回导致反复读取
if (position >= 2) {
@@ -48,54 +51,53 @@ public class PuppetTask implements Task, Initializable {
reader.readChar();
reader.seek(position);
} catch (IOException e) {
- setreaderpostion.setReaderPostion(m_lineFile, 0L);
+ m_readerManager.updatePointer(0L);
reader.seek(0L);
- puppetLogger.error(e.getMessage(), e);
+ Cat.logError(e);
}
}
String line = null;
while ((line = reader.readLine()) != null) {
- alertation = parse.parse(line);
+ alertation = m_alterationParser.parse(line);
+
if (alertation != null) {
- sendhttp.sendHttp(alertation);
+ m_dataSender.send(alertation);
} else {
continue;
}
}
end_position = reader.getFilePointer();
- } catch (IOException e) {
- puppetLogger.error("读文件异常:" + m_logFile);
- puppetLogger.error(e.getMessage(), e);
- } finally {
if (end_position > position) {
- setreaderpostion.setReaderPostion(m_lineFile, end_position);
+ m_readerManager.updatePointer(end_position);
}
+ t.setStatus(Transaction.SUCCESS);
+ } catch (IOException e) {
+ Cat.logError("读文件异常:" + LOG_FILE, e);
+ } finally {
+
try {
- reader.close();
+ if (reader != null) {
+ reader.close();
+ }
} catch (IOException e) {
- puppetLogger.error(e.getMessage(), e);
+ Cat.logError(e);
}
- puppetLogger.info("本次读取的开始偏移量:" + position + " 末尾偏移量:" + end_position);
- }
- try {
- Thread.sleep(1000 * 5);
- } catch (InterruptedException e) {
- puppetLogger.error(e.getMessage(), e);
- }
- }
- }
- @Override
- public void initialize() throws InitializationException {
- m_logFile = "/var/log/messages";
- m_lineFile = "/var/log/line_random.log";
+ long duration = System.currentTimeMillis() - current;
+
+ try {
+ if (duration < DURATION) {
+ Thread.sleep(DURATION - duration);
+ }
+ } catch (InterruptedException e) {
+ active = false;
+ }
- CreatDir creatdir = new CreatDir();
- creatdir.creatDir("/data/applogs/monitor");
- PropertyConfigurator.configure("log4j.properties");
-// Threads.forGroup("Cat").start(this);
+ t.complete();
+ }
+ }
}
@Override
@@ -107,4 +109,11 @@ public class PuppetTask implements Task, Initializable {
public void shutdown() {
}
+
+ @Override
+ public void initialize() throws InitializationException {
+ m_dataSender = new DataSender(m_environmentConfig);
+ m_alterationParser = new AlterationParser(m_environmentConfig);
+ Threads.forGroup("Cat").start(this);
+ }
}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/ReaderManager.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/ReaderManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..3ca164ce619e4fbb32f99514f391ad9137d72c58
--- /dev/null
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/ReaderManager.java
@@ -0,0 +1,61 @@
+package com.dianping.cat.agent.monitor.puppet;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import com.dianping.cat.Cat;
+
+public class ReaderManager {
+
+ private static final String POINTER_FILE = "/var/log/currentPointer";
+
+ public long queryPointer() {
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new FileReader(POINTER_FILE));
+ String str = reader.readLine();
+ if (str != null) {
+ return Long.parseLong(str);
+ } else {
+ return 0L;
+ }
+ } catch (FileNotFoundException e1) {
+ File filename = new File(POINTER_FILE);
+ try {
+ filename.createNewFile();
+ } catch (IOException e2) {
+ Cat.logError("创建文件失败:" + POINTER_FILE, e2);
+ }
+ } catch (Exception e3) {
+ Cat.logError(e3);
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ Cat.logError(e);
+ }
+ }
+
+ }
+ return 0L;
+ }
+
+ public void updatePointer(long end_position) {
+ BufferedWriter output = null;
+ try {
+ output = new BufferedWriter(new FileWriter(POINTER_FILE));
+ output.write(Long.toString(end_position));
+ output.close();
+
+ } catch (IOException e) {
+ Cat.logError("写入文件异:" + POINTER_FILE, e);
+ }
+ }
+
+}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Utils.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Utils.java
new file mode 100644
index 0000000000000000000000000000000000000000..d65d202161b7fd973b6d9504164b27308121a4d5
--- /dev/null
+++ b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/Utils.java
@@ -0,0 +1,55 @@
+package com.dianping.cat.agent.monitor.puppet;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.regex.Pattern;
+
+import com.dianping.cat.Cat;
+import com.dianping.cat.message.Event;
+
+public class Utils {
+
+ public static StringBuffer runSysCmd(String cmd) {
+ String regEx = "^chown.*|^diff.*|^find.*";
+
+ StringBuffer result = new StringBuffer();
+ Runtime run = Runtime.getRuntime();
+ Process p = null;
+
+ if (!Pattern.compile(regEx).matcher(cmd).find()) {
+ Cat.logEvent("Puppet", "runSysCms failed", Event.SUCCESS, "不支持命令:" + cmd);
+ return null;
+ }
+ try {
+ p = run.exec(cmd);
+ BufferedInputStream in = new BufferedInputStream(p.getInputStream());
+ BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
+ String lineStr;
+ while ((lineStr = inBr.readLine()) != null) {
+ result.append(lineStr + "\n");
+ }
+ if (p.waitFor() != 0) {
+ if (p.exitValue() != 0)// p.exitValue()==0表示正常结束,1:非正常结束
+ Cat.logEvent("Puppet", "命令执行失败?: " + cmd);
+ }
+ inBr.close();
+ in.close();
+ } catch (Exception e) {
+ Cat.logError(e);
+ } finally {
+ if (p != null) {
+ try {
+ p.getOutputStream().close();
+ p.getInputStream().close();
+ p.getErrorStream().close();
+ } catch (IOException e) {
+ Cat.logError(e);
+ }
+ }
+ }
+ return result;
+
+ }
+}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/log4j.properties b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/log4j.properties
deleted file mode 100644
index 7cc14ace51e05ab9178a7de4c7e96d7a2d908793..0000000000000000000000000000000000000000
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/log4j.properties
+++ /dev/null
@@ -1,14 +0,0 @@
-
-log4j.rootLogger=info,file
-log4j.logger.myLogger=info
-log4j.logger.myLogger.mySonLogger=,file
-
-log4j.appender.console=org.apache.log4j.ConsoleAppender
-log4j.appender.console.layout=org.apache.log4j.SimpleLayout
-
-
-log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
-log4j.appender.file.File=/data/applogs/monitor/puppetmonitor.log
-log4j.appender.file.DatePattern='.'yyyy-MM-dd
-log4j.appender.file.layout=org.apache.log4j.PatternLayout
-log4j.appender.file.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss,SSS} %t %m%n
\ No newline at end of file
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/CreatDir.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/CreatDir.java
deleted file mode 100644
index a79e324c89fd0358677544e531d5b8a70182b51c..0000000000000000000000000000000000000000
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/CreatDir.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.dianping.cat.agent.monitor.puppet.util;
-
-import java.io.File;
-
-import org.apache.log4j.Logger;
-
-public class CreatDir {
- private Logger puppetLogger = Logger.getLogger("myLogger");
-
- public boolean creatDir(String dir){
- File file =new File(dir);
- if(!file .exists()){
- file.mkdirs();
- return true;
- }else{
- return false;
- }
- }
-
-}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/GetReaderPostion.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/GetReaderPostion.java
deleted file mode 100644
index ef92091949cf3d934d9a586ec4cc4a53694697fd..0000000000000000000000000000000000000000
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/GetReaderPostion.java
+++ /dev/null
@@ -1,54 +0,0 @@
-package com.dianping.cat.agent.monitor.puppet.util;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-
-import org.apache.log4j.Logger;
-
-
-public class GetReaderPostion {
- private Logger puppetLogger = Logger.getLogger("myLogger");
-
- /**
- *
- * @param line_file,记录文件读取位置的文件
- * @return 记录的数据,否则返回0
- * 读取文件失败的时候是否创建文件line_file
- *
- */
- public long getReaderPostion(String line_file) {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(line_file));
- String str = reader.readLine();
- if (str != null) {
- return Long.parseLong(str);
- }else{
- return 0L;
- }
- }catch(FileNotFoundException e1){
- File filename = new File(line_file);
- try {
- filename.createNewFile();
- } catch (IOException e2) {
- puppetLogger.error("创建文件失败:" + line_file);
- puppetLogger.error(e2.getMessage(),e2);
- }
- } catch (Exception e3) {
- puppetLogger.error(e3.getMessage(),e3);
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- puppetLogger.error(e.getMessage(),e);
- }
- }
-
- }
- return 0L;
- }
-}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/HttpPostUtils.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/HttpPostUtils.java
deleted file mode 100644
index 1dfa0ee13e3e46c591bc90f373ccfbd6900185d2..0000000000000000000000000000000000000000
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/HttpPostUtils.java
+++ /dev/null
@@ -1,85 +0,0 @@
-package com.dianping.cat.agent.monitor.puppet.util;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import org.apache.log4j.Logger;
-
-public class HttpPostUtils {
- private static Logger puppetLogger = Logger.getLogger("myLogger");
-
- private String urlAddress;
-
- public String getUrlAddress() {
- return urlAddress;
- }
-
- public void setUrlAddress(String urlAddress) {
- this.urlAddress = urlAddress;
- }
-
- public boolean httpPost(String[] params) {
- boolean flag = false;
- URL url = null;
- HttpURLConnection con = null;
- BufferedReader in = null;
- StringBuffer result = new StringBuffer();
- String paramsTemp = "";
-
- try {
- url = new URL(this.urlAddress);
- con = (HttpURLConnection) url.openConnection();
- con.setUseCaches(false);
- con.setDoOutput(true);
- con.setRequestMethod("POST");
- for (String param : params) {
- if (param != null && !"".equals(param.trim())) {
- paramsTemp += "&" + param;
- }
- }
-
- // puppetLogger.info("POST para:"+paramsTemp);
-
- byte[] b = paramsTemp.getBytes();
- con.getOutputStream().write(b); // to be tested
- con.getOutputStream().flush();
- con.getOutputStream().close();
- in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- while (true) {
- String line = in.readLine();
- if (line == null) {
- break;
- } else {
- result.append(line);
- }
- }
- } catch (IOException e) {
- // e.printStackTrace();
- // puppetLogger.debug(e);
- puppetLogger.error(e.getMessage(), e);
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (con != null) {
- con.disconnect();
- }
- } catch (IOException e) {
- puppetLogger.error(e.getMessage(), e);
- }
- }
- String rs = result.toString();
- if (rs.contains("200")) {
- flag = true;
- puppetLogger.info("POST Succ");
- } else {
- flag = false;
- puppetLogger.error("POST Fail:" + paramsTemp);
- }
- return flag;
- }
-
-}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/Parse.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/Parse.java
deleted file mode 100644
index 6e3239901762cf058628fcda5fa151b1f28c9956..0000000000000000000000000000000000000000
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/Parse.java
+++ /dev/null
@@ -1,128 +0,0 @@
-package com.dianping.cat.agent.monitor.puppet.util;
-
-import java.io.File;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Locale;
-import java.util.regex.Pattern;
-
-import org.apache.log4j.Logger;
-import com.dianping.cat.agent.monitor.puppet.Alertation;
-
-
-public class Parse {
- private Logger puppetLogger = Logger.getLogger("myLogger");
-
- public Alertation parse(String line) {
- RunSysCmd runsyscmd=new RunSysCmd();
- String title_add="";
- String group="puppet";
- String type="puppet";
- String user="puppet";
- String url="";
- String op="insert";
- String host="";
- String IP="";
- String date="";
- String domain="";
- String content="";
- String title="puppet";
- String regEx = ".*puppet-agent.*\\(\\/Stage";
- String regEx_time = ".*[0-9]{2}:[0-9]{2}:[0-9]{2}.*";
-
- String regEx_Filebucketed=".*Filebucketed.*";
- String tmp="";
- Alertation alertation =new Alertation();
- if (Pattern.compile(regEx).matcher(line).find()) {
- String[] tmp_list = line.split(" ");
- Calendar c = Calendar.getInstance();
- int year = c.get(Calendar.YEAR);
- InetAddress ia=null;
- if(tmp_list.length>=4){
- if(Pattern.compile(regEx_time).matcher(tmp_list[3]).find()){
- date = tmp_list[0] +" " + tmp_list[2] + " "+tmp_list[3] + " " +Integer.toString(year);
- }else{
- date = tmp_list[0] +" " + tmp_list[1]+ " " + tmp_list[2] + " " +Integer.toString(year);
- }
- }
- String all_content=line.split("\\(")[1];
- String[] tmp_string=all_content.split("\\)");
- String[] tmp_string_main=tmp_string[0].split("\\[main\\]\\/");
- if (tmp_string_main.length >=2){
- title=tmp_string_main[1].split("\\[")[1].split("\\]")[0];
- if(title==""){
- title="puppet";
- }
- }
- if(tmp_string.length>=2){
- content=all_content.split("\\)")[1];
- if(content.split(" ").length>=3){
- title_add=content.split(" ")[1]+" "+content.split(" ")[2];
- title=title+" "+title_add;
- }
- }
- if(Pattern.compile(regEx_Filebucketed).matcher(content).find()){
- String new_file=content.split(" ")[2];
- if(content.split(" ").length >=8){
- String old_file_index=content.split(" ")[7];
- //==========test======
-// String old_file_dir=runsyscmd.runSysCmd("/usr/bin/find /var/lib/puppet/clientbucket -name "+old_file_index).toString().split("\n")[0];
- String old_file_dir=runsyscmd.runSysCmd("find /var/lib/puppet/clientbucket -name "+old_file_index).toString().split("\n")[0];
- String old_file=old_file_dir+"/contents";
- if(new File(new_file).exists() && new File(old_file).exists() ){
- puppetLogger.info("diff content ing "+" "+old_file+" "+new_file );
- tmp=runsyscmd.runSysCmd("diff "+old_file+" "+ new_file).toString();
- }
- if(tmp.trim()!=""){
- content=tmp;
- }else{
- puppetLogger.warn("diff return content is empty");
- }
- }
- }
- SimpleDateFormat sdf_mmm = new SimpleDateFormat("MMMM dd HH:mm:ss yyyy",Locale.US);
- SimpleDateFormat sdf_normal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.US);
- try {
- date=sdf_normal.format(sdf_mmm.parse(date));
- ia = InetAddress.getLocalHost();
- } catch (ParseException e1) {
- puppetLogger.error(e1.getMessage(),e1);
- sdf_mmm = new SimpleDateFormat("MMMM dd HH:mm:ss yyyy",Locale.US);
- try {
- date=sdf_normal.format(sdf_mmm.parse(date));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }catch(UnknownHostException e){
- alertation.setHostname("Unknow_hostname");
- alertation.setDomain("Unknow_domain");
- e.printStackTrace();
- }
- if(ia!=null){
- host = ia.getHostName();
- IP= ia.getHostAddress();
- domain=host.split("[0-9]")[0].split("-sl-|-gp-|-ppe")[0];
- }
- alertation.setHostname(host);
- alertation.setIp(IP);
- alertation.setDomain(domain);
- alertation.setTitle(title);
- alertation.setContent(content);
- alertation.setOp(op);
- alertation.setUrl(url);
- alertation.setUser(user);
- alertation.setType(type);
- alertation.setDate(date);
- alertation.setGroup(group);
- }
- else{
- alertation=null;
- }
- return alertation;
- }
-
-}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/RunSysCmd.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/RunSysCmd.java
deleted file mode 100644
index ab22cb7b7053ea7fb1178da9ed0792881c5412f8..0000000000000000000000000000000000000000
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/RunSysCmd.java
+++ /dev/null
@@ -1,58 +0,0 @@
-package com.dianping.cat.agent.monitor.puppet.util;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.util.regex.Pattern;
-
-import org.apache.log4j.Logger;
-
-public class RunSysCmd {
- private Logger puppetLogger = Logger.getLogger("myLogger");
-
- public StringBuffer runSysCmd(String cmd){
-// puppetLogger.info("执行命令ing:"+cmd);
- String regEx = "^chown.*|^diff.*|^find.*";
-
- StringBuffer result = new StringBuffer();
- Runtime run = Runtime.getRuntime();
- Process p=null;
- if(!Pattern.compile(regEx).matcher(cmd).find()){
- System.out.println("不支持改命令"+cmd);
- return null;
- }
- try {
- p = run.exec(cmd);
- BufferedInputStream in = new BufferedInputStream(p.getInputStream());
- BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
- String lineStr;
- while ((lineStr = inBr.readLine()) != null){
- result.append(lineStr+"\n");
- }
- if (p.waitFor() != 0) {
- if (p.exitValue() != 0)//p.exitValue()==0表示正常结束,1:非正常结束
- puppetLogger.warn("命令执行失败?: "+cmd);
- }
- inBr.close();
- in.close();
- } catch (Exception e) {
- puppetLogger.error(e.getMessage(),e);
- }finally
- {
- if ( p != null)
- {
- try {
- p.getOutputStream().close();
- p.getInputStream().close();
- p.getErrorStream().close();
- } catch (IOException e) {
- puppetLogger.error(e.getMessage(),e);
- }
- }
- }
- return result;
-
-
- }
-}
diff --git a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/SendHttp.java b/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/SendHttp.java
deleted file mode 100644
index 0ab177a386cc48595d0e0cff72f953f6fc4a8cd0..0000000000000000000000000000000000000000
--- a/cat-agent/src/main/java/com/dianping/cat/agent/monitor/puppet/util/SendHttp.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package com.dianping.cat.agent.monitor.puppet.util;
-
-import com.dianping.cat.agent.monitor.puppet.Alertation;
-
-//import org.apache.log4j.Logger;
-
-public class SendHttp {
-// private Logger puppetLogger = Logger.getLogger("myLogger");
-
- public void sendHttp(Alertation alertation) {
- HttpPostUtils httppost=new HttpPostUtils();
-// StringBuffer pars_str=new StringBuffer();
- String[] catip={"10.1.110.23","10.1.6.102","10.1.6.128 "};
-
- String[] pars= new String[11];
-
- pars[0]="type="+alertation.getType();
- pars[1]="title="+alertation.getTitle();
- pars[2]="domain="+alertation.getDomain();
- pars[3]="ip="+alertation.getIp();
- pars[4]="user="+alertation.getUser();
- pars[5]="content="+alertation.getContent();
- pars[6]="url="+alertation.getUrl();
- pars[7]="op="+alertation.getOp();
- pars[8]="alterationDate="+alertation.getDate();
- pars[9]="hostname="+alertation.getHostname();
- pars[10]="group="+alertation.getGroup();
-// for(int i=0;icom.dianping.cat.agent.monitor.EnvironmentConfig
- com.dianping.cat.agent.monitor.Executor
+ com.dianping.cat.agent.monitor.executors.Executor
JVMMemoryExecutor
- com.dianping.cat.agent.monitor.jvm.JVMMemoryExecutor
+ com.dianping.cat.agent.monitor.executors.jvm.JVMMemoryExecutor
com.dianping.cat.agent.monitor.EnvironmentConfig
@@ -15,9 +15,9 @@
- com.dianping.cat.agent.monitor.Executor
+ com.dianping.cat.agent.monitor.executors.Executor
JVMStateExecutor
- com.dianping.cat.agent.monitor.jvm.JVMStateExecutor
+ com.dianping.cat.agent.monitor.executors.jvm.JVMStateExecutor
com.dianping.cat.agent.monitor.EnvironmentConfig
@@ -25,9 +25,9 @@
- com.dianping.cat.agent.monitor.Executor
+ com.dianping.cat.agent.monitor.executors.Executor
PerformanceExecutor
- com.dianping.cat.agent.monitor.system.SystemPerformanceExecutor
+ com.dianping.cat.agent.monitor.executors.system.SystemPerformanceExecutor
com.dianping.cat.agent.monitor.EnvironmentConfig
@@ -35,9 +35,9 @@
- com.dianping.cat.agent.monitor.Executor
+ com.dianping.cat.agent.monitor.executors.Executor
SystemStateExecutor
- com.dianping.cat.agent.monitor.system.SystemStateExecutor
+ com.dianping.cat.agent.monitor.executors.system.SystemStateExecutor
com.dianping.cat.agent.monitor.EnvironmentConfig
@@ -45,8 +45,8 @@
- com.dianping.cat.agent.monitor.DataSender
- com.dianping.cat.agent.monitor.DataSender
+ com.dianping.cat.agent.monitor.executors.DataSender
+ com.dianping.cat.agent.monitor.executors.DataSender
com.dianping.cat.agent.monitor.EnvironmentConfig
@@ -54,11 +54,20 @@
- com.dianping.cat.agent.monitor.TaskExecutors
- com.dianping.cat.agent.monitor.TaskExecutors
+ com.dianping.cat.agent.monitor.executors.TaskExecutors
+ com.dianping.cat.agent.monitor.executors.TaskExecutors
- com.dianping.cat.agent.monitor.DataSender
+ com.dianping.cat.agent.monitor.executors.DataSender
+
+
+
+
+ com.dianping.cat.agent.monitor.puppet.PuppetTask
+ com.dianping.cat.agent.monitor.puppet.PuppetTask
+
+
+ com.dianping.cat.agent.monitor.EnvironmentConfig
@@ -83,10 +92,13 @@
com.dianping.cat.agent.core.page.index.JspViewer
- com.dianping.cat.agent.monitor.DataSender
+ com.dianping.cat.agent.monitor.executors.DataSender
+
+
+ com.dianping.cat.agent.monitor.executors.TaskExecutors
- com.dianping.cat.agent.monitor.TaskExecutors
+ com.dianping.cat.agent.monitor.puppet.PuppetTask
@@ -100,8 +112,8 @@
- com.dianping.cat.agent.monitor.DataSender
- com.dianping.cat.agent.monitor.DataSender
+ com.dianping.cat.agent.monitor.executors.DataSender
+ com.dianping.cat.agent.monitor.executors.DataSender
com.dianping.cat.agent.monitor.EnvironmentConfig
@@ -113,11 +125,20 @@
com.dianping.cat.agent.monitor.EnvironmentConfig
- com.dianping.cat.agent.monitor.TaskExecutors
- com.dianping.cat.agent.monitor.TaskExecutors
+ com.dianping.cat.agent.monitor.executors.TaskExecutors
+ com.dianping.cat.agent.monitor.executors.TaskExecutors
+
+
+ com.dianping.cat.agent.monitor.executors.DataSender
+
+
+
+
+ com.dianping.cat.agent.monitor.puppet.PuppetTask
+ com.dianping.cat.agent.monitor.puppet.PuppetTask
- com.dianping.cat.agent.monitor.DataSender
+ com.dianping.cat.agent.monitor.EnvironmentConfig
diff --git a/cat-agent/src/test/java/com/dianping/cat/agent/puppet/PuppetMonitorByLineNo.java b/cat-agent/src/test/java/com/dianping/cat/agent/puppet/PuppetMonitorByLineNo.java
index 637a5484b26ebc123cca2c780958969b913aea88..000a590f902f7ce81fc9e0f5e6d9ea5ba017a03e 100644
--- a/cat-agent/src/test/java/com/dianping/cat/agent/puppet/PuppetMonitorByLineNo.java
+++ b/cat-agent/src/test/java/com/dianping/cat/agent/puppet/PuppetMonitorByLineNo.java
@@ -12,142 +12,140 @@ import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.regex.*;
-
public class PuppetMonitorByLineNo {
-
+
public static void main(String[] args) {
- String log_file="/Users/River/messages";
- String line_file="/var/log/line.log";
- File line_f = new File(line_file);
- while(true){
- int line_no_start=get_startline_no(line_file,log_file);
- int line_reading_count=0;
- String regEx=".*puppet-agent.*\\(\\/Stage";
- System.out.println("line_no_start="+Integer.toString(line_no_start));
-
+ String log_file = "/var/log/messages";
+ String line_file = "/var/log/currentPointer";
+ File line_f = new File(line_file);
+ while (true) {
+ int line_no_start = get_startline_no(line_file, log_file);
+ int line_reading_count = 0;
+ String regEx = ".*puppet-agent.*\\(\\/Stage";
+ System.out.println("line_no_start=" + Integer.toString(line_no_start));
+
try {
- FileReader reader = new FileReader(log_file);
- BufferedReader br = new BufferedReader(reader);
-
- String str = null;
-
- while((str = br.readLine()) != null) {
- line_reading_count++;
- if(line_reading_count convertedData = new LinkedHashMap();
for (Entry currentEntry : current.entrySet()) {
- double result = currentEntry.getValue() / 1000.0;
+ double result = currentEntry.getValue() / 1000000.0 / 60;
convertedData.put(currentEntry.getKey(), result);
}
@@ -184,7 +184,7 @@ public class NetworkGraphCreator extends AbstractGraphCreator {
private String buildUnit(String chartTitle) {
if (isFlowMetric(chartTitle)) {
- return "流量(MB/分钟)";
+ return "流量(MB/秒)";
} else {
return "value/分钟";
}
diff --git a/cat-home/src/main/java/com/dianping/cat/report/page/system/graph/SystemGraphCreator.java b/cat-home/src/main/java/com/dianping/cat/report/page/system/graph/SystemGraphCreator.java
index 74c2ed7691ef6d4046f58de52e172881d05e0930..0fa3f3af36ab4540205654491055eb4ffea24216 100644
--- a/cat-home/src/main/java/com/dianping/cat/report/page/system/graph/SystemGraphCreator.java
+++ b/cat-home/src/main/java/com/dianping/cat/report/page/system/graph/SystemGraphCreator.java
@@ -88,6 +88,7 @@ public class SystemGraphCreator extends AbstractGraphCreator {
lineChart.setHtmlTitle(chartTitle);
lineChart.setId(chartTitle);
lineChart.setStart(startDate);
+ lineChart.setUnit("value/分钟");
lineChart.setStep(step * TimeUtil.ONE_MINUTE);
if (keyMapEntry.getValue().entrySet().isEmpty()) {
@@ -135,8 +136,8 @@ public class SystemGraphCreator extends AbstractGraphCreator {
"/var-usage:avg", "eth0-in-flow:sum", "eth0-out-flow:sum", "swap:avg", "load:avg", "uptime:avg",
"md5Change:avg", "hostNameChange:avg", "hostIpChange:avg"));
- List jvmKeys = new ArrayList(Arrays.asList("edenUsage:avg", "oldUsage:avg",
- "permUsage:avg", "tomcatLive:avg", "catalinaLogSize:sum"));
+ List jvmKeys = new ArrayList(Arrays.asList("edenUsage:avg", "oldUsage:avg", "permUsage:avg",
+ "tomcatLive:avg", "catalinaLogSize:sum"));
List nginxKeys = new ArrayList();
@@ -147,7 +148,7 @@ public class SystemGraphCreator extends AbstractGraphCreator {
} else if (NGINX_TYPE.equalsIgnoreCase(type)) {
return nginxKeys;
} else {
- return null;
+ return new ArrayList();
}
}
@@ -155,23 +156,21 @@ public class SystemGraphCreator extends AbstractGraphCreator {
List systemKeys = fetchSystemKeys(type);
Map> aggregationKeys = new LinkedHashMap>();
- if (systemKeys != null) {
- for (String key : systemKeys) {
- int typeIndex = key.lastIndexOf(":");
- String realKey = key.substring(0, typeIndex);
- String metricType = key.substring(typeIndex + 1);
- String des = queryMetricItemDes(metricType.toUpperCase());
- String chartKey = realKey + des;
- Map ipMap = aggregationKeys.get(chartKey);
+ for (String key : systemKeys) {
+ int typeIndex = key.lastIndexOf(":");
+ String realKey = key.substring(0, typeIndex);
+ String metricType = key.substring(typeIndex + 1);
+ String des = queryMetricItemDes(metricType.toUpperCase());
+ String chartKey = realKey + des;
+ Map ipMap = aggregationKeys.get(chartKey);
- if (ipMap == null) {
- ipMap = new HashMap();
+ if (ipMap == null) {
+ ipMap = new HashMap();
- aggregationKeys.put(chartKey, ipMap);
- }
- for (String ip : ipAddrs) {
- ipMap.put(ip, realKey + "_" + ip + ":" + metricType.toUpperCase());
- }
+ aggregationKeys.put(chartKey, ipMap);
+ }
+ for (String ip : ipAddrs) {
+ ipMap.put(ip, realKey + "_" + ip + ":" + metricType.toUpperCase());
}
}
return aggregationKeys;
diff --git a/cat-home/src/main/java/com/dianping/cat/report/task/alert/exception/ExceptionAlert.java b/cat-home/src/main/java/com/dianping/cat/report/task/alert/exception/ExceptionAlert.java
index b940abe007e323369f54b2e9ef395762b901a041..56934499247f8256542064c56c9cbf8d3671c2f1 100644
--- a/cat-home/src/main/java/com/dianping/cat/report/task/alert/exception/ExceptionAlert.java
+++ b/cat-home/src/main/java/com/dianping/cat/report/task/alert/exception/ExceptionAlert.java
@@ -210,6 +210,7 @@ public class ExceptionAlert implements Task, LogEnabled {
}
boolean active = true;
while (active) {
+ long current = System.currentTimeMillis();
int minute = Calendar.getInstance().get(Calendar.MINUTE);
String minuteStr = String.valueOf(minute);
@@ -217,12 +218,15 @@ public class ExceptionAlert implements Task, LogEnabled {
minuteStr = '0' + minuteStr;
}
Transaction t = Cat.newTransaction("ExceptionAlert", "M" + minuteStr);
- long current = System.currentTimeMillis();
try {
TopMetric topMetric = buildTopMetric(new Date(current - TimeUtil.ONE_MINUTE * 2));
Collection> items = topMetric.getError().getResult().values();
- List- item = items.iterator().next();
+ List
- item = new ArrayList
- ();
+
+ if (!items.isEmpty()) {
+ item = items.iterator().next();
+ }
Map> alertExceptions = buildAlertExceptions(item);
for (Entry> entry : alertExceptions.entrySet()) {
@@ -242,7 +246,7 @@ public class ExceptionAlert implements Task, LogEnabled {
try {
if (duration < DURATION) {
- Thread.sleep(TimeUtil.ONE_MINUTE);
+ Thread.sleep(DURATION - duration);
}
} catch (InterruptedException e) {
active = false;