1 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 ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 35 39 40 public class LinuxSystemInfoCollector implements Collector 41 { 42 private static Logger logger = 43 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 44 45 46 private static final String SWAP_SPACE_CMD = "/proc/meminfo"; 47 private static final String TCP_SETTINGS_CMD = 48 "/proc/sys/net/ipv4/tcp_keepalive_time"; 49 private static final String MEMORY_INFO_CMD = "/proc/meminfo"; 50 private static final String PROCESSOR_INFO_CMD = "/proc/cpuinfo"; 51 private static final String HARD_DISK_INFO_CMD = 52 "df -k | grep /dev | grep -v /dev/fd | awk '{print $1, $2, $6}'"; 53 private static final String NETWORK_SETTINGS_CMD = 54 "/sbin/ifconfig | grep MTU"; 55 private static final String IP_ADDRESS_INFO_CMD = 56 "/sbin/ifconfig | grep inet"; 57 private static final String OS_LEVEL_PATCH_INFO_CMD = "rpm -qai"; 58 59 private static final String HOST_NAME_CMD = "hostname"; 60 private static final String DOMAIN_NAME_CMD = "domainname"; 61 private static final String SOFT_FILE_DESC_LIMIT_CMD = "ulimit -n"; 62 private static final String HARD_FILE_DESC_LIMIT_CMD = "ulimit -Hn"; 63 private String destFolder = null; 64 65 66 public LinuxSystemInfoCollector(String destFolder){ 67 this.destFolder = destFolder; 68 } 69 70 71 75 public Data capture(){ 76 77 FileData data = null; 78 String outputFileName = destFolder + File.separator + Defaults.SYSTEM_INFO_FILE; 79 80 81 final String 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 [] 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 swapSpaceInfo = getSwapSpaceInfo(); 102 103 writer.write("SWAP SPACE\n"); 104 writer.write(swapSpaceInfo + "\n"); 105 106 String processorInfo = getProcessorInfo(); 107 108 writer.write("PROCESSOR INFO\n"); 109 writer.write(processorInfo + "\n"); 110 111 String 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 140 public String getSwapSpaceInfo() { 141 String result = null; 142 143 try { 144 File file = new File(SWAP_SPACE_CMD); BufferedReader reader = new BufferedReader(new FileReader(file)); 146 147 String line = null; String total = null; String used = null; String free = null; 152 while (true) { 153 line = reader.readLine(); 154 if (line != null && line.indexOf("Swap:") >= 0) { 155 StringTokenizer tokenizer = new StringTokenizer (line, " "); 156 157 if (tokenizer.countTokens() >= 4) { 160 tokenizer.nextElement(); 161 total = (String ) tokenizer.nextElement(); 162 used = (String ) tokenizer.nextElement(); 163 free = (String ) tokenizer.nextElement(); 164 165 result = "Total : " + total + " , " + " Used : " + used + 166 " , " + "Free : " + free; 167 } 168 break; 169 } else if (line == null) { 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 188 public String getProcessorInfo() { 189 String result = null; 190 try { 191 File file = new File(PROCESSOR_INFO_CMD); BufferedReader reader = new BufferedReader(new FileReader(file)); 193 194 String line = null; 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) { 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 223 public String getMemoryInfo() { 224 String result = null; 225 try { 226 File file = new File(MEMORY_INFO_CMD); BufferedReader reader = new BufferedReader(new FileReader(file)); 228 229 230 String line = null; String total = null; String used = null; String free = null; 235 while (true) { 236 line = reader.readLine(); 237 if (line != null && line.indexOf("Mem:") >= 0) { 238 StringTokenizer tokenizer = new StringTokenizer (line, " "); 239 240 if (tokenizer.countTokens() >= 4) 241 { 243 tokenizer.nextElement(); 244 total = (String ) tokenizer.nextElement(); 245 used = (String ) tokenizer.nextElement(); 246 free = (String ) tokenizer.nextElement(); 247 248 result = "Total : " + total + "\nUsed : " + used + 249 "\nFree : " + free; 250 } 251 break; 252 253 } else if (line == null) { 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 |