|
|
package com.monitor.monit.Job;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
|
|
import com.monitor.monit.constant.MConstants;
|
|
|
import com.monitor.monit.model.*;
|
|
|
import lombok.Getter;
|
|
|
import lombok.Setter;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.dom4j.Document;
|
|
|
import org.dom4j.DocumentException;
|
|
|
import org.dom4j.Element;
|
|
|
import org.dom4j.io.SAXReader;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/5/11.
|
|
|
*/
|
...
|
...
|
@@ -45,10 +51,8 @@ public class CollectorJob implements Callable { |
|
|
private void parseCollectorXml(String monitorInfo) {
|
|
|
MMonit mMonit = null;
|
|
|
try {
|
|
|
MConstants.XMLMAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
|
mMonit = MConstants.XMLMAPPER.readValue(monitorInfo, MMonit.class);
|
|
|
} catch (IOException e) {
|
|
|
mMonit = buildMonit(monitorInfo);
|
|
|
} catch (Exception e) {
|
|
|
DEBUG.error("Failed to parse xml content {} , error {}", monitorInfo, e);
|
|
|
|
|
|
mMonit = null;
|
...
|
...
|
@@ -75,6 +79,134 @@ public class CollectorJob implements Callable { |
|
|
}
|
|
|
}
|
|
|
|
|
|
private MMonit buildMonit(String monitorInfo) throws DocumentException {
|
|
|
|
|
|
|
|
|
SAXReader reader = new SAXReader();
|
|
|
|
|
|
InputStream in = new ByteArrayInputStream(monitorInfo.getBytes());
|
|
|
|
|
|
Document document = reader.read(in);
|
|
|
|
|
|
Element root = document.getRootElement();
|
|
|
|
|
|
if (null == root) {
|
|
|
return null;
|
|
|
}
|
|
|
MMonit monit = new MMonit();
|
|
|
|
|
|
monit.setId(root.attributeValue("id"));
|
|
|
|
|
|
Element serverElement = root.element("server");
|
|
|
|
|
|
monit.setServer(getmServer(serverElement));
|
|
|
|
|
|
Element platformElement = root.element("platform");
|
|
|
|
|
|
monit.setPlatform(getmPlatform(platformElement));
|
|
|
|
|
|
Element servicesElement = root.element("services");
|
|
|
|
|
|
monit.setServices(getmServices(servicesElement));
|
|
|
|
|
|
Element eventElement = root.element("event");
|
|
|
|
|
|
monit.setEvent(getmEvent(eventElement));
|
|
|
|
|
|
return monit;
|
|
|
}
|
|
|
|
|
|
private MEvent getmEvent(Element eventElement) {
|
|
|
if (null == eventElement) {
|
|
|
return null;
|
|
|
}
|
|
|
MEvent event = new MEvent();
|
|
|
|
|
|
event.setType(eventElement.elementText("type"));
|
|
|
|
|
|
event.setCollected_sec(eventElement.elementText("collected_sec"));
|
|
|
|
|
|
event.setMessage(eventElement.elementText("message"));
|
|
|
|
|
|
event.setService(eventElement.elementText("service"));
|
|
|
|
|
|
return event;
|
|
|
}
|
|
|
|
|
|
private List<MService> getmServices(Element servicesElement) {
|
|
|
if (null == servicesElement) {
|
|
|
return null;
|
|
|
}
|
|
|
List<Element> childServiceElement = servicesElement.elements();
|
|
|
|
|
|
List<MService> serviceList = new ArrayList<>();
|
|
|
|
|
|
for (Element oneElement : childServiceElement) {
|
|
|
|
|
|
MService service = new MService();
|
|
|
|
|
|
service.setName(oneElement.attributeValue("name"));
|
|
|
|
|
|
service.setCollected_sec(oneElement.elementText("collected_usec"));
|
|
|
|
|
|
service.setMonitor(oneElement.elementText("monitor"));
|
|
|
|
|
|
service.setStatus(oneElement.elementText("status"));
|
|
|
|
|
|
service.setType(oneElement.elementText("type"));
|
|
|
|
|
|
serviceList.add(service);
|
|
|
}
|
|
|
return serviceList;
|
|
|
}
|
|
|
|
|
|
private MPlatform getmPlatform(Element platformElement) {
|
|
|
if (null == platformElement) {
|
|
|
return null;
|
|
|
}
|
|
|
MPlatform platform = new MPlatform();
|
|
|
|
|
|
platform.setCpu(platformElement.elementText("cpu"));
|
|
|
|
|
|
platform.setMachine(platformElement.elementText("machine"));
|
|
|
|
|
|
platform.setMemory(platformElement.elementText("memory"));
|
|
|
|
|
|
platform.setName(platformElement.elementText("name"));
|
|
|
|
|
|
platform.setRelease(platformElement.elementText("release"));
|
|
|
|
|
|
platform.setSwap(platformElement.elementText("swap"));
|
|
|
|
|
|
platform.setVersion(platformElement.elementText("version"));
|
|
|
return platform;
|
|
|
}
|
|
|
|
|
|
private MServer getmServer(Element serverElement) {
|
|
|
if (null == serverElement) {
|
|
|
return null;
|
|
|
}
|
|
|
MServer server = new MServer();
|
|
|
|
|
|
server.setControlfile(serverElement.elementText("controlfile"));
|
|
|
|
|
|
server.setLocalhostname(serverElement.elementText("localhostname"));
|
|
|
|
|
|
MHttpd httpd = new MHttpd();
|
|
|
|
|
|
Element httpElement = serverElement.element("httpd");
|
|
|
|
|
|
httpd.setAddress(httpElement.elementText("address"));
|
|
|
|
|
|
httpd.setPort(httpElement.elementText("port"));
|
|
|
|
|
|
httpd.setSsl(httpElement.elementText("ssl"));
|
|
|
|
|
|
server.setHttpd(httpd);
|
|
|
|
|
|
return server;
|
|
|
}
|
|
|
|
|
|
private void updateMMonitInfo(MMonitInfo mMonitInfo, MMonit mMonit) {
|
|
|
|
|
|
MHttpd httpInfo = mMonit.getServer().getHttpd();
|
...
|
...
|
@@ -133,5 +265,14 @@ public class CollectorJob implements Callable { |
|
|
MConstants.HOSTSINFO_MAPPER.put(mMonitInfo.getMonitorId(), mMonitInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws DocumentException {
|
|
|
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>";
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
...
|
...
|
|