View Javadoc
1   /*
2    * Copyright (c) 2006 Hyperic, Inc.
3    * Copyright (c) 2010 VMware, Inc.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.miloss.fgsms.osagent;
18  
19  
20  import java.util.ArrayList;
21  import java.util.GregorianCalendar;
22  import java.util.List;
23  import org.miloss.fgsms.common.Utility;
24  import org.miloss.fgsms.services.interfaces.common.DriveInformation;
25  
26  import org.hyperic.sigar.*;
27  import org.hyperic.sigar.cmd.Shell;
28  
29  import org.hyperic.sigar.shell.FileCompleter;
30  import org.hyperic.sigar.util.GetlineCompleter;
31  
32  /**
33   * Report filesytem disk space usage.
34   */
35  public class Df extends SigarCommandBase implements Closable{
36  
37     
38      private GetlineCompleter completer;
39      private boolean opt_i;
40  
41      public Df(Shell shell) {
42          super(shell);
43        
44          this.completer = new FileCompleter(shell);
45      }
46  
47      public Df() {
48          super();
49        
50      }
51  
52      public GetlineCompleter getCompleter() {
53          return this.completer;
54      }
55  
56  
57  
58      public List<DriveInformation> DriveInfo() throws SigarException {
59          List<DriveInformation> info = new ArrayList<DriveInformation>();
60          ArrayList sys = new ArrayList();
61          FileSystem[] fslist = this.proxy.getFileSystemList();
62          for (int i = 0; i < fslist.length; i++) {
63              sys.add(fslist[i]);
64          }
65          long  total=0;
66          FileSystem fs = null;
67          DriveInformation d = null;
68          for (int i = 0; i < sys.size(); i++) {
69              d = new DriveInformation();
70              d.setTimestamp(new GregorianCalendar());
71              fs = (FileSystem) sys.get(i);
72              try {
73                  FileSystemUsage usage;
74                  d.setSystemid(fs.getDevName());
75             //     d.set
76                  d.setPartition(fs.getDirName());
77                  if (Utility.stringIsNullOrEmpty(d.getSystemid())) {
78                      d.setSystemid(d.getPartition());
79                  }
80                  d.setType(fs.getTypeName() + " " + fs.getSysTypeName());
81  
82                  if (fs instanceof NfsFileSystem) {
83                      NfsFileSystem nfs = (NfsFileSystem) fs;
84  
85                      if (!nfs.ping()) {
86                          d.setOperational(false);
87                          println(nfs.getUnreachableMessage());
88                          info.add(d);
89                          continue;
90                      } else {
91                          d.setOperational(true);
92                      }
93                  }
94                  usage = null;
95                  try {
96                      usage = this.sigar.getFileSystemUsage(fs.getDevName());
97                  } catch (Exception ex) {
98                  }
99                  if (usage == null) {
100                     try {
101                         usage = this.sigar.getFileSystemUsage(fs.getDirName());
102                     } catch (Exception ex) {
103                     }
104                 }
105                 if (this.opt_i) {
106            //         used = usage.getFiles() - usage.getFreeFiles();
107              //       avail = usage.getFreeFiles();
108                     total = usage.getFiles();
109                     if (total == 0) {
110                      //   pct = 0;
111                     } else {
112             //            long u100 = used * 100;
113             //            pct = u100 / total
114             //                    + ((u100 % total != 0) ? 1 : 0);
115                     }
116                 } else {
117                  //   used = usage.getTotal() - usage.getFree();
118                   //  avail = usage.getAvail();
119                     total = usage.getTotal();
120 
121             //        pct = (long) (usage.getUsePercent() * 100);
122                 }
123                 d.setTotalspace(total / 1024);  //this is reported in MB
124                 d.setOperational(true);
125 
126                 info.add(d);
127             } catch (Exception e) {
128                 //e.g. on win32 D:\ fails with "Device not ready"
129                 //if there is no cd in the drive.
130          //       e.printStackTrace();
131               //  used = avail = total = pct = 0;
132             }
133         }
134         return info;
135     }
136 
137 
138     public DriveInformation GetDriveInfo(String get) throws SigarException {
139         DriveInformation d = new DriveInformation();
140         FileSystem[] fslist = this.proxy.getFileSystemList();
141         
142         for (int i = 0; i < fslist.length; i++) {
143             //modified to allow for C: , sigars returns C:\
144             if (fslist[i].getDevName().startsWith(get)) {
145 
146                 if (fslist[i] instanceof NfsFileSystem) {
147                     NfsFileSystem nfs = (NfsFileSystem) fslist[i];
148                     if (!nfs.ping()) {
149                         d.setOperational(false);
150                     }
151                 }
152                 FileSystemUsage usage = this.sigar.getFileSystemUsage(fslist[i].getDirName());
153                 d.setId(fslist[i].getDevName());
154                 d.setFreespace(usage.getFree());
155                 d.setPartition(fslist[i].getDirName());
156                 d.setType(fslist[i].getTypeName());
157                 return d;
158             }
159         }
160         return null;
161     }
162 
163       @Override
164     public void close() throws Exception{
165         if (sigar != null) {
166             sigar.close();
167             sigar=null;
168         }
169     }
170     @Override
171     protected void finalize() throws Throwable
172     {
173         
174           if (sigar != null) {
175               System.out.println("WARN,. finalize called without closing sigar first" + this.getClass().getCanonicalName());
176             sigar.close();
177         }
178         super.finalize();
179     }
180 }