首页 > Java开发 > Sigar获取系统信息

Sigar获取系统信息

直接看代码:

[java][/java]

view plaincopy

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.hyperic.sigar.Cpu;
  8. import org.hyperic.sigar.CpuPerc;
  9. import org.hyperic.sigar.Mem;
  10. import org.hyperic.sigar.Sigar;
  11. import org.hyperic.sigar.Swap;
  12. public class SigarServlet extends HttpServlet {
  13.     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14.         try {
  15.             Sigar sigar = new Sigar();
  16.             CpuPerc perc = sigar.getCpuPerc();
  17.             Mem mem = sigar.getMem();
  18.             Swap swap = sigar.getSwap();
  19.             int cpuUsage = (int)perc.getCombined();
  20.             int memFree = (int)(mem.getFree()/(1024l*1024l));
  21.             int memTotal = (int)(mem.getTotal()/(1024l*1024l));
  22.             int pageFileFree = (int)(swap.getPageOut()/(1024l*1024l));
  23.             int pageFileTotal = (int)(swap.getPageIn()/(1024l*1024l));
  24.             int swapFree = (int)(swap.getFree()/(1024l*1024l));
  25.             int swapTotal = (int)(swap.getTotal()/(1024l*1024l));
  26.             response.setContentType("text/html;charset=GB2312");
  27.             PrintWriter out = response.getWriter();
  28.             Cpu cpu = sigar.getCpu();
  29.             out.println("CPU Usage:"+perc.getCombined());
  30.             out.println("<br/>");
  31.             out.println("Memory Free:"+memFree);
  32.             out.println("<br/>");
  33.             out.println("Memory Total:"+memTotal);
  34.             out.println("<br/>");
  35.             out.println("Swap Free:"+swapFree);
  36.             out.println("<br/>");
  37.             out.println("Swap Total:"+swapTotal);
  38.             out.println("<br/>");
  39.             out.println("Page Free:"+pageFileFree);
  40.             out.println("<br/>");
  41.             out.println("Page Total:"+pageFileTotal);
  42.             out.close();
  43.         } catch (Exception e) {
  44.         }
  45.     }
  46. }

配置:

[html][/html]

view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  3.     <display-name>sigar</display-name>
  4.     <servlet>
  5.        <servlet-name>sigarServlet</servlet-name>
  6.        <servlet-class>sigar.SigarServlet</servlet-class>
  7.     </servlet>
  8.     <servlet-mapping>
  9.         <servlet-name>sigarServlet</servlet-name>
  10.         <url-pattern>/sigar</url-pattern>
  11.     </servlet-mapping>
  12.     <welcome-file-list>
  13.         <welcome-file>index.html</welcome-file>
  14.         <welcome-file>index.htm</welcome-file>
  15.         <welcome-file>index.jsp</welcome-file>
  16.         <welcome-file>default.html</welcome-file>
  17.         <welcome-file>default.htm</welcome-file>
  18.         <welcome-file>default.jsp</welcome-file>
  19.     </welcome-file-list>
  20. </web-app>

需要的jar:
.sigar_shellrc
pdh.dll
sigar-1.5.0.1.jar
sigar-x86-winnt.dll
sigar-x86-winnt.lib

测试结果:

访问http://127.0.0.1:8080/sigar/sigar

CPU Usage:0.22815533980582525
Memory Free:863
Memory Total:2046
Swap Free:2048
Swap Total:4330
Page Free:0
Page Total:0

测试系统:windows

下载地址:

本站下站sigar lib包

最新的Sigar官方包hyperic-sigar-1.6.4


本文固定链接: http://www.devba.com/index.php/archives/2771.html | 开发吧

报歉!评论已关闭.