add ProcessorMonitor of Linux

This commit is contained in:
fanfuxiaoran 2013-08-11 15:03:14 +08:00
parent c5aea1d7c9
commit d3de4ec3d6
6 changed files with 130 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package org.bench4q.monitor.api;
import org.bench4q.monitor.entiry.MonitorContorlEntity;
import org.bench4q.monitor.model.ProcessorModel;
import org.bench4q.monitor.service.linux.ProcessorServiceLinux;
import org.bench4q.monitor.service.windows.ProcessorServiceWindows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -12,6 +13,15 @@ import org.springframework.web.bind.annotation.ResponseBody;
@RequestMapping("/Monitor")
public class ProcessorController extends MonitorContorlEntity {
private ProcessorServiceWindows processorServiceWindows;
private ProcessorServiceLinux processorServiceLinux;
public ProcessorServiceLinux getProcessorServiceLinux() {
return processorServiceLinux;
}
@Autowired
public void setProcessorServiceLinux(ProcessorServiceLinux processorServiceLinux) {
this.processorServiceLinux = processorServiceLinux;
}
public ProcessorServiceWindows getProcessorServiceWindows() {
return processorServiceWindows;
}
@ -29,8 +39,8 @@ public class ProcessorController extends MonitorContorlEntity {
if (this.getOsNameString().contains("windows")) {
return this.getProcessorServiceWindows().getProcessorInfo(
this.getIdleTime());
} else
return null;
} else if(this.getOsNameString().contains("linux"))
return this.getProcessorServiceLinux().getProcessorInfo(this.getIdleTime())
}
}

View File

@ -0,0 +1,23 @@
package org.bench4q.monitor.entiry;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
public class LinuxFileRead {
private BufferedReader bufferedReader;
public BufferedReader getBufferedReader() {
return bufferedReader;
}
public void setBufferedReader(BufferedReader bufferedReader) {
this.bufferedReader = bufferedReader;
}
public LinuxFileRead(String fileName) throws FileNotFoundException{
File file = new File(fileName);
this.bufferedReader= new BufferedReader(new InputStreamReader(new FileInputStream(file)));
}
}

View File

@ -8,7 +8,7 @@ public class ProcessorModelChild {
private Double processorTimePercent;
private Double userTimePercent;
private Double privilegedTimePercent;
private Double dpcTimePercent;// getDpcTimePercent
public String getInstance() {
return instance;
}
@ -33,11 +33,5 @@ public class ProcessorModelChild {
public void setPrivilegedTimePercent(Double privilegedTimePercent) {
this.privilegedTimePercent = privilegedTimePercent;
}
public Double getDpcTimePercent() {
return dpcTimePercent;
}
public void setDpcTimePercent(Double dpcTimePercent) {
this.dpcTimePercent = dpcTimePercent;
}
}

View File

@ -0,0 +1,93 @@
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.ProcessorModel;
import org.bench4q.monitor.model.ProcessorModelChild;
import org.springframework.stereotype.Service;
@Service
public class ProcessorServiceLinux {
public ProcessorServiceLinux() throws FileNotFoundException {
this.fileRead = new LinuxFileRead("/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,
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;
String instance;
instance=token.nextToken().toLowerCase();
if(!instance.contains("cpu")) break;
instances[i]=instance;
i++;
token = new StringTokenizer(this.getFileRead()
.getBufferedReader().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.close();
return processorModel;
}
}

View File

@ -24,8 +24,6 @@ public class ProcessorServiceWindows {
for (String elem : instances) {
ProcessorModelChild processorModelChild = new ProcessorModelChild();
processorModelChild.setDpcTimePercent(this.getProcessorMonitor()
.getDpcTimePercent(elem, idleTime));
processorModelChild.setInstance(elem);
processorModelChild.setPrivilegedTimePercent(this
.getProcessorMonitor().getPrivilegedTimePercent(elem,

View File

@ -61,7 +61,7 @@ public class ProcessorTest {
while(iterator.hasNext()){
ProcessorModelChild processorModelChild=iterator.next();
System.out.println("instance"+processorModelChild.getInstance());
System.out.println("dpcTimePercent"+processorModelChild.getDpcTimePercent());
System.out.println("privilegedTimePercent"+processorModelChild.getPrivilegedTimePercent());
System.out.println("processorTimePercent"+processorModelChild.getProcessorTimePercent());
System.out.println("userTimePercent"+processorModelChild.getUserTimePercent());