KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > collect > LinuxSystemInfoCollector


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.diagnostics.collect;
24
25 import com.sun.logging.LogDomains;
26 import com.sun.enterprise.diagnostics.Data;
27 import com.sun.enterprise.diagnostics.Defaults;
28
29
30 import java.io.*;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 /**
36  * Class to collect System Information for Linux OS
37  *
38  */

39
40 public class LinuxSystemInfoCollector implements Collector
41 {
42     private static Logger JavaDoc logger =
43             LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
44
45     /* Command for properties */
46     private static final String JavaDoc SWAP_SPACE_CMD = "/proc/meminfo";
47     private static final String JavaDoc TCP_SETTINGS_CMD =
48             "/proc/sys/net/ipv4/tcp_keepalive_time";
49     private static final String JavaDoc MEMORY_INFO_CMD = "/proc/meminfo";
50     private static final String JavaDoc PROCESSOR_INFO_CMD = "/proc/cpuinfo";
51     private static final String JavaDoc HARD_DISK_INFO_CMD =
52             "df -k | grep /dev | grep -v /dev/fd | awk '{print $1, $2, $6}'";
53     private static final String JavaDoc NETWORK_SETTINGS_CMD =
54             "/sbin/ifconfig | grep MTU";
55     private static final String JavaDoc IP_ADDRESS_INFO_CMD =
56             "/sbin/ifconfig | grep inet";
57     private static final String JavaDoc OS_LEVEL_PATCH_INFO_CMD = "rpm -qai";
58
59     private static final String JavaDoc HOST_NAME_CMD = "hostname";
60     private static final String JavaDoc DOMAIN_NAME_CMD = "domainname";
61     private static final String JavaDoc SOFT_FILE_DESC_LIMIT_CMD = "ulimit -n";
62     private static final String JavaDoc HARD_FILE_DESC_LIMIT_CMD = "ulimit -Hn";
63     private String JavaDoc destFolder = null;
64
65
66     public LinuxSystemInfoCollector(String JavaDoc destFolder){
67         this.destFolder = destFolder;
68     }
69
70
71     /**
72      * captures the System Information for Linux OS
73      * @return Data
74      */

75     public Data capture(){
76
77         FileData data = null;
78         String JavaDoc outputFileName = destFolder + File.separator + Defaults.SYSTEM_INFO_FILE;
79
80
81         final String JavaDoc ALL_CMDS = "( echo HOSTNAME ; " + HOST_NAME_CMD + " " +
82                 ";echo DOMAINNAME ; "+ DOMAIN_NAME_CMD +
83                 ";echo 'HARD DISK INFO' ; " + HARD_DISK_INFO_CMD +
84                 ";echo 'NETWORK SETTINGS ' ; " + NETWORK_SETTINGS_CMD +
85                 ";echo 'IP ADDRESS INFO' ; " + IP_ADDRESS_INFO_CMD +
86                 ";echo 'OS LEVEL PATCH' ; " + OS_LEVEL_PATCH_INFO_CMD +
87                 ";echo 'SOFT FILE DESCRIPTOR LIMIT';"+ SOFT_FILE_DESC_LIMIT_CMD +
88                 ";echo 'HARD FILE DESCRIPTOR LIMIT';"+ HARD_FILE_DESC_LIMIT_CMD +
89                 ") >> " + outputFileName ;
90
91         String JavaDoc[] cmd = {"sh", "-c", ALL_CMDS};
92
93         ProcessExecutor executor = new ProcessExecutor(cmd, 0);
94         try{
95         executor.execute();
96
97         File outputFile = new File(outputFileName);
98
99         FileWriter writer = new FileWriter(outputFile, true);
100
101             String JavaDoc swapSpaceInfo = getSwapSpaceInfo();
102
103             writer.write("SWAP SPACE\n");
104             writer.write(swapSpaceInfo + "\n");
105
106             String JavaDoc processorInfo = getProcessorInfo();
107
108             writer.write("PROCESSOR INFO\n");
109             writer.write(processorInfo + "\n");
110
111             String JavaDoc memoryInfo = getMemoryInfo();
112
113             writer.write("MEMORY INFO\n");
114             writer.write(memoryInfo + "\n");
115
116             writer.close();
117
118             data = new FileData(outputFile.getName(),DataType.SYSTEM_INFO);
119
120         }
121         catch(ProcessExecutorException pee){
122             logger.log(Level.WARNING, "Exception while capturing system info" +
123                      " : " + pee.getMessage());
124         }
125         catch(FileNotFoundException fnfe){
126             logger.log(Level.WARNING, "Exception while capturing system info" +
127                      " : " + fnfe.getMessage());
128         }
129         catch(IOException ioe){
130             logger.log(Level.WARNING, "Exception while capturing system info" +
131                      " : " + ioe.getMessage());
132         }
133         return data;
134     }
135
136     /**
137      * to get the Swap Space Information
138      * @return String representing swap space information
139      */

140     public String JavaDoc getSwapSpaceInfo() {
141         String JavaDoc result = null;
142
143         try {
144             File file = new File(SWAP_SPACE_CMD); //file holding swap details
145
BufferedReader reader = new BufferedReader(new FileReader(file));
146
147             String JavaDoc line = null; //represents one line in the file
148
String JavaDoc total = null; //total swap space
149
String JavaDoc used = null; //used space
150
String JavaDoc free = null; //free space
151

152             while (true) {
153                 line = reader.readLine();
154                 if (line != null && line.indexOf("Swap:") >= 0) {
155                     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(line, " ");
156
157                     if (tokenizer.countTokens() >= 4) // Tokenize and get
158
// individual strings
159
{
160                         tokenizer.nextElement();
161                         total = (String JavaDoc) tokenizer.nextElement();
162                         used = (String JavaDoc) tokenizer.nextElement();
163                         free = (String JavaDoc) tokenizer.nextElement();
164
165                         result = "Total : " + total + " , " + " Used : " + used +
166                                 " , " + "Free : " + free;
167                     }
168                     break;
169                 } else if (line == null) // exit when eof is reached
170
{
171                     break;
172                 }
173             }
174             reader.close();
175         }
176         catch (IOException ioe) {
177
178            logger.log(Level.WARNING, "Exception while retrieving Swap Space Info" +
179                     " : " + ioe.getMessage());
180         }
181         return result;
182     }
183
184     /**
185      * To get Processor Information
186      * @return String representing processor information
187      */

188     public String JavaDoc getProcessorInfo() {
189         String JavaDoc result = null;
190         try {
191             File file = new File(PROCESSOR_INFO_CMD); //file holding CPU details
192
BufferedReader reader = new BufferedReader(new FileReader(file));
193
194             String JavaDoc line = null; //to read a line from the file
195

196             while (true) {
197                 line = reader.readLine();
198                 if (line != null && line.indexOf("model name") >= 0) {
199                     int index = line.indexOf(":");
200                     if (index >= 0) {
201                         result = line.substring(index + 1);
202                     }
203                     break;
204                 } else if (line == null) // exit when eof is reached
205
{
206                     break;
207                 }
208             }
209             reader.close();
210         }
211         catch (IOException ioe) {
212
213             logger.log(Level.WARNING,"Exception while retrieving Processor Info" +
214                     " : " + ioe.getMessage());
215         }
216         return result;
217     }
218
219     /**
220      * To get memory information
221      * @return String representing memory information
222      */

223     public String JavaDoc getMemoryInfo() {
224         String JavaDoc result = null;
225         try {
226             File file = new File(MEMORY_INFO_CMD); //file holding swap details
227
BufferedReader reader = new BufferedReader(new FileReader(file));
228
229
230             String JavaDoc line = null; //to read a line from the file
231
String JavaDoc total = null; //total swap space
232
String JavaDoc used = null; //used space
233
String JavaDoc free = null; //free space
234

235             while (true) {
236                 line = reader.readLine();
237                 if (line != null && line.indexOf("Mem:") >= 0) {
238                     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(line, " ");
239
240                     if (tokenizer.countTokens() >= 4)
241                     // Tokenize and get individual strings
242
{
243                         tokenizer.nextElement();
244                         total = (String JavaDoc) tokenizer.nextElement();
245                         used = (String JavaDoc) tokenizer.nextElement();
246                         free = (String JavaDoc) tokenizer.nextElement();
247
248                         result = "Total : " + total + "\nUsed : " + used +
249                                 "\nFree : " + free;
250                     }
251                     break;
252
253                 } else if (line == null) // exit when eof is reached
254
{
255                     break;
256                 }
257             }
258             reader.close();
259         }
260         catch (IOException ioe) {
261
262             logger.log(Level.WARNING, "Exception while retrieving Memory " +
263                     "Info : " + ioe.getMessage());
264         }
265         return result;
266     }
267 }
268
Popular Tags