probe stubs added.

This commit is contained in:
Zhen Tang 2013-07-05 16:46:01 +08:00
parent eefbd4aee6
commit 4adc8ec00f
14 changed files with 402 additions and 300 deletions

25
descriptor.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>dir</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<files>
<file>
<source>target/bench4q-monitor.jar</source>
<outputDirectory>/</outputDirectory>
</file>
</files>
</assembly>

117
pom.xml
View File

@ -1,44 +1,75 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>org.bench4q</groupId> <groupId>org.bench4q</groupId>
<artifactId>bench4q-monitor</artifactId> <artifactId>bench4q-monitor</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>Bench4Q Monitor</name> <name>Bench4Q Monitor</name>
<description>Bench4Q Monitor</description> <description>Bench4Q Monitor</description>
<organization> <organization>
<name>TCSE, ISCAS</name> <name>TCSE, ISCAS</name>
</organization> </organization>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.11</version> <version>4.11</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId> <artifactId>jetty-server</artifactId>
<version>8.1.11.v20130520</version> <version>8.1.11.v20130520</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId> <artifactId>jetty-servlet</artifactId>
<version>8.1.11.v20130520</version> <version>8.1.11.v20130520</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
<version>3.2.3.RELEASE</version> <version>3.2.3.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.codehaus.jackson</groupId> <groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId> <artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version> <version>1.9.12</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>bench4q-monitor</finalName> <plugins>
</build> <plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.bench4q.monitor.Main</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>descriptor.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>bench4q-monitor</finalName>
</build>
</project> </project>

View File

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

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.probe;
public class LinuxCpuProbe {
// /proc/stat
}

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.probe;
public class LinuxCpuProbeInfo {
}

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.probe;
public class LinuxDiskProbe {
// /proc/diskstats
}

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.probe;
public class LinuxDiskProbeInfo {
}

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.probe;
public class LinuxMemoryProbe {
// /proc/meminfo
}

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.probe;
public class LinuxMemoryProbeInfo {
}

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.probe;
public class LinuxNetworkProbe {
}

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.probe;
public class LinuxNetworkProbeInfo {
}

View File

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

View File

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

View File

@ -0,0 +1,5 @@
package org.bench4q.monitor.test;
public class MonitorServiceTest {
}