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.2
Committed: Thu Dec 7 23:19:49 2000 UTC (23 years, 11 months ago) by tdb
Branch: MAIN
Changes since 1.1: +66 -22 lines
Log Message:
Quite a bit of change to the grabbing of lines. Also, moved the reading of data
to here, which saves using a file.

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     System.out.println("line1: "+line);
28 ab11 1.1 StringTokenizer tok = new StringTokenizer(line,":",false);
29 tdb 1.2 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 ab11 1.1 String sTime = tok.nextToken(); // "16:58:10"
38 tdb 1.2 System.out.println("sTime: "+sTime);
39 ab11 1.1
40     // get the next line
41     line = in.readLine(); // 632 processes: 591 sleeping, 10 zombie, 30 stopped, 1 on cpu
42 tdb 1.2 System.out.println("line2: "+line);
43 ab11 1.1 tok = new StringTokenizer(line," ",false);
44     String totalProcesses = tok.nextToken(); // "632"
45 tdb 1.2 System.out.println("totalProcesses: "+totalProcesses);
46     tok.nextToken(); // processes
47 ab11 1.1 String sleeping = tok.nextToken(": ,"); // "591"
48 tdb 1.2 System.out.println("sleeping: "+sleeping);
49     tok.nextToken(); // sleeping
50 ab11 1.1 String zombie = tok.nextToken(); // "10"
51 tdb 1.2 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 ab11 1.1
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 tdb 1.2 System.out.println("line3: "+line);
62 ab11 1.1 tok = new StringTokenizer(line,":%,",false);
63 tdb 1.2 tok.nextToken(); // CPU states
64 ab11 1.1 String idle = tok.nextToken(); // "71.1"
65 tdb 1.2 System.out.println("idle: "+idle);
66     tok.nextToken(); // idle
67 ab11 1.1 String user = tok.nextToken(",%"); // " 1.8"
68 tdb 1.2 System.out.println("user: "+user);
69     tok.nextToken(); // user
70 ab11 1.1 String kernel = tok.nextToken(); // " 4.8"
71 tdb 1.2 System.out.println("kernel: "+kernel);
72     tok.nextToken(); // kernel
73 ab11 1.1 String iowait = tok.nextToken(); // " 22.2"
74 tdb 1.2 System.out.println("iowait: "+iowait);
75     tok.nextToken(); // iowait
76 ab11 1.1 String swap = tok.nextToken(); // " 0.0"
77 tdb 1.2 System.out.println("swap: "+swap);
78 ab11 1.1
79     line = in.readLine(); // Memory: 4096M real, 2380M free, 1237M swap in use, 9774M swap free
80 tdb 1.2 System.out.println("line4: "+line);
81 ab11 1.1 tok = new StringTokenizer(line,": ,",false);
82     tok.nextToken(); // Memory:
83     String real = tok.nextToken(); // "4096M"
84 tdb 1.2 System.out.println("real: "+real);
85     tok.nextToken(); // real
86 ab11 1.1 String free = tok.nextToken(", "); // "2380M"
87 tdb 1.2 System.out.println("free: "+free);
88     tok.nextToken(); // free
89 ab11 1.1 String swapInUse = tok.nextToken(); // "1237M"
90 tdb 1.2 System.out.println("swapInUse: "+swapInUse);
91     tok.nextToken(); // swap
92     tok.nextToken(); // in
93     tok.nextToken(); // use
94 ab11 1.1 String swapFree = tok.nextToken(", "); // "9774M"
95 tdb 1.2 System.out.println("swapFree: "+swapFree);
96 ab11 1.1
97     // done now make it into an xml packet ;)
98 tdb 1.2
99     packet = new XMLFormatter();
100 ab11 1.1 packet.addNest("load");
101 tdb 1.2 packet.addElement("load1",load1);
102     packet.addElement("load5",load5);
103     packet.addElement("load15",load15);
104 ab11 1.1 packet.closeNest();
105     packet.addNest("processes");
106     packet.addElement("total",totalProcesses);
107     packet.addElement("sleeping",sleeping);
108     packet.addElement("zombie",zombie);
109     packet.addElement("stopped",stopped);
110     packet.closeNest();
111     packet.addNest("cpu");
112 tdb 1.2 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 ab11 1.1 packet.closeNest();
118     packet.addNest("memory");
119     packet.addElement("real",real);
120     packet.addElement("free",free);
121     packet.addElement("swapinuse",swapInUse);
122     packet.addElement("swapfree",swapFree);
123     packet.closeNest();
124    
125 tdb 1.2 in.close();
126 ab11 1.1 }
127 tdb 1.2 catch (Exception e ){
128     // probably couldn't find the file
129     System.out.println(e);
130 ab11 1.1 }
131    
132    
133     }
134    
135     public String getItems(){
136     // return the xml
137     return packet.returnXML();
138     }
139    
140    
141     } // class