1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
import java.io.*; |
22 |
|
import java.util.*; |
23 |
|
|
24 |
|
class DecodeCPU_TXT { |
25 |
|
|
26 |
< |
XMLFormatter packet; |
26 |
> |
XMLFormatter packet; |
27 |
|
|
28 |
< |
public DecodeCPU_TXT(){ |
28 |
> |
public DecodeCPU_TXT(){ |
29 |
> |
HashMap data; |
30 |
> |
try { |
31 |
> |
String[] cmd = {"statgrab.pl"}; |
32 |
> |
Process proc = Runtime.getRuntime().exec(cmd); |
33 |
> |
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); |
34 |
> |
|
35 |
> |
data = new HashMap(); |
36 |
> |
|
37 |
> |
// Messy, but it'll keep going until no more data :/ |
38 |
> |
try { |
39 |
> |
String line = new String(); |
40 |
> |
while(true) { |
41 |
> |
line = in.readLine(); |
42 |
> |
int split = line.indexOf(' '); |
43 |
> |
data.put(line.substring(0, split), line.substring(split+1)); |
44 |
> |
} |
45 |
> |
} |
46 |
> |
catch (Exception e) {} |
47 |
> |
|
48 |
> |
String load1 = (String) data.get("packet.load.load1"); |
49 |
> |
String load5 = (String) data.get("packet.load.load5"); |
50 |
> |
String load15 = (String) data.get("packet.load.load15"); |
51 |
> |
|
52 |
> |
String totalProcesses = (String) data.get("packet.processes.total"); |
53 |
> |
String sleeping = (String) data.get("packet.processes.sleeping"); |
54 |
> |
String zombie = (String) data.get("packet.processes.zombie"); |
55 |
> |
String stopped = (String) data.get("packet.processes.stopped"); |
56 |
> |
String onCPU = (String) data.get("packet.processes.cpu"); |
57 |
> |
|
58 |
> |
String idle = (String) data.get("packet.cpu.idle"); |
59 |
> |
String user = (String) data.get("packet.cpu.user"); |
60 |
> |
String kernel = (String) data.get("packet.cpu.kernel"); |
61 |
> |
String iowait = (String) data.get("packet.cpu.iowait"); |
62 |
> |
String swap = (String) data.get("packet.cpu.swap"); |
63 |
> |
|
64 |
> |
String real = (String) data.get("packet.memory.real"); |
65 |
> |
String free = (String) data.get("packet.memory.free"); |
66 |
> |
String swapTotal = (String) data.get("packet.memory.swap_total"); |
67 |
> |
String swapFree = (String) data.get("packet.memory.swap_free"); |
68 |
> |
|
69 |
> |
String osname = (String) data.get("packet.os.name"); |
70 |
> |
String osrelease = (String) data.get("packet.os.release"); |
71 |
> |
String osplatform = (String) data.get("packet.os.platform"); |
72 |
> |
String ossysname = (String) data.get("packet.os.sysname"); |
73 |
> |
String osversion = (String) data.get("packet.os.version"); |
74 |
> |
|
75 |
> |
String usercount = (String) data.get("packet.users.count"); |
76 |
> |
String userlist = (String) data.get("packet.users.list"); |
77 |
> |
|
78 |
> |
packet = new XMLFormatter(); |
79 |
> |
packet.addNest("os"); |
80 |
> |
packet.addElement("name",osname); |
81 |
> |
packet.addElement("release",osrelease); |
82 |
> |
packet.addElement("platform",osplatform); |
83 |
> |
packet.addElement("sysname",ossysname); |
84 |
> |
packet.addElement("version",osversion); |
85 |
> |
packet.closeNest(); |
86 |
> |
packet.addNest("load"); |
87 |
> |
packet.addElement("load1",load1); |
88 |
> |
packet.addElement("load5",load5); |
89 |
> |
packet.addElement("load15",load15); |
90 |
> |
packet.closeNest(); |
91 |
> |
packet.addNest("processes"); |
92 |
> |
packet.addElement("total",totalProcesses); |
93 |
> |
packet.addElement("sleeping",sleeping); |
94 |
> |
packet.addElement("zombie",zombie); |
95 |
> |
packet.addElement("stopped",stopped); |
96 |
> |
packet.addElement("cpu",onCPU); |
97 |
> |
packet.closeNest(); |
98 |
> |
packet.addNest("cpu"); |
99 |
> |
packet.addElement("idle",idle); |
100 |
> |
packet.addElement("user",user); |
101 |
> |
packet.addElement("kernel",kernel); |
102 |
> |
packet.addElement("iowait",iowait); |
103 |
> |
packet.addElement("swap",swap); |
104 |
> |
packet.closeNest(); |
105 |
> |
packet.addNest("memory"); |
106 |
> |
packet.addElement("total",real); |
107 |
> |
packet.addElement("free",free); |
108 |
> |
packet.closeNest(); |
109 |
> |
packet.addNest("swap"); |
110 |
> |
packet.addElement("total",swapTotal); |
111 |
> |
packet.addElement("free",swapFree); |
112 |
> |
packet.closeNest(); |
113 |
> |
packet.addNest("users"); |
114 |
> |
packet.addElement("count",usercount); |
115 |
> |
packet.addElement("list",userlist); |
116 |
> |
packet.closeNest(); |
117 |
> |
packet.addNest("disk"); |
118 |
> |
int i=0; |
119 |
> |
while(true) { |
120 |
> |
String check = (String) data.get("packet.disk.p"+i+".attributes.name"); |
121 |
> |
if(check == null) { |
122 |
> |
break; |
123 |
> |
} |
124 |
> |
packet.addNest("p"+i); |
125 |
> |
packet.addElement("name",check); |
126 |
> |
packet.addElement("kbytes",(String) data.get("packet.disk.p"+i+".attributes.kbytes")); |
127 |
> |
packet.addElement("used",(String) data.get("packet.disk.p"+i+".attributes.used")); |
128 |
> |
packet.addElement("avail",(String) data.get("packet.disk.p"+i+".attributes.avail")); |
129 |
> |
packet.addElement("mount",(String) data.get("packet.disk.p"+i+".attributes.mount")); |
130 |
> |
packet.closeNest(); |
131 |
> |
i++; |
132 |
> |
} |
133 |
> |
packet.closeNest(); |
134 |
> |
in.close(); |
135 |
> |
|
136 |
> |
} |
137 |
> |
catch(Exception e) { |
138 |
> |
System.out.println("ERROR IN DECODE: "+e); |
139 |
> |
e.printStackTrace(); |
140 |
> |
} |
141 |
|
|
142 |
< |
// open the file and read in all the relevant details |
11 |
< |
try { |
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())); |
142 |
> |
} |
143 |
|
|
144 |
< |
String line = new String(); |
145 |
< |
|
146 |
< |
// get rid of the first output (it doesn't contain cpu info) |
147 |
< |
for ( int i=0; i < 7; i++ ){ |
148 |
< |
line = in.readLine(); |
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 |
< |
// it seems some versions of top prefix this line with something else... lets remove it :) |
28 |
< |
line = line.substring(line.indexOf("load averages:")); |
29 |
< |
// System.out.println("line1: "+line); |
30 |
< |
StringTokenizer tok = new StringTokenizer(line,":",false); |
31 |
< |
tok.nextToken(); // load averages |
32 |
< |
tok.nextToken(" ,"); // : |
33 |
< |
String load1 = tok.nextToken(); // "0.30" |
34 |
< |
// System.out.println("load1: "+load1); |
35 |
< |
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); |
41 |
< |
|
42 |
< |
// get the next line |
43 |
< |
line = in.readLine(); // 632 processes: 591 sleeping, 10 zombie, 30 stopped, 1 on cpu |
44 |
< |
// System.out.println("line2: "+line); |
45 |
< |
tok = new StringTokenizer(line," ",false); |
46 |
< |
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); |
60 |
< |
|
61 |
< |
// get the next line |
62 |
< |
line = in.readLine(); // CPU states: 71.1% idle, 1.8% user, 4.8% kernel, 22.2% iowait, 0.0% swap |
63 |
< |
// System.out.println("line3: "+line); |
64 |
< |
tok = new StringTokenizer(line,":%,",false); |
65 |
< |
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); |
80 |
< |
|
81 |
< |
line = in.readLine(); // Memory: 4096M real, 2380M free, 1237M swap in use, 9774M swap free |
82 |
< |
// System.out.println("line4: "+line); |
83 |
< |
tok = new StringTokenizer(line,": ,",false); |
84 |
< |
tok.nextToken(); // Memory: |
85 |
< |
String real = tok.nextToken(); // "4096M" |
86 |
< |
// System.out.println("real: "+real); |
87 |
< |
tok.nextToken(); // real |
88 |
< |
String free = tok.nextToken(", "); // "2380M" |
89 |
< |
// System.out.println("free: "+free); |
90 |
< |
tok.nextToken(); // free |
91 |
< |
String swapInUse = tok.nextToken(); // "1237M" |
92 |
< |
// System.out.println("swapInUse: "+swapInUse); |
93 |
< |
tok.nextToken(); // swap |
94 |
< |
tok.nextToken(); // in |
95 |
< |
tok.nextToken(); // use |
96 |
< |
String swapFree = tok.nextToken(", "); // "9774M" |
97 |
< |
// System.out.println("swapFree: "+swapFree); |
98 |
< |
|
99 |
< |
// done now make it into an xml packet ;) |
100 |
< |
|
101 |
< |
packet = new XMLFormatter(); |
102 |
< |
packet.addElement("sTime",sTime); |
103 |
< |
packet.addNest("load"); |
104 |
< |
packet.addElement("load1",load1); |
105 |
< |
packet.addElement("load5",load5); |
106 |
< |
packet.addElement("load15",load15); |
107 |
< |
packet.closeNest(); |
108 |
< |
packet.addNest("processes"); |
109 |
< |
packet.addElement("total",totalProcesses); |
110 |
< |
packet.addElement("sleeping",sleeping); |
111 |
< |
packet.addElement("zombie",zombie); |
112 |
< |
packet.addElement("stopped",stopped); |
113 |
< |
packet.closeNest(); |
114 |
< |
packet.addNest("cpu"); |
115 |
< |
packet.addElement("idle",idle.trim()); |
116 |
< |
packet.addElement("user",user.trim()); |
117 |
< |
packet.addElement("kernel",kernel.trim()); |
118 |
< |
packet.addElement("iowait",iowait.trim()); |
119 |
< |
packet.addElement("swap",swap.trim()); |
120 |
< |
packet.closeNest(); |
121 |
< |
packet.addNest("memory"); |
122 |
< |
packet.addElement("real",real); |
123 |
< |
packet.addElement("free",free); |
124 |
< |
packet.addElement("swapinuse",swapInUse); |
125 |
< |
packet.addElement("swapfree",swapFree); |
126 |
< |
packet.closeNest(); |
127 |
< |
|
128 |
< |
in.close(); |
129 |
< |
} |
130 |
< |
catch (Exception e ){ |
131 |
< |
// probably couldn't find the file |
132 |
< |
System.out.println(e); |
133 |
< |
} |
134 |
< |
|
135 |
< |
|
136 |
< |
} |
137 |
< |
|
138 |
< |
public String getItems(){ |
139 |
< |
// return the xml |
140 |
< |
return packet.returnXML(); |
141 |
< |
} |
142 |
< |
|
144 |
> |
public String getItems(){ |
145 |
> |
// return the xml |
146 |
> |
return packet.returnXML(); |
147 |
> |
} |
148 |
> |
|
149 |
|
|
150 |
< |
} // class |
150 |
> |
} // class |