Authored by jack

增加monit

@@ -78,11 +78,10 @@ @@ -78,11 +78,10 @@
78 <artifactId>jackson-core-asl</artifactId> 78 <artifactId>jackson-core-asl</artifactId>
79 <version>1.9.13</version> 79 <version>1.9.13</version>
80 </dependency> 80 </dependency>
81 -  
82 <dependency> 81 <dependency>
83 - <groupId>com.fasterxml.jackson.dataformat</groupId>  
84 - <artifactId>jackson-dataformat-xml</artifactId>  
85 - <version>2.4.2</version> 82 + <groupId>org.dom4j</groupId>
  83 + <artifactId>dom4j</artifactId>
  84 + <version>2.0.0</version>
86 </dependency> 85 </dependency>
87 </dependencies> 86 </dependencies>
88 87
1 package com.monitor.monit.Job; 1 package com.monitor.monit.Job;
2 2
3 -import com.fasterxml.jackson.databind.DeserializationFeature;  
4 3
5 import com.monitor.monit.constant.MConstants; 4 import com.monitor.monit.constant.MConstants;
6 import com.monitor.monit.model.*; 5 import com.monitor.monit.model.*;
7 import lombok.Getter; 6 import lombok.Getter;
8 import lombok.Setter; 7 import lombok.Setter;
9 import org.apache.commons.lang3.StringUtils; 8 import org.apache.commons.lang3.StringUtils;
  9 +import org.dom4j.Document;
  10 +import org.dom4j.DocumentException;
  11 +import org.dom4j.Element;
  12 +import org.dom4j.io.SAXReader;
10 import org.slf4j.Logger; 13 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 14 import org.slf4j.LoggerFactory;
12 15
13 -import java.io.IOException; 16 +import java.io.ByteArrayInputStream;
  17 +import java.io.InputStream;
  18 +import java.util.ArrayList;
14 import java.util.List; 19 import java.util.List;
15 import java.util.concurrent.Callable; 20 import java.util.concurrent.Callable;
16 21
  22 +
17 /** 23 /**
18 * Created by yoho on 2016/5/11. 24 * Created by yoho on 2016/5/11.
19 */ 25 */
@@ -45,10 +51,8 @@ public class CollectorJob implements Callable { @@ -45,10 +51,8 @@ public class CollectorJob implements Callable {
45 private void parseCollectorXml(String monitorInfo) { 51 private void parseCollectorXml(String monitorInfo) {
46 MMonit mMonit = null; 52 MMonit mMonit = null;
47 try { 53 try {
48 - MConstants.XMLMAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);  
49 -  
50 - mMonit = MConstants.XMLMAPPER.readValue(monitorInfo, MMonit.class);  
51 - } catch (IOException e) { 54 + mMonit = buildMonit(monitorInfo);
  55 + } catch (Exception e) {
52 DEBUG.error("Failed to parse xml content {} , error {}", monitorInfo, e); 56 DEBUG.error("Failed to parse xml content {} , error {}", monitorInfo, e);
53 57
54 mMonit = null; 58 mMonit = null;
@@ -75,6 +79,134 @@ public class CollectorJob implements Callable { @@ -75,6 +79,134 @@ public class CollectorJob implements Callable {
75 } 79 }
76 } 80 }
77 81
  82 + private MMonit buildMonit(String monitorInfo) throws DocumentException {
  83 +
  84 +
  85 + SAXReader reader = new SAXReader();
  86 +
  87 + InputStream in = new ByteArrayInputStream(monitorInfo.getBytes());
  88 +
  89 + Document document = reader.read(in);
  90 +
  91 + Element root = document.getRootElement();
  92 +
  93 + if (null == root) {
  94 + return null;
  95 + }
  96 + MMonit monit = new MMonit();
  97 +
  98 + monit.setId(root.attributeValue("id"));
  99 +
  100 + Element serverElement = root.element("server");
  101 +
  102 + monit.setServer(getmServer(serverElement));
  103 +
  104 + Element platformElement = root.element("platform");
  105 +
  106 + monit.setPlatform(getmPlatform(platformElement));
  107 +
  108 + Element servicesElement = root.element("services");
  109 +
  110 + monit.setServices(getmServices(servicesElement));
  111 +
  112 + Element eventElement = root.element("event");
  113 +
  114 + monit.setEvent(getmEvent(eventElement));
  115 +
  116 + return monit;
  117 + }
  118 +
  119 + private MEvent getmEvent(Element eventElement) {
  120 + if (null == eventElement) {
  121 + return null;
  122 + }
  123 + MEvent event = new MEvent();
  124 +
  125 + event.setType(eventElement.elementText("type"));
  126 +
  127 + event.setCollected_sec(eventElement.elementText("collected_sec"));
  128 +
  129 + event.setMessage(eventElement.elementText("message"));
  130 +
  131 + event.setService(eventElement.elementText("service"));
  132 +
  133 + return event;
  134 + }
  135 +
  136 + private List<MService> getmServices(Element servicesElement) {
  137 + if (null == servicesElement) {
  138 + return null;
  139 + }
  140 + List<Element> childServiceElement = servicesElement.elements();
  141 +
  142 + List<MService> serviceList = new ArrayList<>();
  143 +
  144 + for (Element oneElement : childServiceElement) {
  145 +
  146 + MService service = new MService();
  147 +
  148 + service.setName(oneElement.attributeValue("name"));
  149 +
  150 + service.setCollected_sec(oneElement.elementText("collected_usec"));
  151 +
  152 + service.setMonitor(oneElement.elementText("monitor"));
  153 +
  154 + service.setStatus(oneElement.elementText("status"));
  155 +
  156 + service.setType(oneElement.elementText("type"));
  157 +
  158 + serviceList.add(service);
  159 + }
  160 + return serviceList;
  161 + }
  162 +
  163 + private MPlatform getmPlatform(Element platformElement) {
  164 + if (null == platformElement) {
  165 + return null;
  166 + }
  167 + MPlatform platform = new MPlatform();
  168 +
  169 + platform.setCpu(platformElement.elementText("cpu"));
  170 +
  171 + platform.setMachine(platformElement.elementText("machine"));
  172 +
  173 + platform.setMemory(platformElement.elementText("memory"));
  174 +
  175 + platform.setName(platformElement.elementText("name"));
  176 +
  177 + platform.setRelease(platformElement.elementText("release"));
  178 +
  179 + platform.setSwap(platformElement.elementText("swap"));
  180 +
  181 + platform.setVersion(platformElement.elementText("version"));
  182 + return platform;
  183 + }
  184 +
  185 + private MServer getmServer(Element serverElement) {
  186 + if (null == serverElement) {
  187 + return null;
  188 + }
  189 + MServer server = new MServer();
  190 +
  191 + server.setControlfile(serverElement.elementText("controlfile"));
  192 +
  193 + server.setLocalhostname(serverElement.elementText("localhostname"));
  194 +
  195 + MHttpd httpd = new MHttpd();
  196 +
  197 + Element httpElement = serverElement.element("httpd");
  198 +
  199 + httpd.setAddress(httpElement.elementText("address"));
  200 +
  201 + httpd.setPort(httpElement.elementText("port"));
  202 +
  203 + httpd.setSsl(httpElement.elementText("ssl"));
  204 +
  205 + server.setHttpd(httpd);
  206 +
  207 + return server;
  208 + }
  209 +
78 private void updateMMonitInfo(MMonitInfo mMonitInfo, MMonit mMonit) { 210 private void updateMMonitInfo(MMonitInfo mMonitInfo, MMonit mMonit) {
79 211
80 MHttpd httpInfo = mMonit.getServer().getHttpd(); 212 MHttpd httpInfo = mMonit.getServer().getHttpd();
@@ -133,5 +265,14 @@ public class CollectorJob implements Callable { @@ -133,5 +265,14 @@ public class CollectorJob implements Callable {
133 MConstants.HOSTSINFO_MAPPER.put(mMonitInfo.getMonitorId(), mMonitInfo); 265 MConstants.HOSTSINFO_MAPPER.put(mMonitInfo.getMonitorId(), mMonitInfo);
134 266
135 } 267 }
  268 +
  269 +
  270 + public static void main(String[] args) throws DocumentException {
  271 + String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><monit id=\"27603e5eecd8169c0bf0b0bb327b6c04\" incarnation=\"1477636106\" version=\"5.17.1\"><server><uptime>2350</uptime><poll>30</poll><startdelay>0</startdelay><localhostname>java_76</localhostname><controlfile>/Data/local/monit/monitrc</controlfile><httpd><address>192.168.102.76</address><port>2812</port><ssl>0</ssl></httpd></server><platform><name>Linux</name><release>2.6.32-642.el6.x86_64</release><version>#1 SMP Tue May 10 17:27:01 UTC 2016</version><machine>x86_64</machine><cpu>2</cpu><memory>7514536</memory><swap>4063228</swap></platform><services><service name=\"java_76\"><type>5</type><collected_sec>1477636141</collected_sec><collected_usec>936584</collected_usec><status>0</status><status_hint>0</status_hint><monitor>0</monitor><monitormode>0</monitormode><pendingaction>0</pendingaction></service></services><servicegroups></servicegroups><event><collected_sec>1477636170</collected_sec><collected_usec>361266</collected_usec><service>java_76</service><type>5</type><id>131072</id><state>2</state><action>1</action><message><![CDATA[stop action done]]></message></event></monit>";
  272 +
  273 +
  274 + }
  275 +
  276 +
136 } 277 }
137 278
1 package com.monitor.monit.constant; 1 package com.monitor.monit.constant;
2 2
3 -import com.fasterxml.jackson.dataformat.xml.XmlMapper;  
4 import com.monitor.monit.model.MMonitInfo; 3 import com.monitor.monit.model.MMonitInfo;
5 import org.codehaus.jackson.map.ObjectMapper; 4 import org.codehaus.jackson.map.ObjectMapper;
6 5
@@ -13,8 +12,6 @@ public interface MConstants { @@ -13,8 +12,6 @@ public interface MConstants {
13 12
14 ObjectMapper OBJECTMAPPER = new ObjectMapper(); 13 ObjectMapper OBJECTMAPPER = new ObjectMapper();
15 14
16 - XmlMapper XMLMAPPER = new XmlMapper();  
17 -  
18 ConcurrentHashMap<String, MMonitInfo> HOSTSINFO_MAPPER = new ConcurrentHashMap<>(); 15 ConcurrentHashMap<String, MMonitInfo> HOSTSINFO_MAPPER = new ConcurrentHashMap<>();
19 16
20 Integer HTTPTIMEOUT = 2; 17 Integer HTTPTIMEOUT = 2;
@@ -24,5 +21,4 @@ public interface MConstants { @@ -24,5 +21,4 @@ public interface MConstants {
24 String MONITED = "1"; 21 String MONITED = "1";
25 22
26 String NORMALSTATUS = "0"; 23 String NORMALSTATUS = "0";
27 -  
28 } 24 }
@@ -95,6 +95,7 @@ @@ -95,6 +95,7 @@
95 <groupId>commons-collections</groupId> 95 <groupId>commons-collections</groupId>
96 <artifactId>commons-collections</artifactId> 96 <artifactId>commons-collections</artifactId>
97 </dependency> 97 </dependency>
  98 +
98 </dependencies> 99 </dependencies>
99 100
100 </project> 101 </project>
@@ -66,10 +66,10 @@ @@ -66,10 +66,10 @@
66 <groupId>monitor-service</groupId> 66 <groupId>monitor-service</groupId>
67 <artifactId>monitor-service-compare</artifactId> 67 <artifactId>monitor-service-compare</artifactId>
68 </dependency> 68 </dependency>
69 -<!-- <dependency> 69 + <dependency>
70 <groupId>monitor-service</groupId> 70 <groupId>monitor-service</groupId>
71 <artifactId>monitor-service-monit</artifactId> 71 <artifactId>monitor-service-monit</artifactId>
72 - </dependency>--> 72 + </dependency>
73 <dependency> 73 <dependency>
74 <groupId>junit</groupId> 74 <groupId>junit</groupId>
75 <artifactId>junit</artifactId> 75 <artifactId>junit</artifactId>
@@ -153,11 +153,11 @@ @@ -153,11 +153,11 @@
153 <artifactId>monitor-service-compare</artifactId> 153 <artifactId>monitor-service-compare</artifactId>
154 <version>${project-version}</version> 154 <version>${project-version}</version>
155 </dependency> 155 </dependency>
156 -<!-- <dependency> 156 + <dependency>
157 <groupId>monitor-service</groupId> 157 <groupId>monitor-service</groupId>
158 <artifactId>monitor-service-monit</artifactId> 158 <artifactId>monitor-service-monit</artifactId>
159 <version>${project-version}</version> 159 <version>${project-version}</version>
160 - </dependency>--> 160 + </dependency>
161 <dependency> 161 <dependency>
162 <groupId>org.projectlombok</groupId> 162 <groupId>org.projectlombok</groupId>
163 <artifactId>lombok</artifactId> 163 <artifactId>lombok</artifactId>