码迷,mamicode.com
首页 > 编程语言 > 详细

Java中获取系统相关信息——sigar

时间:2015-01-27 18:34:41      阅读:985      评论:0      收藏:0      [点我收藏+]

标签:

一、sigar简介

sigar中文名是系统信息收集和报表工具,是一个开源的工具,提供了跨平台的系统信息收集的API,可以和绝大多数操作系统和大多数版本打交道,可以收集的信息包括:
1.操作系统的信息,包括:dataModel、cpuEndian、name、version、arch、machine、description、patchLevel、vendor、vendorVersion、vendorName、vendorCodeName
2.CPU信息,包括:基本信息(vendor、model、mhz、cacheSize)和统计信息(user、sys、idle、nice、wait)
3.内存信息,物理内存和交换内存的总数、使用数、剩余数;RAM的大小
4.进程信息,包括每个进程的内存、CPU占用数、状态、参数、句柄等。
5.文件系统信息,包括名称、容量、剩余数、使用数、分区类型等
6.网络接口信息,包括基本信息和统计信息。
7.网络路由和链接表信息。

二、sigar的使用

//接口定义

public interface LoadInfo { 
     //获取cpu使用率
    public String getCpuInfo(); 
     // 获取系统内存使用量
    public String getRamInfo();
     //获取网络使用流量
    public String getNetworkInfo();
     //获取操作系统信息
    public String getSystemInfo();
     // 获取运行环境信息
    public String getRunLoadInfo();
    //获取虚拟机剩余内存
    public String getVmRamInfo();
}

 

//以下是接口的实现

获取cpu信息代码  技术分享
  1. // CPU数量(单位:个)  
  2. int cpuLength = sigar.getCpuInfoList().length;  
  3. print(cpuLength);  
  4.   
  5. // CPU的总量(单位:HZ)及CPU的相关信息  
  6. CpuInfo infos[] = sigar.getCpuInfoList();  
  7. for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用  
  8.     CpuInfo info = infos[i];  
  9.     print("mhz=" + info.getMhz());// CPU的总量MHz  
  10.     print("vendor=" + info.getVendor());// 获得CPU的卖主,如:Intel  
  11.     print("model=" + info.getModel());// 获得CPU的类别,如:Celeron  
  12.     print("cache size=" + info.getCacheSize());// 缓冲存储器数量  
  13. }  
  14.   
  15.   
  16. // 方式一,主要是针对一块CPU的情况  
  17. CpuPerc cpu;  
  18. try {  
  19.     cpu = sigar.getCpuPerc();  
  20.     printCpuPerc(cpu);  
  21. } catch (SigarException e) {  
  22.     e.printStackTrace();  
  23. }  
  24.   
  25. // 方式二,不管是单块CPU还是多CPU都适用  
  26. CpuPerc cpuList[] = null;  
  27. try {  
  28.     cpuList = sigar.getCpuPercList();  
  29. } catch (SigarException e) {  
  30.     e.printStackTrace();  
  31. }  
  32. for (int i = 0; i < cpuList.length; i++) {  
  33.     // printCpuPerc(cpuList[i]);  
  34. }  

 

 

 

 

获取内存信息代码  技术分享
  1. // 物理内存信息  
  2. Mem mem = sigar.getMem();  
  3. // 内存总量  
  4. print("Total = " + mem.getTotal() / 1024L / 1024 + "M av");  
  5. // 当前内存使用量  
  6. print("Used = " + mem.getUsed() / 1024L / 1024 + "M used");  
  7. // 当前内存剩余量  
  8. print("Free = " + mem.getFree() / 1024L / 1024 + "M free");  
  9.   
  10. // 系统页面文件交换区信息  
  11. Swap swap = sigar.getSwap();  
  12. // 交换区总量  
  13. print("Total = " + swap.getTotal() / 1024L + "K av");  
  14. // 当前交换区使用量  
  15. print("Used = " + swap.getUsed() / 1024L + "K used");  
  16. // 当前交换区剩余量  
  17. print("Free = " + swap.getFree() / 1024L + "K free");  

 

 

 

 

获取操作系统信息代码  技术分享
  1. "white-space: normal; background-color: #ffffff;"> "white-space: normal; background-color: #ffffff;">// 取到当前操作系统的名称  
  2. String hostname = "";  
  3. try {  
  4.     hostname = InetAddress.getLocalHost().getHostName();  
  5. } catch (Exception exc) {  
  6.     try {  
  7.         hostname = sigar.getNetInfo().getHostName();  
  8.     } catch (SigarException e) {  
  9.         hostname = "localhost.unknown";  
  10.     } finally {  
  11.         sigar.close();  
  12.     }  
  13. }  
  14. print(hostname);  
  15.   
  16. // 取当前操作系统的信息  
  17. OperatingSystem OS = OperatingSystem.getInstance();  
  18. // 操作系统内核类型如: 386、486、586等x86  
  19. print("OS.getArch() = " + OS.getArch());  
  20. print("OS.getCpuEndian() = " + OS.getCpuEndian());//  
  21. print("OS.getDataModel() = " + OS.getDataModel());//  
  22. // 系统描述  
  23. print("OS.getDescription() = " + OS.getDescription());  
  24. print("OS.getMachine() = " + OS.getMachine());//  
  25. // 操作系统类型  
  26. print("OS.getName() = " + OS.getName());  
  27. print("OS.getPatchLevel() = " + OS.getPatchLevel());//  
  28. // 操作系统的卖主  
  29. print("OS.getVendor() = " + OS.getVendor());  
  30. // 卖主名称  
  31. System.out  
  32.         .println("OS.getVendorCodeName() = " + OS.getVendorCodeName());  
  33. // 操作系统名称  
  34. print("OS.getVendorName() = " + OS.getVendorName());  
  35. // 操作系统卖主类型  
  36. print("OS.getVendorVersion() = " + OS.getVendorVersion());  
  37. // 操作系统的版本号  
  38. print("OS.getVersion() = " + OS.getVersion());  
  39.   
  40. // 取当前系统进程表中的用户信息  
  41. Who who[] = sigar.getWhoList();  
  42. if (who != null && who.length > 0) {  
  43.     for (int i = 0; i < who.length; i++) {  
  44.         print("\n~~~~~~~~~" + String.valueOf(i) + "~~~~~~~~~");  
  45.         Who _who = who[i];  
  46.         print("getDevice() = " + _who.getDevice());  
  47.         print("getHost() = " + _who.getHost());  
  48.         print("getTime() = " + _who.getTime());  
  49.         // 当前系统进程表中的用户名  
  50.         print("getUser() = " + _who.getUser());  
  51.     }  
  52. }  

 

 

 

获取磁盘信息代码  技术分享
  1. "white-space: normal; background-color: #ffffff;">// 取硬盘已有的分区及其详细信息(通过sigar.getFileSystemList()来获得FileSystem列表对象,然后对其进行编历  
  2. FileSystem fslist[] = sigar.getFileSystemList();  
  3. String dir = System.getProperty("user.home");// 当前用户文件夹路径  
  4. print(dir + "   " + fslist.length);  
  5. for (int i = 0; i < fslist.length; i++) {  
  6.     print("\n~~~~~~~~~~" + i + "~~~~~~~~~~");  
  7. FileSystem fs = fslist[i];  
  8. // 分区的盘符名称  
  9. print("fs.getDevName() = " + fs.getDevName());  
  10. // 分区的盘符名称  
  11. print("fs.getDirName() = " + fs.getDirName());  
  12. print("fs.getFlags() = " + fs.getFlags());//  
  13. // 文件系统类型,比如 FAT32、NTFS  
  14. print("fs.getSysTypeName() = " + fs.getSysTypeName());  
  15. // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等  
  16. print("fs.getTypeName() = " + fs.getTypeName());  
  17. // 文件系统类型  
  18. print("fs.getType() = " + fs.getType());  
  19. FileSystemUsage usage = null;  
  20. try {  
  21.     usage = sigar.getFileSystemUsage(fs.getDirName());  
  22. } catch (SigarException e) {  
  23.     if (fs.getType() == 2)  
  24.         throw e;  
  25.     continue;  
  26. }  
  27. switch (fs.getType()) {  
  28. case 0: // TYPE_UNKNOWN :未知  
  29.     break;  
  30. case 1: // TYPE_NONE  
  31.     break;  
  32. case 2: // TYPE_LOCAL_DISK : 本地硬盘  
  33.     // 文件系统总大小  
  34.     print(" Total = " + usage.getTotal() + "KB");  
  35.     // 文件系统剩余大小  
  36.     print(" Free = " + usage.getFree() + "KB");  
  37.     // 文件系统可用大小  
  38.     print(" Avail = " + usage.getAvail() + "KB");  
  39.     // 文件系统已经使用量  
  40.     print(" Used = " + usage.getUsed() + "KB");  
  41.     double usePercent = usage.getUsePercent() * 100D;  
  42.     // 文件系统资源的利用率  
  43.     print(" Usage = " + usePercent + "%");  
  44.     break;  
  45. case 3:// TYPE_NETWORK :网络  
  46.     break;  
  47. case 4:// TYPE_RAM_DISK :闪存  
  48.     break;  
  49. case 5:// TYPE_CDROM :光驱  
  50.     break;  
  51. case 6:// TYPE_SWAP :页面交换  
  52.     break;  
  53. }  
  54. print(" DiskReads = " + usage.getDiskReads());  
  55. print(" DiskWrites = " + usage.getDiskWrites());  
  56. }  

 

 

 

获取网络信息代码  技术分享
  1. "white-space: normal; background-color: #ffffff;"> // 当前机器的正式域名  
  2. try {  
  3.     print(InetAddress.getLocalHost().getCanonicalHostName());  
  4. } catch (UnknownHostException e) {  
  5.     try {  
  6.         print(sigar.getFQDN());  
  7.     } catch (SigarException ex) {  
  8.     } finally {  
  9.         sigar.close();  
  10.     }  
  11. }  
  12.   
  13. // 取到当前机器的IP地址  
  14. String address = null;  
  15. try {  
  16.     address = InetAddress.getLocalHost().getHostAddress();  
  17.     // 没有出现异常而正常当取到的IP时,如果取到的不是网卡循回地址时就返回  
  18.     // 否则再通过Sigar工具包中的方法来获取  
  19.     print(address);  
  20.     if (!NetFlags.LOOPBACK_ADDRESS.equals(address)) {  
  21.     }  
  22. } catch (UnknownHostException e) {  
  23.     // hostname not in DNS or /etc/hosts  
  24. }  
  25. try {  
  26.     address = sigar.getNetInterfaceConfig().getAddress();  
  27. } catch (SigarException e) {  
  28.     address = NetFlags.LOOPBACK_ADDRESS;  
  29. } finally {  
  30. }  
  31. print(address);  
  32.   
  33. // 取到当前机器的MAC地址  
  34. String[] ifaces = sigar.getNetInterfaceList();  
  35. String hwaddr = null;  
  36. for (int i = 0; i < ifaces.length; i++) {  
  37.     NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);  
  38.     if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())  
  39.             || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0  
  40.             || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {  
  41.         continue;  
  42.     }  
  43.     hwaddr = cfg.getHwaddr();  
  44.     print(hwaddr);  
  45.     // break;  
  46. }  
  47. print(hwaddr != null ? hwaddr : null);  
  48.   
  49. // 获取网络流量等信息  
  50. String ifNames[] = sigar.getNetInterfaceList();  
  51. for (int i = 0; i < ifNames.length; i++) {  
  52.     String name = ifNames[i];  
  53.     NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);  
  54.     print("\nname = " + name);// 网络设备名  
  55.     print("Address = " + ifconfig.getAddress());// IP地址  
  56.     print("Netmask = " + ifconfig.getNetmask());// 子网掩码  
  57.     if ((ifconfig.getFlags() & 1L) <= 0L) {  
  58.         print("!IFF_UP...skipping getNetInterfaceStat");  
  59.         continue;  
  60.     }  
  61.     try {  
  62.         NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);  
  63.         print("RxPackets = " + ifstat.getRxPackets());// 接收的总包裹数  
  64.         print("TxPackets = " + ifstat.getTxPackets());// 发送的总包裹数  
  65.         print("RxBytes = " + ifstat.getRxBytes());// 接收到的总字节数  
  66.         print("TxBytes = " + ifstat.getTxBytes());// 发送的总字节数  
  67.         print("RxErrors = " + ifstat.getRxErrors());// 接收到的错误包数  
  68.         print("TxErrors = " + ifstat.getTxErrors());// 发送数据包时的错误数  
  69.         print("RxDropped = " + ifstat.getRxDropped());// 接收时丢弃的包数  
  70.         print("TxDropped = " + ifstat.getTxDropped());// 发送时丢弃的包数  
  71.     } catch (SigarNotImplementedException e) {  
  72.     } catch (SigarException e) {  
  73.         print(e.getMessage());  
  74.     }  
  75. }  
  76.   
  77. // 一些其他的信息  
  78. for (int i = 0; i < ifaces.length; i++) {  
  79.     NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);  
  80.     if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())  
  81.             || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0  
  82.             || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {  
  83.         continue;  
  84.     }  
  85.     print("cfg.getAddress() = " + cfg.getAddress());// IP地址  
  86.     print("cfg.getBroadcast() = " + cfg.getBroadcast());// 网关广播地址  
  87.     print("cfg.getHwaddr() = " + cfg.getHwaddr());// 网卡MAC地址  
  88.     print("cfg.getNetmask() = " + cfg.getNetmask());// 子网掩码  
  89.     System.out  
  90.             .println("cfg.getDescription() = " + cfg.getDescription());// 网卡描述信息  
  91.     print("cfg.getType() = " + cfg.getType());//  
  92.     System.out  
  93.             .println("cfg.getDestination() = " + cfg.getDestination());  
  94.     print("cfg.getFlags() = " + cfg.getFlags());//  
  95.     print("cfg.getMetric() = " + cfg.getMetric());  
  96.     print("cfg.getMtu() = " + cfg.getMtu());  
  97.     print("cfg.getName() = " + cfg.getName());  
  98. }  

 

三、sigar的包及相关文件

 该类需要引入jar包,进入下载

另外为了防止程序运行时的错误,还需要相关文件,进入下载

四、在Windows中的常见问题

 使用该类常常会遇到问题:

java.lang.UnsatisfiedLinkError: org.hyperic.sigar.SysInfo.gather(Lorg/hyperic/sigar/Sigar;)V
org.hyperic.sigar.SysInfo.gather(Native Method)
org.hyperic.sigar.OperatingSystem.getInstance(OperatingSystem.java:90)
com.shuhaiserver.page.servlet.LoginServlet.doLogin(LoginServlet.java:146)
com.shuhaiserver.page.servlet.LoginServlet.doPost(LoginServlet.java:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.shuhaiserver.page.servlet.MyFilter.doFilter(MyFilter.java:82)

此时,需要将libsigar-amd64-linux.so、libsigar-x86-linux.so、sigar-amd64-winnt.dll、sigar-x86-winnt.dll、sigar-x86-winnt.lib文件放在项目的lib文件夹下

五、在Linux中的常见问题

 如果在Linux中还会包如上问题,可以尝试将相关文件放在项目的src文件夹下

 

附:代码转自http://blog.sina.com.cn/s/blog_5017ea6c0101byrr.html

Java中获取系统相关信息——sigar

标签:

原文地址:http://blog.csdn.net/u010515761/article/details/43196151

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!