1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.hadoop.fs;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.BufferedReader;
24
25 import java.util.StringTokenizer;
26
27 import org.apache.hadoop.conf.Configuration;
28
29 /** Filesystem disk space usage statistics. Uses the unix 'df' program.
30 * Tested on Linux, FreeBSD, Cygwin. */
31 public class DF {
32 public static final long DF_INTERVAL_DEFAULT = 3 * 1000; // default DF refresh interval
33
34 private String dirPath;
35 private long dfInterval; // DF refresh interval in msec
36 private long lastDF; // last time doDF() was performed
37
38 private String filesystem;
39 private long capacity;
40 private long used;
41 private long available;
42 private int percentUsed;
43 private String mount;
44
45 public DF(File path, Configuration conf) throws IOException {
46 this(path, conf.getLong("dfs.df.interval", DF.DF_INTERVAL_DEFAULT));
47 }
48
49 public DF(File path, long dfInterval) throws IOException {
50 this.dirPath = path.getCanonicalPath();
51 this.dfInterval = dfInterval;
52 lastDF = (dfInterval < 0) ? 0 : -dfInterval;
53 this.doDF();
54 }
55
56 private void doDF() throws IOException {
57 if (lastDF + dfInterval > System.currentTimeMillis())
58 return;
59 Process process;
60 process = Runtime.getRuntime().exec(getExecString());
61
62 try {
63 if (process.waitFor() != 0) {
64 throw new IOException
65 (new BufferedReader(new InputStreamReader(process.getErrorStream()))
66 .readLine());
67 }
68 parseExecResult(
69 new BufferedReader(new InputStreamReader(process.getInputStream())));
70 } catch (InterruptedException e) {
71 throw new IOException(e.toString());
72 } finally {
73 process.destroy();
74 }
75 }
76
77 /// ACCESSORS
78
79 public String getDirPath() {
80 return dirPath;
81 }
82
83 public String getFilesystem() throws IOException {
84 doDF();
85 return filesystem;
86 }
87
88 public long getCapacity() throws IOException {
89 doDF();
90 return capacity;
91 }
92
93 public long getUsed() throws IOException {
94 doDF();
95 return used;
96 }
97
98 public long getAvailable() throws IOException {
99 doDF();
100 return available;
101 }
102
103 public int getPercentUsed() throws IOException {
104 doDF();
105 return percentUsed;
106 }
107
108 public String getMount() throws IOException {
109 doDF();
110 return mount;
111 }
112
113 public long getCapacitySkipRefresh() {
114 return capacity;
115 }
116
117 public long getUsedSkipRefresh() {
118 return used;
119 }
120
121 public long getAvailableSkipRefresh() {
122 return available;
123 }
124
125 public int getPercentUsedSkipRefresh() {
126 return percentUsed;
127 }
128
129 public String toString() {
130 return
131 "df -k " + mount +"\n" +
132 filesystem + "\t" +
133 capacity / 1024 + "\t" +
134 used / 1024 + "\t" +
135 available / 1024 + "\t" +
136 percentUsed + "%\t" +
137 mount;
138 }
139
140 private String[] getExecString() {
141 return new String[] {"df","-k", dirPath};
142 }
143
144 private void parseExecResult(BufferedReader lines) throws IOException {
145 lines.readLine(); // skip headings
146
147 String line = lines.readLine();
148 if (line == null) {
149 throw new IOException( "Expecting a line not the end of stream" );
150 }
151 StringTokenizer tokens =
152 new StringTokenizer(line, " \t\n\r\f%");
153
154 this.filesystem = tokens.nextToken();
155 if (!tokens.hasMoreTokens()) { // for long filesystem name
156 line = lines.readLine();
157 if (line == null) {
158 throw new IOException( "Expecting a line not the end of stream" );
159 }
160 tokens = new StringTokenizer(line, " \t\n\r\f%");
161 }
162 this.capacity = Long.parseLong(tokens.nextToken()) * 1024;
163 this.used = Long.parseLong(tokens.nextToken()) * 1024;
164 this.available = Long.parseLong(tokens.nextToken()) * 1024;
165 this.percentUsed = Integer.parseInt(tokens.nextToken());
166 this.mount = tokens.nextToken();
167 this.lastDF = System.currentTimeMillis();
168 }
169
170 public static void main(String[] args) throws Exception {
171 String path = ".";
172 if (args.length > 0)
173 path = args[0];
174
175 System.out.println(new DF(new File(path), DF_INTERVAL_DEFAULT).toString());
176 }
177 }