Sigar(System Information Gatherer And Reporter),是一个开源的工具,提供了跨平台的系统信息收集的API,由C语言实现的。可以收集的信息包括:
1, CPU信息,包括基本信息(vendor、model、mhz、cacheSize)和统计信息(user、sys、idle、nice、wait)
2, 文件系统信息,包括Filesystem、Size、Used、Avail、Use%、Type
3, 事件信息,类似Service Control Manager
4, 内存信息,物理内存和交换内存的总数、使用数、剩余数;RAM的大小
5, 网络信息,包括网络接口信息和网络路由信息
6, 进程信息,包括每个进程的内存、CPU占用数、状态、参数、句柄
7, IO信息,包括IO的状态,读写大小等
8, 服务状态信息
9, 系统信息,包括操作系统版本,系统资源限制情况,系统运行时间以及负载,JAVA的版本信息等.
Sigar在Java中的使用:
示例代码:
package cn.jsi.buaa; import org.hyperic.sigar.Cpu; import org.hyperic.sigar.CpuInfo; import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; import com.thoughtworks.xstream.XStream; /** * Cpu数据 * * 使用Sigar获得CPU的基本信息、使用百分比、使用时间 * */ public class SigarTest { private CpuInfo info; private CpuPerc perc; private Cpu timer; public SigarTest() { } public void populate(Sigar sigar) throws SigarException { info = sigar.getCpuInfoList()[0]; perc = sigar.getCpuPerc(); timer = sigar.getCpu(); } public static SigarTest gather(Sigar sigar) throws SigarException { SigarTest data = new SigarTest(); data.populate(sigar); return data; } public static void main(String[] args) throws Exception { System.out.println(System.getProperty("java.library.path")); Sigar sigar = new Sigar(); SigarTest cpuData = SigarTest.gather(sigar); XStream xstream = new XStream(); xstream.alias("CpuData", SigarTest.class); System.out.println(xstream.toXML(cpuData)); } }
Windows:将sigar.jar包放到classpath下,将sigar-amd64-winnt.dll放到环境变量java.library.path下。
Linux:将sigar.jar包放到classpath下,将libsigar-x86-linux.so放到环境变量java.library.path下。
获取网卡的流量
和网卡相关的统计数据主要有:网卡信息(ip地址、子网掩码、mac地址等)、网卡的吞吐量、流量等等。
当然了使用sigar也可以获得这些数据。
下面通过例子说明:
Java代码
1. package lab.sigar; 2. 3. import java.util.ArrayList; 4. import java.util.List; 5. 6. import org.hyperic.sigar.NetInterfaceConfig; 7. import org.hyperic.sigar.NetInterfaceStat; 8. import org.hyperic.sigar.Sigar; 9. import org.hyperic.sigar.SigarException; 10. 11. import com.thoughtworks.xstream.XStream; 12. 13. /** 14. * 网卡信息、接口数据、流量 15. * 16. * 使用Sigar获得网卡信息 17. * 18. */ 19. public class NetInterfaceData { 20. 21. private NetInterfaceConfig config; 22. private NetInterfaceStat stat; 23. private long rxbps; 24. private long txbps; 25. 26. public NetInterfaceData() {} 27. 28. public void populate(Sigar sigar, String name) 29. throws SigarException { 30. 31. config = sigar.getNetInterfaceConfig(name); 32. 33. try { 34. 35. long start = System.currentTimeMillis(); 36. NetInterfaceStat statStart = sigar.getNetInterfaceStat(name); 37. long rxBytesStart = statStart.getRxBytes(); 38. long txBytesStart = statStart.getTxBytes(); 39. Thread.sleep(1000); 40. long end = System.currentTimeMillis(); 41. NetInterfaceStat statEnd = sigar.getNetInterfaceStat(name); 42. long rxBytesEnd = statEnd.getRxBytes(); 43. long txBytesEnd = statEnd.getTxBytes(); 44. 45. rxbps = (rxBytesEnd - rxBytesStart)*8/(end-start)*1000; 46. txbps = (txBytesEnd - txBytesStart)*8/(end-start)*1000; 47. stat = sigar.getNetInterfaceStat(name); 48. } catch (SigarException e) { 49. 50. } catch (Exception e) { 51. 52. } 53. } 54. 55. public static NetInterfaceData gather(Sigar sigar, String name) 56. throws SigarException { 57. 58. NetInterfaceData data = new NetInterfaceData(); 59. data.populate(sigar, name); 60. return data; 61. } 62. 63. public NetInterfaceConfig getConfig() { 64. return config; 65. } 66. 67. public NetInterfaceStat getStat() { 68. return stat; 69. } 70. 71. 72. 73. public long getRxbps() { 74. return rxbps; 75. } 76. 77. public long getTxbps() { 78. return txbps; 79. } 80. 81. public static void main(String[] args) throws Exception { 82. Sigar sigar = new Sigar(); 83. String[] netIfs = sigar.getNetInterfaceList(); 84. List netIfList = new ArrayList(); 85. for ( String name:netIfs ) { 86. NetInterfaceData netIfData1 = NetInterfaceData.gather(sigar, name); 87. netIfList.add(netIfData1); 88. } 89. XStream xstream = new XStream(); 90. xstream.alias("NetInterfaceDatas", List.class); 91. xstream.alias("NetInterfaceData", NetInterfaceData.class); 92. System.out.println(xstream.toXML(netIfList)); 93. } 94. }
获取内存数据
内存的主要指标有:物理内存的大小、使用数、剩余数;交换内存的大小、使用数、剩余数;RAM的大小等等。
下面通过例子说明:
Java代码
1. package lab.sigar; 2. 3. import org.hyperic.sigar.Mem; 4. import org.hyperic.sigar.Sigar; 5. import org.hyperic.sigar.SigarException; 6. import org.hyperic.sigar.Swap; 7. 8. import com.thoughtworks.xstream.XStream; 9. 10. /** 11. * 内存数据 12. * 13. * 使用Sigar获得系统内存信息 14. * 15. */ 16. public class MemoryData { 17. private Mem mem; 18. private Swap swap; 19. 20. public MemoryData() { 21. } 22. 23. public void populate(Sigar sigar) throws SigarException { 24. mem = sigar.getMem(); 25. swap = sigar.getSwap(); 26. } 27. 28. public static MemoryData gather(Sigar sigar) throws SigarException { 29. MemoryData data = new MemoryData(); 30. data.populate(sigar); 31. return data; 32. } 33. 34. public static void main(String[] args) throws Exception { 35. Sigar sigar = new Sigar(); 36. MemoryData memData = MemoryData.gather(sigar); 37. XStream xstream = new XStream(); 38. xstream.alias("MemData", MemoryData.class); 39. System.out.println(xstream.toXML(memData)); 40. } 41. 42. }
输出:
Xml代码
1. <MemData> 2. <mem> 3. <total>1073102848</total> 4. <ram>1024</ram> 5. <used>932274176</used> 6. <free>140828672</free> 7. <actualUsed>932274176</actualUsed> 8. <actualFree>140828672</actualFree> 9. </mem> 10. <swap> 11. <total>2580328448</total> 12. <used>870432768</used> 13. <free>1709895680</free> 14. <pageIn>137691</pageIn> 15. <pageOut>19392</pageOut> 16. </swap> 17. </MemData>