sigar获取系统运行时资源状态

来源:互联网 发布:湖南省2016年经济数据 编辑:程序博客网 时间:2024/05/22 04:08

先看看效果

 

第1块CPU信息CPU的总量MHz:2594CPU生产商:IntelCPU类别:Core(TM) i5-3230M CPU @ 2.60GHzCPU缓存数量:-1CPU用户使用率:3.2%CPU系统使用率:3.2%CPU当前等待率:0.0%CPU当前错误率:0.0%CPU当前空闲率:93.6%CPU总的使用率:6.4%第2块CPU信息CPU的总量MHz:2594CPU生产商:IntelCPU类别:Core(TM) i5-3230M CPU @ 2.60GHzCPU缓存数量:-1CPU用户使用率:0.0%CPU系统使用率:6.2%CPU当前等待率:0.0%CPU当前错误率:0.0%CPU当前空闲率:93.7%CPU总的使用率:6.2%第3块CPU信息CPU的总量MHz:2594CPU生产商:IntelCPU类别:Core(TM) i5-3230M CPU @ 2.60GHzCPU缓存数量:-1CPU用户使用率:0.0%CPU系统使用率:3.2%CPU当前等待率:0.0%CPU当前错误率:0.0%CPU当前空闲率:96.8%CPU总的使用率:3.2%第4块CPU信息CPU的总量MHz:2594CPU生产商:IntelCPU类别:Core(TM) i5-3230M CPU @ 2.60GHzCPU缓存数量:-1CPU用户使用率:3.0%CPU系统使用率:12.4%CPU当前等待率:0.0%CPU当前错误率:0.0%CPU当前空闲率:84.5%CPU总的使用率:15.4%----------------------------------内存总量:16611692K av当前内存使用量:5449440K used当前内存剩余量:11162252K free交换区总量:33221520K av当前交换区使用量:6529460K used当前交换区剩余量:26692060K free----------------网络信息------------网络设备名:eth6IP地址:192.168.0.156子网掩码:255.255.255.0eth6接收的总包裹数:1787517eth6发送的总包裹数:395870eth6接收到的总字节数:586903449eth6发送的总字节数:65788052eth6接收到的错误包数:0eth6发送数据包时的错误数:0eth6接收时丢弃的包数:0eth6发送时丢弃的包数:0网络设备名:eth12IP地址:192.168.56.1子网掩码:255.255.255.0eth12接收的总包裹数:0eth12发送的总包裹数:3705eth12接收到的总字节数:0eth12发送的总字节数:547559eth12接收到的错误包数:0eth12发送数据包时的错误数:0eth12接收时丢弃的包数:0eth12发送时丢弃的包数:0网络设备名:eth17IP地址:10.11.0.157子网掩码:255.255.255.252eth17接收的总包裹数:22eth17发送的总包裹数:3156eth17接收到的总字节数:1448eth17发送的总字节数:472273eth17接收到的错误包数:0eth17发送数据包时的错误数:0eth17接收时丢弃的包数:0eth17发送时丢弃的包数:0网络设备名:lo0IP地址:127.0.0.1子网掩码:255.0.0.0lo0接收的总包裹数:0lo0发送的总包裹数:0lo0接收到的总字节数:0lo0发送的总字节数:0lo0接收到的错误包数:0lo0发送数据包时的错误数:0lo0接收时丢弃的包数:0lo0发送时丢弃的包数:0分区的盘符名称0盘符名称:C:\盘符路径:C:\盘符标志:0盘符类型:NTFS盘符类型名:local盘符文件系统类型:2C:\总大小:157292380KBC:\剩余大小:104494488KBC:\可用大小:104494488KBC:\已经使用量:52797892KBC:\资源的利用率:34.0%C:\读出:104242C:\写入:304686读/写比:0.3421分区的盘符名称1盘符名称:D:\盘符路径:D:\盘符标志:0盘符类型:NTFS盘符类型名:local盘符文件系统类型:2D:\总大小:111161732KBD:\剩余大小:65196216KBD:\可用大小:65196216KBD:\已经使用量:45965516KBD:\资源的利用率:42.0%D:\读出:46402D:\写入:50971读/写比:0.9104分区的盘符名称2盘符名称:E:\盘符路径:E:\盘符标志:0盘符类型:NTFS盘符类型名:local盘符文件系统类型:2E:\总大小:110109476KBE:\剩余大小:78923096KBE:\可用大小:78923096KBE:\已经使用量:31186380KBE:\资源的利用率:28.999999999999996%E:\读出:35962E:\写入:6737读/写比:5.338分区的盘符名称3盘符名称:F:\盘符路径:F:\盘符标志:0盘符类型:NTFS盘符类型名:local盘符文件系统类型:2F:\总大小:109820308KBF:\剩余大小:81319804KBF:\可用大小:81319804KBF:\已经使用量:28500504KBF:\资源的利用率:26.0%F:\读出:2081F:\写入:1561读/写比:1.3331分区的盘符名称4盘符名称:G:\盘符路径:G:\盘符标志:0盘符类型:cdrom盘符类型名:cdrom盘符文件系统类型:5The device is not ready.


 
import java.math.BigDecimal;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Map;import java.util.Properties;import org.hyperic.sigar.CpuInfo;import org.hyperic.sigar.CpuPerc;import org.hyperic.sigar.FileSystem;import org.hyperic.sigar.FileSystemUsage;import org.hyperic.sigar.Mem;import org.hyperic.sigar.NetFlags;import org.hyperic.sigar.NetInterfaceConfig;import org.hyperic.sigar.NetInterfaceStat;import org.hyperic.sigar.OperatingSystem;import org.hyperic.sigar.Sigar;import org.hyperic.sigar.SigarException;import org.hyperic.sigar.Swap;import org.hyperic.sigar.Who;public class RuntimeTest {public static void main(String[] args) { try {//运行时状态// System信息,从jvm获取//property();//System.out.println("----------------------------------");// cpu信息cpu();System.out.println("----------------------------------");// 内存信息memory();//System.out.println("-----------------操作系统信息 ----------------");// 操作系统信息 //os();//System.out.println("----------------------------------");// 用户信息//who();System.out.println("----------------网络信息------------");// 网络信息net();//System.out.println("---------------以太网信息----------------");// 以太网信息//ethernet(); //System.out.println("----------------文件系统信息------------------"); file();} catch (Exception e1) {e1.printStackTrace();}}private static void property() throws UnknownHostException {Runtime r = Runtime.getRuntime();Properties props = System.getProperties();InetAddress addr;addr = InetAddress.getLocalHost();String ip = addr.getHostAddress();Map<String, String> map = System.getenv();String userName = map.get("USERNAME");// 获取用户名String computerName = map.get("COMPUTERNAME");// 获取计算机名String userDomain = map.get("USERDOMAIN");// 获取计算机域名System.out.println("用户名:" + userName);System.out.println("计算机名:" + computerName);System.out.println("计算机域名:" + userDomain);System.out.println("本地ip地址:" + ip);System.out.println("本地主机名:" + addr.getHostName());System.out.println("JVM可以使用的总内存:" + r.totalMemory());System.out.println("JVM可以使用的剩余内存:" + r.freeMemory());System.out.println("JVM可以使用的处理器个数:" + r.availableProcessors());System.out.println("Java的运行环境版本:" + props.getProperty("java.version"));System.out.println("Java的运行环境供应商:" + props.getProperty("java.vendor"));System.out.println("Java供应商的URL:" + props.getProperty("java.vendor.url"));System.out.println("Java的安装路径:" + props.getProperty("java.home"));System.out.println("Java的虚拟机规范版本:" + props.getProperty("java.vm.specification.version"));System.out.println("Java的虚拟机规范供应商:" + props.getProperty("java.vm.specification.vendor"));System.out.println("Java的虚拟机规范名称:" + props.getProperty("java.vm.specification.name"));System.out.println("Java的虚拟机实现版本:" + props.getProperty("java.vm.version"));System.out.println("Java的虚拟机实现供应商:" + props.getProperty("java.vm.vendor"));System.out.println("Java的虚拟机实现名称:" + props.getProperty("java.vm.name"));System.out.println("Java运行时环境规范版本:" + props.getProperty("java.specification.version"));System.out.println("Java运行时环境规范供应商:" + props.getProperty("java.specification.vender"));System.out.println("Java运行时环境规范名称:" + props.getProperty("java.specification.name"));System.out.println("Java的类格式版本号:" + props.getProperty("java.class.version"));System.out.println("Java的类路径:" + props.getProperty("java.class.path"));System.out.println("加载库时搜索的路径列表:" + props.getProperty("java.library.path"));System.out.println("默认的临时文件路径:" + props.getProperty("java.io.tmpdir"));System.out.println("一个或多个扩展目录的路径:" + props.getProperty("java.ext.dirs"));System.out.println("操作系统的名称:" + props.getProperty("os.name"));System.out.println("操作系统的构架:" + props.getProperty("os.arch"));System.out.println("操作系统的版本:" + props.getProperty("os.version"));System.out.println("文件分隔符:" + props.getProperty("file.separator"));System.out.println("路径分隔符:" + props.getProperty("path.separator"));System.out.println("行分隔符:" + props.getProperty("line.separator"));System.out.println("用户的账户名称:" + props.getProperty("user.name"));System.out.println("用户的主目录:" + props.getProperty("user.home"));System.out.println("用户的当前工作目录:" + props.getProperty("user.dir"));}private static void memory() throws SigarException {Sigar sigar = new Sigar();Mem mem = sigar.getMem();// 内存总量System.out.println("内存总量:" + mem.getTotal() / 1024L + "K av");// 当前内存使用量System.out.println("当前内存使用量:" + mem.getUsed() / 1024L + "K used");// 当前内存剩余量System.out.println("当前内存剩余量:" + mem.getFree() / 1024L + "K free");Swap swap = sigar.getSwap();// 交换区总量System.out.println("交换区总量:" + swap.getTotal() / 1024L + "K av");// 当前交换区使用量System.out.println("当前交换区使用量:" + swap.getUsed() / 1024L + "K used");// 当前交换区剩余量System.out.println("当前交换区剩余量:" + swap.getFree() / 1024L + "K free");}private static void cpu() throws SigarException {Sigar sigar = new Sigar();CpuInfo infos[] = sigar.getCpuInfoList();CpuPerc cpuList[] = null;cpuList = sigar.getCpuPercList();for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用CpuInfo info = infos[i];System.out.println("第" + (i + 1) + "块CPU信息");System.out.println("CPU的总量MHz:" + info.getMhz());// CPU的总量MHzSystem.out.println("CPU生产商:" + info.getVendor());// 获得CPU的卖主,如:IntelSystem.out.println("CPU类别:" + info.getModel());// 获得CPU的类别,如:CeleronSystem.out.println("CPU缓存数量:" + info.getCacheSize());// 缓冲存储器数量printCpuPerc(cpuList[i]);}}private static void printCpuPerc(CpuPerc cpu) {System.out.println("CPU用户使用率:" + CpuPerc.format(cpu.getUser()));// 用户使用率System.out.println("CPU系统使用率:" + CpuPerc.format(cpu.getSys()));// 系统使用率System.out.println("CPU当前等待率:" + CpuPerc.format(cpu.getWait()));// 当前等待率System.out.println("CPU当前错误率:" + CpuPerc.format(cpu.getNice()));//System.out.println("CPU当前空闲率:" + CpuPerc.format(cpu.getIdle()));// 当前空闲率System.out.println("CPU总的使用率:" + CpuPerc.format(cpu.getCombined()));// 总的使用率}private static void os() {OperatingSystem OS = OperatingSystem.getInstance();// 操作系统内核类型如: 386、486、586等x86System.out.println("操作系统:" + OS.getArch());System.out.println("操作系统CpuEndian():" + OS.getCpuEndian());//System.out.println("操作系统DataModel():" + OS.getDataModel());//// 系统描述System.out.println("操作系统的描述:" + OS.getDescription());// 操作系统类型// System.out.println("OS.getName():" + OS.getName());// System.out.println("OS.getPatchLevel():" + OS.getPatchLevel());//// 操作系统的卖主System.out.println("操作系统的卖主:" + OS.getVendor());// 卖主名称System.out.println("操作系统的卖主名:" + OS.getVendorCodeName());// 操作系统名称System.out.println("操作系统名称:" + OS.getVendorName());// 操作系统卖主类型System.out.println("操作系统卖主类型:" + OS.getVendorVersion());// 操作系统的版本号System.out.println("操作系统的版本号:" + OS.getVersion());}private static void who() throws SigarException {Sigar sigar = new Sigar();Who who[] = sigar.getWhoList();if (who != null && who.length > 0) {for (int i = 0; i < who.length; i++) {// System.out.println("当前系统进程表中的用户名" + String.valueOf(i));Who _who = who[i];System.out.println("用户控制台:" + _who.getDevice());System.out.println("用户host:" + _who.getHost());// System.out.println("getTime():" + _who.getTime());// 当前系统进程表中的用户名System.out.println("当前系统进程表中的用户名:" + _who.getUser());}}}private static void file() throws Exception {Sigar sigar = new Sigar();FileSystem fslist[] = sigar.getFileSystemList();for (int i = 0; i < fslist.length; i++) {System.out.println("分区的盘符名称" + i);FileSystem fs = fslist[i];// 分区的盘符名称System.out.println("盘符名称:" + fs.getDevName());// 分区的盘符名称System.out.println("盘符路径:" + fs.getDirName());System.out.println("盘符标志:" + fs.getFlags());//// 文件系统类型,比如 FAT32、NTFSSystem.out.println("盘符类型:" + fs.getSysTypeName());// 文件系统类型名,比如本地硬盘、光驱、网络文件系统等System.out.println("盘符类型名:" + fs.getTypeName());// 文件系统类型System.out.println("盘符文件系统类型:" + fs.getType());FileSystemUsage usage = null;String dirName=fs.getDirName();try{usage = sigar.getFileSystemUsage(dirName);switch (fs.getType()) {case 0: // TYPE_UNKNOWN :未知break;case 1: // TYPE_NONEbreak;case 2: // TYPE_LOCAL_DISK : 本地硬盘// 文件系统总大小System.out.println(fs.getDevName() + "总大小:" + usage.getTotal() + "KB");// 文件系统剩余大小System.out.println(fs.getDevName() + "剩余大小:" + usage.getFree() + "KB");// 文件系统可用大小System.out.println(fs.getDevName() + "可用大小:" + usage.getAvail() + "KB");// 文件系统已经使用量System.out.println(fs.getDevName() + "已经使用量:" + usage.getUsed() + "KB");double usePercent = usage.getUsePercent() * 100D;// 文件系统资源的利用率System.out.println(fs.getDevName() + "资源的利用率:" + usePercent + "%");break;case 3:// TYPE_NETWORK :网络break;case 4:// TYPE_RAM_DISK :闪存break;case 5:// TYPE_CDROM :光驱break;case 6:// TYPE_SWAP :页面交换break;}System.out.println(fs.getDevName() + "读出:" + usage.getDiskReads());System.out.println(fs.getDevName() + "写入:" + usage.getDiskWrites()); System.out.println("读/写比:"+getLongDivide(usage.getDiskReads(), usage.getDiskWrites(), 4));}catch(SigarException e){System.out.println("The device is not ready.");}}return;}public static Double getLongDivide(Long l1,Long l2,int point){  BigDecimal bd1 = new BigDecimal(l1);  BigDecimal bd2 = new BigDecimal(l2);  BigDecimal bd3 = bd1.divide(bd2,point, BigDecimal.ROUND_HALF_EVEN); //point为小数点后几位  return bd3.doubleValue();}private static void net() throws Exception {Sigar sigar = new Sigar();String ifNames[] = sigar.getNetInterfaceList();for (int i = 0; i < ifNames.length; i++) {String name = ifNames[i];NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);if("0.0.0.0".equals(ifconfig.getAddress().trim())){continue;}System.out.println("网络设备名:" + name);// 网络设备名System.out.println("IP地址:" + ifconfig.getAddress());// IP地址System.out.println("子网掩码:" + ifconfig.getNetmask());// 子网掩码if ((ifconfig.getFlags() & 1L) <= 0L) {System.out.println("!IFF_UP...skipping getNetInterfaceStat");continue;}NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数}}private static void ethernet() throws SigarException {Sigar sigar = null;sigar = new Sigar();String[] ifaces = sigar.getNetInterfaceList();for (int i = 0; i < ifaces.length; i++) {NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);if("0.0.0.0".equals(cfg.getAddress())){continue;}if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0|| NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {continue;}System.out.println(cfg.getName() + "IP地址:" + cfg.getAddress());// IP地址System.out.println(cfg.getName() + "网关广播地址:" + cfg.getBroadcast());// 网关广播地址System.out.println(cfg.getName() + "网卡MAC地址:" + cfg.getHwaddr());// 网卡MAC地址System.out.println(cfg.getName() + "子网掩码:" + cfg.getNetmask());// 子网掩码System.out.println(cfg.getName() + "网卡描述信息:" + cfg.getDescription());// 网卡描述信息System.out.println(cfg.getName() + "网卡类型" + cfg.getType());//}}}
3.项目依赖
sigar.jar
sigar-amd64-winnt.dll 【根据系统而定】
4.资源下载
二进制dll,so等
http://jaist.dl.sourceforge.net/project/sigar/sigar/1.6/hyperic-sigar-1.6.4.zip
其他资源百度sigar的官网即可


0 0
原创粉丝点击