1 |
ab11 |
1.1 |
import java.io.*; |
2 |
|
|
import java.util.*; |
3 |
|
|
|
4 |
|
|
class DecodeCPU_TXT { |
5 |
|
|
|
6 |
|
|
XMLFormatter packet; |
7 |
tdb |
1.8 |
|
8 |
|
|
public static final String VERSION = "statgrab.pl $Revision: 1.4 $"; |
9 |
ab11 |
1.1 |
|
10 |
tdb |
1.2 |
public DecodeCPU_TXT(){ |
11 |
tdb |
1.8 |
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 |
ab11 |
1.1 |
|
39 |
tdb |
1.8 |
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 |
ab11 |
1.1 |
|
45 |
tdb |
1.8 |
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 |
ab11 |
1.1 |
|
51 |
tdb |
1.8 |
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 |
ab11 |
1.1 |
packet.addNest("load"); |
73 |
tdb |
1.2 |
packet.addElement("load1",load1); |
74 |
|
|
packet.addElement("load5",load5); |
75 |
|
|
packet.addElement("load15",load15); |
76 |
ab11 |
1.1 |
packet.closeNest(); |
77 |
|
|
packet.addNest("processes"); |
78 |
|
|
packet.addElement("total",totalProcesses); |
79 |
|
|
packet.addElement("sleeping",sleeping); |
80 |
|
|
packet.addElement("zombie",zombie); |
81 |
|
|
packet.addElement("stopped",stopped); |
82 |
|
|
packet.closeNest(); |
83 |
|
|
packet.addNest("cpu"); |
84 |
tdb |
1.8 |
packet.addElement("idle",idle); |
85 |
|
|
packet.addElement("user",user); |
86 |
|
|
packet.addElement("kernel",kernel); |
87 |
|
|
packet.addElement("iowait",iowait); |
88 |
|
|
packet.addElement("swap",swap); |
89 |
ab11 |
1.1 |
packet.closeNest(); |
90 |
|
|
packet.addNest("memory"); |
91 |
tdb |
1.8 |
packet.addElement("total",real); |
92 |
|
|
packet.addElement("free",free); |
93 |
tdb |
1.6 |
packet.closeNest(); |
94 |
|
|
packet.addNest("swap"); |
95 |
tdb |
1.8 |
packet.addElement("inuse",swapInUse); |
96 |
|
|
packet.addElement("free",swapFree); |
97 |
|
|
packet.addNest("users"); |
98 |
|
|
packet.addElement("count",usercount); |
99 |
|
|
packet.addElement("list",userlist); |
100 |
ab11 |
1.1 |
packet.closeNest(); |
101 |
|
|
|
102 |
tdb |
1.2 |
in.close(); |
103 |
tdb |
1.8 |
|
104 |
|
|
} |
105 |
|
|
catch(Exception e) { |
106 |
|
|
System.out.println("ERROR IN DECODE: "+e); |
107 |
|
|
e.printStackTrace(); |
108 |
|
|
} |
109 |
|
|
|
110 |
ab11 |
1.1 |
} |
111 |
|
|
|
112 |
|
|
public String getItems(){ |
113 |
|
|
// return the xml |
114 |
|
|
return packet.returnXML(); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
|
118 |
tdb |
1.5 |
} // class |