This commit is contained in:
fanfuxiaoran 2013-08-14 11:38:07 +08:00
parent eecbf3a5db
commit d6051dae65
2 changed files with 26 additions and 85 deletions

View File

@ -78,13 +78,13 @@ public class MemoryMonitor extends LinuxFileRead {
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringTokenizer token ;
String str=in.readLine();
str=in.readLine();
int si=0,so=0;
token=new StringTokenizer(str);
for(int i=0;;i++){
str=token.nextToken().toLowerCase();
if(str.contains("si")) {si=i;System.out.println(str+" "+si);}
if(str.contains("so")) {so=i;System.out.println(str+" "+si);}
if(str.contains("si")) {si=i;}
if(str.contains("so")) {so=i;}
if(!(token.hasMoreTokens())) break;
@ -113,11 +113,11 @@ public class MemoryMonitor extends LinuxFileRead {
continue;
if (str.equalsIgnoreCase("Cached:"))
cacheBytes = Integer.parseInt(token.nextToken()) ;
cacheBytes = Integer.parseInt(token.nextToken())*1024 ;
else if (str.equalsIgnoreCase("MemFree:"))
availableKiloBytes = Integer.parseInt(token.nextToken());
else if (str.equalsIgnoreCase("Committed_AS:"))
committedBytes = Integer.parseInt(token.nextToken());
committedBytes = Integer.parseInt(token.nextToken())*1024;
}
}
}

View File

@ -1,91 +1,32 @@
package org.bench4q.monitor.service.linux;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import org.bench4q.monitor.entiry.LinuxFileRead;
import org.bench4q.monitor.model.MemoryModel;
import org.bench4q.monitor.performance.linux.MemoryMonitor;
public class MemoryServiceLinux extends LinuxFileRead {
public MemoryServiceLinux() throws FileNotFoundException {
super("/proc/meminfo");
public class MemoryServiceLinux {
private MemoryMonitor memoryMonitor;
public MemoryServiceLinux() throws FileNotFoundException {
memoryMonitor=new MemoryMonitor();
}
public MemoryModel getMemoryInfo()
throws NumberFormatException, IOException {
System.out.println("jin ru");
MemoryModel memoryModel = new MemoryModel();
// double pageFaultsPerSecond;
// double pagesPerSecond;
// double pagesInputPerSecond;
// double pageReadsPerSecond;
double cacheBytes = 0;
double committedBytes = 0;
double availableKiloBytes = 0;
double pagesPerSecond=0;//(k)
double pagesInputPerSecond=0;
double pagesOutputPerSecond = 0;
String str = null;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("vmstat");//df -hl 查看硬盘空间
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
str=in.readLine();
str=in.readLine();
System.out.println(str);
StringTokenizer token = null;
int si=0,so=0;
token=new StringTokenizer(str);
for(int i=0;;i++){
str=token.nextToken().toLowerCase();
if(str.contains("si")) {si=i;}
if(str.contains("so")) {so=i;}
if(!(token.hasMoreTokens())) break;
}
str=in.readLine();
System.out.println(str);
token=new StringTokenizer(str);
for(int i=0;;i++){
str=token.nextToken().toLowerCase();
if(i==si) pagesInputPerSecond=(double)Integer.parseInt(str);
if(i==so) pagesOutputPerSecond=(double)Integer.parseInt(str);
if(!(token.hasMoreTokens())) break;
}
pagesPerSecond=pagesInputPerSecond+pagesOutputPerSecond;
BufferedReader br = this.getBufferedReader();
public MemoryModel getMemoryInfo(){
MemoryModel memoryModel=new MemoryModel();
double pagesPerSecond=this.memoryMonitor.getPagesPerSecond();
double pagesInputPerSecond=this.memoryMonitor.getPagesInputPerSecond();
double pagesOutputPerSecond=this.memoryMonitor.getPagesOutputPerSecond();
double cacheBytes=this.memoryMonitor.getCacheBytes();
double committedBytes=this.memoryMonitor.getCommittedBytes();
double availableKiloBytes=this.memoryMonitor.getAvailableKiloBytes();
memoryModel.setAvailableKiloBytes(availableKiloBytes);
memoryModel.setCacheBytes(cacheBytes);
memoryModel.setCommittedBytes(committedBytes);
memoryModel.setPagesInputPerSecond(pagesInputPerSecond);
memoryModel.setPagesOutputPerSecond(pagesOutputPerSecond);
memoryModel.setPagesPerSecond(pagesPerSecond);
return memoryModel;
while ((str = br.readLine()) != null) {
token = new StringTokenizer(str);
if (!token.hasMoreTokens())
continue;
str = token.nextToken();
if (!token.hasMoreTokens())
continue;
if (str.equalsIgnoreCase("Cached:"))
cacheBytes = Integer.parseInt(token.nextToken()) ;
else if (str.equalsIgnoreCase("MemFree:"))
availableKiloBytes = Integer.parseInt(token.nextToken());
else if (str.equalsIgnoreCase("Committed_AS:"))
committedBytes = Integer.parseInt(token.nextToken());
}
memoryModel.setPagesOutputPerSecond(pagesOutputPerSecond);
memoryModel.setPagesPerSecond(pagesPerSecond);
memoryModel.setPagesInputPerSecond(pagesInputPerSecond);
memoryModel.setAvailableKiloBytes(availableKiloBytes);
memoryModel.setCacheBytes(cacheBytes);
memoryModel.setCommittedBytes(committedBytes);
return memoryModel;
}
}