4 |
|
class DecodeCPU_TXT { |
5 |
|
|
6 |
|
XMLFormatter packet; |
7 |
+ |
|
8 |
+ |
public static final String VERSION = "statgrab.pl $Revision$"; |
9 |
|
|
10 |
< |
public DecodeCPU_TXT(String _file){ |
11 |
< |
|
12 |
< |
// open the file and read in all the relevant details |
13 |
< |
try { |
14 |
< |
BufferedReader in = new BufferedReader(new FileReader(_file)); |
15 |
< |
String line = new String(); |
16 |
< |
|
17 |
< |
// get rid of the first output (it doesn't contain cpu info) |
18 |
< |
for ( int i=0; i < 7; i++ ){ |
19 |
< |
line = in.readLine(); |
20 |
< |
System.out.println("Throwing away line: "+line); |
21 |
< |
} |
22 |
< |
// first line has the format: load averages: 0.30, 0.27, 0.29 16:58:10 |
23 |
< |
// tokenize it using spaces as delimiters? |
24 |
< |
StringTokenizer tok = new StringTokenizer(line,":",false); |
25 |
< |
tok.nextToken(); // load averages: |
26 |
< |
String load1 = tok.nextToken(" ,"); // "0.30" |
27 |
< |
String load2 = tok.nextToken(); // "0.27" |
28 |
< |
String load3 = tok.nextToken(); // "0.29" |
29 |
< |
String sTime = tok.nextToken(); // "16:58:10" |
10 |
> |
public DecodeCPU_TXT(){ |
11 |
> |
HashMap data; |
12 |
> |
try { |
13 |
> |
String[] cmd = {"statgrab.pl"}; |
14 |
> |
Process proc = Runtime.getRuntime().exec(cmd); |
15 |
> |
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); |
16 |
> |
|
17 |
> |
data = new HashMap(); |
18 |
> |
|
19 |
> |
// Messy, but it'll keep going until no more data :/ |
20 |
> |
try { |
21 |
> |
String line = new String(); |
22 |
> |
while(true) { |
23 |
> |
line = in.readLine(); |
24 |
> |
int split = line.indexOf(' '); |
25 |
> |
data.put(line.substring(0, split), line.substring(split+1)); |
26 |
> |
} |
27 |
> |
} |
28 |
> |
catch (Exception e) {} |
29 |
> |
|
30 |
> |
String version = (String) data.get("version"); |
31 |
> |
if(!version.equals(VERSION)) { |
32 |
> |
System.out.println("!!!!!! statgrab.pl has changed version, please check code is still up-to-date !!!!!!"); |
33 |
> |
} |
34 |
> |
|
35 |
> |
String load1 = (String) data.get("packet.load.load1"); |
36 |
> |
String load5 = (String) data.get("packet.load.load5"); |
37 |
> |
String load15 = (String) data.get("packet.load.load15"); |
38 |
|
|
39 |
< |
// get the next line |
40 |
< |
line = in.readLine(); // 632 processes: 591 sleeping, 10 zombie, 30 stopped, 1 on cpu |
41 |
< |
tok = new StringTokenizer(line," ",false); |
42 |
< |
String totalProcesses = tok.nextToken(); // "632" |
43 |
< |
String sleeping = tok.nextToken(": ,"); // "591" |
34 |
< |
String zombie = tok.nextToken(); // "10" |
35 |
< |
String stopped = tok.nextToken(","); // "30" |
39 |
> |
String totalProcesses = (String) data.get("packet.processes.total"); |
40 |
> |
String sleeping = (String) data.get("packet.processes.sleeping"); |
41 |
> |
String zombie = (String) data.get("packet.processes.zombie"); |
42 |
> |
String stopped = (String) data.get("packet.processes.stopped"); |
43 |
> |
String onCPU = (String) data.get("packet.processes.cpu"); |
44 |
|
|
45 |
< |
// get the next line |
46 |
< |
line = in.readLine(); // CPU states: 71.1% idle, 1.8% user, 4.8% kernel, 22.2% iowait, 0.0% swap |
47 |
< |
tok = new StringTokenizer(line,":%,",false); |
48 |
< |
String idle = tok.nextToken(); // "71.1" |
49 |
< |
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 |
> |
String idle = (String) data.get("packet.cpu.idle"); |
46 |
> |
String user = (String) data.get("packet.cpu.user"); |
47 |
> |
String kernel = (String) data.get("packet.cpu.kernel"); |
48 |
> |
String iowait = (String) data.get("packet.cpu.iowait"); |
49 |
> |
String swap = (String) data.get("packet.cpu.swap"); |
50 |
|
|
51 |
< |
line = in.readLine(); // Memory: 4096M real, 2380M free, 1237M swap in use, 9774M swap free |
52 |
< |
tok = new StringTokenizer(line,": ,",false); |
53 |
< |
tok.nextToken(); // Memory: |
54 |
< |
String real = tok.nextToken(); // "4096M" |
55 |
< |
String free = tok.nextToken(", "); // "2380M" |
56 |
< |
String swapInUse = tok.nextToken(); // "1237M" |
57 |
< |
String swapFree = tok.nextToken(", "); // "9774M" |
58 |
< |
|
59 |
< |
// done now make it into an xml packet ;) |
60 |
< |
|
61 |
< |
packet = new XMLFormatter("core"); |
51 |
> |
String real = (String) data.get("packet.memory.real"); |
52 |
> |
String free = (String) data.get("packet.memory.free"); |
53 |
> |
String swapInUse = (String) data.get("packet.memory.swap_in_use"); |
54 |
> |
String swapFree = (String) data.get("packet.memory.swap_free"); |
55 |
> |
|
56 |
> |
String osname = (String) data.get("packet.os.name"); |
57 |
> |
String osrelease = (String) data.get("packet.os.release"); |
58 |
> |
String osplatform = (String) data.get("packet.os.platform"); |
59 |
> |
String ossysname = (String) data.get("packet.os.sysname"); |
60 |
> |
String osversion = (String) data.get("packet.os.version"); |
61 |
> |
|
62 |
> |
String usercount = (String) data.get("packet.users.count"); |
63 |
> |
String userlist = (String) data.get("packet.users.list"); |
64 |
> |
|
65 |
> |
packet = new XMLFormatter(); |
66 |
> |
packet.addNest("os"); |
67 |
> |
packet.addElement("name",osname); |
68 |
> |
packet.addElement("release",osrelease); |
69 |
> |
packet.addElement("platform",osplatform); |
70 |
> |
packet.addElement("sysname",ossysname); |
71 |
> |
packet.addElement("version",osversion); |
72 |
|
packet.addNest("load"); |
73 |
< |
packet.addElement("1",load1); |
74 |
< |
packet.addElement("2",load2); |
75 |
< |
packet.addElement("3",load3); |
73 |
> |
packet.addElement("load1",load1); |
74 |
> |
packet.addElement("load5",load5); |
75 |
> |
packet.addElement("load15",load15); |
76 |
|
packet.closeNest(); |
77 |
|
packet.addNest("processes"); |
78 |
|
packet.addElement("total",totalProcesses); |
88 |
|
packet.addElement("swap",swap); |
89 |
|
packet.closeNest(); |
90 |
|
packet.addNest("memory"); |
91 |
< |
packet.addElement("real",real); |
91 |
> |
packet.addElement("total",real); |
92 |
|
packet.addElement("free",free); |
78 |
– |
packet.addElement("swapinuse",swapInUse); |
79 |
– |
packet.addElement("swapfree",swapFree); |
93 |
|
packet.closeNest(); |
94 |
+ |
packet.addNest("swap"); |
95 |
+ |
packet.addElement("inuse",swapInUse); |
96 |
+ |
packet.addElement("free",swapFree); |
97 |
+ |
packet.addNest("users"); |
98 |
+ |
packet.addElement("count",usercount); |
99 |
+ |
packet.addElement("list",userlist); |
100 |
+ |
packet.closeNest(); |
101 |
|
|
102 |
< |
} |
103 |
< |
catch ( IOException e ){ |
104 |
< |
// probably couldn't find the file |
105 |
< |
} |
106 |
< |
|
107 |
< |
|
102 |
> |
in.close(); |
103 |
> |
|
104 |
> |
} |
105 |
> |
catch(Exception e) { |
106 |
> |
System.out.println("ERROR IN DECODE: "+e); |
107 |
> |
e.printStackTrace(); |
108 |
> |
} |
109 |
> |
|
110 |
|
} |
111 |
|
|
112 |
|
public String getItems(){ |
113 |
|
// return the xml |
114 |
|
return packet.returnXML(); |
93 |
– |
|
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
< |
} // class |
118 |
> |
} // class |