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" |
8 |
> |
public DecodeCPU_TXT(){ |
9 |
> |
HashMap data; |
10 |
> |
try { |
11 |
> |
String[] cmd = {"statgrab.pl"}; |
12 |
> |
Process proc = Runtime.getRuntime().exec(cmd); |
13 |
> |
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); |
14 |
> |
|
15 |
> |
data = new HashMap(); |
16 |
> |
|
17 |
> |
// Messy, but it'll keep going until no more data :/ |
18 |
> |
try { |
19 |
> |
String line = new String(); |
20 |
> |
while(true) { |
21 |
> |
line = in.readLine(); |
22 |
> |
int split = line.indexOf(' '); |
23 |
> |
data.put(line.substring(0, split), line.substring(split+1)); |
24 |
> |
} |
25 |
> |
} |
26 |
> |
catch (Exception e) {} |
27 |
> |
|
28 |
> |
String load1 = (String) data.get("packet.load.load1"); |
29 |
> |
String load5 = (String) data.get("packet.load.load5"); |
30 |
> |
String load15 = (String) data.get("packet.load.load15"); |
31 |
|
|
32 |
< |
// get the next line |
33 |
< |
line = in.readLine(); // 632 processes: 591 sleeping, 10 zombie, 30 stopped, 1 on cpu |
34 |
< |
tok = new StringTokenizer(line," ",false); |
35 |
< |
String totalProcesses = tok.nextToken(); // "632" |
36 |
< |
String sleeping = tok.nextToken(": ,"); // "591" |
34 |
< |
String zombie = tok.nextToken(); // "10" |
35 |
< |
String stopped = tok.nextToken(","); // "30" |
32 |
> |
String totalProcesses = (String) data.get("packet.processes.total"); |
33 |
> |
String sleeping = (String) data.get("packet.processes.sleeping"); |
34 |
> |
String zombie = (String) data.get("packet.processes.zombie"); |
35 |
> |
String stopped = (String) data.get("packet.processes.stopped"); |
36 |
> |
String onCPU = (String) data.get("packet.processes.cpu"); |
37 |
|
|
38 |
< |
// get the next line |
39 |
< |
line = in.readLine(); // CPU states: 71.1% idle, 1.8% user, 4.8% kernel, 22.2% iowait, 0.0% swap |
40 |
< |
tok = new StringTokenizer(line,":%,",false); |
41 |
< |
String idle = tok.nextToken(); // "71.1" |
42 |
< |
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" |
38 |
> |
String idle = (String) data.get("packet.cpu.idle"); |
39 |
> |
String user = (String) data.get("packet.cpu.user"); |
40 |
> |
String kernel = (String) data.get("packet.cpu.kernel"); |
41 |
> |
String iowait = (String) data.get("packet.cpu.iowait"); |
42 |
> |
String swap = (String) data.get("packet.cpu.swap"); |
43 |
|
|
44 |
< |
line = in.readLine(); // Memory: 4096M real, 2380M free, 1237M swap in use, 9774M swap free |
45 |
< |
tok = new StringTokenizer(line,": ,",false); |
46 |
< |
tok.nextToken(); // Memory: |
47 |
< |
String real = tok.nextToken(); // "4096M" |
48 |
< |
String free = tok.nextToken(", "); // "2380M" |
49 |
< |
String swapInUse = tok.nextToken(); // "1237M" |
50 |
< |
String swapFree = tok.nextToken(", "); // "9774M" |
51 |
< |
|
52 |
< |
// done now make it into an xml packet ;) |
53 |
< |
|
54 |
< |
packet = new XMLFormatter("core"); |
44 |
> |
String real = (String) data.get("packet.memory.real"); |
45 |
> |
String free = (String) data.get("packet.memory.free"); |
46 |
> |
String swapInUse = (String) data.get("packet.memory.swap_in_use"); |
47 |
> |
String swapFree = (String) data.get("packet.memory.swap_free"); |
48 |
> |
|
49 |
> |
String osname = (String) data.get("packet.os.name"); |
50 |
> |
String osrelease = (String) data.get("packet.os.release"); |
51 |
> |
String osplatform = (String) data.get("packet.os.platform"); |
52 |
> |
String ossysname = (String) data.get("packet.os.sysname"); |
53 |
> |
String osversion = (String) data.get("packet.os.version"); |
54 |
> |
|
55 |
> |
String usercount = (String) data.get("packet.users.count"); |
56 |
> |
String userlist = (String) data.get("packet.users.list"); |
57 |
> |
|
58 |
> |
packet = new XMLFormatter(); |
59 |
> |
packet.addNest("os"); |
60 |
> |
packet.addElement("name",osname); |
61 |
> |
packet.addElement("release",osrelease); |
62 |
> |
packet.addElement("platform",osplatform); |
63 |
> |
packet.addElement("sysname",ossysname); |
64 |
> |
packet.addElement("version",osversion); |
65 |
> |
packet.closeNest(); |
66 |
|
packet.addNest("load"); |
67 |
< |
packet.addElement("1",load1); |
68 |
< |
packet.addElement("2",load2); |
69 |
< |
packet.addElement("3",load3); |
67 |
> |
packet.addElement("load1",load1); |
68 |
> |
packet.addElement("load5",load5); |
69 |
> |
packet.addElement("load15",load15); |
70 |
|
packet.closeNest(); |
71 |
|
packet.addNest("processes"); |
72 |
|
packet.addElement("total",totalProcesses); |
82 |
|
packet.addElement("swap",swap); |
83 |
|
packet.closeNest(); |
84 |
|
packet.addNest("memory"); |
85 |
< |
packet.addElement("real",real); |
85 |
> |
packet.addElement("total",real); |
86 |
|
packet.addElement("free",free); |
78 |
– |
packet.addElement("swapinuse",swapInUse); |
79 |
– |
packet.addElement("swapfree",swapFree); |
87 |
|
packet.closeNest(); |
88 |
+ |
packet.addNest("swap"); |
89 |
+ |
packet.addElement("inuse",swapInUse); |
90 |
+ |
packet.addElement("free",swapFree); |
91 |
+ |
packet.closeNest(); |
92 |
+ |
packet.addNest("users"); |
93 |
+ |
packet.addElement("count",usercount); |
94 |
+ |
//packet.addElement("list",userlist); |
95 |
+ |
packet.closeNest(); |
96 |
|
|
97 |
< |
} |
98 |
< |
catch ( IOException e ){ |
99 |
< |
// probably couldn't find the file |
100 |
< |
} |
101 |
< |
|
102 |
< |
|
97 |
> |
in.close(); |
98 |
> |
|
99 |
> |
} |
100 |
> |
catch(Exception e) { |
101 |
> |
System.out.println("ERROR IN DECODE: "+e); |
102 |
> |
e.printStackTrace(); |
103 |
> |
} |
104 |
> |
|
105 |
|
} |
106 |
|
|
107 |
|
public String getItems(){ |
108 |
|
// return the xml |
109 |
|
return packet.returnXML(); |
93 |
– |
|
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
< |
} // class |
113 |
> |
} // class |