5 |
|
|
6 |
|
XMLFormatter packet; |
7 |
|
|
8 |
< |
public DecodeCPU_TXT(String _file){ |
8 |
> |
public DecodeCPU_TXT(){ |
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(); |
12 |
> |
String[] cmd = {"/usr/local/sbin/top","-s1","-d2","0"}; |
13 |
> |
Process proc = Runtime.getRuntime().exec(cmd); |
14 |
> |
// this process will take atleast 2 seconds to complete |
15 |
> |
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); |
16 |
|
|
17 |
+ |
String line = new String(); |
18 |
+ |
|
19 |
|
// get rid of the first output (it doesn't contain cpu info) |
20 |
|
for ( int i=0; i < 7; i++ ){ |
21 |
|
line = in.readLine(); |
22 |
< |
System.out.println("Throwing away line: "+line); |
22 |
> |
//System.out.println("Throwing away line: "+line); |
23 |
|
} |
24 |
|
// first line has the format: load averages: 0.30, 0.27, 0.29 16:58:10 |
25 |
|
// tokenize it using spaces as delimiters? |
26 |
+ |
line = in.readLine(); |
27 |
+ |
System.out.println("line1: "+line); |
28 |
|
StringTokenizer tok = new StringTokenizer(line,":",false); |
29 |
< |
tok.nextToken(); // load averages: |
30 |
< |
String load1 = tok.nextToken(" ,"); // "0.30" |
31 |
< |
String load2 = tok.nextToken(); // "0.27" |
32 |
< |
String load3 = tok.nextToken(); // "0.29" |
29 |
> |
tok.nextToken(); // load averages |
30 |
> |
tok.nextToken(" ,"); // : |
31 |
> |
String load1 = tok.nextToken(); // "0.30" |
32 |
> |
System.out.println("load1: "+load1); |
33 |
> |
String load5 = tok.nextToken(); // "0.27" |
34 |
> |
System.out.println("load5: "+load5); |
35 |
> |
String load15 = tok.nextToken(); // "0.29" |
36 |
> |
System.out.println("load15: "+load15); |
37 |
|
String sTime = tok.nextToken(); // "16:58:10" |
38 |
+ |
System.out.println("sTime: "+sTime); |
39 |
|
|
40 |
|
// get the next line |
41 |
|
line = in.readLine(); // 632 processes: 591 sleeping, 10 zombie, 30 stopped, 1 on cpu |
42 |
+ |
System.out.println("line2: "+line); |
43 |
|
tok = new StringTokenizer(line," ",false); |
44 |
|
String totalProcesses = tok.nextToken(); // "632" |
45 |
+ |
System.out.println("totalProcesses: "+totalProcesses); |
46 |
+ |
tok.nextToken(); // processes |
47 |
|
String sleeping = tok.nextToken(": ,"); // "591" |
48 |
+ |
System.out.println("sleeping: "+sleeping); |
49 |
+ |
tok.nextToken(); // sleeping |
50 |
|
String zombie = tok.nextToken(); // "10" |
51 |
< |
String stopped = tok.nextToken(","); // "30" |
51 |
> |
System.out.println("zombie: "+zombie); |
52 |
> |
tok.nextToken(); // zombie |
53 |
> |
String stopped = tok.nextToken(); // "30" |
54 |
> |
System.out.println("stopped: "+stopped); |
55 |
> |
tok.nextToken(); // stopped |
56 |
> |
String onCPU = tok.nextToken(); // "1" |
57 |
> |
System.out.println("onCPU: "+onCPU); |
58 |
|
|
59 |
|
// get the next line |
60 |
|
line = in.readLine(); // CPU states: 71.1% idle, 1.8% user, 4.8% kernel, 22.2% iowait, 0.0% swap |
61 |
+ |
System.out.println("line3: "+line); |
62 |
|
tok = new StringTokenizer(line,":%,",false); |
63 |
+ |
tok.nextToken(); // CPU states |
64 |
|
String idle = tok.nextToken(); // "71.1" |
65 |
+ |
System.out.println("idle: "+idle); |
66 |
+ |
tok.nextToken(); // idle |
67 |
|
String user = tok.nextToken(",%"); // " 1.8" |
68 |
+ |
System.out.println("user: "+user); |
69 |
+ |
tok.nextToken(); // user |
70 |
|
String kernel = tok.nextToken(); // " 4.8" |
71 |
+ |
System.out.println("kernel: "+kernel); |
72 |
+ |
tok.nextToken(); // kernel |
73 |
|
String iowait = tok.nextToken(); // " 22.2" |
74 |
+ |
System.out.println("iowait: "+iowait); |
75 |
+ |
tok.nextToken(); // iowait |
76 |
|
String swap = tok.nextToken(); // " 0.0" |
77 |
+ |
System.out.println("swap: "+swap); |
78 |
|
|
79 |
|
line = in.readLine(); // Memory: 4096M real, 2380M free, 1237M swap in use, 9774M swap free |
80 |
+ |
System.out.println("line4: "+line); |
81 |
|
tok = new StringTokenizer(line,": ,",false); |
82 |
|
tok.nextToken(); // Memory: |
83 |
|
String real = tok.nextToken(); // "4096M" |
84 |
+ |
System.out.println("real: "+real); |
85 |
+ |
tok.nextToken(); // real |
86 |
|
String free = tok.nextToken(", "); // "2380M" |
87 |
+ |
System.out.println("free: "+free); |
88 |
+ |
tok.nextToken(); // free |
89 |
|
String swapInUse = tok.nextToken(); // "1237M" |
90 |
+ |
System.out.println("swapInUse: "+swapInUse); |
91 |
+ |
tok.nextToken(); // swap |
92 |
+ |
tok.nextToken(); // in |
93 |
+ |
tok.nextToken(); // use |
94 |
|
String swapFree = tok.nextToken(", "); // "9774M" |
95 |
+ |
System.out.println("swapFree: "+swapFree); |
96 |
|
|
97 |
|
// done now make it into an xml packet ;) |
98 |
< |
|
99 |
< |
packet = new XMLFormatter("core"); |
98 |
> |
|
99 |
> |
packet = new XMLFormatter(); |
100 |
|
packet.addNest("load"); |
101 |
< |
packet.addElement("1",load1); |
102 |
< |
packet.addElement("2",load2); |
103 |
< |
packet.addElement("3",load3); |
101 |
> |
packet.addElement("load1",load1); |
102 |
> |
packet.addElement("load5",load5); |
103 |
> |
packet.addElement("load15",load15); |
104 |
|
packet.closeNest(); |
105 |
|
packet.addNest("processes"); |
106 |
|
packet.addElement("total",totalProcesses); |
109 |
|
packet.addElement("stopped",stopped); |
110 |
|
packet.closeNest(); |
111 |
|
packet.addNest("cpu"); |
112 |
< |
packet.addElement("idle",idle); |
113 |
< |
packet.addElement("user",user); |
114 |
< |
packet.addElement("kernel",kernel); |
115 |
< |
packet.addElement("iowait",iowait); |
116 |
< |
packet.addElement("swap",swap); |
112 |
> |
packet.addElement("idle",idle.trim()); |
113 |
> |
packet.addElement("user",user.trim()); |
114 |
> |
packet.addElement("kernel",kernel.trim()); |
115 |
> |
packet.addElement("iowait",iowait.trim()); |
116 |
> |
packet.addElement("swap",swap.trim()); |
117 |
|
packet.closeNest(); |
118 |
|
packet.addNest("memory"); |
119 |
|
packet.addElement("real",real); |
122 |
|
packet.addElement("swapfree",swapFree); |
123 |
|
packet.closeNest(); |
124 |
|
|
125 |
+ |
in.close(); |
126 |
|
} |
127 |
< |
catch ( IOException e ){ |
128 |
< |
// probably couldn't find the file |
127 |
> |
catch (Exception e ){ |
128 |
> |
// probably couldn't find the file |
129 |
> |
System.out.println(e); |
130 |
|
} |
131 |
|
|
132 |
|
|
135 |
|
public String getItems(){ |
136 |
|
// return the xml |
137 |
|
return packet.returnXML(); |
93 |
– |
|
138 |
|
} |
139 |
|
|
140 |
|
|