java web 监控cpu、内存等。hyperic-sigar

用到一个插件hyperic-sigar

1:下载hyperic-sigar后解压,把sigar-amd64-winnt.dll(64位机器,32位用sigar-x86-winnt.dll)放到你本机的jdk\bin、jdk\jre\bin、jre\bin目录下。

2:maven引入hyperic-sigar依赖,这样就可以用了。

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目录下这里面有很多官方提供的例子,可以直接复制到你的工程下运行测试。


我用的是websocket+java-timer做的。

 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:

 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

效果:

 

这里面用了websocket,没提出来具体的代码。

下次我写一个基于netty、websocket.io、springmvc、mybatis、h2database、httpclient、fastjson、https的整合,我现在就在写这样一个项目,等写好了做一个简化版的发上来。

posted @ 2017-03-20 09:02  江湖危险快点跑  阅读(798)  评论(0编辑  收藏  举报