add MemoryMonitor of Linux

This commit is contained in:
fanfuxiaoran 2013-08-11 17:32:38 +08:00
parent d3de4ec3d6
commit 3afbf4bff2
4 changed files with 132 additions and 73 deletions

View File

@ -1,7 +1,10 @@
package org.bench4q.monitor.api;
import java.io.IOException;
import org.bench4q.monitor.entiry.MonitorContorlEntity;
import org.bench4q.monitor.model.MemoryModel;
import org.bench4q.monitor.service.linux.MemoryServiceLinux;
import org.bench4q.monitor.service.windows.MemoryServiceWindows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -13,7 +16,14 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class MemoryController extends MonitorContorlEntity {
private MemoryServiceWindows memoryServiceWindows;
private MemoryServiceLinux memoryServiceLinux;
public MemoryServiceLinux getMemoryServiceLinux() {
return memoryServiceLinux;
}
@Autowired
public void setMemoryServiceLinux(MemoryServiceLinux memoryServiceLinux) {
this.memoryServiceLinux = memoryServiceLinux;
}
public MemoryServiceWindows getMemoryServiceWindows() {
return memoryServiceWindows;
}
@ -23,11 +33,12 @@ public class MemoryController extends MonitorContorlEntity {
}
@RequestMapping("/Memory")
@ResponseBody
public MemoryModel getMemoryInfo(){
public MemoryModel getMemoryInfo() throws NumberFormatException, IOException{
if(this.getOsNameString().contains("windows")){
return this.getMemoryServiceWindows().getMemoryInfo(this.getIdleTime());
}
else if(this.getOsNameString().contains("linux"))
return this.getMemoryServiceLinux().getMemoryInfo(this.getIdleTime());
else return null;
}
}

View File

@ -1,5 +1,7 @@
package org.bench4q.monitor.api;
import java.io.IOException;
import org.bench4q.monitor.entiry.MonitorContorlEntity;
import org.bench4q.monitor.model.ProcessorModel;
import org.bench4q.monitor.service.linux.ProcessorServiceLinux;
@ -14,11 +16,14 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class ProcessorController extends MonitorContorlEntity {
private ProcessorServiceWindows processorServiceWindows;
private ProcessorServiceLinux processorServiceLinux;
public ProcessorServiceLinux getProcessorServiceLinux() {
return processorServiceLinux;
}
@Autowired
public void setProcessorServiceLinux(ProcessorServiceLinux processorServiceLinux) {
@Autowired
public void setProcessorServiceLinux(
ProcessorServiceLinux processorServiceLinux) {
this.processorServiceLinux = processorServiceLinux;
}
@ -34,13 +39,15 @@ public class ProcessorController extends MonitorContorlEntity {
@RequestMapping("/Processor")
@ResponseBody
public ProcessorModel getProcessorInfo() {
public ProcessorModel getProcessorInfo() throws IOException, InterruptedException {
if (this.getOsNameString().contains("windows")) {
return this.getProcessorServiceWindows().getProcessorInfo(
this.getIdleTime());
} else if(this.getOsNameString().contains("linux"))
return this.getProcessorServiceLinux().getProcessorInfo(this.getIdleTime())
} else if (this.getOsNameString().contains("linux"))
return this.getProcessorServiceLinux().getProcessorInfo(
this.getIdleTime());
else
return null;
}
}

View File

@ -0,0 +1,53 @@
package org.bench4q.monitor.service.linux;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
import org.bench4q.monitor.entiry.LinuxFileRead;
import org.bench4q.monitor.model.MemoryModel;
public class MemoryServiceLinux extends LinuxFileRead {
public MemoryServiceLinux() throws FileNotFoundException {
super("/proc/meminfo");
}
public MemoryModel getMemoryInfo(int idleTime)
throws NumberFormatException, IOException {
MemoryModel memoryModel = new MemoryModel();
// double pageFaultsPerSecond;
// double pagesPerSecond;
// double pagesInputPerSecond;
// double pageReadsPerSecond;
double cacheBytes = 0;
double committedBytes = 0;
double availableKiloBytes = 0;
BufferedReader br = this.getBufferedReader();
String str = null;
StringTokenizer token = null;
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()) * 1024;
else if (str.equalsIgnoreCase("MemFree:"))
availableKiloBytes = Integer.parseInt(token.nextToken());
else if (str.equalsIgnoreCase("Committed_AS:"))
committedBytes = Integer.parseInt(token.nextToken());
}
memoryModel.setAvailableKiloBytes(availableKiloBytes);
memoryModel.setCacheBytes(cacheBytes);
memoryModel.setCommittedBytes(committedBytes);
return memoryModel;
}
}

View File

@ -11,81 +11,69 @@ import org.bench4q.monitor.model.ProcessorModelChild;
import org.springframework.stereotype.Service;
@Service
public class ProcessorServiceLinux {
public class ProcessorServiceLinux extends LinuxFileRead {
public ProcessorServiceLinux() throws FileNotFoundException {
this.fileRead = new LinuxFileRead("/proc/stat");
super("/proc/stat");
}
private LinuxFileRead fileRead;
public LinuxFileRead getFileRead() {
return fileRead;
}
public void setFileRead(LinuxFileRead fileRead) {
this.fileRead = fileRead;
}
public ProcessorModel getProcessorInfo(int idleTime) throws IOException,
public ProcessorModel getProcessorInfo(int idleTime) throws IOException,
InterruptedException {
ProcessorModel processorModel=new ProcessorModel();
ProcessorModelChild processorModelChild=new ProcessorModelChild();
BufferedReader br = this.getFileRead().getBufferedReader();
StringTokenizer tokens[]=new StringTokenizer[20];
StringTokenizer token = new StringTokenizer(this.getFileRead()
.getBufferedReader().readLine());
String instances[]=new String[20];
int i=0;
while(token!=null){
tokens[i]=token;
ProcessorModel processorModel = new ProcessorModel();
ProcessorModelChild processorModelChild = new ProcessorModelChild();
BufferedReader br = this.getBufferedReader();
StringTokenizer tokens[] = new StringTokenizer[20];
StringTokenizer token = new StringTokenizer(br.readLine());
String instances[] = new String[20];
int i = 0;
while (token != null) {
tokens[i] = token;
String instance;
instance=token.nextToken().toLowerCase();
if(!instance.contains("cpu")) break;
instances[i]=instance;
instance = token.nextToken().toLowerCase();
if (!instance.contains("cpu"))
break;
instances[i] = instance;
i++;
token = new StringTokenizer(this.getFileRead()
.getBufferedReader().readLine());
token = new StringTokenizer(br.readLine());
}
br.close();
Thread.sleep(idleTime);
br = this.getFileRead().getBufferedReader();
for(i=0;i<instances.length;i++)
{
int user1,user2=0,nice1,nice2=0,sys1,sys2=0,idle1,idle2=0;
processorModelChild.setInstance(instances[i]);
user1 = Integer.parseInt(tokens[i].nextToken());
nice1 = Integer.parseInt(tokens[i].nextToken());
sys1 = Integer.parseInt(tokens[i].nextToken());
idle1 = Integer.parseInt(tokens[i].nextToken());
token = new StringTokenizer(this.getFileRead()
.getBufferedReader().readLine());
while(token!=null){
token.nextToken();
user2 = Integer.parseInt(token.nextToken());
nice2 = Integer.parseInt(token.nextToken());
sys2 = Integer.parseInt(token.nextToken());
idle2 = Integer.parseInt(token.nextToken());
}
Double processorTimePercent=(double) ((user2 + sys2 + nice2) - (user1 + sys1 + nice1))
/( double) ((user2 + sys2 + idle2 + nice2) - (user1 + sys1
+ idle1 + nice1));
Double userTimePercent=(double) ((user2 ) - (user1 ))
/( double) ((user2 + sys2 + idle2 + nice2) - (user1 + sys1
+ idle1 + nice1));
Double privilegedTimePercent= (double) ( sys2 ) - ( sys1)
/( double) ((user2 + sys2 + idle2 + nice2) - (user1 + sys1
+ idle1 + nice1));;
processorModelChild.setPrivilegedTimePercent(privilegedTimePercent);
processorModelChild.setProcessorTimePercent(processorTimePercent);
processorModelChild.setUserTimePercent(userTimePercent);
processorModel.getProcessorModelList().add(processorModelChild);
br = this.getBufferedReader();
for (i = 0; i < instances.length; i++) {
int user1, user2 = 0, nice1, nice2 = 0, sys1, sys2 = 0, idle1, idle2 = 0;
processorModelChild.setInstance(instances[i]);
user1 = Integer.parseInt(tokens[i].nextToken());
nice1 = Integer.parseInt(tokens[i].nextToken());
sys1 = Integer.parseInt(tokens[i].nextToken());
idle1 = Integer.parseInt(tokens[i].nextToken());
token = new StringTokenizer(this.getBufferedReader().readLine());
while (token != null) {
token.nextToken();
user2 = Integer.parseInt(token.nextToken());
nice2 = Integer.parseInt(token.nextToken());
sys2 = Integer.parseInt(token.nextToken());
idle2 = Integer.parseInt(token.nextToken());
}
Double processorTimePercent = (double) ((user2 + sys2 + nice2) - (user1
+ sys1 + nice1))
/ (double) ((user2 + sys2 + idle2 + nice2) - (user1 + sys1
+ idle1 + nice1)) * 100;
Double userTimePercent = (double) ((user2) - (user1))
/ (double) ((user2 + sys2 + nice2 + idle2) - (user1 + sys1
+ nice1 + idle1)) * 100;
Double privilegedTimePercent = (double) (sys2)
- (sys1)
/ (double) ((user2 + sys2 + nice2 + idle2) - (user1 + sys1
+ nice1 + idle2)) * 100;
processorModelChild.setPrivilegedTimePercent(privilegedTimePercent);
processorModelChild.setProcessorTimePercent(processorTimePercent);
processorModelChild.setUserTimePercent(userTimePercent);
processorModel.getProcessorModelList().add(processorModelChild);
}
br.close();
return processorModel;