首页 > Java开发 > Sigar获取磁盘信息

Sigar获取磁盘信息

在网络应用中,从最初为用户提供文本数据,发展到现在为用户提供视频数据。网络的内容更丰富了,当然资源所占用的服务器硬盘空间也更大了。经常会出现磁盘满,无法写入数据的情况。为此我们就需要对磁盘进行监控,也就是对文件系统进行监控。

文件系统的指标有:名称、分区类型等基本信息;读写次数等统计信息。

当然了使用sigar也可以获得分件系统的这些数据。

下面通过例子说明:

[java][/java]

view plaincopy

  1. package lab.sigar;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.hyperic.sigar.FileSystem;
  5. import org.hyperic.sigar.FileSystemUsage;
  6. import org.hyperic.sigar.Sigar;
  7. import org.hyperic.sigar.SigarException;
  8. import com.thoughtworks.xstream.XStream;
  9. /**
  10.  * 文件系统数据
  11.  *
  12.  * 使用Sigar获得文件系统信息
  13.  *
  14.  * @author wangrui
  15.  *
  16.  */
  17. public class FileSystemData {
  18.     private FileSystem config;
  19.     private FileSystemUsage stat;
  20.     public FileSystemData() {}
  21.     public void populate(Sigar sigar, FileSystem fs)
  22.         throws SigarException {
  23.         config = fs;
  24.         try {
  25.             stat = sigar.getFileSystemUsage(fs.getDirName());
  26.         } catch (SigarException e) {
  27.         }
  28.     }
  29.     public static FileSystemData gather(Sigar sigar, FileSystem fs)
  30.         throws SigarException {
  31.         FileSystemData data = new FileSystemData();
  32.         data.populate(sigar, fs);
  33.         return data;
  34.     }
  35.     public FileSystem getConfig() {
  36.         return config;
  37.     }
  38.     public FileSystemUsage getStat() {
  39.         return stat;
  40.     }
  41.     public static void main(String[] args) throws Exception {
  42.         Sigar sigar = new Sigar();
  43.         FileSystem[] fsArr = sigar.getFileSystemList();
  44.         List fsList = new ArrayList();
  45.         for ( FileSystem fs:fsArr ) {
  46.             FileSystemData fsData = FileSystemData.gather(sigar, fs);
  47.             fsList.add(fsData);
  48.         }
  49.         XStream xstream = new XStream();
  50.         xstream.alias("FileSystemDatas", List.class);
  51.         xstream.alias("FileSystemData", FileSystemData.class);
  52.         System.out.println(xstream.toXML(fsList));
  53.     }
  54. }
[html][/html]

view plaincopy

  1. <FileSystemDatas>
  2.   <FileSystemData>
  3.     <config>
  4.       <dirName>C:\</dirName>
  5.       <devName>C:\</devName>
  6.       <typeName>local</typeName>
  7.       <sysTypeName>FAT32</sysTypeName>
  8.       <type>2</type>
  9.       <flags>0</flags>
  10.     </config>
  11.     <stat>
  12.       <total>38418048</total>
  13.       <free>27689856</free>
  14.       <used>10728192</used>
  15.       <avail>27689856</avail>
  16.       <files>-1</files>
  17.       <freeFiles>-1</freeFiles>
  18.       <diskReads>17762</diskReads>
  19.       <diskWrites>50748</diskWrites>
  20.       <diskReadBytes>317399040</diskReadBytes>
  21.       <diskWriteBytes>1591675392</diskWriteBytes>
  22.       <diskQueue>0</diskQueue>
  23.       <diskServiceTime>-1.0</diskServiceTime>
  24.       <usePercent>0.28</usePercent>
  25.     </stat>
  26.   </FileSystemData>
  27.   <FileSystemData>
  28.     <config>
  29.       <dirName>D:\</dirName>
  30.       <devName>D:\</devName>
  31.       <typeName>local</typeName>
  32.       <sysTypeName>FAT32</sysTypeName>
  33.       <type>2</type>
  34.       <flags>0</flags>
  35.     </config>
  36.     <stat>
  37.       <total>66496576</total>
  38.       <free>49064928</free>
  39.       <used>17431648</used>
  40.       <avail>49064928</avail>
  41.       <files>-1</files>
  42.       <freeFiles>-1</freeFiles>
  43.       <diskReads>67354</diskReads>
  44.       <diskWrites>19482</diskWrites>
  45.       <diskReadBytes>817295872</diskReadBytes>
  46.       <diskWriteBytes>230753792</diskWriteBytes>
  47.       <diskQueue>0</diskQueue>
  48.       <diskServiceTime>-1.0</diskServiceTime>
  49.       <usePercent>0.27</usePercent>
  50.     </stat>
  51.   </FileSystemData>
  52.   <FileSystemData>
  53.     <config>
  54.       <dirName>E:\</dirName>
  55.       <devName>E:\</devName>
  56.       <typeName>local</typeName>
  57.       <sysTypeName>FAT32</sysTypeName>
  58.       <type>2</type>
  59.       <flags>0</flags>
  60.     </config>
  61.     <stat>
  62.       <total>69111712</total>
  63.       <free>49214624</free>
  64.       <used>19897088</used>
  65.       <avail>49214624</avail>
  66.       <files>-1</files>
  67.       <freeFiles>-1</freeFiles>
  68.       <diskReads>41340</diskReads>
  69.       <diskWrites>8119</diskWrites>
  70.       <diskReadBytes>222592512</diskReadBytes>
  71.       <diskWriteBytes>91557888</diskWriteBytes>
  72.       <diskQueue>0</diskQueue>
  73.       <diskServiceTime>-1.0</diskServiceTime>
  74.       <usePercent>0.29</usePercent>
  75.     </stat>
  76.   </FileSystemData>
  77.   <FileSystemData>
  78.     <config>
  79.       <dirName>F:\</dirName>
  80.       <devName>F:\</devName>
  81.       <typeName>local</typeName>
  82.       <sysTypeName>FAT32</sysTypeName>
  83.       <type>2</type>
  84.       <flags>0</flags>
  85.     </config>
  86.     <stat>
  87.       <total>70109376</total>
  88.       <free>46776096</free>
  89.       <used>23333280</used>
  90.       <avail>46776096</avail>
  91.       <files>-1</files>
  92.       <freeFiles>-1</freeFiles>
  93.       <diskReads>16</diskReads>
  94.       <diskWrites>39</diskWrites>
  95.       <diskReadBytes>151552</diskReadBytes>
  96.       <diskWriteBytes>830976</diskWriteBytes>
  97.       <diskQueue>0</diskQueue>
  98.       <diskServiceTime>-1.0</diskServiceTime>
  99.       <usePercent>0.34</usePercent>
  100.     </stat>
  101.   </FileSystemData>
  102.   <FileSystemData>
  103.     <config>
  104.       <dirName>H:\</dirName>
  105.       <devName>H:\</devName>
  106.       <typeName>cdrom</typeName>
  107.       <sysTypeName>cdrom</sysTypeName>
  108.       <type>5</type>
  109.       <flags>0</flags>
  110.     </config>
  111.   </FileSystemData>
  112. </FileSystemDatas>

下载地址:

本站下站sigar lib包

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


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

Sigar获取磁盘信息:目前有1 条留言

  1. 沙发
    上海:

    呵呵,谢谢啦@抓紧时间收藏

    2015-01-05 上午 2:59