1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package org.openslx.taskmanager.tasks;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openslx.taskmanager.api.SystemCommandTask;
public class DiskStat extends SystemCommandTask
{
private static final Pattern dfLine = Pattern.compile( "^(.*\\S)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)%\\s+(/.*)$" );
private final Output status = new Output();
private final List<Output.Entry> list = new ArrayList<>();
@Override
protected String[] initCommandLine()
{
this.timeoutSeconds = 2;
return new String[] {
"/bin/df",
"-P", "-B", "1024", "-a"
};
}
@Override
protected boolean processEnded( int exitCode )
{
if ( exitCode != 0 && list.isEmpty() ) {
status.error = "df returned exit code " + exitCode;
return false;
}
status.list = list;
return true;
}
@Override
protected void processStdOut( String line )
{
Matcher matcher = dfLine.matcher( line );
while ( matcher.find() ) {
try {
list.add( new Output.Entry(
matcher.group( 1 ),
matcher.group( 6 ),
Long.parseLong( matcher.group( 2 ) ),
Long.parseLong( matcher.group( 3 ) ),
Long.parseLong( matcher.group( 4 ) ),
Integer.parseInt( matcher.group( 5 ) ) )
);
} catch ( Exception e ) {
// Silently skip line, can't parse....
}
}
}
@Override
protected void processStdErr( String line )
{
status.error = line;
}
@Override
protected boolean initTask()
{
this.setStatusObject( status );
return true;
}
/**
* Output - contains additional status data of this task
*/
@SuppressWarnings( "unused" )
private static class Output
{
protected String error = null;
protected List<Entry> list = null;
public static class Entry
{
protected final String fileSystem, mountPoint;
protected final long sizeKb, usedKb, freeKb;
protected final int usedPercent;
public Entry( String fileSystem, String mountPoint, long sizeKb, long usedKb, long freeKb, int usedPercent )
{
this.fileSystem = fileSystem;
this.mountPoint = mountPoint;
this.sizeKb = sizeKb;
this.usedKb = usedKb;
this.freeKb = freeKb;
this.usedPercent = usedPercent;
}
}
}
}
|