From 503857de09294a3d0a8643a67da90f420ea7ac21 Mon Sep 17 00:00:00 2001 From: fanfuxiaoran <495538672@qq.com> Date: Wed, 17 Jul 2013 20:37:39 +0800 Subject: [PATCH] Add a simple LogicalDiskModel and LogicalDiskControl as a test. --- .../org/bench4q/monitor/MonitorServer.java | 2 + .../bench4q/monitor/api/MonitorControl.java | 45 +++ .../monitor/model/LogicalDiskModel.java | 34 +++ .../windows/LogicalDiskMonitor.java | 3 + src/main/java/wxr/LogicalDisk.java | 136 +++++++++ .../monitor/test/MonitorServiceTest.java | 52 +++- .../test/communication/HttpRequester.java | 275 ++++++++++++++++++ 7 files changed, 546 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/bench4q/monitor/api/MonitorControl.java create mode 100644 src/main/java/org/bench4q/monitor/model/LogicalDiskModel.java create mode 100644 src/main/java/wxr/LogicalDisk.java create mode 100644 src/test/java/org/bench4q/monitor/test/communication/HttpRequester.java diff --git a/src/main/java/org/bench4q/monitor/MonitorServer.java b/src/main/java/org/bench4q/monitor/MonitorServer.java index 0f90f7f3..a742e19e 100644 --- a/src/main/java/org/bench4q/monitor/MonitorServer.java +++ b/src/main/java/org/bench4q/monitor/MonitorServer.java @@ -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()); diff --git a/src/main/java/org/bench4q/monitor/api/MonitorControl.java b/src/main/java/org/bench4q/monitor/api/MonitorControl.java new file mode 100644 index 00000000..022b7913 --- /dev/null +++ b/src/main/java/org/bench4q/monitor/api/MonitorControl.java @@ -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 freeSpacePercent = new HashMap(); + + for (String elem : instances) { + + freeSpacePercent.put(elem, this.getLogicalDiskMonitor() + .getFreeSpacePercent(elem)); + } + LogicalDiskModel logicalDiskModel = new LogicalDiskModel(); + logicalDiskModel.setInstances(instances); + logicalDiskModel.setFreeSpacePercent(freeSpacePercent); + return logicalDiskModel; + } + +} diff --git a/src/main/java/org/bench4q/monitor/model/LogicalDiskModel.java b/src/main/java/org/bench4q/monitor/model/LogicalDiskModel.java new file mode 100644 index 00000000..07a7b99c --- /dev/null +++ b/src/main/java/org/bench4q/monitor/model/LogicalDiskModel.java @@ -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 freeSpacePercent; + + @XmlElementWrapper(name = "instanceList") + @XmlElement(name = "instance") + public String[] getInstances() { + return instances; + } + + public void setInstances(String[] instances) { + this.instances = instances; + } + + public Map getFreeSpacePercent() { + return freeSpacePercent; + } + + public void setFreeSpacePercent(Map freeSpacePercent) { + this.freeSpacePercent = freeSpacePercent; + } + +} diff --git a/src/main/java/org/bench4q/monitor/performance/windows/LogicalDiskMonitor.java b/src/main/java/org/bench4q/monitor/performance/windows/LogicalDiskMonitor.java index 0ad31f6c..a7cfe91f 100644 --- a/src/main/java/org/bench4q/monitor/performance/windows/LogicalDiskMonitor.java +++ b/src/main/java/org/bench4q/monitor/performance/windows/LogicalDiskMonitor.java @@ -1,5 +1,8 @@ package org.bench4q.monitor.performance.windows; +import org.springframework.stereotype.Component; + +@Component public class LogicalDiskMonitor { public native String[] getInstances(); diff --git a/src/main/java/wxr/LogicalDisk.java b/src/main/java/wxr/LogicalDisk.java new file mode 100644 index 00000000..c2f5b37c --- /dev/null +++ b/src/main/java/wxr/LogicalDisk.java @@ -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);*/ +} + +} diff --git a/src/test/java/org/bench4q/monitor/test/MonitorServiceTest.java b/src/test/java/org/bench4q/monitor/test/MonitorServiceTest.java index d70a323c..72845d1c 100644 --- a/src/test/java/org/bench4q/monitor/test/MonitorServiceTest.java +++ b/src/test/java/org/bench4q/monitor/test/MonitorServiceTest.java @@ -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; + } } diff --git a/src/test/java/org/bench4q/monitor/test/communication/HttpRequester.java b/src/test/java/org/bench4q/monitor/test/communication/HttpRequester.java new file mode 100644 index 00000000..e0f7877a --- /dev/null +++ b/src/test/java/org/bench4q/monitor/test/communication/HttpRequester.java @@ -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 params) + throws IOException { + return this.send(urlString, "POST", params, "", null); + } + + public HttpResponse sendPostXml(String urlString, String contentString) throws IOException + { + HashMap hashMap = new HashMap(); + hashMap.put("Content-Type", "application/xml"); + return this.send(urlString, "POST", null, contentString, hashMap); + } + + public HttpResponse sendGet(String urlString, Map params, + Map properties) throws IOException { + return this.send(urlString, "GET", params, "", properties); + } + + private HttpResponse send(String urlString, String method, + Map parameters, String Content, Map 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(); + 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 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 getContentCollection() { + return contentCollection; + } + + public void setContentCollection(Vector contentCollection) { + this.contentCollection = contentCollection; + } + + } +}