From d3de4ec3d644d85a73026fda466337f74444e308 Mon Sep 17 00:00:00 2001 From: fanfuxiaoran Date: Sun, 11 Aug 2013 15:03:14 +0800 Subject: [PATCH] add ProcessorMonitor of Linux --- .../monitor/api/ProcessorController.java | 14 ++- .../bench4q/monitor/entiry/LinuxFileRead.java | 23 +++++ .../monitor/model/ProcessorModelChild.java | 8 +- .../service/linux/ProcessorServiceLinux.java | 93 +++++++++++++++++++ .../windows/ProcessorServiceWindows.java | 2 - .../bench4q/monitor/test/ProcessorTest.java | 2 +- 6 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/bench4q/monitor/entiry/LinuxFileRead.java create mode 100644 src/main/java/org/bench4q/monitor/service/linux/ProcessorServiceLinux.java diff --git a/src/main/java/org/bench4q/monitor/api/ProcessorController.java b/src/main/java/org/bench4q/monitor/api/ProcessorController.java index 89707567..d7cfaf02 100644 --- a/src/main/java/org/bench4q/monitor/api/ProcessorController.java +++ b/src/main/java/org/bench4q/monitor/api/ProcessorController.java @@ -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()) } } diff --git a/src/main/java/org/bench4q/monitor/entiry/LinuxFileRead.java b/src/main/java/org/bench4q/monitor/entiry/LinuxFileRead.java new file mode 100644 index 00000000..065a4936 --- /dev/null +++ b/src/main/java/org/bench4q/monitor/entiry/LinuxFileRead.java @@ -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))); + } +} diff --git a/src/main/java/org/bench4q/monitor/model/ProcessorModelChild.java b/src/main/java/org/bench4q/monitor/model/ProcessorModelChild.java index 0b682bdf..382a1824 100644 --- a/src/main/java/org/bench4q/monitor/model/ProcessorModelChild.java +++ b/src/main/java/org/bench4q/monitor/model/ProcessorModelChild.java @@ -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; - } } diff --git a/src/main/java/org/bench4q/monitor/service/linux/ProcessorServiceLinux.java b/src/main/java/org/bench4q/monitor/service/linux/ProcessorServiceLinux.java new file mode 100644 index 00000000..0dec8258 --- /dev/null +++ b/src/main/java/org/bench4q/monitor/service/linux/ProcessorServiceLinux.java @@ -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