Skip to content

Commit 4fdfcc4

Browse files
authored
Merge pull request #71 from hareldev/added-JENKINS-FS
2 parents afd5b4e + 188ab9a commit 4fdfcc4

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/main/java/com/cloudbees/simplediskusage/QuickDiskUsagePlugin.java

+38
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,34 @@ private void registerJobs(UsageComputation uc) throws IOException, InterruptedEx
205205
}
206206
}
207207

208+
private File getJenkinsBaseDirectory() throws NullPointerException {
209+
// finds the lowest non-null value of parent.
210+
// for example, would return "/" for "/var/jenkins_home"
211+
Jenkins jenkins = Jenkins.get();
212+
Path basePath = jenkins.getRootDir().toPath();
213+
try {
214+
while (basePath.getParent() != null) {
215+
basePath = basePath.getParent();
216+
}
217+
}
218+
catch (NullPointerException e){
219+
logger.log(Level.WARNING, "cloudbees-disk-usage-plugin: Could not find Jenkins Base Directory");
220+
}
221+
return basePath.toFile();
222+
}
223+
224+
private void registerDirectoriesFS(UsageComputation uc) throws IOException, InterruptedException {
225+
Map<File, String> directoriesToProcess = new HashMap<>();
226+
// Display JENKINS_FS size
227+
File rootPath = getJenkinsBaseDirectory();
228+
directoriesToProcess.put(rootPath, "JENKINS_FS");
229+
230+
// Add or update entries for directories
231+
for (Map.Entry<File, String> item : directoriesToProcess.entrySet()) {
232+
uc.addListener(item.getKey().toPath(), new DirectoryUsageListener(item.getValue()));
233+
}
234+
}
235+
208236
private void registerDirectories(UsageComputation uc) throws IOException, InterruptedException {
209237
Jenkins jenkins = Jenkins.get();
210238
Map<File, String> directoriesToProcess = new HashMap<>();
@@ -256,7 +284,17 @@ public void run() {
256284
registerDirectories(uc);
257285
total.set(uc.getItemsCount());
258286
uc.compute();
287+
288+
// Adds JENKINS_FS section with relevant disk usage info
289+
File rootPath = getJenkinsBaseDirectory();
290+
UsageComputation ucfs = new UsageComputation(Arrays.asList(rootPath.toPath()));
291+
registerJobs(ucfs);
292+
registerDirectoriesFS(ucfs);
293+
total.set(ucfs.getItemsCount());
294+
ucfs.computeFS();
295+
259296
logger.fine("Finished re-estimating disk usage.");
297+
260298
lastRunEnd = System.currentTimeMillis();
261299
} catch (IOException | InterruptedException e) {
262300
logger.log(Level.WARNING, "Unable to run disk usage check", e);

src/main/java/com/cloudbees/simplediskusage/UsageComputation.java

+30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hudson.FilePath;
44
import jenkins.model.Jenkins;
55

6+
import java.io.File;
67
import java.io.IOException;
78
import java.nio.file.FileVisitResult;
89
import java.nio.file.Files;
@@ -52,6 +53,35 @@ public void compute() throws IOException {
5253
}
5354
}
5455

56+
public void computeFS() {
57+
// setting the disk space usage for the entire FS
58+
for (Path path : pathsToScan) {
59+
try {
60+
Path dir = path.toAbsolutePath();
61+
long pathDiskUsage = jenkinsFSUsage();
62+
CompletionListener listener = listenerMap.get(dir);
63+
if (listener != null) {
64+
listener.onCompleted(dir, pathDiskUsage);
65+
}
66+
}
67+
catch (Exception e){
68+
logger.log(Level.WARNING, "cloudbees-disk-usage-plugin: FS information could not get acquired.");
69+
}
70+
}
71+
}
72+
73+
protected long jenkinsFSUsage() {
74+
File rd = Jenkins.get().getRootDir();
75+
long totalJenkins = rd.getTotalSpace();
76+
long usableJenkins = rd.getUsableSpace();
77+
if (usableJenkins <= 0 || totalJenkins <= 0) {
78+
// information unavailable. pointless to try.
79+
logger.log(Level.WARNING, "cloudbees-disk-usage-plugin: JENKINS_HOME disk usage information isn't available.");
80+
return -1;
81+
}
82+
return (totalJenkins - usableJenkins);
83+
}
84+
5585
protected void computeUsage(Path path) throws IOException {
5686
// we don't really need AtomicLong here, walking the tree is synchronous, but
5787
// it's convenient for the operations it provides

0 commit comments

Comments
 (0)