1 |
ab11 |
1.1 |
import java.io.*; |
2 |
|
|
import java.util.*; |
3 |
|
|
|
4 |
|
|
class DecodeCPU_TXT { |
5 |
|
|
|
6 |
|
|
XMLFormatter packet; |
7 |
|
|
|
8 |
|
|
public DecodeCPU_TXT(String _file){ |
9 |
|
|
|
10 |
|
|
// open the file and read in all the relevant details |
11 |
|
|
try { |
12 |
|
|
BufferedReader in = new BufferedReader(new FileReader(_file)); |
13 |
|
|
String line = new String(); |
14 |
|
|
|
15 |
|
|
// get rid of the first output (it doesn't contain cpu info) |
16 |
|
|
for ( int i=0; i < 7; i++ ){ |
17 |
|
|
line = in.readLine(); |
18 |
|
|
System.out.println("Throwing away line: "+line); |
19 |
|
|
} |
20 |
|
|
// first line has the format: load averages: 0.30, 0.27, 0.29 16:58:10 |
21 |
|
|
// tokenize it using spaces as delimiters? |
22 |
|
|
StringTokenizer tok = new StringTokenizer(line,":",false); |
23 |
|
|
tok.nextToken(); // load averages: |
24 |
|
|
String load1 = tok.nextToken(" ,"); // "0.30" |
25 |
|
|
String load2 = tok.nextToken(); // "0.27" |
26 |
|
|
String load3 = tok.nextToken(); // "0.29" |
27 |
|
|
String sTime = tok.nextToken(); // "16:58:10" |
28 |
|
|
|
29 |
|
|
// get the next line |
30 |
|
|
line = in.readLine(); // 632 processes: 591 sleeping, 10 zombie, 30 stopped, 1 on cpu |
31 |
|
|
tok = new StringTokenizer(line," ",false); |
32 |
|
|
String totalProcesses = tok.nextToken(); // "632" |
33 |
|
|
String sleeping = tok.nextToken(": ,"); // "591" |
34 |
|
|
String zombie = tok.nextToken(); // "10" |
35 |
|
|
String stopped = tok.nextToken(","); // "30" |
36 |
|
|
|
37 |
|
|
// get the next line |
38 |
|
|
line = in.readLine(); // CPU states: 71.1% idle, 1.8% user, 4.8% kernel, 22.2% iowait, 0.0% swap |
39 |
|
|
tok = new StringTokenizer(line,":%,",false); |
40 |
|
|
String idle = tok.nextToken(); // "71.1" |
41 |
|
|
String user = tok.nextToken(",%"); // " 1.8" |
42 |
|
|
String kernel = tok.nextToken(); // " 4.8" |
43 |
|
|
String iowait = tok.nextToken(); // " 22.2" |
44 |
|
|
String swap = tok.nextToken(); // " 0.0" |
45 |
|
|
|
46 |
|
|
line = in.readLine(); // Memory: 4096M real, 2380M free, 1237M swap in use, 9774M swap free |
47 |
|
|
tok = new StringTokenizer(line,": ,",false); |
48 |
|
|
tok.nextToken(); // Memory: |
49 |
|
|
String real = tok.nextToken(); // "4096M" |
50 |
|
|
String free = tok.nextToken(", "); // "2380M" |
51 |
|
|
String swapInUse = tok.nextToken(); // "1237M" |
52 |
|
|
String swapFree = tok.nextToken(", "); // "9774M" |
53 |
|
|
|
54 |
|
|
// done now make it into an xml packet ;) |
55 |
|
|
|
56 |
|
|
packet = new XMLFormatter("core"); |
57 |
|
|
packet.addNest("load"); |
58 |
|
|
packet.addElement("1",load1); |
59 |
|
|
packet.addElement("2",load2); |
60 |
|
|
packet.addElement("3",load3); |
61 |
|
|
packet.closeNest(); |
62 |
|
|
packet.addNest("processes"); |
63 |
|
|
packet.addElement("total",totalProcesses); |
64 |
|
|
packet.addElement("sleeping",sleeping); |
65 |
|
|
packet.addElement("zombie",zombie); |
66 |
|
|
packet.addElement("stopped",stopped); |
67 |
|
|
packet.closeNest(); |
68 |
|
|
packet.addNest("cpu"); |
69 |
|
|
packet.addElement("idle",idle); |
70 |
|
|
packet.addElement("user",user); |
71 |
|
|
packet.addElement("kernel",kernel); |
72 |
|
|
packet.addElement("iowait",iowait); |
73 |
|
|
packet.addElement("swap",swap); |
74 |
|
|
packet.closeNest(); |
75 |
|
|
packet.addNest("memory"); |
76 |
|
|
packet.addElement("real",real); |
77 |
|
|
packet.addElement("free",free); |
78 |
|
|
packet.addElement("swapinuse",swapInUse); |
79 |
|
|
packet.addElement("swapfree",swapFree); |
80 |
|
|
packet.closeNest(); |
81 |
|
|
|
82 |
|
|
} |
83 |
|
|
catch ( IOException e ){ |
84 |
|
|
// probably couldn't find the file |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
|
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
public String getItems(){ |
91 |
|
|
// return the xml |
92 |
|
|
return packet.returnXML(); |
93 |
|
|
|
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
} // class |