View Javadoc
1   /*
2    * Copyright (c) 2006-2008 Hyperic, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.miloss.fgsms.osagent;
17  
18  import java.util.ArrayList;
19  
20  import org.hyperic.sigar.Sigar;
21  import org.hyperic.sigar.SigarException;
22  import org.hyperic.sigar.FileSystem;
23  import org.hyperic.sigar.FileSystemMap;
24  import org.hyperic.sigar.FileSystemUsage;
25  import org.hyperic.sigar.DiskUsage;
26  import org.hyperic.sigar.cmd.Shell;
27  
28  import org.hyperic.sigar.shell.FileCompleter;
29  import org.hyperic.sigar.util.GetlineCompleter;
30  
31  /**
32   * Report filesytem disk space usage.
33   */
34  public class Iostat extends SigarCommandBase implements Closable{
35  
36     
37      public class DiskRates {
38  
39          long bytesreadpersecond;
40          long byteswritepersecond;
41      }
42  
43      public DiskRates GetBytesPerSecond(String PartitionName, boolean isWindows) throws SigarException, InterruptedException {
44  
45          DiskRates d = new DiskRates();
46          FileSystemMap mounts = this.proxy.getFileSystemMap();
47        
48          DiskUsage disk =null;
49          if (isWindows)
50                  disk = this.sigar.getDiskUsage(PartitionName.toUpperCase());
51          else
52              disk  = this.sigar.getDiskUsage(PartitionName.toUpperCase());
53          if (disk.getReadBytes() == Sigar.FIELD_NOTIMPL) {
54              d.bytesreadpersecond = -1;
55              d.byteswritepersecond = -1;
56              return d;
57          } else {
58              d.bytesreadpersecond = (disk.getReadBytes());
59              d.byteswritepersecond = (disk.getWriteBytes());
60              Thread.sleep(1000);
61              disk  = this.sigar.getDiskUsage(PartitionName.toUpperCase());
62              d.bytesreadpersecond = (disk.getReadBytes()) - d.bytesreadpersecond;
63              d.byteswritepersecond = (disk.getWriteBytes()) - d.byteswritepersecond;
64  
65          }
66          return d;
67      }
68      private GetlineCompleter completer;
69  
70      public Iostat(Shell shell) {
71          super(shell);
72         
73          this.completer = new FileCompleter(shell);
74      }
75  
76      public Iostat() {
77          super();
78        
79      }
80  
81      public GetlineCompleter getCompleter() {
82          return this.completer;
83      }
84  
85  
86      /*
87       * public static void main(String[] args) throws Exception { new
88       * Iostat().processCommand(args);
89      }
90       */
91          @Override
92      public void close() throws Exception{
93          if (sigar != null) {
94              sigar.close();
95              sigar = null;
96          }
97      }
98      @Override
99      protected void finalize() throws Throwable
100     {
101         
102           if (sigar != null) {
103               System.out.println("WARN,. finalize called without closing sigar first"+ this.getClass().getCanonicalName());
104             sigar.close();
105         }
106         super.finalize();
107     }
108 }