- 浏览: 457138 次
- 性别:
- 来自: 潘多拉
文章分类
最新评论
-
lijunwyf:
代码好像不完整,后端没串起来的感觉
Json-RPC for java首次尝试 -
jerry.chen:
我用这种方式去,貌似没啥效果,楼主!
Xfire开发的webservice客户端超时问题解决 -
漫步邃思:
这个问题常遇到,老是想不起来解决方法,记住了
resin3.1.0无法解析EL表达式问题解决 -
dampce032:
在不删掉integratedSecurity=true;的情况 ...
JDBC 连接SQLServer数据库(Failed to load the sqljdbc_auth.dll) -
kill_e680:
取CPU号和取硬盘号,在linux下可以用吗?
sigar使用:在web中应用sigar取得系统信息
[转]
需要的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
=============================================
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hyperic.sigar.Cpu; import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.Mem; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Swap; public class SigarServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Sigar sigar = new Sigar(); CpuPerc perc = sigar.getCpuPerc(); Mem mem = sigar.getMem(); Swap swap = sigar.getSwap(); int cpuUsage = (int)perc.getCombined(); int memFree = (int)(mem.getFree()/(1024l*1024l)); int memTotal = (int)(mem.getTotal()/(1024l*1024l)); int pageFileFree = (int)(swap.getPageOut()/(1024l*1024l)); int pageFileTotal = (int)(swap.getPageIn()/(1024l*1024l)); int swapFree = (int)(swap.getFree()/(1024l*1024l)); int swapTotal = (int)(swap.getTotal()/(1024l*1024l)); response.setContentType("text/html;charset=GB2312"); PrintWriter out = response.getWriter(); Cpu cpu = sigar.getCpu(); out.println("CPU Usage:"+perc.getCombined()); out.println("<br/>"); out.println("Memory Free:"+memFree); out.println("<br/>"); out.println("Memory Total:"+memTotal); out.println("<br/>"); out.println("Swap Free:"+swapFree); out.println("<br/>"); out.println("Swap Total:"+swapTotal); out.println("<br/>"); out.println("Page Free:"+pageFileFree); out.println("<br/>"); out.println("Page Total:"+pageFileTotal); out.close(); } catch (Exception e) { } } }
<?xml version="1.0" encoding="UTF-8"?> <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"> <display-name>sigar</display-name> <servlet> <servlet-name>sigarServlet</servlet-name> <servlet-class>sigar.SigarServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>sigarServlet</servlet-name> <url-pattern>/sigar</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </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
=============================================
package cn.com.mypm.common.util; import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarLoader; import org.hyperic.sigar.cmd.Shell; import org.hyperic.sigar.cmd.SigarCommandBase; public class CpuInfo extends SigarCommandBase { public boolean displayTimes = true; public CpuInfo(Shell shell) { super(shell); } public CpuInfo() { super(); } public String getUsageShort() { return "Display cpu information"; } private void output(CpuPerc cpu) { println("User Time....." + CpuPerc.format(cpu.getUser())); println("Sys Time......" + CpuPerc.format(cpu.getSys())); println("Idle Time....." + CpuPerc.format(cpu.getIdle())); println("Wait Time....." + CpuPerc.format(cpu.getWait())); println("Nice Time....." + CpuPerc.format(cpu.getNice())); println("Combined......" + CpuPerc.format(cpu.getCombined())); println("Irq Time......" + CpuPerc.format(cpu.getIrq())); if (SigarLoader.IS_LINUX) { println("SoftIrq Time.." + CpuPerc.format(cpu.getSoftIrq())); println("Stolen Time...." + CpuPerc.format(cpu.getStolen())); } println(""); } public void output(String[] args) throws SigarException { org.hyperic.sigar.CpuInfo[] infos = this.sigar.getCpuInfoList(); CpuPerc[] cpus = this.sigar.getCpuPercList(); org.hyperic.sigar.CpuInfo info = infos[0]; long cacheSize = info.getCacheSize(); println("Vendor........." + info.getVendor()); println("Model.........." + info.getModel()); println("Mhz............" + info.getMhz()); println("Total CPUs....." + info.getTotalCores()); if ((info.getTotalCores() != info.getTotalSockets()) || (info.getCoresPerSocket() > info.getTotalCores())) { println("Physical CPUs.." + info.getTotalSockets()); println("Cores per CPU.." + info.getCoresPerSocket()); } if (cacheSize != Sigar.FIELD_NOTIMPL) { println("Cache size...." + cacheSize); } println(""); if (!this.displayTimes) { return; } for (int i = 0; i < cpus.length; i++) { println("CPU " + i + "........."); output(cpus[i]); } println("Totals........"); output(this.sigar.getCpuPerc()); StringBuffer sb=new StringBuffer("cpu号="+getCPUSerial()+"\n"); String[] interfaces = sigar.getNetInterfaceList(); if(interfaces!=null || interfaces.length>0) sb.append("第一个网卡号="+sigar.getNetInterfaceConfig(interfaces[0]).getHwaddr()); org.hyperic.sigar.FileSystem[] filesystems = sigar.getFileSystemList(); if(filesystems!=null || filesystems.length>0) sb.append("\n"+"硬盘第一个分区的卷标="+getHDSerial(filesystems[0].getDevName())); System.out.println(sb.toString()); } public static void main(String[] args) throws Exception { //先加载siga动太库 在不同的平台只要加载特定的动态库,这里我就全加载不区分了 //在IDE环境中,可以不加载动态库 设置natinve lib patch location 既可 File nativeDir = new File("E:\\mypm10_new\\mypmdoc\\WebRoot\\WEB-INF\\native"); File[] libs = nativeDir.listFiles(); for (int i = 0; i < libs.length; i++) { if (libs[i].isFile()) try { System.load(new File(nativeDir, libs[i].getName()).getPath()); } catch (Throwable t) { } } new CpuInfo().processCommand(args); } /** * * 返回CPU的闲置率 * */ public static double getCpuIdle() { Sigar sigar = null; try { sigar = new Sigar(); return sigar.getCpuPerc().getIdle(); } catch (Throwable t) { t.printStackTrace(); } finally { if (sigar != null) sigar.close(); } return 0; } /** * * @param drive 硬盘驱动器分区 如C,D * @return 该分区的卷标 */ public static String getHDSerial(String drive) { String result = ""; try { File file = File.createTempFile("tmp", ".vbs"); file.deleteOnExit(); FileWriter fw = new java.io.FileWriter(file); String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n" + "Set colDrives = objFSO.Drives\n" + "Set objDrive = colDrives.item(\"" + drive + "\")\n" + "Wscript.Echo objDrive.SerialNumber"; fw.write(vbs); fw.close(); Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath()); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = input.readLine()) != null) { result += line; } input.close(); file.delete(); } catch (Exception e) { } if (result.trim().length() < 1 || result == null) { result = "无磁盘ID被读取"; } return result.trim(); } /** * 获取CPU号,多CPU时,只取第一个 * @return */ public static String getCPUSerial() { String result = ""; try { File file = File.createTempFile("tmp", ".vbs"); file.deleteOnExit(); FileWriter fw = new java.io.FileWriter(file); String vbs = "On Error Resume Next \r\n\r\n" + "strComputer = \".\" \r\n" + "Set objWMIService = GetObject(\"winmgmts:\" _ \r\n" + " & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n" + "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\") \r\n " + "For Each objItem in colItems\r\n " + " Wscript.Echo objItem.ProcessorId \r\n " + " exit for ' do the first cpu only! \r\n" + "Next "; fw.write(vbs); fw.close(); Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath()); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = input.readLine()) != null) { result += line; } input.close(); file.delete(); } catch (Exception e) { e.fillInStackTrace(); } if (result.trim().length() < 1 || result == null) { result = "无CPU_ID被读取"; } return result.trim(); } }
- hyperic-sigar-1.6.3.zip (3.5 MB)
- 下载次数: 33
发表评论
-
eclipse
2012-01-19 10:19 939eclipse操作常识:eclipse进行项目开发时往往会遇 ... -
解决struts1.x表单重复提交问题
2012-01-17 10:40 2336每次填写完表单后单击提交后,struts中action执行相关 ... -
在树初始化时,加一个load图片
2012-01-16 09:45 1817做个浮动图层放在 zTree 上面, 然后利用 asnycS ... -
利用JS做到隐藏div和显示div
2012-01-16 09:05 108718div的visibility可以控制div的显示和隐藏,但是隐 ... -
虚拟机使用NAT不能上网
2012-01-05 08:41 2975虚拟机上网问题 许多虚拟机的初学者,都会询问这样一个问题:在 ... -
[转]用eclipse pdt 配置php开发,调试环境
2011-12-30 16:15 1475本文档是windows操作系统下php开发环境的配置。 1 ... -
RESIN 连接池数据库密码加密方法
2011-12-28 15:34 1594[转] Resin 连接池写在resin.conf里 ... -
好的技术博客
2011-12-28 15:18 842http://www.findjar.com/index.x ... -
利用jawin完成调用window中dll的调用
2011-12-28 15:14 2079[转] Java/Win32互动项目( Jawin)是 ... -
WebService的测试
2011-12-20 09:26 1449在浏览器中输入地址:http://localhost ... -
我的SQLSERVER 分页存储过程
2011-12-13 08:54 3158/** * procedure name : kk_f ... -
Myeclipse中将Web项目发布时重命名
2011-12-12 09:58 1132选中项目->反键->Properties ... -
Sql xtype 类型
2011-11-30 09:39 1218sysobjects 表 在数据库内创建的每个对象(约束、默认 ... -
使用SQL语句清空数据库所有表的数据[转]
2011-11-30 09:14 768近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的 ... -
js实现焦点进入文本框内关闭输入法
2011-11-30 08:56 3536要用到的东西: imeMode:xxx 有四个参数 act ... -
数据库设计三大范式应用实例剖析[转]
2011-11-29 17:25 767数据库的设计范式是数据库设计所需要满足的规范,满足这些规范的数 ... -
Ajax.Request方法
2011-11-29 13:47 1251Prototype 的 Ajax.Request 对象绑定了浏 ... -
临时表(Template Table)[转]
2011-11-25 17:41 15611、MS SQLSERVER SQL ... -
测试sql语句性能
2011-11-25 17:22 1014[size=medium]有时候我们经常为我们的sql语句执行 ... -
quartz cronExpression配置说明
2011-11-18 15:26 1274字段 允许值 ...
相关推荐
sigar 取cpu,进程,内存,硬盘信息的实例代码
sigar获取系统CPU,内存,磁盘利用率的基本信息 里面附带一个完整的Demo供参考和适用!
首先给大家介绍一个开源工具Sigar 官网:...Sigar(System Information Gatherer And Reporter),是一个开源的工具,提供了跨平台的系统信息收集的API,由C语言实现的。可以收集的信息包括:
程序用到的jar 博文链接:https://wangrui.iteye.com/blog/153002
Sigar(System Information Gatherer And Reporter),是一个开源的工具,提供了跨平台的系统信息收集的API。 Sigar API 提供一个方便的接口来收集系统信息,如: ◆系统内存,页面交换,cpu,平均负载,运行时间,...
开源工具包 SIGAR System Information Gatherer And Reporter 即 系统信息收集和报告 官方站点: http: support hyperic com display SIGAR Home;jsessionid A9705887A07D20BBAC9A96500BC60822 SIGAR 官方站点 写道...
windows和linux系统使用sigar所需各个版本的扩展文件集合及版本对应文件说明,用于获取系统信息。
JAVA 获取服务器CPU、磁盘、用户连接、内存、操作系统、网络等数据信息 支持window、linux 操作系统,包含完整的依赖库文件 依赖文件:sigar.jar、sigar-amd64-winnt.dll、sigar-x86-winnt.dll、sigar-x86-winnt.lib
使用sigar获取系统信息,例如内存,cpu,磁盘大小,jvm虚拟机信息,内置jar包已及工具类,工具类写了注释,很详细。
Sigar(System Information Gatherer And Reporter),是一个开源的工具,提供了跨平台的系统信息收集的API,核心由C语言实现的。 Sigar资料地址:https://support.hyperic.com/display/SIGAR/Home Sigar提供了一...
系统信息监控sigar, jar包及libsigar-amd64-linux.so
sigar_mirror(系统信息收集API_Sigar)
利用Sigar获取系统信息.pdf
使用Sigar实现系统服务器端的监控,里面包括实现的代码以及用到的sigar所有的包和库
基于java的开发源码-sigar_mirror(系统信息收集API_Sigar).zip 基于java的开发源码-sigar_mirror(系统信息收集API_Sigar).zip 基于java的开发源码-sigar_mirror(系统信息收集API_Sigar).zip 基于java的开发源码-...
包括:libsigar-aarch64-linux.so、libsigar-amd64-linux.so、libsigar-x86-linux.so、sigar-amd64-winnt.dll、sigar-x86-winnt.dll、sigar-x86-winnt.lib
本工程用于演示借助Sigar API获取磁盘信息 本工程编码方式:UTF-8 本工程开发工具:MyEclipse
java源码:sigar_mirror(系统信息收集API_Sigar).rar