ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/DecodeCPU_TXT.java
Revision: 1.5
Committed: Wed Dec 13 19:00:37 2000 UTC (23 years, 11 months ago) by tdb
Branch: MAIN
Changes since 1.4: +5 -5 lines
Log Message:
Removed the trailing M on memory/swap values.

File Contents

# User Rev Content
1 ab11 1.1 import java.io.*;
2     import java.util.*;
3    
4     class DecodeCPU_TXT {
5    
6     XMLFormatter packet;
7    
8 tdb 1.2 public DecodeCPU_TXT(){
9 ab11 1.1
10     // open the file and read in all the relevant details
11     try {
12 tdb 1.2 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 ab11 1.1
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 tdb 1.2 //System.out.println("Throwing away line: "+line);
23 ab11 1.1 }
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 tdb 1.2 line = in.readLine();
27 tdb 1.3 // it seems some versions of top prefix this line with something else... lets remove it :)
28     line = line.substring(line.indexOf("load averages:"));
29 ab11 1.4 // System.out.println("line1: "+line);
30 ab11 1.1 StringTokenizer tok = new StringTokenizer(line,":",false);
31 tdb 1.2 tok.nextToken(); // load averages
32     tok.nextToken(" ,"); // :
33     String load1 = tok.nextToken(); // "0.30"
34 ab11 1.4 // System.out.println("load1: "+load1);
35 tdb 1.2 String load5 = tok.nextToken(); // "0.27"
36 ab11 1.4 // System.out.println("load5: "+load5);
37 tdb 1.2 String load15 = tok.nextToken(); // "0.29"
38 ab11 1.4 // System.out.println("load15: "+load15);
39 ab11 1.1 String sTime = tok.nextToken(); // "16:58:10"
40 ab11 1.4 // System.out.println("sTime: "+sTime);
41 ab11 1.1
42     // get the next line
43     line = in.readLine(); // 632 processes: 591 sleeping, 10 zombie, 30 stopped, 1 on cpu
44 ab11 1.4 // System.out.println("line2: "+line);
45 ab11 1.1 tok = new StringTokenizer(line," ",false);
46     String totalProcesses = tok.nextToken(); // "632"
47 ab11 1.4 // System.out.println("totalProcesses: "+totalProcesses);
48 tdb 1.2 tok.nextToken(); // processes
49 ab11 1.1 String sleeping = tok.nextToken(": ,"); // "591"
50 ab11 1.4 // System.out.println("sleeping: "+sleeping);
51 tdb 1.2 tok.nextToken(); // sleeping
52 ab11 1.1 String zombie = tok.nextToken(); // "10"
53 ab11 1.4 // System.out.println("zombie: "+zombie);
54 tdb 1.2 tok.nextToken(); // zombie
55     String stopped = tok.nextToken(); // "30"
56 ab11 1.4 // System.out.println("stopped: "+stopped);
57 tdb 1.2 tok.nextToken(); // stopped
58     String onCPU = tok.nextToken(); // "1"
59 ab11 1.4 // System.out.println("onCPU: "+onCPU);
60 ab11 1.1
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 ab11 1.4 // System.out.println("line3: "+line);
64 ab11 1.1 tok = new StringTokenizer(line,":%,",false);
65 tdb 1.2 tok.nextToken(); // CPU states
66 ab11 1.1 String idle = tok.nextToken(); // "71.1"
67 ab11 1.4 // System.out.println("idle: "+idle);
68 tdb 1.2 tok.nextToken(); // idle
69 ab11 1.1 String user = tok.nextToken(",%"); // " 1.8"
70 ab11 1.4 // System.out.println("user: "+user);
71 tdb 1.2 tok.nextToken(); // user
72 ab11 1.1 String kernel = tok.nextToken(); // " 4.8"
73 ab11 1.4 // System.out.println("kernel: "+kernel);
74 tdb 1.2 tok.nextToken(); // kernel
75 ab11 1.1 String iowait = tok.nextToken(); // " 22.2"
76 ab11 1.4 // System.out.println("iowait: "+iowait);
77 tdb 1.2 tok.nextToken(); // iowait
78 ab11 1.1 String swap = tok.nextToken(); // " 0.0"
79 ab11 1.4 // System.out.println("swap: "+swap);
80 ab11 1.1
81     line = in.readLine(); // Memory: 4096M real, 2380M free, 1237M swap in use, 9774M swap free
82 ab11 1.4 // System.out.println("line4: "+line);
83 ab11 1.1 tok = new StringTokenizer(line,": ,",false);
84     tok.nextToken(); // Memory:
85     String real = tok.nextToken(); // "4096M"
86 ab11 1.4 // System.out.println("real: "+real);
87 tdb 1.2 tok.nextToken(); // real
88 ab11 1.1 String free = tok.nextToken(", "); // "2380M"
89 ab11 1.4 // System.out.println("free: "+free);
90 tdb 1.2 tok.nextToken(); // free
91 ab11 1.1 String swapInUse = tok.nextToken(); // "1237M"
92 ab11 1.4 // System.out.println("swapInUse: "+swapInUse);
93 tdb 1.2 tok.nextToken(); // swap
94     tok.nextToken(); // in
95     tok.nextToken(); // use
96 ab11 1.1 String swapFree = tok.nextToken(", "); // "9774M"
97 ab11 1.4 // System.out.println("swapFree: "+swapFree);
98 ab11 1.1
99     // done now make it into an xml packet ;)
100 tdb 1.2
101     packet = new XMLFormatter();
102 tdb 1.3 packet.addElement("sTime",sTime);
103 ab11 1.1 packet.addNest("load");
104 tdb 1.2 packet.addElement("load1",load1);
105     packet.addElement("load5",load5);
106     packet.addElement("load15",load15);
107 ab11 1.1 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 tdb 1.2 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 ab11 1.1 packet.closeNest();
121     packet.addNest("memory");
122 tdb 1.5 packet.addElement("real",real.substring(0, real.length()-1));
123     packet.addElement("free",free.substring(0, free.length()-1));
124     packet.addElement("swapinuse",swapInUse.substring(0, swapInUse.length()-1));
125     packet.addElement("swapfree",swapFree.substring(0, swapFree.length()-1));
126 ab11 1.1 packet.closeNest();
127    
128 tdb 1.2 in.close();
129 ab11 1.1 }
130 tdb 1.2 catch (Exception e ){
131     // probably couldn't find the file
132     System.out.println(e);
133 ab11 1.1 }
134    
135    
136     }
137    
138     public String getItems(){
139     // return the xml
140     return packet.returnXML();
141     }
142    
143    
144 tdb 1.5 } // class