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>
</organization>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -52,6 +57,7 @@
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>

View File

@ -1,10 +1,6 @@
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.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
@ -76,11 +72,7 @@ public class MonitorServer {
public static void main(String[] args) throws InterruptedException {
MonitorServer monitorServer = new MonitorServer(5556);
MonitorServer monitorServer = new MonitorServer(5557);
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.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "fileSystem")
public class FileSystemModel {
@Expose
private String fileDir;
@Expose
private Double diskReadKBytesRate;
@Expose
private Double diskWriteKBytesRate;
@Expose
private double curDiskQueLength;
@Expose
private double totalGB;
@Expose
private double usedGB;
@Expose
private double freeGB;
@Expose
private double usedPercent;
@Expose
private double diskTotalKBytesRate;
private FileSystemUsage fileSystemUsage;
public static void main(String args[]) throws SigarException,
@ -193,6 +205,7 @@ class CalculateDiskWriteRate extends CalculateDiskRate {
long getFileKBytes() throws SigarException {
return fileSystemUsage.getDiskWriteBytes() / 1024L;
}
}
class CalculateDiskReadRate extends CalculateDiskRate {
@ -206,4 +219,5 @@ class CalculateDiskReadRate extends CalculateDiskRate {
return fileSystemUsage.getDiskReadBytes() / 1024L;
}
}

View File

@ -1,42 +1,50 @@
package org.bench4q.monitor.model;
import java.beans.Transient;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;
import org.bench4q.monitor.service.GetSigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.Mem;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "Memory")
public class MemoryModel {
@Expose
private long pagesPerSecond;
@Expose
private long pagesInputPerSecond;
@Expose
private long pagesOutputPerSecond;
@Expose
private long availableKiloBytes;
@Expose
private long totalKiloBytes;
@Expose
private double memoryUsedPercent;
@Expose
private double swapKiloBytes;
@Expose
private double swapFreeKiloBytes;
private Sigar sigar = GetSigar.getSigar();
private Swap swap;
private Mem mem;
private Logger logger = Logger.getLogger(MemoryModel.class);
// test code
public static void main(String[] args) throws SigarException {
MemoryModel model = new MemoryModel();
System.out.println(model.logger.getLoggerRepository());
System.out.println("PagesRate: " + model.getPagesPerSecond());
System.out.println("PagesIn: " + model.getPagesInputPerSecond());
System.out.println("PagesIn: " + model.getPagesOutputPerSecond());
System.out.println("UsedPerc: " + model.getMemoryUsedPercent()+"%");
System.out.println("Total: " + model.getTotalKiloBytes()+"kb");
System.out.println("Aval: " + model.getAvailableKiloBytes()+"kb");
System.out.println("swap total:"+model.getSwapKiloBytes()+"kb");
System.out.println("swap free :"+model.getSwapFreeKiloBytes()+"kb");
System.out.println("UsedPerc: " + model.getMemoryUsedPercent() + "%");
System.out.println("Total: " + model.getTotalKiloBytes() + "kb");
System.out.println("Aval: " + model.getAvailableKiloBytes() + "kb");
System.out.println("swap total:" + model.getSwapKiloBytes() + "kb");
System.out.println("swap free :" + model.getSwapFreeKiloBytes() + "kb");
}
public MemoryModel() throws SigarException {
@ -62,7 +70,7 @@ public class MemoryModel {
@XmlElement
public long getPagesInputPerSecond() throws SigarException {
swap = sigar.getSwap();
swap = sigar.getSwap();
setPagesInputPerSecond(swap.getPageIn());
return pagesInputPerSecond;
}
@ -73,7 +81,7 @@ public class MemoryModel {
@XmlElement
public long getPagesOutputPerSecond() throws SigarException {
swap = sigar.getSwap();
swap = sigar.getSwap();
setPagesOutputPerSecond(swap.getPageOut());
return pagesOutputPerSecond;
}
@ -84,8 +92,8 @@ public class MemoryModel {
@XmlElement
public long getAvailableKiloBytes() throws SigarException {
mem = sigar.getMem();
setAvailableKiloBytes(mem.getActualFree()/ 1024L);
mem = sigar.getMem();
setAvailableKiloBytes(mem.getActualFree() / 1024L);
return availableKiloBytes;
}
@ -95,7 +103,7 @@ public class MemoryModel {
@XmlElement
public long getTotalKiloBytes() throws SigarException {
mem = sigar.getMem();
mem = sigar.getMem();
setTotalKiloBytes(mem.getTotal() / 1024L);
return totalKiloBytes;
}
@ -106,7 +114,7 @@ public class MemoryModel {
@XmlElement
public double getMemoryUsedPercent() throws SigarException {
mem = sigar.getMem();
mem = sigar.getMem();
long temp = Math.round(mem.getUsedPercent() * 100);
setMemoryUsedPercent(temp / 100.0);
return this.memoryUsedPercent;
@ -115,21 +123,50 @@ public class MemoryModel {
private void setMemoryUsedPercent(double memoryUsedPercent) {
this.memoryUsedPercent = memoryUsedPercent;
}
@XmlElement
@XmlElement
public double getSwapKiloBytes() {
return swapKiloBytes;
}
private void setSwapKiloBytes() {
this.swapKiloBytes = this.swap.getTotal()/1024L;
this.swapKiloBytes = this.swap.getTotal() / 1024L;
}
@XmlElement
@XmlElement
public double getSwapFreeKiloBytes() {
return swapFreeKiloBytes;
}
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;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.ExecutionException;
import java.text.SimpleDateFormat;
@ -11,37 +13,70 @@ import javax.xml.bind.annotation.XmlType;
import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "history")
@XmlType
public class MonitorMain {
/* @XmlElement
private String date;*/
@Expose
@XmlElement
private String date;
@Expose
@XmlElement(name = "processor_info")
private ProcessorModel processorModel;
@Expose
@XmlElement(name = "memory_info")
private MemoryModel memoryModel;
@Expose
@XmlElement(name = "disk_info")
private PhysicalDiskModel physicalDiskModel;
@Expose
@XmlElement(name = "network_info")
private NetworkInterfaceModel networkInterfaceModel;
@Expose
@XmlElement(name = "process_info")
private ProcessModel processModel;
private SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss");
public MonitorMain() {
}
public MonitorMain(Date date) throws SigarException, InterruptedException,
ExecutionException {
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
/*this.date = dateFormat.format(date);*/
processorModel = new ProcessorModel();
this.date = dateFormat.format(date);
long startTime = System.currentTimeMillis();
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();
physicalDiskModel = new PhysicalDiskModel();
networkInterfaceModel = new NetworkInterfaceModel();
processModel = new ProcessModel();
boolean threadIsAlive = true;
while (threadIsAlive) {
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.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "NetworkInterface")
public class NetworkInterfaceModel {
public class NetworkInterfaceModel implements Runnable {
@Expose
private Double kiloBytesTotalPerSecond;
@Expose
private Double kiloBytesReceivedPerSecond;
@Expose
private Double kiloBytesSentPerSecond;
private long interval = 500;
private Logger logger = Logger.getLogger(NetworkInterfaceModel.class);
/* private Logger logger = Logger.getLogger(NetworkInterfaceModel.class); */
public static void main(String[] args) {
while (true) {
long Time = System.currentTimeMillis();
NetworkInterfaceModel test = new NetworkInterfaceModel();
test.logger.info("ll");
System.out.println("KiloBytesReceivedPerSecond:"
+ test.getKiloBytesReceivedPerSecond() + "kb/s");
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")
public NetworkInterfaceModel() {
logger.error("hello");
// this can be used for all
ExecutorService executorService = Executors.newFixedThreadPool(2);
@ -50,7 +64,6 @@ public class NetworkInterfaceModel {
Future<Double> futureBytesSentPerSecond = executorService
.submit(new CalculateBytesSentPerSecond(interval));
try {
System.out.println(futureBytesReceivedPerSecond.get());
this.setKiloBytesReceivedPerSecond(futureBytesReceivedPerSecond
.get());
@ -121,7 +134,7 @@ abstract class CalculateBytesPerSecond implements Callable {
postBytesSentSoFar = this.getBytesSoFar();
this.kiloBytesPerSecond = (double) ((postBytesSentSoFar - preBytesSentSoFar)
/ 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) {
logger.error(e, e.fillInStackTrace());
return new Double(0);

View File

@ -12,6 +12,7 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.apache.log4j.Logger;
import org.bench4q.monitor.service.GetSigar;
import org.bench4q.monitor.service.GetThreadPool;
import org.hyperic.sigar.Sigar;
@ -19,21 +20,31 @@ import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement
public class PhysicalDiskModel {
public class PhysicalDiskModel implements Runnable{
@Expose
private Double diskReadKBytesRate;
@Expose
private Double diskWriteKBytesRate;
@Expose
private double curDiskQueLength;
@Expose
private double totalGB;
@Expose
private double usedGB;
@Expose
private double freeGB;
@Expose
private double usedPercent;
@Expose
private List<FileSystemModel> fieFileSystemModels;
private Sigar sigar = GetSigar.getSigar();
private FileSystem[] fileSystemList;
private final int interval = 500;
// private Logger logger = Logger.getLogger(PhysicalDiskModel.class);
private MonitorMain monitorMain;
private Logger logger = Logger.getLogger(PhysicalDiskModel.class);
public static void main(String[] args) throws SigarException,
InterruptedException, ExecutionException {
@ -50,7 +61,25 @@ public class PhysicalDiskModel {
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,
ExecutionException {

View File

@ -10,18 +10,25 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;
import org.bench4q.monitor.service.GetSigar;
import org.bench4q.monitor.service.GetThreadPool;
import org.hyperic.sigar.ProcState;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement
public class ProcessModel {
public class ProcessModel implements Runnable {
private Sigar sigar = GetSigar.getSigar();
@Expose
private List<ProcessModelChild> processModelList;
@Expose
private long[] processPids;
@Expose
private List<String> processNameList;
@Expose
private int size;
public static void main(String args[]) throws SigarException,
@ -47,10 +54,9 @@ public class ProcessModel {
.getResidentKBytes());
System.out.println("virtural Kbytes:"
+ processModel.getProcessModelList().get(i).getVSize());
System.out
.println(" Kbytes:"
+ processModel.getProcessModelList().get(i)
.getMemSize());
System.out.println(" Kbytes:"
+ processModel.getProcessModelList().get(i)
.getMemSize());
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,
ExecutionException {
this.setProcessPids();
@ -79,7 +108,7 @@ public class ProcessModel {
}
@XmlElementWrapper()
@XmlElement( type = ProcessModelChild.class)
@XmlElement(type = ProcessModelChild.class)
public List<ProcessModelChild> getProcessModelList() {
return processModelList;
}
@ -102,7 +131,8 @@ public class ProcessModel {
}
}
@XmlElement
@XmlElement
public long[] getProcessPids() {
return processPids;
}
@ -120,8 +150,11 @@ public class ProcessModel {
private void setProcessNameList() throws SigarException {
this.processNameList = new ArrayList<String>();
for (long pid : this.getProcessPids()) {
ProcState procState = sigar.getProcState(pid);
processNameList.add(procState.getName());
try {
ProcState procState = sigar.getProcState(pid);
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.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement
public class ProcessModelChild {
@Expose
private String instanceString;
@Expose
private long residentKBytes;
@Expose
private double processorTimePercent;
@Expose
private long memSize;
@Expose
private long processId;
@Expose
private long vSize;
private ProcessSigarReleatedModel processSigarReleatedModel;

View File

@ -19,18 +19,27 @@ import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement
public class ProcessorModel {
public class ProcessorModel implements Runnable {
private List<ProcessorModelChild> processorModelList;
private Sigar sigar = GetSigar.getSigar();
private CpuPerc cpuPerc;
@Expose
private double processorTimePercent;
@Expose
private double privilegedTimePercent;
@Expose
private double userTimePercent;
@Expose
private double speed;
@Expose
private List<String> cpuInstanceList;
@Expose
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[]) {
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 {
cpuPerc = sigar.getCpuPerc();
@ -154,10 +177,10 @@ public class ProcessorModel {
try {
processorModelList.add(future.get());
} catch (InterruptedException e) {
logger.info(e, e.fillInStackTrace());
e.printStackTrace();
} catch (ExecutionException e) {
// 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.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement
public class ProcessorModelChild {
@Expose
private String instance;
@Expose
private double processorTimePercent;
@Expose
private double userTimePercent;
@Expose
private double privilegedTimePercent;
private CpuPerc cpuPerc;
public static void main(String[] agrs) throws SigarException {

View File

@ -1,12 +1,15 @@
package org.bench4q.monitor.service;
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() {
sigar = new Sigar();
return sigar;
}
}

View File

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

View File

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