Monitor service added.

This commit is contained in:
Zhen Tang 2013-06-25 23:27:15 +08:00
parent 6908651506
commit eefbd4aee6
8 changed files with 523 additions and 0 deletions

44
pom.xml Normal file
View File

@ -0,0 +1,44 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.bench4q</groupId>
<artifactId>bench4q-monitor</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Bench4Q Monitor</name>
<description>Bench4Q Monitor</description>
<organization>
<name>TCSE, ISCAS</name>
</organization>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.11.v20130520</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.1.11.v20130520</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
</dependencies>
<build>
<finalName>bench4q-monitor</finalName>
</build>
</project>

View File

@ -0,0 +1,10 @@
package org.bench4q.monitor;
public class Main {
public static void main(String[] args) {
MonitorServer monitorServer = new MonitorServer(5555);
monitorServer.start();
}
}

View File

@ -0,0 +1,67 @@
package org.bench4q.monitor;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.servlet.DispatcherServlet;
public class MonitorServer {
private Server server;
private int port;
private Server getServer() {
return server;
}
private void setServer(Server server) {
this.server = server;
}
private int getPort() {
return port;
}
private void setPort(int port) {
this.port = port;
}
public MonitorServer(int port) {
this.setPort(port);
}
public boolean start() {
try {
this.setServer(new Server());
Connector connector = new SocketConnector();
connector.setPort(this.getPort());
this.getServer().addConnector(connector);
ServletContextHandler servletContextHandler = new ServletContextHandler();
ServletHolder servletHolder = servletContextHandler.addServlet(
DispatcherServlet.class, "/");
servletHolder.setInitParameter("contextConfigLocation",
"classpath*:/application-context.xml");
this.getServer().setHandler(servletContextHandler);
this.getServer().start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean stop() {
try {
if (this.getServer() != null) {
this.getServer().stop();
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
this.setServer(null);
}
}
}

View File

@ -0,0 +1,47 @@
package org.bench4q.monitor.api;
import org.bench4q.monitor.api.model.MonitorResultModel;
import org.bench4q.monitor.entity.MonitorInfo;
import org.bench4q.monitor.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/monitor")
public class MonitorController {
private MonitorService monitorService;
private MonitorService getMonitorService() {
return monitorService;
}
@Autowired
private void setMonitorService(MonitorService monitorService) {
this.monitorService = monitorService;
}
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public MonitorResultModel index() {
MonitorInfo monitorInfo = this.getMonitorService().getMonitorInfo();
MonitorResultModel monitorResultModel = new MonitorResultModel();
monitorResultModel.setCpuRatio(monitorInfo.getCpuRatio());
monitorResultModel.setFreePhysicsMemory(monitorInfo
.getFreePhysicsMemory());
monitorResultModel.setFreeVirtualMachineMemory(monitorInfo
.getFreeVirtualMachineMemory());
monitorResultModel.setMaxVirtualMachineMemory(monitorInfo
.getMaxVirtualMachineMemory());
monitorResultModel.setOperationSystem(monitorInfo.getOperationSystem());
monitorResultModel.setTotalPhysicsMemory(monitorInfo
.getTotalPhysicsMemory());
monitorResultModel.setTotalVirtualMachineMemory(monitorInfo
.getTotalVirtualMachineMemory());
monitorResultModel.setUsedPhysicsMemory(monitorInfo
.getUsedPhysicsMemory());
return monitorResultModel;
}
}

View File

@ -0,0 +1,88 @@
package org.bench4q.monitor.api.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "monitorResult")
public class MonitorResultModel {
private long totalVirtualMachineMemory;
private long freeVirtualMachineMemory;
private long maxVirtualMachineMemory;
private String operationSystem;
private long totalPhysicsMemory;
private long freePhysicsMemory;
private long usedPhysicsMemory;
private double cpuRatio;
@XmlElement
public long getTotalVirtualMachineMemory() {
return totalVirtualMachineMemory;
}
public void setTotalVirtualMachineMemory(long totalVirtualMachineMemory) {
this.totalVirtualMachineMemory = totalVirtualMachineMemory;
}
@XmlElement
public long getFreeVirtualMachineMemory() {
return freeVirtualMachineMemory;
}
public void setFreeVirtualMachineMemory(long freeVirtualMachineMemory) {
this.freeVirtualMachineMemory = freeVirtualMachineMemory;
}
@XmlElement
public long getMaxVirtualMachineMemory() {
return maxVirtualMachineMemory;
}
public void setMaxVirtualMachineMemory(long maxVirtualMachineMemory) {
this.maxVirtualMachineMemory = maxVirtualMachineMemory;
}
@XmlElement
public String getOperationSystem() {
return operationSystem;
}
public void setOperationSystem(String operationSystem) {
this.operationSystem = operationSystem;
}
@XmlElement
public long getTotalPhysicsMemory() {
return totalPhysicsMemory;
}
public void setTotalPhysicsMemory(long totalPhysicsMemory) {
this.totalPhysicsMemory = totalPhysicsMemory;
}
@XmlElement
public long getFreePhysicsMemory() {
return freePhysicsMemory;
}
public void setFreePhysicsMemory(long freePhysicsMemory) {
this.freePhysicsMemory = freePhysicsMemory;
}
@XmlElement
public long getUsedPhysicsMemory() {
return usedPhysicsMemory;
}
public void setUsedPhysicsMemory(long usedPhysicsMemory) {
this.usedPhysicsMemory = usedPhysicsMemory;
}
@XmlElement
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
}

View File

@ -0,0 +1,77 @@
package org.bench4q.monitor.entity;
public class MonitorInfo {
private long totalVirtualMachineMemory;
private long freeVirtualMachineMemory;
private long maxVirtualMachineMemory;
private String operationSystem;
private long totalPhysicsMemory;
private long freePhysicsMemory;
private long usedPhysicsMemory;
private double cpuRatio;
public long getTotalVirtualMachineMemory() {
return totalVirtualMachineMemory;
}
public void setTotalVirtualMachineMemory(long totalVirtualMachineMemory) {
this.totalVirtualMachineMemory = totalVirtualMachineMemory;
}
public long getFreeVirtualMachineMemory() {
return freeVirtualMachineMemory;
}
public void setFreeVirtualMachineMemory(long freeVirtualMachineMemory) {
this.freeVirtualMachineMemory = freeVirtualMachineMemory;
}
public long getMaxVirtualMachineMemory() {
return maxVirtualMachineMemory;
}
public void setMaxVirtualMachineMemory(long maxVirtualMachineMemory) {
this.maxVirtualMachineMemory = maxVirtualMachineMemory;
}
public String getOperationSystem() {
return operationSystem;
}
public void setOperationSystem(String operationSystem) {
this.operationSystem = operationSystem;
}
public long getTotalPhysicsMemory() {
return totalPhysicsMemory;
}
public void setTotalPhysicsMemory(long totalPhysicsMemory) {
this.totalPhysicsMemory = totalPhysicsMemory;
}
public long getFreePhysicsMemory() {
return freePhysicsMemory;
}
public void setFreePhysicsMemory(long freePhysicsMemory) {
this.freePhysicsMemory = freePhysicsMemory;
}
public long getUsedPhysicsMemory() {
return usedPhysicsMemory;
}
public void setUsedPhysicsMemory(long usedPhysicsMemory) {
this.usedPhysicsMemory = usedPhysicsMemory;
}
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
}

View File

@ -0,0 +1,180 @@
package org.bench4q.monitor.service;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.StringTokenizer;
import sun.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import org.bench4q.monitor.entity.MonitorInfo;
import org.springframework.stereotype.Component;
@Component
public class MonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
public MonitorInfo getMonitorInfo() {
MonitorInfo monitorInfo = new MonitorInfo();
monitorInfo.setTotalVirtualMachineMemory(Runtime.getRuntime()
.totalMemory());
monitorInfo.setFreeVirtualMachineMemory(Runtime.getRuntime()
.freeMemory());
monitorInfo
.setMaxVirtualMachineMemory(Runtime.getRuntime().maxMemory());
monitorInfo.setOperationSystem(System.getProperty("os.name"));
OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
monitorInfo.setTotalPhysicsMemory(operatingSystemMXBean
.getTotalPhysicalMemorySize());
monitorInfo.setFreePhysicsMemory(operatingSystemMXBean
.getFreePhysicalMemorySize());
monitorInfo.setUsedPhysicsMemory(monitorInfo.getTotalPhysicsMemory()
- monitorInfo.getFreePhysicsMemory());
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != null; parentThread = parentThread.getParent())
;
double cpuRatio = 0;
if (monitorInfo.getOperationSystem().toLowerCase()
.startsWith("windows")) {
cpuRatio = this.getCpuRatioForWindows();
} else {
cpuRatio = this.getCpuRatioForLinux();
}
monitorInfo.setCpuRatio(cpuRatio);
return monitorInfo;
}
private double getCpuRatioForLinux() {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringTokenizer tokenStat = null;
try {
Process process = Runtime.getRuntime().exec("top -b -n 1");
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
br.readLine();
br.readLine();
tokenStat = new StringTokenizer(br.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
String cpuUsage = tokenStat.nextToken();
Float usage = new Float(
cpuUsage.substring(0, cpuUsage.indexOf("%")));
return (1 - usage.floatValue() / 100);
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
try {
if (is != null) {
is.close();
}
if (isr != null) {
isr.close();
}
if (br != null) {
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private double getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir")
+ "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
+ "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// È¡½ø³ÌÐÅÏ¢
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return Double.valueOf(
PERCENT * (busytime) / (busytime + idletime))
.doubleValue();
} else {
return 0.0;
}
} catch (Exception ex) {
ex.printStackTrace();
return 0.0;
}
}
private long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
String caption = line.substring(capidx, cmdidx - 1).trim();
String cmd = line.substring(cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
if (caption.equals("System Idle Process")
|| caption.equals("System")) {
idletime += Long.valueOf(
line.substring(kmtidx, rocidx - 1).trim())
.longValue();
idletime += Long.valueOf(
line.substring(umtidx, wocidx - 1).trim())
.longValue();
continue;
}
kneltime += Long.valueOf(
line.substring(kmtidx, rocidx - 1).trim()).longValue();
usertime += Long.valueOf(
line.substring(umtidx, wocidx - 1).trim()).longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.bench4q" />
<mvc:annotation-driven />
</beans>