4 |
|
class DecodeCPU_TXT { |
5 |
|
|
6 |
|
XMLFormatter packet; |
7 |
+ |
|
8 |
+ |
public static final String VERSION = "statgrab.pl $Revision$"; |
9 |
|
|
10 |
|
public DecodeCPU_TXT(){ |
11 |
< |
|
12 |
< |
// open the file and read in all the relevant details |
13 |
< |
try { |
14 |
< |
String[] cmd = {"/usr/local/sbin/top","-s1","-d2","0"}; |
15 |
< |
Process proc = Runtime.getRuntime().exec(cmd); |
16 |
< |
// this process will take atleast 2 seconds to complete |
17 |
< |
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); |
18 |
< |
|
19 |
< |
String line = new String(); |
20 |
< |
|
21 |
< |
// get rid of the first output (it doesn't contain cpu info) |
22 |
< |
for ( int i=0; i < 7; i++ ){ |
23 |
< |
line = in.readLine(); |
24 |
< |
//System.out.println("Throwing away line: "+line); |
25 |
< |
} |
26 |
< |
// first line has the format: load averages: 0.30, 0.27, 0.29 16:58:10 |
27 |
< |
// tokenize it using spaces as delimiters? |
28 |
< |
line = in.readLine(); |
29 |
< |
// it seems some versions of top prefix this line with something else... lets remove it :) |
30 |
< |
line = line.substring(line.indexOf("load averages:")); |
31 |
< |
// System.out.println("line1: "+line); |
32 |
< |
StringTokenizer tok = new StringTokenizer(line,":",false); |
33 |
< |
tok.nextToken(); // load averages |
34 |
< |
tok.nextToken(" ,"); // : |
35 |
< |
String load1 = tok.nextToken(); // "0.30" |
36 |
< |
// System.out.println("load1: "+load1); |
37 |
< |
String load5 = tok.nextToken(); // "0.27" |
36 |
< |
// System.out.println("load5: "+load5); |
37 |
< |
String load15 = tok.nextToken(); // "0.29" |
38 |
< |
// System.out.println("load15: "+load15); |
39 |
< |
String sTime = tok.nextToken(); // "16:58:10" |
40 |
< |
// System.out.println("sTime: "+sTime); |
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 |
< |
// System.out.println("line2: "+line); |
42 |
< |
tok = new StringTokenizer(line," ",false); |
43 |
< |
String totalProcesses = tok.nextToken(); // "632" |
47 |
< |
// System.out.println("totalProcesses: "+totalProcesses); |
48 |
< |
tok.nextToken(":"); // processes: |
49 |
< |
String sleeping = tok.nextToken(": ,"); // "591" |
50 |
< |
// System.out.println("sleeping: "+sleeping); |
51 |
< |
tok.nextToken(); // sleeping |
52 |
< |
String zombie = tok.nextToken(); // "10" |
53 |
< |
// System.out.println("zombie: "+zombie); |
54 |
< |
tok.nextToken(); // zombie |
55 |
< |
String stopped = tok.nextToken(); // "30" |
56 |
< |
// System.out.println("stopped: "+stopped); |
57 |
< |
tok.nextToken(); // stopped |
58 |
< |
String onCPU = tok.nextToken(); // "1" |
59 |
< |
// System.out.println("onCPU: "+onCPU); |
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 |
< |
// System.out.println("line3: "+line); |
48 |
< |
tok = new StringTokenizer(line,":%,",false); |
49 |
< |
tok.nextToken(); // CPU states |
66 |
< |
String idle = tok.nextToken(); // "71.1" |
67 |
< |
// System.out.println("idle: "+idle); |
68 |
< |
tok.nextToken(); // idle |
69 |
< |
String user = tok.nextToken(",%"); // " 1.8" |
70 |
< |
// System.out.println("user: "+user); |
71 |
< |
tok.nextToken(); // user |
72 |
< |
String kernel = tok.nextToken(); // " 4.8" |
73 |
< |
// System.out.println("kernel: "+kernel); |
74 |
< |
tok.nextToken(); // kernel |
75 |
< |
String iowait = tok.nextToken(); // " 22.2" |
76 |
< |
// System.out.println("iowait: "+iowait); |
77 |
< |
tok.nextToken(); // iowait |
78 |
< |
String swap = tok.nextToken(); // " 0.0" |
79 |
< |
// System.out.println("swap: "+swap); |
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 |
< |
// System.out.println("line4: "+line); |
53 |
< |
tok = new StringTokenizer(line,": ,",false); |
54 |
< |
tok.nextToken(); // Memory: |
55 |
< |
String real = tok.nextToken(); // "4096M" |
56 |
< |
// System.out.println("real: "+real); |
57 |
< |
tok.nextToken(); // real |
58 |
< |
String free = tok.nextToken(", "); // "2380M" |
59 |
< |
// System.out.println("free: "+free); |
60 |
< |
tok.nextToken(); // free |
61 |
< |
String swapInUse = tok.nextToken(); // "1237M" |
62 |
< |
// System.out.println("swapInUse: "+swapInUse); |
63 |
< |
tok.nextToken(); // swap |
64 |
< |
tok.nextToken(); // in |
65 |
< |
tok.nextToken(); // use |
66 |
< |
String swapFree = tok.nextToken(", "); // "9774M" |
67 |
< |
// System.out.println("swapFree: "+swapFree); |
68 |
< |
|
69 |
< |
// done now make it into an xml packet ;) |
70 |
< |
|
71 |
< |
packet = new XMLFormatter(); |
102 |
< |
packet.addElement("sTime",sTime); |
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("load1",load1); |
74 |
|
packet.addElement("load5",load5); |
81 |
|
packet.addElement("stopped",stopped); |
82 |
|
packet.closeNest(); |
83 |
|
packet.addNest("cpu"); |
84 |
< |
packet.addElement("idle",idle.trim()); |
85 |
< |
packet.addElement("user",user.trim()); |
86 |
< |
packet.addElement("kernel",kernel.trim()); |
87 |
< |
packet.addElement("iowait",iowait.trim()); |
88 |
< |
packet.addElement("swap",swap.trim()); |
84 |
> |
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 |
|
packet.closeNest(); |
90 |
|
packet.addNest("memory"); |
91 |
< |
packet.addElement("total",real.substring(0, real.length()-1)); |
92 |
< |
packet.addElement("free",free.substring(0, free.length()-1)); |
91 |
> |
packet.addElement("total",real); |
92 |
> |
packet.addElement("free",free); |
93 |
|
packet.closeNest(); |
94 |
|
packet.addNest("swap"); |
95 |
< |
packet.addElement("inuse",swapInUse.substring(0, swapInUse.length()-1)); |
96 |
< |
packet.addElement("free",swapFree.substring(0, swapFree.length()-1)); |
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 |
|
in.close(); |
103 |
< |
} |
104 |
< |
catch (Exception e ){ |
105 |
< |
// probably couldn't find the file |
106 |
< |
System.out.println(e); |
107 |
< |
} |
108 |
< |
|
109 |
< |
|
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(){ |