Add a simple LogicalDiskModel and LogicalDiskControl as a test.

This commit is contained in:
fanfuxiaoran 2013-07-17 20:37:39 +08:00
parent 311181948f
commit 503857de09
7 changed files with 546 additions and 1 deletions

View File

@ -33,6 +33,8 @@ public class MonitorServer {
public boolean start() {
try {
System.out.println(this.getPort());
this.setServer(new Server());
Connector connector = new SocketConnector();
connector.setPort(this.getPort());

View File

@ -0,0 +1,45 @@
package org.bench4q.monitor.api;
import java.util.HashMap;
import java.util.Map;
import org.bench4q.monitor.model.LogicalDiskModel;
import org.bench4q.monitor.performance.windows.LogicalDiskMonitor;
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 MonitorControl {
private LogicalDiskMonitor logicalDiskMonitor;
private LogicalDiskMonitor getLogicalDiskMonitor() {
return logicalDiskMonitor;
}
@Autowired
private void setLogicalDiskMonitor(LogicalDiskMonitor logicalDiskMonitor) {
this.logicalDiskMonitor = logicalDiskMonitor;
}
@RequestMapping(value = "/LogicalDisk", method = RequestMethod.GET)
@ResponseBody
public LogicalDiskModel getLogicalDiskInstances() {
String[] instances = this.getLogicalDiskMonitor().getInstances();
Map<String, Double> freeSpacePercent = new HashMap<String, Double>();
for (String elem : instances) {
freeSpacePercent.put(elem, this.getLogicalDiskMonitor()
.getFreeSpacePercent(elem));
}
LogicalDiskModel logicalDiskModel = new LogicalDiskModel();
logicalDiskModel.setInstances(instances);
logicalDiskModel.setFreeSpacePercent(freeSpacePercent);
return logicalDiskModel;
}
}

View File

@ -0,0 +1,34 @@
package org.bench4q.monitor.model;
import java.util.Map;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "LogicalDisk")
public class LogicalDiskModel {
private String[] instances;
private Map<String, Double> freeSpacePercent;
@XmlElementWrapper(name = "instanceList")
@XmlElement(name = "instance")
public String[] getInstances() {
return instances;
}
public void setInstances(String[] instances) {
this.instances = instances;
}
public Map<String, Double> getFreeSpacePercent() {
return freeSpacePercent;
}
public void setFreeSpacePercent(Map<String, Double> freeSpacePercent) {
this.freeSpacePercent = freeSpacePercent;
}
}

View File

@ -1,5 +1,8 @@
package org.bench4q.monitor.performance.windows;
import org.springframework.stereotype.Component;
@Component
public class LogicalDiskMonitor {
public native String[] getInstances();

View File

@ -0,0 +1,136 @@
package wxr;
import java.io.File;
import org.bench4q.monitor.Main;
import org.bench4q.monitor.performance.windows.*;
public class LogicalDisk {
static {
loadLibraries();
}
private static void loadLibraries() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows")) {
String directory = Main.class.getProtectionDomain().getCodeSource()
.getLocation().getFile().replace("\\", "/");
File file = new File(directory);
if (!file.isDirectory()) {
directory = directory.substring(0, directory.lastIndexOf("/"));
if (!directory.endsWith("/")) {
directory += "/";
}
int arch = Integer.parseInt(System
.getProperty("sun.arch.data.model"));
if (arch == 64) {
System.load(directory + "lib/x64/Monitor.dll");
System.load(directory + "lib/x64/Native.dll");
} else {
System.load(directory + "lib/x86/Monitor.dll");
System.load(directory + "lib/x86/Native.dll");
}
} else {
// In IDE
String userDir = System.getProperty("user.dir").replace("\\",
"/");
userDir += "/WindowsMonitor";
int arch = Integer.parseInt(System
.getProperty("sun.arch.data.model"));
if (arch == 64) {
System.load(userDir + "/x64/Release/Monitor.dll");
System.load(userDir + "/x64/Release/Native.dll");
} else {
System.load(userDir + "/Release/Monitor.dll");
System.load(userDir + "/Release/Native.dll");
}
}
}
}
public static void main(String args[]){
LogicalDiskMonitor ld=new LogicalDiskMonitor();
String[] instances=ld.getInstances();
System.out.println("getInstances():");
for(String elem:instances){
System.out.println(elem);
}
String[] countlist=ld.getCounterList();
System.out.println("getCounterList():");
for(String elem:countlist){
System.out.println(elem);
}
System.out.println("getFreeSpacePercent(c )");
System.out.println(ld.getFreeSpacePercent("c:"));
System.out.println("getFreeMegabytes ");
System.out.println(ld.getFreeMegabytes("c:"));
/*public native double getFreeMegabytes(String instanceName);
public native double getCurrentDiskQueueLength(String instanceName);
public native double getDiskTimePercent(String instanceName, int idleTime);
public native double getAverageDiskQueueLength(String instanceName,
idleTime);
public native double getDiskReadTimePercent(String instanceName,
int idleTime);
public native double getAverageDiskReadQueueLength(String instanceName,
int idleTime);
public native double getDiskWriteTimePercent(String instanceName,
int idleTime);
public native double getAverageDiskWriteQueueLength(String instanceName,
int idleTime);
public native double getAverageDiskTransferTimeInSecond(
String instanceName, int idleTime);
public native double getAverageDiskReadTimeInSecond(String instanceName,
int idleTime);
public native double getAverageDiskWriteTimeInSecond(String instanceName,
int idleTime);
public native double getDiskTransfersPerSecond(String instanceName,
int idleTime);
public native double getDiskReadsPerSecond(String instanceName, int idleTime);
public native double getDiskWritesPerSecond(String instanceName,
int idleTime);
public native double getDiskBytesPerSecond(String instanceName, int idleTime);
public native double getDiskReadBytesPerSecond(String instanceName,
int idleTime);
public native double getDiskWriteBytesPerSecond(String instanceName,
int idleTime);
public native double getAverageDiskBytesPerTransfer(String instanceName,
int idleTime);
public native double getAverageDiskBytesPerRead(String instanceName,
int idleTime);
public native double getAverageDiskBytesPerWrite(String instanceName,
int idleTime);
public native double getIdleTimePercent(String instanceName, int idleTime);
public native double getSplitIOPerSecond(String instanceName, int idleTime);*/
}
}

View File

@ -1,5 +1,55 @@
package org.bench4q.monitor.test;
public class MonitorServiceTest {
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.bench4q.monitor.model.LogicalDiskModel;
import org.bench4q.monitor.test.communication.HttpRequester;
import org.bench4q.monitor.test.communication.HttpRequester.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
public class MonitorServiceTest {
private HttpRequester httpRequester;
public HttpRequester getHttpRequester() {
return httpRequester;
}
@Autowired
public void setHttpRequester(HttpRequester httpRequester) {
this.httpRequester = httpRequester;
}
public static void main(String[] args) {
String urlString = "http://localhost:5555/Monitor/LogicalDisk";
MonitorServiceTest monitorServiceTest = new MonitorServiceTest();
try {
HttpResponse httpResponse = monitorServiceTest.getHttpRequester()
.sendGet(urlString, null, null);
LogicalDiskModel logicalDiskModel = monitorServiceTest
.extractLogicalDiskModel(httpResponse.getContent());
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public LogicalDiskModel extractLogicalDiskModel(String content)
throws JAXBException {
LogicalDiskModel resultModel = new LogicalDiskModel();
Unmarshaller ummarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (LogicalDiskModel) ummarshaller
.unmarshal(new ByteArrayInputStream(content.getBytes()));
return resultModel;
}
}

View File

@ -0,0 +1,275 @@
package org.bench4q.monitor.test.communication;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import org.springframework.stereotype.Component;
@Component
public class HttpRequester {
private String defaultContentEncoding;
public HttpRequester()
{
this.setDefaultContentEncoding(Charset.defaultCharset().name());
}
public String getDefaultContentEncoding() {
return defaultContentEncoding;
}
public void setDefaultContentEncoding(String defaultContentEncoding) {
this.defaultContentEncoding = defaultContentEncoding;
}
public HttpResponse sendPost(String urlString, Map<String, String> params)
throws IOException {
return this.send(urlString, "POST", params, "", null);
}
public HttpResponse sendPostXml(String urlString, String contentString) throws IOException
{
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("Content-Type", "application/xml");
return this.send(urlString, "POST", null, contentString, hashMap);
}
public HttpResponse sendGet(String urlString, Map<String, String> params,
Map<String, String> properties) throws IOException {
return this.send(urlString, "GET", params, "", properties);
}
private HttpResponse send(String urlString, String method,
Map<String, String> parameters, String Content, Map<String, String> propertys)
throws IOException {
HttpURLConnection urlConnection = null;
if (method.equalsIgnoreCase("GET") && parameters != null) {
StringBuffer param = new StringBuffer();
int i = 0;
for (String key : parameters.keySet()) {
if (i == 0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(parameters.get(key));
i++;
}
urlString += param;
}
if (!urlString.startsWith("http://")) {
urlString = "http://" + urlString;
}
//urlString = URLEncoder.encode(urlString, "UTF-8");
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(method);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
if (propertys != null)
for (String key : propertys.keySet()) {
urlConnection.addRequestProperty(key, propertys.get(key));
}
if (method.equalsIgnoreCase("POST") && parameters != null) {
StringBuffer param = new StringBuffer();
for (String key : parameters.keySet()) {
param.append("&");
param.append(key).append("=").append(parameters.get(key));
}
urlConnection.getOutputStream().write(param.toString().getBytes());
urlConnection.getOutputStream().flush();
urlConnection.getOutputStream().close();
}
else if (method.equalsIgnoreCase("POST") && !Content.isEmpty()) {
urlConnection.getOutputStream().write(Content.getBytes());
urlConnection.getOutputStream().flush();
urlConnection.getOutputStream().close();
}
return this.makeContent(urlString, urlConnection);
}
private HttpResponse makeContent(String urlString,
HttpURLConnection urlConnection) {
// TODO Auto-generated method stub
HttpResponse httpResponser = new HttpResponse();
try {
InputStream in = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in));
httpResponser.contentCollection = new Vector<String>();
StringBuffer temp = new StringBuffer();
String line = bufferedReader.readLine();
while (line != null) {
httpResponser.contentCollection.add(line);
temp.append(line).append("\r\n");
line = bufferedReader.readLine();
}
bufferedReader.close();
String ecod = urlConnection.getContentEncoding();
if (ecod == null)
ecod = this.defaultContentEncoding;
httpResponser.setUrlString(urlString);
httpResponser.setDefaultPort(urlConnection.getURL()
.getDefaultPort());
httpResponser.setPort(urlConnection.getURL().getPort());
httpResponser.setProtocol(urlConnection.getURL().getProtocol());
httpResponser.setContent(new String(temp.toString().getBytes(),
ecod));
httpResponser.setContentEncoding(ecod);
httpResponser.setCode(urlConnection.getResponseCode());
httpResponser.setMessage(urlConnection.getResponseMessage());
httpResponser.setContentType(urlConnection.getContentType());
httpResponser.setConnectTimeout(urlConnection.getConnectTimeout());
httpResponser.setReadTimeout(urlConnection.getReadTimeout());
return httpResponser;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
return null;
}
public class HttpResponse {
String urlString;
int defaultPort;
int port;
String protocol;
String contentEncoding;
String content;
String contentType;
int code;
String message;
int connectTimeout;
int readTimeout;
Vector<String> contentCollection;
public String getUrlString() {
return urlString;
}
public void setUrlString(String urlString) {
this.urlString = urlString;
}
public int getDefaultPort() {
return defaultPort;
}
public void setDefaultPort(int defaultPort) {
this.defaultPort = defaultPort;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getContentEncoding() {
return contentEncoding;
}
public void setContentEncoding(String contentEncoding) {
this.contentEncoding = contentEncoding;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
public Vector<String> getContentCollection() {
return contentCollection;
}
public void setContentCollection(Vector<String> contentCollection) {
this.contentCollection = contentCollection;
}
}
}