1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
107
108 total = usage.getFiles();
109 if (total == 0) {
110
111 } else {
112
113
114
115 }
116 } else {
117
118
119 total = usage.getTotal();
120
121
122 }
123 d.setTotalspace(total / 1024);
124 d.setOperational(true);
125
126 info.add(d);
127 } catch (Exception e) {
128
129
130
131
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
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 }