java web 監控cpu、內存等。hyperic-sigar

用到一個插件hyperic-sigarhtml

1:下載hyperic-sigar後解壓,把sigar-amd64-winnt.dll(64位機器,32位用sigar-x86-winnt.dll)放到你本機的jdk\bin、jdk\jre\bin、jre\bin目錄下。java

2:maven引入hyperic-sigar依賴,這樣就能夠用了。web

1         <dependency>
2             <groupId>org.fusesource</groupId>
3             <artifactId>sigar</artifactId>
4             <version>1.6.4</version>
5         </dependency>

3:hyperic-sigar解壓後到hyperic-sigar-1.6.4\bindings\java\examples目錄下這裏面有不少官方提供的例子,能夠直接複製到你的工程下運行測試。spring


我用的是websocket+java-timer作的。json

 1 public class KeepRunTime extends TimerTask {
 2 
 3     private static Sigar sigar = new Sigar();  
 4     
 5     @Override
 6     public void run() {
 7         RunTime rt = new RunTime();
 8         rt.setMem(mem());
 9         rt.setCpus(cpu());
10         
11         DataPushService.pushData(JSONObject.toJSONString(new WebMessage(rt, WebMessage.RUNTIME)));
12     }
13     
14     // CPU使用率
15     public static TreeSet<Cpu> cpu() {
16         try {
17             TreeSet<Cpu> cpus = new TreeSet<Cpu>();
18             CpuPerc[] list = sigar.getCpuPercList();
19             for (int i=1; i<list.length+1; i++) {
20                 Cpu cpu = new Cpu();
21                 cpu.setNo(i);
22                 cpu.setCpuName("CPU-"+i);
23                 cpu.setCpuCombined(CpuPerc.format(list[i-1].getCombined()));
24                 cpus.add(cpu);
25             }
26             return cpus;
27         } catch (SigarException e) {
28             System.err.println("獲取CPU使用率異常");
29             e.printStackTrace();
30             return null;
31         }
32     }
33     
34     // MEM使用率
35     public static String mem() {
36         try {
37             Mem mem = sigar.getMem();
38             // 內存總量  
39             Long memTotal = mem.getTotal() / 1024L;
40             // 當前內存使用量 
41             Long memUsed = mem.getUsed() / 1024L;
42             // 使用率
43             Double usePercent = ((memUsed*1.0)/memTotal)*100;
44             // 四捨五入
45             return String.format("%.2f", usePercent);
46         } catch (SigarException e) {
47             System.err.println("獲取MEM使用率異常");
48             e.printStackTrace();
49             return null;
50         }
51     }
52 
53 }
View Code
 1 public class RunTime {
 2 
 3     // 內存使用率
 4     private String mem;
 5     // CPU使用率
 6     private TreeSet<Cpu> cpus;
 7     
 8     public String getMem() {
 9         return mem;
10     }
11     public void setMem(String mem) {
12         this.mem = mem;
13     }
14     public TreeSet<Cpu> getCpus() {
15         return cpus;
16     }
17     public void setCpus(TreeSet<Cpu> cpus) {
18         this.cpus = cpus;
19     }
20     
21     @Override
22     public String toString() {
23         return "RunTime [mem=" + mem + ", cpus=" + cpus + "]";
24     }
25     
26 }
View Code
 1 public class Cpu implements Comparable<Cpu>{
 2 
 3     private int no;
 4     
 5     private String cpuName;
 6     
 7     private String cpuCombined;
 8 
 9     public int getNo() {
10         return no;
11     }
12 
13     public void setNo(int no) {
14         this.no = no;
15     }
16 
17     public String getCpuName() {
18         return cpuName;
19     }
20 
21     public void setCpuName(String cpuName) {
22         this.cpuName = cpuName;
23     }
24 
25     public String getCpuCombined() {
26         return cpuCombined;
27     }
28 
29     public void setCpuCombined(String cpuCombined) {
30         this.cpuCombined = cpuCombined;
31     }
32 
33     // 正序
34     @Override
35     public int compareTo(Cpu o) {
36         return (this.no - o.no);
37     }
38 
39     @Override
40     public String toString() {
41         return "Cpu [no=" + no + ", cpuName=" + cpuName + ", cpuCombined="
42                 + cpuCombined + "]";
43     }
44     
45 }
View Code

 js:websocket

 1 var runtime = function(data) {
 2     $("#mem").html(data.obj.mem + '%');
 3 
 4     var cpus = data.obj.cpus;
 5     var html = "<p>";
 6     for (var i = 1; i < cpus.length + 1; i++) {
 7         html += "<b>" + cpus[i-1].cpuName + ":</b>" +
 8                 "<span class='runtime' id='span-cpu'>"+ cpus[i-1].cpuCombined + "</span>";
 9         if (i % 2 == 0) {
10             html += "</p><p>";
11         }
12     }
13     $("#cpu").html(html);
14 };
View Code

效果:mybatis

 

這裏面用了websocket,沒提出來具體的代碼。mvc

下次我寫一個基於netty、websocket.io、springmvc、mybatis、h2database、httpclient、fastjson、https的整合,我如今就在寫這樣一個項目,等寫好了作一個簡化版的發上來。socket

相關文章
相關標籤/搜索