add start and stop record

This commit is contained in:
fanfuxiaoran 2014-03-18 14:24:06 +08:00
parent 6248013221
commit f2a02bb374
15 changed files with 325 additions and 94 deletions

View File

@ -11,6 +11,11 @@
<name>TCSE, ISCAS</name> <name>TCSE, ISCAS</name>
</organization> </organization>
<dependencies> <dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@ -52,6 +57,7 @@
<artifactId>sigar</artifactId> <artifactId>sigar</artifactId>
<version>1.6.4</version> <version>1.6.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>dom4j</groupId> <groupId>dom4j</groupId>
<artifactId>dom4j</artifactId> <artifactId>dom4j</artifactId>

View File

@ -1,10 +1,6 @@
package org.bench4q.monitor; package org.bench4q.monitor;
import java.util.Timer;
import org.bench4q.monitor.service.TimerService;
import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector; import org.eclipse.jetty.server.bio.SocketConnector;
@ -76,11 +72,7 @@ public class MonitorServer {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
MonitorServer monitorServer = new MonitorServer(5556); MonitorServer monitorServer = new MonitorServer(5557);
monitorServer.start(); monitorServer.start();
Timer timer = new Timer();
timer.schedule(new TimerService(), 1000, 60000);
} }
} }

View File

@ -0,0 +1,29 @@
package org.bench4q.monitor.api;
import java.util.Timer;
import org.bench4q.monitor.service.TimerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/monitor")
public class StartToRecord {
private Timer timer = new Timer();
@RequestMapping("/start")
@ResponseBody
public String start() {
timer.schedule(new TimerService(), 1000, 2000);
return new String("startted");
}
@RequestMapping("/stop")
@ResponseBody
public String stop() {
timer.cancel();
return new String("stopped");
}
}

View File

@ -14,17 +14,29 @@ import org.bench4q.monitor.service.GetSigar;
import org.hyperic.sigar.FileSystemUsage; import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "fileSystem") @XmlRootElement(name = "fileSystem")
public class FileSystemModel { public class FileSystemModel {
@Expose
private String fileDir; private String fileDir;
@Expose
private Double diskReadKBytesRate; private Double diskReadKBytesRate;
@Expose
private Double diskWriteKBytesRate; private Double diskWriteKBytesRate;
@Expose
private double curDiskQueLength; private double curDiskQueLength;
@Expose
private double totalGB; private double totalGB;
@Expose
private double usedGB; private double usedGB;
@Expose
private double freeGB; private double freeGB;
@Expose
private double usedPercent; private double usedPercent;
@Expose
private double diskTotalKBytesRate; private double diskTotalKBytesRate;
private FileSystemUsage fileSystemUsage; private FileSystemUsage fileSystemUsage;
public static void main(String args[]) throws SigarException, public static void main(String args[]) throws SigarException,
@ -193,6 +205,7 @@ class CalculateDiskWriteRate extends CalculateDiskRate {
long getFileKBytes() throws SigarException { long getFileKBytes() throws SigarException {
return fileSystemUsage.getDiskWriteBytes() / 1024L; return fileSystemUsage.getDiskWriteBytes() / 1024L;
} }
} }
class CalculateDiskReadRate extends CalculateDiskRate { class CalculateDiskReadRate extends CalculateDiskRate {
@ -206,4 +219,5 @@ class CalculateDiskReadRate extends CalculateDiskRate {
return fileSystemUsage.getDiskReadBytes() / 1024L; return fileSystemUsage.getDiskReadBytes() / 1024L;
} }
} }

View File

@ -1,42 +1,50 @@
package org.bench4q.monitor.model; package org.bench4q.monitor.model;
import java.beans.Transient;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;
import org.bench4q.monitor.service.GetSigar; import org.bench4q.monitor.service.GetSigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap; import org.hyperic.sigar.Swap;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.Mem; import org.hyperic.sigar.Mem;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "Memory") @XmlRootElement(name = "Memory")
public class MemoryModel { public class MemoryModel {
@Expose
private long pagesPerSecond; private long pagesPerSecond;
@Expose
private long pagesInputPerSecond; private long pagesInputPerSecond;
@Expose
private long pagesOutputPerSecond; private long pagesOutputPerSecond;
@Expose
private long availableKiloBytes; private long availableKiloBytes;
@Expose
private long totalKiloBytes; private long totalKiloBytes;
@Expose
private double memoryUsedPercent; private double memoryUsedPercent;
@Expose
private double swapKiloBytes; private double swapKiloBytes;
@Expose
private double swapFreeKiloBytes; private double swapFreeKiloBytes;
private Sigar sigar = GetSigar.getSigar(); private Sigar sigar = GetSigar.getSigar();
private Swap swap; private Swap swap;
private Mem mem; private Mem mem;
private Logger logger = Logger.getLogger(MemoryModel.class);
// test code
public static void main(String[] args) throws SigarException { public static void main(String[] args) throws SigarException {
MemoryModel model = new MemoryModel(); MemoryModel model = new MemoryModel();
System.out.println(model.logger.getLoggerRepository());
System.out.println("PagesRate: " + model.getPagesPerSecond()); System.out.println("PagesRate: " + model.getPagesPerSecond());
System.out.println("PagesIn: " + model.getPagesInputPerSecond()); System.out.println("PagesIn: " + model.getPagesInputPerSecond());
System.out.println("PagesIn: " + model.getPagesOutputPerSecond()); System.out.println("PagesIn: " + model.getPagesOutputPerSecond());
System.out.println("UsedPerc: " + model.getMemoryUsedPercent()+"%"); System.out.println("UsedPerc: " + model.getMemoryUsedPercent() + "%");
System.out.println("Total: " + model.getTotalKiloBytes()+"kb"); System.out.println("Total: " + model.getTotalKiloBytes() + "kb");
System.out.println("Aval: " + model.getAvailableKiloBytes()+"kb"); System.out.println("Aval: " + model.getAvailableKiloBytes() + "kb");
System.out.println("swap total:"+model.getSwapKiloBytes()+"kb"); System.out.println("swap total:" + model.getSwapKiloBytes() + "kb");
System.out.println("swap free :"+model.getSwapFreeKiloBytes()+"kb"); System.out.println("swap free :" + model.getSwapFreeKiloBytes() + "kb");
} }
public MemoryModel() throws SigarException { public MemoryModel() throws SigarException {
@ -85,7 +93,7 @@ public class MemoryModel {
@XmlElement @XmlElement
public long getAvailableKiloBytes() throws SigarException { public long getAvailableKiloBytes() throws SigarException {
mem = sigar.getMem(); mem = sigar.getMem();
setAvailableKiloBytes(mem.getActualFree()/ 1024L); setAvailableKiloBytes(mem.getActualFree() / 1024L);
return availableKiloBytes; return availableKiloBytes;
} }
@ -115,21 +123,50 @@ public class MemoryModel {
private void setMemoryUsedPercent(double memoryUsedPercent) { private void setMemoryUsedPercent(double memoryUsedPercent) {
this.memoryUsedPercent = memoryUsedPercent; this.memoryUsedPercent = memoryUsedPercent;
} }
@XmlElement
@XmlElement
public double getSwapKiloBytes() { public double getSwapKiloBytes() {
return swapKiloBytes; return swapKiloBytes;
} }
private void setSwapKiloBytes() { private void setSwapKiloBytes() {
this.swapKiloBytes = this.swap.getTotal()/1024L; this.swapKiloBytes = this.swap.getTotal() / 1024L;
} }
@XmlElement
@XmlElement
public double getSwapFreeKiloBytes() { public double getSwapFreeKiloBytes() {
return swapFreeKiloBytes; return swapFreeKiloBytes;
} }
private void setSwapFreeKiloBytes() { private void setSwapFreeKiloBytes() {
this.swapFreeKiloBytes = this.swap.getFree()/1024L; this.swapFreeKiloBytes = this.swap.getFree() / 1024L;
}
@Transient
public Sigar getSigar() {
return sigar;
}
public void setSigar(Sigar sigar) {
this.sigar = sigar;
}
@Transient
public Swap getSwap() {
return swap;
}
public void setSwap(Swap swap) {
this.swap = swap;
}
@Transient
public Mem getMem() {
return mem;
}
public void setMem(Mem mem) {
this.mem = mem;
} }
} }

View File

@ -1,6 +1,8 @@
package org.bench4q.monitor.model; package org.bench4q.monitor.model;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -11,37 +13,70 @@ import javax.xml.bind.annotation.XmlType;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "history") @XmlRootElement(name = "history")
@XmlType @XmlType
public class MonitorMain { public class MonitorMain {
/* @XmlElement @Expose
private String date;*/ @XmlElement
private String date;
@Expose
@XmlElement(name = "processor_info") @XmlElement(name = "processor_info")
private ProcessorModel processorModel; private ProcessorModel processorModel;
@Expose
@XmlElement(name = "memory_info") @XmlElement(name = "memory_info")
private MemoryModel memoryModel; private MemoryModel memoryModel;
@Expose
@XmlElement(name = "disk_info") @XmlElement(name = "disk_info")
private PhysicalDiskModel physicalDiskModel; private PhysicalDiskModel physicalDiskModel;
@Expose
@XmlElement(name = "network_info") @XmlElement(name = "network_info")
private NetworkInterfaceModel networkInterfaceModel; private NetworkInterfaceModel networkInterfaceModel;
@Expose
@XmlElement(name = "process_info") @XmlElement(name = "process_info")
private ProcessModel processModel; private ProcessModel processModel;
private SimpleDateFormat dateFormat = new SimpleDateFormat( private SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss"); "yyyy-MM-dd-HH-mm-ss");
public MonitorMain() {
}
public MonitorMain(Date date) throws SigarException, InterruptedException, public MonitorMain(Date date) throws SigarException, InterruptedException,
ExecutionException { ExecutionException {
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
/*this.date = dateFormat.format(date);*/ this.date = dateFormat.format(date);
long startTime = System.currentTimeMillis();
processorModel = new ProcessorModel(); List<Thread> threadList = new ArrayList<Thread>();
threadList.add(new Thread(new ProcessModel(this)));
threadList.add(new Thread(new PhysicalDiskModel(this)));
threadList.add(new Thread(new ProcessorModel(this)));
threadList.add(new Thread(new NetworkInterfaceModel(this)));
for (Thread thread : threadList)
thread.start();
memoryModel = new MemoryModel(); memoryModel = new MemoryModel();
physicalDiskModel = new PhysicalDiskModel(); boolean threadIsAlive = true;
networkInterfaceModel = new NetworkInterfaceModel(); while (threadIsAlive) {
processModel = new ProcessModel(); threadIsAlive = false;
for (Thread thread : threadList) {
if (thread.isAlive())
threadIsAlive = true;
}
}
System.out.println(System.currentTimeMillis() - startTime);
}
public void setProcesssModel(ProcessModel processModel) {
this.processModel = processModel;
}
public void setPhysicalDiskModel(PhysicalDiskModel physicalDiskModel) {
this.physicalDiskModel = physicalDiskModel;
}
public void setProcessorModel(ProcessorModel processorModel) {
this.processorModel = processorModel;
}
public void setNetworkInterfaceModel(
NetworkInterfaceModel networkInterfaceModel) {
this.networkInterfaceModel = networkInterfaceModel;
} }
} }

View File

@ -14,20 +14,25 @@ import org.bench4q.monitor.service.DataFomat;
import org.bench4q.monitor.service.GetSigar; import org.bench4q.monitor.service.GetSigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "NetworkInterface") @XmlRootElement(name = "NetworkInterface")
public class NetworkInterfaceModel { public class NetworkInterfaceModel implements Runnable {
@Expose
private Double kiloBytesTotalPerSecond; private Double kiloBytesTotalPerSecond;
@Expose
private Double kiloBytesReceivedPerSecond; private Double kiloBytesReceivedPerSecond;
@Expose
private Double kiloBytesSentPerSecond; private Double kiloBytesSentPerSecond;
private long interval = 500; private long interval = 500;
private Logger logger = Logger.getLogger(NetworkInterfaceModel.class);
/* private Logger logger = Logger.getLogger(NetworkInterfaceModel.class); */
public static void main(String[] args) { public static void main(String[] args) {
while (true) { while (true) {
long Time = System.currentTimeMillis(); long Time = System.currentTimeMillis();
NetworkInterfaceModel test = new NetworkInterfaceModel(); NetworkInterfaceModel test = new NetworkInterfaceModel();
test.logger.info("ll");
System.out.println("KiloBytesReceivedPerSecond:" System.out.println("KiloBytesReceivedPerSecond:"
+ test.getKiloBytesReceivedPerSecond() + "kb/s"); + test.getKiloBytesReceivedPerSecond() + "kb/s");
System.out.println("KiloBytesSentPerSecond:" System.out.println("KiloBytesSentPerSecond:"
@ -38,9 +43,18 @@ public class NetworkInterfaceModel {
} }
} }
private MonitorMain monitorMain;
public NetworkInterfaceModel(MonitorMain monitorMain) {
this.monitorMain = monitorMain;
}
public void run() {
monitorMain.setNetworkInterfaceModel(new NetworkInterfaceModel());
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public NetworkInterfaceModel() { public NetworkInterfaceModel() {
logger.error("hello");
// this can be used for all // this can be used for all
ExecutorService executorService = Executors.newFixedThreadPool(2); ExecutorService executorService = Executors.newFixedThreadPool(2);
@ -50,7 +64,6 @@ public class NetworkInterfaceModel {
Future<Double> futureBytesSentPerSecond = executorService Future<Double> futureBytesSentPerSecond = executorService
.submit(new CalculateBytesSentPerSecond(interval)); .submit(new CalculateBytesSentPerSecond(interval));
try { try {
System.out.println(futureBytesReceivedPerSecond.get());
this.setKiloBytesReceivedPerSecond(futureBytesReceivedPerSecond this.setKiloBytesReceivedPerSecond(futureBytesReceivedPerSecond
.get()); .get());
@ -121,7 +134,7 @@ abstract class CalculateBytesPerSecond implements Callable {
postBytesSentSoFar = this.getBytesSoFar(); postBytesSentSoFar = this.getBytesSoFar();
this.kiloBytesPerSecond = (double) ((postBytesSentSoFar - preBytesSentSoFar) this.kiloBytesPerSecond = (double) ((postBytesSentSoFar - preBytesSentSoFar)
/ DataFomat.caculateTimeInterval(startTime, endTime) / 1024L); / DataFomat.caculateTimeInterval(startTime, endTime) / 1024L);
return new Double(Math.round(this.kiloBytesPerSecond*100)/100); return new Double(Math.round(this.kiloBytesPerSecond * 100) / 100);
} catch (SigarException e) { } catch (SigarException e) {
logger.error(e, e.fillInStackTrace()); logger.error(e, e.fillInStackTrace());
return new Double(0); return new Double(0);

View File

@ -12,6 +12,7 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.apache.log4j.Logger;
import org.bench4q.monitor.service.GetSigar; import org.bench4q.monitor.service.GetSigar;
import org.bench4q.monitor.service.GetThreadPool; import org.bench4q.monitor.service.GetThreadPool;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
@ -19,21 +20,31 @@ import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage; import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement @XmlRootElement
public class PhysicalDiskModel { public class PhysicalDiskModel implements Runnable{
@Expose
private Double diskReadKBytesRate; private Double diskReadKBytesRate;
@Expose
private Double diskWriteKBytesRate; private Double diskWriteKBytesRate;
@Expose
private double curDiskQueLength; private double curDiskQueLength;
@Expose
private double totalGB; private double totalGB;
@Expose
private double usedGB; private double usedGB;
@Expose
private double freeGB; private double freeGB;
@Expose
private double usedPercent; private double usedPercent;
@Expose
private List<FileSystemModel> fieFileSystemModels; private List<FileSystemModel> fieFileSystemModels;
private Sigar sigar = GetSigar.getSigar(); private Sigar sigar = GetSigar.getSigar();
private FileSystem[] fileSystemList; private FileSystem[] fileSystemList;
private final int interval = 500; private final int interval = 500;
private MonitorMain monitorMain;
// private Logger logger = Logger.getLogger(PhysicalDiskModel.class); private Logger logger = Logger.getLogger(PhysicalDiskModel.class);
public static void main(String[] args) throws SigarException, public static void main(String[] args) throws SigarException,
InterruptedException, ExecutionException { InterruptedException, ExecutionException {
@ -50,7 +61,25 @@ public class PhysicalDiskModel {
System.out.println("total:" + physicalDiskModel.getTotalGB()); System.out.println("total:" + physicalDiskModel.getTotalGB());
} }
public PhysicalDiskModel(MonitorMain monitorMain) {
this.monitorMain = monitorMain;
}
public void run() {
try {
monitorMain.setPhysicalDiskModel(new PhysicalDiskModel());
} catch (SigarException e) {
// TODO Auto-generated catch block
logger.info(e, e.fillInStackTrace());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
logger.info(e, e.fillInStackTrace());
} catch (ExecutionException e) {
// TODO Auto-generated catch block
logger.info(e, e.fillInStackTrace());
}
}
public PhysicalDiskModel() throws SigarException, InterruptedException, public PhysicalDiskModel() throws SigarException, InterruptedException,
ExecutionException { ExecutionException {

View File

@ -10,18 +10,25 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;
import org.bench4q.monitor.service.GetSigar; import org.bench4q.monitor.service.GetSigar;
import org.bench4q.monitor.service.GetThreadPool; import org.bench4q.monitor.service.GetThreadPool;
import org.hyperic.sigar.ProcState; import org.hyperic.sigar.ProcState;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement @XmlRootElement
public class ProcessModel { public class ProcessModel implements Runnable {
private Sigar sigar = GetSigar.getSigar(); private Sigar sigar = GetSigar.getSigar();
@Expose
private List<ProcessModelChild> processModelList; private List<ProcessModelChild> processModelList;
@Expose
private long[] processPids; private long[] processPids;
@Expose
private List<String> processNameList; private List<String> processNameList;
@Expose
private int size; private int size;
public static void main(String args[]) throws SigarException, public static void main(String args[]) throws SigarException,
@ -47,8 +54,7 @@ public class ProcessModel {
.getResidentKBytes()); .getResidentKBytes());
System.out.println("virtural Kbytes:" System.out.println("virtural Kbytes:"
+ processModel.getProcessModelList().get(i).getVSize()); + processModel.getProcessModelList().get(i).getVSize());
System.out System.out.println(" Kbytes:"
.println(" Kbytes:"
+ processModel.getProcessModelList().get(i) + processModel.getProcessModelList().get(i)
.getMemSize()); .getMemSize());
break; break;
@ -61,6 +67,29 @@ public class ProcessModel {
} }
private MonitorMain monitorMain;
private Logger logger = Logger.getLogger(ProcessModel.class);
public ProcessModel(MonitorMain monitorMain) {
this.monitorMain = monitorMain;
}
public void run() {
try {
monitorMain.setProcesssModel(new ProcessModel());
} catch (SigarException e) {
// TODO Auto-generated catch block
logger.info(e, e.fillInStackTrace());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
logger.info(e, e.fillInStackTrace());
} catch (ExecutionException e) {
// TODO Auto-generated catch block
logger.info(e, e.fillInStackTrace());
}
}
public ProcessModel() throws SigarException, InterruptedException, public ProcessModel() throws SigarException, InterruptedException,
ExecutionException { ExecutionException {
this.setProcessPids(); this.setProcessPids();
@ -79,7 +108,7 @@ public class ProcessModel {
} }
@XmlElementWrapper() @XmlElementWrapper()
@XmlElement( type = ProcessModelChild.class) @XmlElement(type = ProcessModelChild.class)
public List<ProcessModelChild> getProcessModelList() { public List<ProcessModelChild> getProcessModelList() {
return processModelList; return processModelList;
} }
@ -102,7 +131,8 @@ public class ProcessModel {
} }
} }
@XmlElement
@XmlElement
public long[] getProcessPids() { public long[] getProcessPids() {
return processPids; return processPids;
} }
@ -120,8 +150,11 @@ public class ProcessModel {
private void setProcessNameList() throws SigarException { private void setProcessNameList() throws SigarException {
this.processNameList = new ArrayList<String>(); this.processNameList = new ArrayList<String>();
for (long pid : this.getProcessPids()) { for (long pid : this.getProcessPids()) {
try {
ProcState procState = sigar.getProcState(pid); ProcState procState = sigar.getProcState(pid);
processNameList.add(procState.getName()); processNameList.add(procState.getName());
} catch (SigarException e) {
}
} }
} }

View File

@ -9,13 +9,21 @@ import org.hyperic.sigar.ProcMem;
import org.hyperic.sigar.ProcState; import org.hyperic.sigar.ProcState;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement @XmlRootElement
public class ProcessModelChild { public class ProcessModelChild {
@Expose
private String instanceString; private String instanceString;
@Expose
private long residentKBytes; private long residentKBytes;
@Expose
private double processorTimePercent; private double processorTimePercent;
@Expose
private long memSize; private long memSize;
@Expose
private long processId; private long processId;
@Expose
private long vSize; private long vSize;
private ProcessSigarReleatedModel processSigarReleatedModel; private ProcessSigarReleatedModel processSigarReleatedModel;

View File

@ -19,18 +19,27 @@ import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement @XmlRootElement
public class ProcessorModel { public class ProcessorModel implements Runnable {
private List<ProcessorModelChild> processorModelList; private List<ProcessorModelChild> processorModelList;
private Sigar sigar = GetSigar.getSigar(); private Sigar sigar = GetSigar.getSigar();
private CpuPerc cpuPerc; private CpuPerc cpuPerc;
@Expose
private double processorTimePercent; private double processorTimePercent;
@Expose
private double privilegedTimePercent; private double privilegedTimePercent;
@Expose
private double userTimePercent; private double userTimePercent;
@Expose
private double speed; private double speed;
@Expose
private List<String> cpuInstanceList; private List<String> cpuInstanceList;
@Expose
private int size; private int size;
private Logger logger = Logger.getLogger(ProcessModelChild.class); private MonitorMain monitorMain;
private Logger logger = Logger.getLogger(ProcessorModel.class);
public static void main(String args[]) { public static void main(String args[]) {
try { try {
@ -52,6 +61,20 @@ public class ProcessorModel {
} }
} }
public ProcessorModel(MonitorMain monitorMain) {
this.monitorMain = monitorMain;
}
public void run() {
try {
monitorMain.setProcessorModel(new ProcessorModel());
} catch (SigarException e) {
// TODO Auto-generated catch block
logger.info(e, e.fillInStackTrace());
}
}
public ProcessorModel() throws SigarException { public ProcessorModel() throws SigarException {
cpuPerc = sigar.getCpuPerc(); cpuPerc = sigar.getCpuPerc();
@ -154,10 +177,10 @@ public class ProcessorModel {
try { try {
processorModelList.add(future.get()); processorModelList.add(future.get());
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.info(e, e.fillInStackTrace()); e.printStackTrace();
} catch (ExecutionException e) { } catch (ExecutionException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
logger.info(e, e.fillInStackTrace()); e.printStackTrace();
} }
} }

View File

@ -7,11 +7,17 @@ import org.bench4q.monitor.service.GetSigar;
import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement @XmlRootElement
public class ProcessorModelChild { public class ProcessorModelChild {
@Expose
private String instance; private String instance;
@Expose
private double processorTimePercent; private double processorTimePercent;
@Expose
private double userTimePercent; private double userTimePercent;
@Expose
private double privilegedTimePercent; private double privilegedTimePercent;
private CpuPerc cpuPerc; private CpuPerc cpuPerc;
public static void main(String[] agrs) throws SigarException { public static void main(String[] agrs) throws SigarException {

View File

@ -1,12 +1,15 @@
package org.bench4q.monitor.service; package org.bench4q.monitor.service;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
public class GetSigar{
private static Sigar sigar = new Sigar(); public class GetSigar {
public static Sigar sigar;
public static Sigar getSigar() { public static Sigar getSigar() {
sigar = new Sigar();
return sigar; return sigar;
} }
} }

View File

@ -1,6 +1,7 @@
package org.bench4q.monitor.service; package org.bench4q.monitor.service;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -22,6 +23,9 @@ public class TimerService extends TimerTask {
} catch (ExecutionException e) { } catch (ExecutionException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} catch (SigarException e) { } catch (SigarException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block

View File

@ -4,46 +4,45 @@ import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.*; import java.io.*;
import org.apache.log4j.Logger;
import org.bench4q.monitor.model.*; import org.bench4q.monitor.model.*;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class WriteSystemInfoToLocalDisk { public class WriteSystemInfoToLocalDisk {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); private SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss");
private String savePath; private String savePath;
private Logger logger = Logger.getLogger(WriteSystemInfoToLocalDisk.class);
//test code public static void main(String[] args) throws SigarException,
public static void main(String[] args) throws SigarException, InterruptedException, ExecutionException{ InterruptedException, ExecutionException, IOException {
WriteSystemInfoToLocalDisk testWrite = new WriteSystemInfoToLocalDisk(); WriteSystemInfoToLocalDisk testWrite = new WriteSystemInfoToLocalDisk();
testWrite.setSavaPath("D:/sigartmp/"); testWrite.setSavaPath("D:/sigartmp/");
testWrite.writeCurrentSystemInfoToLocalDisk(); testWrite.writeCurrentSystemInfoToLocalDisk();
} }
public void setSavaPath(String savePath){ public void setSavaPath(String savePath) {
this.savePath = savePath; this.savePath = savePath;
} }
public void writeCurrentSystemInfoToLocalDisk() throws SigarException, InterruptedException, ExecutionException{
public void writeCurrentSystemInfoToLocalDisk() throws SigarException,
InterruptedException, ExecutionException, IOException {
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
Date date = new Date(); Date date = new Date();
MonitorMain mainModel = new MonitorMain(date); MonitorMain mainModel = new MonitorMain(date);
FileWriter writer = null; FileWriter writer = null;
try { try {
writer = new FileWriter(savePath + dateFormat.format(date)
JAXBContext context = JAXBContext.newInstance(MonitorMain.class); + ".json");
Marshaller marshal = context.createMarshaller(); Gson gson = new GsonBuilder()
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); .excludeFieldsWithoutExposeAnnotation().create();
marshal.marshal(mainModel, System.out); gson.toJson(mainModel, writer);
writer = new FileWriter(savePath + dateFormat.format(date) + ".xml");
marshal.marshal(mainModel, writer);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error(e, e.fillInStackTrace());
} finally {
writer.close();
} }
} }
} }