1 /* |
|
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.servicetag; |
|
27 |
|
28 // This class is a copy of the com.sun.scn.servicetags.LinuxSystemEnvironment |
|
29 // class from the Sun Connection source. |
|
30 // |
|
31 // The Service Tags team maintains the latest version of the implementation |
|
32 // for system environment data collection. JDK will include a copy of |
|
33 // the most recent released version for a JDK release. We rename |
|
34 // the package to com.sun.servicetag so that the Sun Connection |
|
35 // product always uses the latest version from the com.sun.scn.servicetags |
|
36 // package. JDK and users of the com.sun.servicetag API |
|
37 // (e.g. NetBeans and SunStudio) will use the version in JDK. |
|
38 // |
|
39 // So we keep this class in src/share/classes instead of src/<os>/classes. |
|
40 |
|
41 import java.io.*; |
|
42 |
|
43 /** |
|
44 * Linux implementation of the SystemEnvironment class. |
|
45 */ |
|
46 class LinuxSystemEnvironment extends SystemEnvironment { |
|
47 LinuxSystemEnvironment() { |
|
48 setHostId(getLinuxHostId()); |
|
49 setSystemModel(getCommandOutput("/bin/uname", "-i")); |
|
50 setSystemManufacturer(getLinuxSystemManufacturer()); |
|
51 setCpuManufacturer(getLinuxCpuManufacturer()); |
|
52 setSerialNumber(getLinuxSN()); |
|
53 } |
|
54 private String dmiInfo = null; |
|
55 |
|
56 private static final int SN = 1; |
|
57 private static final int SYS = 2; |
|
58 private static final int CPU = 3; |
|
59 |
|
60 private String getLinuxHostId() { |
|
61 String output = getCommandOutput("/usr/bin/hostid"); |
|
62 // trim off the leading 0x |
|
63 if (output.startsWith("0x")) { |
|
64 output = output.substring(2); |
|
65 } |
|
66 return output; |
|
67 } |
|
68 |
|
69 /** |
|
70 * Tries to obtain and return the cpu manufacturer. |
|
71 * @return The cpu manufacturer (an empty string if not found or an error occurred) |
|
72 */ |
|
73 private String getLinuxCpuManufacturer() { |
|
74 String tmp = getLinuxPSNInfo(CPU); |
|
75 if (tmp.length() > 0) { |
|
76 return tmp; |
|
77 } |
|
78 |
|
79 String contents = getFileContent("/proc/cpuinfo"); |
|
80 for (String line : contents.split("\n")) { |
|
81 if (line.contains("vendor_id")) { |
|
82 String[] ss = line.split(":", 2); |
|
83 if (ss.length > 1) { |
|
84 return ss[1].trim(); |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 // returns an empty string if it can't be found or an error happened |
|
90 return getLinuxDMIInfo("dmi type 4", "manufacturer"); |
|
91 } |
|
92 |
|
93 |
|
94 /** |
|
95 * Tries to obtain and return the system manufacturer. |
|
96 * @return The system manufacturer (an empty string if not found or an error occurred) |
|
97 */ |
|
98 private String getLinuxSystemManufacturer() { |
|
99 String tmp = getLinuxPSNInfo(SYS); |
|
100 if (tmp.length() > 0) { |
|
101 return tmp; |
|
102 } |
|
103 |
|
104 // returns an empty string if it can't be found or an error happened |
|
105 return getLinuxDMIInfo("dmi type 1", "manufacturer"); |
|
106 } |
|
107 |
|
108 /** |
|
109 * Tries to obtain and return the serial number of the system. |
|
110 * @return The serial number (an empty string if not found or an error occurred) |
|
111 */ |
|
112 private String getLinuxSN() { |
|
113 String tmp = getLinuxPSNInfo(SN); |
|
114 if (tmp.length() > 0) { |
|
115 return tmp; |
|
116 } |
|
117 |
|
118 // returns an empty string if it can't be found or an error happened |
|
119 return getLinuxDMIInfo("dmi type 1", "serial number"); |
|
120 } |
|
121 |
|
122 private String getLinuxPSNInfo(int target) { |
|
123 // try to read from the psn file if it exists |
|
124 String contents = getFileContent("/var/run/psn"); |
|
125 String[] ss = contents.split("\n"); |
|
126 if (target <= ss.length) { |
|
127 return ss[target-1]; |
|
128 } |
|
129 |
|
130 // default case is to return "" |
|
131 return ""; |
|
132 } |
|
133 |
|
134 // reads from dmidecode with the given type and target |
|
135 // returns an empty string if nothing was found or an error occurred |
|
136 // |
|
137 // Sample output segment: |
|
138 // Handle 0x0001 |
|
139 // DMI type 1, 25 bytes. |
|
140 // System Information |
|
141 // Manufacturer: System manufacturer |
|
142 // Product Name: System Product Name |
|
143 // Version: System Version |
|
144 // Serial Number: System Serial Number |
|
145 // UUID: 3091D719-B25B-D911-959D-6D1B12C7686E |
|
146 // Wake-up Type: Power Switch |
|
147 |
|
148 private synchronized String getLinuxDMIInfo(String dmiType, String target) { |
|
149 // only try to get dmidecode information once, after that, we can |
|
150 // reuse the output |
|
151 if (dmiInfo == null) { |
|
152 Thread dmidecodeThread = new Thread() { |
|
153 public void run() { |
|
154 dmiInfo = getCommandOutput("/usr/sbin/dmidecode"); |
|
155 } |
|
156 }; |
|
157 dmidecodeThread.start(); |
|
158 |
|
159 try { |
|
160 dmidecodeThread.join(2000); |
|
161 if (dmidecodeThread.isAlive()) { |
|
162 dmidecodeThread.interrupt(); |
|
163 dmiInfo = ""; |
|
164 } |
|
165 } catch (InterruptedException ie) { |
|
166 dmidecodeThread.interrupt(); |
|
167 } |
|
168 } |
|
169 |
|
170 if (dmiInfo.length() == 0) { |
|
171 return ""; |
|
172 } |
|
173 boolean dmiFlag = false; |
|
174 for (String s : dmiInfo.split("\n")) { |
|
175 String line = s.toLowerCase(); |
|
176 if (dmiFlag) { |
|
177 if (line.contains(target)) { |
|
178 String key = target + ":"; |
|
179 int indx = line.indexOf(key) + key.length(); |
|
180 if (line.contains(key) && indx < line.length()) { |
|
181 return line.substring(indx).trim(); |
|
182 } |
|
183 String[] ss = line.split(":"); |
|
184 return ss[ss.length-1]; |
|
185 } |
|
186 } else if (line.contains(dmiType)) { |
|
187 dmiFlag = true; |
|
188 } |
|
189 } |
|
190 return ""; |
|
191 } |
|
192 |
|
193 } |
|